Debian Codegraph @

packages/belos/epetra/test/MINRES/test_minres_hb.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 // This driver reads a problem from a Harwell-Boeing (HB) file.
11 // The right-hand-side from the HB file is used instead of random vectors.
12 // The initial guesses are all set to zero.
13 //
14 // NOTE: No preconditioner is used in this case.
15 //
16 #include "BelosConfigDefs.hpp"
17 #include "BelosLinearProblem.hpp"
18 #include "BelosEpetraAdapter.hpp"
19 #include "BelosSolverFactory.hpp"
20 #include "BelosEpetraUtils.h"
21 #include "Trilinos_Util.h"
22 #include "Epetra_Comm.h"
23 #include "Epetra_CrsMatrix.h"
24 #include "Epetra_Map.h"
25 #include "Teuchos_CommandLineProcessor.hpp"
26 #include "Teuchos_ParameterList.hpp"
27 #include <Teuchos_StandardCatchMacros.hpp>
28
29 int
30 main (int argc, char *argv[])
31 {
32 using Teuchos::inOutArg;
33 using Teuchos::ParameterList;
34 using Teuchos::parameterList;
35 using Teuchos::RCP;
36 using Teuchos::rcp;
37 using Teuchos::rcpFromRef;
38 using std::endl;
39 typedef double ST;
40 typedef Epetra_Operator OP;
41 typedef Epetra_MultiVector MV;
42 typedef Belos::OperatorTraits<ST,MV,OP> OPT;
43 typedef Belos::MultiVecTraits<ST,MV> MVT;
44
45 // This calls MPI_Init and MPI_Finalize as necessary.
46 Belos::Test::MPISession session (inOutArg (argc), inOutArg (argv));
47 RCP<const Epetra_Comm> comm = session.getComm ();
48
49 bool success = false;
50 bool verbose = false;
51 try {
52 int MyPID = comm->MyPID ();
53
54 //
55 // Parameters to read from command-line processor
56 //
57 int frequency = -1; // how often residuals are printed by solver
58 int numRHS = 1; // total number of right-hand sides to solve for
59 int maxIters = 13000; // maximum number of iterations for solver to use
60 std::string filename ("bcsstk14.hb");
61 double tol = 1.0e-5; // relative residual tolerance
62
63 //
64 // Read in command-line arguments
65 //
66 Teuchos::CommandLineProcessor cmdp (false, true);
67 cmdp.setOption ("verbose", "quiet", &verbose, "Print messages and results.");
68 cmdp.setOption ("frequency", &frequency, "Solvers frequency for printing "
69 "residuals (#iters).");
70 cmdp.setOption ("tol", &tol, "Relative residual tolerance used by MINRES "
71 "solver.");
72 cmdp.setOption ("filename", &filename, "Filename for Harwell-Boeing test "
73 "matrix.");
74 cmdp.setOption ("num-rhs", &numRHS, "Number of right-hand sides to solve.");
75 cmdp.setOption ("max-iters", &maxIters, "Maximum number of iterations per "
76 "linear system (-1 means \"adapt to problem/block size\").");
77 if (cmdp.parse (argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
78 return EXIT_FAILURE;
79 }
80 Teuchos::oblackholestream blackHole;
81 std::ostream& verbOut = (verbose && MyPID == 0) ? std::cout : blackHole;
82
83 //
84 // Generate the linear system(s) to solve.
85 //
86 verbOut << "Generating the linear system(s) to solve" << endl << endl;
87 RCP<Epetra_CrsMatrix> A;
88 RCP<Epetra_MultiVector> B, X;
89 RCP<Epetra_Map> rowMap;
90 int tmp_numRHS = numRHS;
91 try {
92 // This might change the number of right-hand sides, if we read in
93 // a right-hand side from the Harwell-Boeing file.
94 Belos::Util::createEpetraProblem (filename, &rowMap, &A, &B, &X, &MyPID, tmp_numRHS);
95 } catch (std::exception& e) {
96 TEUCHOS_TEST_FOR_EXCEPTION (true, std::runtime_error,
97 "Failed to create Epetra problem for matrix "
98 "filename \"" << filename << "\". "
99 "createEpetraProblem() reports the following "
100 "error: " << e.what());
101 }
102
103 //
104 // *****Construct initial guess and random right-hand-sides *****
105 //
106 if (tmp_numRHS != numRHS)
107 {
108 X = rcp( new Epetra_MultiVector( A->Map(), numRHS ) );
109 MVT::MvRandom( *X );
110 B = rcp( new Epetra_MultiVector( A->Map(), numRHS ) );
111 OPT::Apply( *A, *X, *B );
112 MVT::MvInit( *X, 0.0 );
113 }
114 //
115 // Compute the initial residual norm of the problem, so we can see
116 // by how much it improved after the solve.
117 //
118 std::vector<double> initialResidualNorms (numRHS);
119 std::vector<double> initialResidualInfNorms (numRHS);
120 Epetra_MultiVector R (*rowMap, numRHS);
121 OPT::Apply (*A, *X, R);
122 MVT::MvAddMv (-1.0, R, 1.0, *B, R); // R := -(A*X) + B.
123 MVT::MvNorm (R, initialResidualNorms);
124 MVT::MvNorm (R, initialResidualInfNorms, Belos::InfNorm);
125 if (verbose) {
126 verbOut << "Initial residual 2-norms: \t";
127 for (int i = 0; i < numRHS; ++i) {
128 verbOut << initialResidualNorms[i];
129 if (i < numRHS-1) {
130 verbOut << ", ";
131 }
132 }
133 verbOut << endl << "Initial residual Inf-norms: \t";
134 for (int i = 0; i < numRHS; ++i) {
135 verbOut << initialResidualInfNorms[i];
136 if (i < numRHS-1) {
137 verbOut << ", ";
138 }
139 }
140 verbOut << endl;
141 }
142
143 std::vector<double> rhs2Norms (numRHS);
144 std::vector<double> rhsInfNorms (numRHS);
145 MVT::MvNorm (*B, rhs2Norms);
146 MVT::MvNorm (*B, rhsInfNorms, Belos::InfNorm);
147 if (verbose) {
148 verbOut << "Right-hand side 2-norms: \t";
149 for (int i = 0; i < numRHS; ++i) {
150 verbOut << rhs2Norms[i];
151 if (i < numRHS-1) {
152 verbOut << ", ";
153 }
154 }
155 verbOut << endl << "Right-hand side Inf-norms: \t";
156 for (int i = 0; i < numRHS; ++i) {
157 verbOut << rhsInfNorms[i];
158 if (i < numRHS-1) {
159 verbOut << ", ";
160 }
161 }
162 verbOut << endl;
163 }
164
165 std::vector<double> initialGuess2Norms (numRHS);
166 std::vector<double> initialGuessInfNorms (numRHS);
167 MVT::MvNorm (*X, initialGuess2Norms);
168 MVT::MvNorm (*X, initialGuessInfNorms, Belos::InfNorm);
169 if (verbose) {
170 verbOut << "Initial guess 2-norms: \t";
171 for (int i = 0; i < numRHS; ++i) {
172 verbOut << initialGuess2Norms[i];
173 if (i < numRHS-1) {
174 verbOut << ", ";
175 }
176 }
177 verbOut << endl << "Initial guess Inf-norms: \t";
178 for (int i = 0; i < numRHS; ++i) {
179 verbOut << initialGuessInfNorms[i];
180 if (i < numRHS-1) {
181 verbOut << ", ";
182 }
183 }
184 verbOut << endl;
185 }
186 //
187 // Compute the infinity-norm of A.
188 //
189 const double normOfA = A->NormInf ();
190 verbOut << "||A||_inf: \t" << normOfA << endl;
191 //
192 // Compute ||A|| ||X_i|| + ||B_i|| for each right-hand side B_i.
193 //
194 std::vector<double> scaleFactors (numRHS);
195 for (int i = 0; i < numRHS; ++i) {
196 scaleFactors[i] = normOfA * initialGuessInfNorms[i] + rhsInfNorms[i];
197 }
198 if (verbose) {
199 verbOut << "||A||_inf ||X_i||_inf + ||B_i||_inf: \t";
200 for (int i = 0; i < numRHS; ++i) {
201 verbOut << scaleFactors[i];
202 if (i < numRHS-1) {
203 verbOut << ", ";
204 }
205 }
206 verbOut << endl;
207 }
208
209 //
210 // Solve using Belos
211 //
212 verbOut << endl << "Setting up Belos" << endl;
213 const int NumGlobalElements = B->GlobalLength();
214
215 // Set up Belos solver parameters.
216 RCP<ParameterList> belosList = parameterList ("MINRES");
217 belosList->set ("Maximum Iterations", maxIters);
218 belosList->set ("Convergence Tolerance", tol);
219 if (verbose) {
220 belosList->set ("Verbosity", Belos::Errors + Belos::Warnings +
221 Belos::IterationDetails + Belos::OrthoDetails +
222 Belos::FinalSummary + Belos::TimingDetails + Belos::Debug);
223 belosList->set ("Output Frequency", frequency);
224 }
225 else {
226 belosList->set ("Verbosity", Belos::Errors + Belos::Warnings);
227 }
228 belosList->set ("Output Stream", rcpFromRef (verbOut));
229
230 // Construct an unpreconditioned linear problem instance.
231 typedef Belos::LinearProblem<double,MV,OP> prob_type;
232 RCP<prob_type> problem = rcp (new prob_type (A, X, B));
233 if (! problem->setProblem()) {
234 verbOut << endl << "ERROR: Failed to set up Belos::LinearProblem!" << endl;
235 return EXIT_FAILURE;
236 }
237
238 // Create an iterative solver manager.
239 Belos::SolverFactory<double, MV, OP> factory;
240 RCP<Belos::SolverManager<double,MV,OP> > newSolver =
241 factory.create ("MINRES", belosList);
242 newSolver->setProblem (problem);
243
244 // Print out information about problem. Make sure to use the
245 // information as stored in the Belos ParameterList, so that we know
246 // what the solver will do.
247 verbOut << endl
248 << "Dimension of matrix: " << NumGlobalElements << endl
249 << "Number of right-hand sides: " << numRHS << endl
250 << "Max number of MINRES iterations: "
251 << belosList->get<int> ("Maximum Iterations") << endl
252 << "Relative residual tolerance: "
253 << belosList->get<double> ("Convergence Tolerance") << endl
254 << "Output frequency: "
255 << belosList->get<int> ("Output Frequency") << endl
256 << endl;
257
258 // Solve the linear system.
259 verbOut << "Solving the linear system" << endl << endl;
260 Belos::ReturnType ret = newSolver->solve();
261 verbOut << "Belos results:" << endl
262 << "- Number of iterations: "
263 << newSolver->getNumIters () << endl
264 << "- " << (ret == Belos::Converged ? "Converged" : "Not converged")
265 << endl;
266 //
267 // After the solve, compute residual(s) explicitly. This tests
268 // whether the Belos solver did so correctly.
269 //
270 std::vector<double> absoluteResidualNorms (numRHS);
271 OPT::Apply (*A, *X, R);
272 MVT::MvAddMv (-1.0, R, 1.0, *B, R);
273 MVT::MvNorm (R, absoluteResidualNorms);
274
275 std::vector<double> relativeResidualNorms (numRHS);
276 for (int i = 0; i < numRHS; ++i) {
277 relativeResidualNorms[i] = (initialResidualNorms[i] == 0.0) ?
278 absoluteResidualNorms[i] :
279 absoluteResidualNorms[i] / initialResidualNorms[i];
280 }
281
282 verbOut << "---------- Computed relative residual norms ----------"
283 << endl << endl;
284 bool badRes = false;
285 if (verbose) {
286 for (int i = 0; i < numRHS; ++i) {
287 const double actRes = relativeResidualNorms[i];
288 verbOut << "Problem " << i << " : \t" << actRes << endl;
289 if (actRes > tol) {
290 badRes = true;
291 }
292 }
293 }
294 success = (ret == Belos::Converged && !badRes);
295
296 if (success) {
297 verbOut << endl << "End Result: TEST PASSED" << endl;
298 } else {
299 verbOut << endl << "End Result: TEST FAILED" << endl;
300 }
301 } // try
302 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
303
304 return success ? EXIT_SUCCESS : EXIT_FAILURE;
305 } // end test_minres_hb.cpp