Debian Codegraph @

packages/belos/epetra/example/BlockGmres/BlockGmresEpetraExFile.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 file, which can be in Harwell-Boeing (*.hb),
11 // Matrix Market (*.mtx), or triplet format (*.triU, *.triS). The right-hand side
12 // from the problem, if it exists, will be used instead of multiple
13 // random right-hand-sides. The initial guesses are all set to zero.
14 //
15 // NOTE: No preconditioner is used in this example.
16 //
17 #include "BelosConfigDefs.hpp"
18 #include "BelosLinearProblem.hpp"
19 #include "BelosEpetraAdapter.hpp"
20 #include "BelosBlockGmresSolMgr.hpp"
21
22 #include "EpetraExt_readEpetraLinearSystem.h"
23 #include "Epetra_Map.h"
24 #ifdef EPETRA_MPI
25 #include "Epetra_MpiComm.h"
26 #else
27 #include "Epetra_SerialComm.h"
28 #endif
29 #include "Epetra_CrsMatrix.h"
30
31 #include "Teuchos_CommandLineProcessor.hpp"
32 #include "Teuchos_ParameterList.hpp"
33 #include "Teuchos_StandardCatchMacros.hpp"
34
35 int main(int argc, char *argv[]) {
36 //
37 int MyPID = 0;
38 #ifdef EPETRA_MPI
39 // Initialize MPI
40 MPI_Init(&argc,&argv);
41 Epetra_MpiComm Comm(MPI_COMM_WORLD);
42 MyPID = Comm.MyPID();
43 #else
44 Epetra_SerialComm Comm;
45 #endif
46 //
47 typedef double ST;
48 typedef Teuchos::ScalarTraits<ST> SCT;
49 typedef SCT::magnitudeType MT;
50 typedef Epetra_MultiVector MV;
51 typedef Epetra_Operator OP;
52 typedef Belos::MultiVecTraits<ST,MV> MVT;
53 typedef Belos::OperatorTraits<ST,MV,OP> OPT;
54
55 using Teuchos::ParameterList;
56 using Teuchos::RCP;
57 using Teuchos::rcp;
58
59 bool verbose = false;
60 bool success = true;
61 try {
62 bool proc_verbose = false;
63 bool debug = false;
64 int frequency = -1; // frequency of status test output.
65 int blocksize = 1; // blocksize
66 int numrhs = 1; // number of right-hand sides to solve for
67 int maxiters = -1; // maximum number of iterations allowed per linear system
68 int maxsubspace = 50; // maximum number of blocks the solver can use for the subspace
69 int maxrestarts = 15; // number of restarts allowed
70 std::string filename("orsirr1.hb");
71 MT tol = 1.0e-5; // relative residual tolerance
72 std::string ortho = "ICGS";// orthogonalization method
73
74 Teuchos::CommandLineProcessor cmdp(false,true);
75 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
76 cmdp.setOption("debug","nondebug",&debug,"Print debugging information from solver.");
77 cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
78 cmdp.setOption("filename",&filename,"Filename for test matrix. Acceptable file extensions: *.hb,*.mtx,*.triU,*.triS");
79 cmdp.setOption("tol",&tol,"Relative residual tolerance used by GMRES solver.");
80 cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for.");
81 cmdp.setOption("block-size",&blocksize,"Block size used by GMRES.");
82 cmdp.setOption("max-iters",&maxiters,"Maximum number of iterations per linear system (-1 = adapted to problem/block size).");
83 cmdp.setOption("max-subspace",&maxsubspace,"Maximum number of blocks the solver can use for the subspace.");
84 cmdp.setOption("max-restarts",&maxrestarts,"Maximum number of restarts allowed for GMRES solver.");
85 cmdp.setOption("ortho",&ortho,"Orthogonalization being used by GMRES solver.");
86 if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
87 return -1;
88 }
89 if (!verbose)
90 frequency = -1; // reset frequency if test is not verbose
91 //
92 // Get the problem
93 //
94 RCP<Epetra_Map> Map;
95 RCP<Epetra_CrsMatrix> A;
96 RCP<Epetra_MultiVector> B, X;
97 RCP<Epetra_Vector> vecB, vecX;
98 EpetraExt::readEpetraLinearSystem(filename, Comm, &A, &Map, &vecX, &vecB);
99 A->OptimizeStorage();
100 proc_verbose = verbose && (MyPID==0); /* Only print on the zero processor */
101
102 // Check to see if the number of right-hand sides is the same as requested.
103 if (numrhs>1) {
104 X = rcp( new Epetra_MultiVector( *Map, numrhs ) );
105 B = rcp( new Epetra_MultiVector( *Map, numrhs ) );
106 X->Random();
107 OPT::Apply( *A, *X, *B );
108 X->PutScalar( 0.0 );
109 }
110 else {
111 X = Teuchos::rcp_implicit_cast<Epetra_MultiVector>(vecX);
112 B = Teuchos::rcp_implicit_cast<Epetra_MultiVector>(vecB);
113 }
114 //
115 // ********Other information used by block solver***********
116 // *****************(can be user specified)******************
117 //
118 const int NumGlobalElements = B->GlobalLength();
119 if (maxiters == -1)
120 maxiters = NumGlobalElements/blocksize - 1; // maximum number of iterations to run
121 //
122 ParameterList belosList;
123 belosList.set( "Num Blocks", maxsubspace); // Maximum number of blocks in Krylov factorization
124 belosList.set( "Block Size", blocksize ); // Blocksize to be used by iterative solver
125 belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed
126 belosList.set( "Maximum Restarts", maxrestarts ); // Maximum number of restarts allowed
127 belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
128 belosList.set( "Orthogonalization", ortho ); // Orthogonalization used by iterative solver
129 int verbosity = Belos::Errors + Belos::Warnings;
130 if (verbose) {
131 verbosity += Belos::TimingDetails + Belos::StatusTestDetails;
132 if (frequency > 0)
133 belosList.set( "Output Frequency", frequency );
134 }
135 if (debug) {
136 verbosity += Belos::Debug;
137 }
138 belosList.set( "Verbosity", verbosity );
139 //
140 // Construct an unpreconditioned linear problem instance.
141 //
142 Belos::LinearProblem<double,MV,OP> problem( A, X, B );
143 bool set = problem.setProblem();
144 if (set == false) {
145 if (proc_verbose)
146 std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
147 return -1;
148 }
149 //
150 // *******************************************************************
151 // *************Start the block Gmres iteration*************************
152 // *******************************************************************
153 //
154 // Create an iterative solver manager.
155 RCP< Belos::SolverManager<double,MV,OP> > newSolver
156 = rcp( new Belos::BlockGmresSolMgr<double,MV,OP>(rcp(&problem,false), rcp(&belosList,false)));
157
158 //
159 // **********Print out information about problem*******************
160 //
161 if (proc_verbose) {
162 std::cout << std::endl << std::endl;
163 std::cout << "Dimension of matrix: " << NumGlobalElements << std::endl;
164 std::cout << "Number of right-hand sides: " << numrhs << std::endl;
165 std::cout << "Block size used by solver: " << blocksize << std::endl;
166 std::cout << "Max number of restarts allowed: " << maxrestarts << std::endl;
167 std::cout << "Max number of Gmres iterations per linear system: " << maxiters << std::endl;
168 std::cout << "Relative residual tolerance: " << tol << std::endl;
169 std::cout << std::endl;
170 }
171 //
172 // Perform solve
173 //
174 Belos::ReturnType ret = newSolver->solve();
175 //
176 // Get the number of iterations for this solve.
177 //
178 int numIters = newSolver->getNumIters();
179 if (proc_verbose)
180 std::cout << "Number of iterations performed for this solve: " << numIters << std::endl;
181 //
182 // Compute actual residuals.
183 //
184 bool badRes = false;
185 std::vector<double> actual_resids( numrhs );
186 std::vector<double> rhs_norm( numrhs );
187 Epetra_MultiVector resid(*Map, numrhs);
188 OPT::Apply( *A, *X, resid );
189 MVT::MvAddMv( -1.0, resid, 1.0, *B, resid );
190 MVT::MvNorm( resid, actual_resids );
191 MVT::MvNorm( *B, rhs_norm );
192 if (proc_verbose) {
193 std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl;
194 for ( int i=0; i<numrhs; i++) {
195 double actRes = actual_resids[i]/rhs_norm[i];
196 std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl;
197 if (actRes > tol) badRes = true;
198 }
199 }
200
201 if (ret!=Belos::Converged || badRes) {
202 success = false;
203 if (proc_verbose)
204 std::cout << std::endl << "ERROR: Belos did not converge!" << std::endl;
205 } else {
206 success = true;
207 if (proc_verbose)
208 std::cout << std::endl << "SUCCESS: Belos converged!" << std::endl;
209 }
210 }
211 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
212
213 #ifdef EPETRA_MPI
214 MPI_Finalize();
215 #endif
216
217 return success ? EXIT_SUCCESS : EXIT_FAILURE;
218 }