Debian Codegraph @

packages/belos/tpetra/test/MINRES/test_minres_indefinite.cpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // Belos: Block Linear Solvers Package
4 //
5 // Copyright 2004-2016 NTESS and the Belos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 /* Originally convert test here: belos/epetra/test/MINRES/test_minres_indefinite.cpp */
11
12 // Tpetra
13 #include <Tpetra_Core.hpp>
14 #include <Tpetra_CrsMatrix.hpp>
15 #include <Tpetra_Map.hpp>
16 #include <Tpetra_Vector.hpp>
17
18 // Teuchos
19 #include <Teuchos_CommandLineProcessor.hpp>
20 #include <Teuchos_ParameterList.hpp>
21 #include <Teuchos_StandardCatchMacros.hpp>
22
23 // Belos
24 #include "BelosConfigDefs.hpp"
25 #include "BelosLinearProblem.hpp"
26 #include "BelosTpetraAdapter.hpp"
27 #include "BelosMinresSolMgr.hpp"
28
29 template<class ScalarType>
30 int run(int argc, char *argv[])
31 {
32 using ST = typename Tpetra::Vector<ScalarType>::scalar_type;
33 using LO = typename Tpetra::Vector<>::local_ordinal_type;
34 using GO = typename Tpetra::Vector<>::global_ordinal_type;
35 using NT = typename Tpetra::Vector<>::node_type;
36
37 using MV = typename Tpetra::MultiVector<ST,LO,GO,NT>;
38 using OP = typename Tpetra::Operator<ST,LO,GO,NT>;
39 using MVT = typename Belos::MultiVecTraits<ST,MV>;
40 using OPT = typename Belos::OperatorTraits<ST,MV,OP>;
41
42 using tcrsmatrix_t = typename Tpetra::CrsMatrix<ST,LO,GO,NT>;
43 using tmap_t = typename Tpetra::Map<LO,GO,NT>;
44
45 using Teuchos::CommandLineProcessor;
46 using Teuchos::ParameterList;
47 using Teuchos::RCP;
48 using Teuchos::rcp;
49 using Teuchos::tuple;
50
51 Teuchos::GlobalMPISession session(&argc, &argv, NULL);
52
53 const auto comm = Tpetra::getDefaultComm();
54 const int MyPID = 0;
55
56 bool success = true;
57 bool verbose = false;
58 bool debug = false;
59
60 try {
61 int frequency = -1; // frequency of status test output.
62 int numrhs = 1; // number of right-hand sides to solve for
63 int maxiters = -1; // maximum number of iterations allowed per linear system
64 ST tol = sqrt(std::numeric_limits<ST>::epsilon()); // relative residual tolerance
65
66 // Define command-line arguments
67 CommandLineProcessor cmdp(false,true);
68 cmdp.setOption ("verbose", "quiet", &verbose, "Print messages and results.");
69 cmdp.setOption ("debug", "nodebug", &debug,
70 "Print debugging information from the solver.");
71 cmdp.setOption ("frequency", &frequency,
72 "Solver's frequency for printing residuals (#iters).");
73 cmdp.setOption ("tol", &tol,
74 "Relative residual tolerance used by MINRES solver.");
75 cmdp.setOption ("num-rhs", &numrhs,
76 "Number of right-hand sides for which to solve (> 0).");
77 cmdp.setOption ("max-iters", &maxiters,
78 "Maximum number of iterations per linear system. -1 means "
79 "we choose, based on problem size.");
80
81 // Parse command-line arguments and fetch values
82 if (cmdp.parse(argc,argv) != CommandLineProcessor::PARSE_SUCCESSFUL)
83 {
84 std::cout << "Failed to parse command-line arguments!" << std::endl;
85 std::cout << "End Result: TEST FAILED" << std::endl;
86 return -1;
87 }
88
89 // **********************************************************************
90 // ******************Set up the problem to be solved*********************
91 // construct diagonal matrix
92 const int numGlobalElements = 100;
93 const int m = 4; // number of negative eigenvalues
94
95 // Create diagonal matrix with n-m positive and m negative eigenvalues.
96 RCP<tmap_t> map = rcp(new tmap_t(numGlobalElements, 0, comm));
97
98 RCP<tcrsmatrix_t> A = rcp( new tcrsmatrix_t(map, 1));
99 for ( size_t k=0; k<map->getLocalNumElements(); k++ )
100 {
101 GO gid = map->getGlobalElement(k);
102 const ST val = static_cast<ST> (2 * (gid-m) + 1);
103 A->insertGlobalValues(gid, tuple<GO> (gid), tuple<ST> (val));
104 }
105 A->fillComplete();
106 TEUCHOS_ASSERT( A->isFillComplete() );
107 TEUCHOS_ASSERT( A->isStorageOptimized() );
108
109 //
110 // Make some (multi)vectors, for use in testing: an exact solution,
111 // its corresponding right-hand side, and an initial guess.
112 //
113
114 // Make a random exact solution.
115 RCP<MV> X_exact (new MV(map, numrhs));
116 MVT::MvRandom (*X_exact);
117
118 // Compute the right-hand side as B = A*X.
119 RCP<MV> B = MVT::Clone (*X_exact, numrhs);
120 OPT::Apply (*A, *X_exact, *B);
121
122 // Choose an initial guess of all zeros.
123 RCP<MV> X = MVT::Clone (*X_exact, numrhs);
124 MVT::MvInit (*X, ST(0.0));
125
126 // **********************************************************************
127 //
128 // Set parameters that the user asked us to pick intelligently.
129 //
130
131 // Maximum number of iterations: set according to problem size.
132 // maxiters=numGlobalElements-1 may not always converge, so we
133 // set to numGlobalElements+1 for good measure.
134 if (maxiters == -1)
135 maxiters = numGlobalElements + 1;
136
137 // In a nonverbose test, the frequency should be set to -1, which
138 // Belos interprets as "no intermediate status output." Override
139 // whatever the user may have specified.
140 if (! verbose)
141 frequency = -1;
142 // Silently fix a bad frequency value.
143 else if (frequency < 0 && frequency != -1)
144 frequency = -1;
145
146 // Validate command-line arguments
147 TEUCHOS_TEST_FOR_EXCEPTION( tol < 0, std::invalid_argument,
148 "Relative residual tolerance must be nonnegative, but "
149 "you supplied tol = " << tol << "." );
150 TEUCHOS_TEST_FOR_EXCEPTION( numrhs < 1, std::invalid_argument,
151 "MINRES test requires at least one right-hand side, but "
152 "you set the number of right-hand sides to "
153 << numrhs << "." );
154 TEUCHOS_TEST_FOR_EXCEPTION( maxiters < 1, std::invalid_argument,
155 "MINRES test requires at least one iteration, but you "
156 "set the maximum number of iterations to "
157 << maxiters << "." );
158
159 const bool proc_verbose = verbose && (MyPID==0); /* Only print on the zero processor */
160
161 //
162 // ********Other information used by block solver***********
163 // *****************(can be user specified)******************
164 //
165
166 //
167 ParameterList belosList;
168 belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed
169 belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
170
171 int verbosity = Belos::Errors + Belos::Warnings;
172 if (verbose) {
173 verbosity += Belos::TimingDetails + Belos::StatusTestDetails;
174 if (frequency > 0)
175 belosList.set( "Output Frequency", frequency );
176 }
177 if (debug) {
178 verbosity += Belos::Debug;
179 }
180 belosList.set( "Verbosity", (int) verbosity );
181 //
182 // Construct an unpreconditioned linear problem instance.
183 //
184 Belos::LinearProblem< ST, MV, OP > problem (A, X, B);
185 {
186 const bool set = problem.setProblem();
187 TEUCHOS_TEST_FOR_EXCEPTION( set == false, std::logic_error,
188 "Belos::LinearProblem failed to set up correctly (setP"
189 "roblem() returned false)! This probably means we imp"
190 "lemented our test incorrectly." );
191 }
192 //
193 // *******************************************************************
194 // ****************Start the MINRES iteration*************************
195 // *******************************************************************
196 //
197 // Create an iterative solver manager.
198 RCP< Belos::SolverManager<ST,MV,OP> > newSolver
199 = rcp( new Belos::MinresSolMgr<ST,MV,OP>(rcp(&problem,false), rcp(&belosList,false)));
200
201 //
202 // **********Print out information about problem*******************
203 //
204 if (proc_verbose) {
205 std::cout << std::endl << std::endl;
206 std::cout << "Dimension of matrix: " << numGlobalElements << std::endl;
207 std::cout << "Number of right-hand sides: " << numrhs << std::endl;
208 std::cout << "Relative residual tolerance: " << tol << std::endl;
209 std::cout << std::endl;
210 }
211 //
212 // Perform solve
213 //
214 Belos::ReturnType ret = Belos::Unconverged;
215 try {
216 ret = newSolver->solve();
217 } catch (std::exception& e) {
218 if (MyPID == 0)
219 std::cout << "MINRES solver threw an exception: " << e.what() << std::endl;
220 throw e;
221 }
222 //
223 // Compute actual residuals.
224 //
225 bool badRes = false;
226 std::vector<ST> actualResids( numrhs );
227 std::vector<ST> rhsNorm( numrhs );
228 MV resid(A->getMap(), numrhs);
229 OPT::Apply( *A, *X, resid );
230 MVT::MvAddMv( -1.0, resid, 1.0, *B, resid );
231 MVT::MvNorm( resid, actualResids );
232 MVT::MvNorm( *B, rhsNorm );
233 if (proc_verbose) {
234 std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl;
235 for ( int i=0; i<numrhs; i++) {
236 double actRes = actualResids[i]/rhsNorm[i];
237 std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl;
238 if (actRes > tol) badRes = true;
239 }
240 }
241
242 if (ret!=Belos::Converged || badRes) {
243 success = false;
244 if (proc_verbose)
245 std::cout << std::endl << "End Result: TEST FAILED" << std::endl;
246 } else {
247 success = true;
248 if (proc_verbose)
249 std::cout << std::endl << "End Result: TEST PASSED" << std::endl;
250 }
251 }
252 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
253
254 return (success ? EXIT_SUCCESS : EXIT_FAILURE);
255 }
256
257 int main(int argc, char *argv[]) {
258 // run with different scalar types
259 return run<double>(argc, argv);
260 // return run<float>(argc, argv); // FAILS
261 }