Debian Codegraph @

packages/belos/epetra/example/BlockGmres/BlockGmresPolyEpetraExFile.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 "BelosGmresPolySolMgr.hpp"
21 #include "BelosBlockGmresSolMgr.hpp"
22
23 #include "EpetraExt_readEpetraLinearSystem.h"
24 #include "Epetra_Map.h"
25 #ifdef EPETRA_MPI
26 #include "Epetra_MpiComm.h"
27 #else
28 #include "Epetra_SerialComm.h"
29 #endif
30 #include "Epetra_CrsMatrix.h"
31
32 #include "Ifpack.h"
33
34 #include "Teuchos_CommandLineProcessor.hpp"
35 #include "Teuchos_ParameterList.hpp"
36 #include "Teuchos_StandardCatchMacros.hpp"
37
38 int main(int argc, char *argv[]) {
39 //
40 int MyPID = 0;
41 #ifdef EPETRA_MPI
42 // Initialize MPI
43 MPI_Init(&argc,&argv);
44 Epetra_MpiComm Comm(MPI_COMM_WORLD);
45 MyPID = Comm.MyPID();
46 #else
47 Epetra_SerialComm Comm;
48 #endif
49 //
50 typedef double ST;
51 typedef Teuchos::ScalarTraits<ST> SCT;
52 typedef SCT::magnitudeType MT;
53 typedef Epetra_MultiVector MV;
54 typedef Epetra_Operator OP;
55 typedef Belos::MultiVecTraits<ST,MV> MVT;
56 typedef Belos::OperatorTraits<ST,MV,OP> OPT;
57
58 using Teuchos::ParameterList;
59 using Teuchos::RCP;
60 using Teuchos::rcp;
61
62 bool verbose = false;
63 bool success = true;
64 try {
65 bool proc_verbose = false;
66 bool debug = false;
67 bool userandomrhs = true;
68 int frequency = -1; // frequency of status test output.
69 int blocksize = 1; // blocksize
70 int numrhs = 1; // number of right-hand sides to solve for
71 int maxiters = -1; // maximum number of iterations allowed per linear system
72 int maxdegree = 25; // maximum degree of polynomial
73 int maxsubspace = 50; // maximum number of blocks the solver can use for the subspace
74 int maxrestarts = 15; // number of restarts allowed
75 std::string outersolver("Block Gmres");
76 std::string polytype("Arnoldi");
77 std::string filename("orsirr1.hb");
78 std::string precond("right");
79 MT tol = 1.0e-5; // relative residual tolerance
80 MT polytol = tol/10; // relative residual tolerance for polynomial construction
81
82 Teuchos::CommandLineProcessor cmdp(false,true);
83 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
84 cmdp.setOption("debug","nondebug",&debug,"Print debugging information from solver.");
85 cmdp.setOption("use-random-rhs","use-rhs",&userandomrhs,"Use linear system RHS or random RHS to generate polynomial.");
86 cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
87 cmdp.setOption("filename",&filename,"Filename for test matrix. Acceptable file extensions: *.hb,*.mtx,*.triU,*.triS");
88 cmdp.setOption("outersolver",&outersolver,"Name of outer solver to be used with GMRES poly");
89 cmdp.setOption("poly-type",&polytype,"Name of the polynomial to be generated.");
90 cmdp.setOption("precond",&precond,"Preconditioning type (none, left, right).");
91 cmdp.setOption("tol",&tol,"Relative residual tolerance used by GMRES solver.");
92 cmdp.setOption("poly-tol",&polytol,"Relative residual tolerance used to construct the GMRES polynomial.");
93 cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for.");
94 cmdp.setOption("block-size",&blocksize,"Block size used by GMRES.");
95 cmdp.setOption("max-iters",&maxiters,"Maximum number of iterations per linear system (-1 = adapted to problem/block size).");
96 cmdp.setOption("max-degree",&maxdegree,"Maximum degree of the GMRES polynomial.");
97 cmdp.setOption("max-subspace",&maxsubspace,"Maximum number of blocks the solver can use for the subspace.");
98 cmdp.setOption("max-restarts",&maxrestarts,"Maximum number of restarts allowed for GMRES solver.");
99 if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
100 return -1;
101 }
102 if (!verbose)
103 frequency = -1; // reset frequency if test is not verbose
104 //
105 // Get the problem
106 //
107 RCP<Epetra_Map> Map;
108 RCP<Epetra_CrsMatrix> A;
109 RCP<Epetra_MultiVector> B, X;
110 RCP<Epetra_Vector> vecB, vecX;
111 EpetraExt::readEpetraLinearSystem(filename, Comm, &A, &Map, &vecX, &vecB);
112 A->OptimizeStorage();
113 proc_verbose = verbose && (MyPID==0); /* Only print on the zero processor */
114
115 // Check to see if the number of right-hand sides is the same as requested.
116 if (numrhs>1) {
117 X = rcp( new Epetra_MultiVector( *Map, numrhs ) );
118 B = rcp( new Epetra_MultiVector( *Map, numrhs ) );
119 X->Random();
120 OPT::Apply( *A, *X, *B );
121 X->PutScalar( 0.0 );
122 }
123 else {
124 X = Teuchos::rcp_implicit_cast<Epetra_MultiVector>(vecX);
125 B = Teuchos::rcp_implicit_cast<Epetra_MultiVector>(vecB);
126 }
127 //
128 // ************Construct preconditioner*************
129 //
130 RCP<Belos::EpetraPrecOp> belosPrec;
131
132 if (precond != "none") {
133 ParameterList ifpackList;
134
135 // allocates an IFPACK factory. No data is associated
136 // to this object (only method Create()).
137 Ifpack Factory;
138
139 // create the preconditioner. For valid PrecType values,
140 // please check the documentation
141 std::string PrecType = "ILU"; // incomplete LU
142 int OverlapLevel = 1; // must be >= 0. If Comm.NumProc() == 1,
143 // it is ignored.
144
145 RCP<Ifpack_Preconditioner> Prec = Teuchos::rcp( Factory.Create(PrecType, &*A, OverlapLevel) );
146 assert(Prec != Teuchos::null);
147
148 // specify parameters for ILU
149 ifpackList.set("fact: level-of-fill", 1);
150 // the combine mode is on the following:
151 // "Add", "Zero", "Insert", "InsertAdd", "Average", "AbsMax"
152 // Their meaning is as defined in file Epetra_CombineMode.h
153 ifpackList.set("schwarz: combine mode", "Add");
154 // sets the parameters
155 IFPACK_CHK_ERR(Prec->SetParameters(ifpackList));
156
157 // initialize the preconditioner. At this point the matrix must
158 // have been FillComplete()'d, but actual values are ignored.
159 IFPACK_CHK_ERR(Prec->Initialize());
160
161 // Builds the preconditioners, by looking for the values of
162 // the matrix.
163 IFPACK_CHK_ERR(Prec->Compute());
164
165 // Create the Belos preconditioned operator from the Ifpack preconditioner.
166 // NOTE: This is necessary because Belos expects an operator to apply the
167 // preconditioner with Apply() NOT ApplyInverse().
168 belosPrec = rcp( new Belos::EpetraPrecOp( Prec ) );
169 }
170 //
171 // ********Other information used by block solver***********
172 // *****************(can be user specified)******************
173 //
174 const int NumGlobalElements = B->GlobalLength();
175 if (maxiters == -1)
176 maxiters = NumGlobalElements/blocksize - 1; // maximum number of iterations to run
177 //
178 ParameterList belosList;
179 belosList.set( "Num Blocks", maxsubspace); // Maximum number of blocks in Krylov factorization
180 belosList.set( "Block Size", blocksize ); // Blocksize to be used by iterative solver
181 belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed
182 belosList.set( "Maximum Restarts", maxrestarts ); // Maximum number of restarts allowed
183 belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
184 int verbosity = Belos::Errors + Belos::Warnings;
185 if (verbose) {
186 verbosity += Belos::FinalSummary + Belos::TimingDetails + Belos::StatusTestDetails;
187 if (frequency > 0)
188 belosList.set( "Output Frequency", frequency );
189 }
190 if (debug) {
191 verbosity += Belos::Debug;
192 }
193 belosList.set( "Verbosity", verbosity );
194
195 ParameterList polyList;
196 polyList.set( "Polynomial Type", polytype ); // Type of polynomial to be generated
197 polyList.set( "Maximum Degree", maxdegree ); // Maximum degree of the GMRES polynomial
198 polyList.set( "Polynomial Tolerance", polytol ); // Polynomial convergence tolerance requested
199 polyList.set( "Verbosity", verbosity ); // Verbosity for polynomial construction
200 polyList.set( "Random RHS", userandomrhs ); // Use RHS from linear system or random vector
201 if ( outersolver != "" ) {
202 polyList.set( "Outer Solver", outersolver );
203 polyList.set( "Outer Solver Params", belosList );
204 }
205 //
206 // Construct an unpreconditioned linear problem instance.
207 //
208 Belos::LinearProblem<double,MV,OP> problem( A, X, B );
209 problem.setInitResVec( B );
210 if (precond == "left") {
211 problem.setLeftPrec( belosPrec );
212 }
213 if (precond == "right") {
214 problem.setRightPrec( belosPrec );
215 }
216 bool set = problem.setProblem();
217 if (set == false) {
218 if (proc_verbose)
219 std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
220 return -1;
221 }
222 //
223 // *******************************************************************
224 // *************Start the block Gmres iteration*************************
225 // *******************************************************************
226 //
227 // Create an iterative solver manager.
228 RCP< Belos::SolverManager<double,MV,OP> > newSolver
229 = rcp( new Belos::GmresPolySolMgr<double,MV,OP>(rcp(&problem,false), rcp(&polyList,false)));
230
231 //
232 // **********Print out information about problem*******************
233 //
234 if (proc_verbose) {
235 std::cout << std::endl << std::endl;
236 std::cout << "Dimension of matrix: " << NumGlobalElements << std::endl;
237 std::cout << "Number of right-hand sides: " << numrhs << std::endl;
238 std::cout << "Block size used by solver: " << blocksize << std::endl;
239 std::cout << "Max number of restarts allowed: " << maxrestarts << std::endl;
240 std::cout << "Max number of Gmres iterations per restart cycle: " << maxiters << std::endl;
241 std::cout << "Relative residual tolerance: " << tol << std::endl;
242 std::cout << std::endl;
243 }
244 //
245 // Perform solve
246 //
247 Belos::ReturnType ret = newSolver->solve();
248 //
249 // Compute actual residuals.
250 //
251 bool badRes = false;
252 std::vector<double> actual_resids( numrhs );
253 std::vector<double> rhs_norm( numrhs );
254 Epetra_MultiVector resid(*Map, numrhs);
255 OPT::Apply( *A, *X, resid );
256 MVT::MvAddMv( -1.0, resid, 1.0, *B, resid );
257 MVT::MvNorm( resid, actual_resids );
258 MVT::MvNorm( *B, rhs_norm );
259 if (proc_verbose) {
260 std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl;
261 for ( int i=0; i<numrhs; i++) {
262 double actRes = actual_resids[i]/rhs_norm[i];
263 std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl;
264 if (actRes > tol) badRes = true;
265 }
266 }
267
268 if (ret!=Belos::Converged || badRes) {
269 success = false;
270 if (proc_verbose)
271 std::cout << std::endl << "ERROR: Belos did not converge!" << std::endl;
272 } else {
273 success = true;
274 if (proc_verbose)
275 std::cout << std::endl << "SUCCESS: Belos converged!" << std::endl;
276 }
277 }
278 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
279
280 #ifdef EPETRA_MPI
281 MPI_Finalize();
282 #endif
283
284 return success ? EXIT_SUCCESS : EXIT_FAILURE;
285 }