Debian Codegraph @

packages/belos/epetra/example/BlockGmres/BlockPrecGmresEpetraExFile.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 random
13 // right-hand-sides. The initial guesses are all set to zero. An ILU preconditioner
14 // is constructed using the Ifpack factory.
15 //
16 #include "BelosConfigDefs.hpp"
17 #include "BelosLinearProblem.hpp"
18 #include "BelosEpetraAdapter.hpp"
19 #include "BelosBlockGmresSolMgr.hpp"
20
21 #include "EpetraExt_readEpetraLinearSystem.h"
22 #include "Epetra_Map.h"
23 #ifdef EPETRA_MPI
24 #include "Epetra_MpiComm.h"
25 #else
26 #include "Epetra_SerialComm.h"
27 #endif
28 #include "Epetra_CrsMatrix.h"
29
30 #include "Ifpack.h"
31
32 #include "Teuchos_CommandLineProcessor.hpp"
33 #include "Teuchos_ParameterList.hpp"
34 #include "Teuchos_StandardCatchMacros.hpp"
35
36 int main(int argc, char *argv[]) {
37 //
38 int MyPID = 0;
39 #ifdef EPETRA_MPI
40 // Initialize MPI
41 MPI_Init(&argc,&argv);
42 Epetra_MpiComm Comm(MPI_COMM_WORLD);
43 MyPID = Comm.MyPID();
44 #else
45 Epetra_SerialComm Comm;
46 #endif
47 //
48 typedef double ST;
49 typedef Teuchos::ScalarTraits<ST> SCT;
50 typedef SCT::magnitudeType MT;
51 typedef Epetra_MultiVector MV;
52 typedef Epetra_Operator OP;
53 typedef Belos::MultiVecTraits<ST,MV> MVT;
54 typedef Belos::OperatorTraits<ST,MV,OP> OPT;
55
56 using Teuchos::ParameterList;
57 using Teuchos::RCP;
58 using Teuchos::rcp;
59
60 bool verbose = false;
61 bool success = true;
62 try {
63 bool proc_verbose = false;
64 bool leftprec = true; // left preconditioning or right.
65 int frequency = -1; // frequency of status test output.
66 int blocksize = 1; // blocksize
67 int numrhs = 1; // number of right-hand sides to solve for
68 int maxrestarts = 15; // maximum number of restarts allowed
69 int maxiters = -1; // maximum number of iterations allowed per linear system
70 int maxsubspace = 25; // maximum number of blocks the solver can use for the subspace
71 std::string filename("orsirr1.hb");
72 MT tol = 1.0e-5; // relative residual tolerance
73
74 Teuchos::CommandLineProcessor cmdp(false,true);
75 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
76 cmdp.setOption("left-prec","right-prec",&leftprec,"Left preconditioning or right.");
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 if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
86 return -1;
87 }
88 if (!verbose)
89 frequency = -1; // reset frequency if test is not verbose
90
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 // ************Construct preconditioner*************
116 //
117 ParameterList ifpackList;
118
119 // allocates an IFPACK factory. No data is associated
120 // to this object (only method Create()).
121 Ifpack Factory;
122
123 // create the preconditioner. For valid PrecType values,
124 // please check the documentation
125 std::string PrecType = "ILU"; // incomplete LU
126 int OverlapLevel = 1; // must be >= 0. If Comm.NumProc() == 1,
127 // it is ignored.
128
129 RCP<Ifpack_Preconditioner> Prec = Teuchos::rcp( Factory.Create(PrecType, &*A, OverlapLevel) );
130 assert(Prec != Teuchos::null);
131
132 // specify parameters for ILU
133 ifpackList.set("fact: level-of-fill", 1);
134 // the combine mode is on the following:
135 // "Add", "Zero", "Insert", "InsertAdd", "Average", "AbsMax"
136 // Their meaning is as defined in file Epetra_CombineMode.h
137 ifpackList.set("schwarz: combine mode", "Add");
138 // sets the parameters
139 IFPACK_CHK_ERR(Prec->SetParameters(ifpackList));
140
141 // initialize the preconditioner. At this point the matrix must
142 // have been FillComplete()'d, but actual values are ignored.
143 IFPACK_CHK_ERR(Prec->Initialize());
144
145 // Builds the preconditioners, by looking for the values of
146 // the matrix.
147 IFPACK_CHK_ERR(Prec->Compute());
148
149 // Create the Belos preconditioned operator from the Ifpack preconditioner.
150 // NOTE: This is necessary because Belos expects an operator to apply the
151 // preconditioner with Apply() NOT ApplyInverse().
152 RCP<Belos::EpetraPrecOp> belosPrec = rcp( new Belos::EpetraPrecOp( Prec ) );
153
154 //
155 // *****Create parameter list for the block GMRES solver manager*****
156 //
157 const int NumGlobalElements = B->GlobalLength();
158 if (maxiters == -1)
159 maxiters = NumGlobalElements/blocksize - 1; // maximum number of iterations to run
160 //
161 ParameterList belosList;
162 belosList.set( "Num Blocks", maxsubspace ); // Maximum number of blocks in Krylov factorization
163 belosList.set( "Block Size", blocksize ); // Blocksize to be used by iterative solver
164 belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed
165 belosList.set( "Maximum Restarts", maxrestarts ); // Maximum number of restarts allowed
166 belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
167 if (numrhs > 1) {
168 belosList.set( "Show Maximum Residual Norm Only", true ); // Show only the maximum residual norm
169 }
170 if (verbose) {
171 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings +
172 Belos::TimingDetails + Belos::StatusTestDetails );
173 if (frequency > 0)
174 belosList.set( "Output Frequency", frequency );
175 }
176 else
177 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings );
178 //
179 // *******Construct a preconditioned linear problem********
180 //
181 RCP<Belos::LinearProblem<double,MV,OP> > problem
182 = rcp( new Belos::LinearProblem<double,MV,OP>( A, X, B ) );
183 if (leftprec) {
184 problem->setLeftPrec( belosPrec );
185 }
186 else {
187 problem->setRightPrec( belosPrec );
188 }
189 bool set = problem->setProblem();
190 if (set == false) {
191 if (proc_verbose)
192 std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
193 return -1;
194 }
195
196 // Create an iterative solver manager.
197 RCP< Belos::SolverManager<double,MV,OP> > solver
198 = rcp( new Belos::BlockGmresSolMgr<double,MV,OP>(problem, rcp(&belosList,false)));
199
200 //
201 // *******************************************************************
202 // *************Start the block Gmres iteration*************************
203 // *******************************************************************
204 //
205 if (proc_verbose) {
206 std::cout << std::endl << std::endl;
207 std::cout << "Dimension of matrix: " << NumGlobalElements << std::endl;
208 std::cout << "Number of right-hand sides: " << numrhs << std::endl;
209 std::cout << "Block size used by solver: " << blocksize << std::endl;
210 std::cout << "Number of restarts allowed: " << maxrestarts << std::endl;
211 std::cout << "Max number of Gmres iterations per restart cycle: " << maxiters << std::endl;
212 std::cout << "Relative residual tolerance: " << tol << std::endl;
213 std::cout << std::endl;
214 }
215 //
216 // Perform solve
217 //
218 Belos::ReturnType ret = solver->solve();
219 //
220 // Get the number of iterations for this solve.
221 //
222 int numIters = solver->getNumIters();
223 if (proc_verbose)
224 std::cout << "Number of iterations performed for this solve: " << numIters << std::endl;
225 //
226 // Compute actual residuals.
227 //
228 bool badRes = false;
229 std::vector<double> actual_resids( numrhs );
230 std::vector<double> rhs_norm( numrhs );
231 Epetra_MultiVector resid(*Map, numrhs);
232 OPT::Apply( *A, *X, resid );
233 MVT::MvAddMv( -1.0, resid, 1.0, *B, resid );
234 MVT::MvNorm( resid, actual_resids );
235 MVT::MvNorm( *B, rhs_norm );
236 if (proc_verbose) {
237 std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl;
238 for ( int i=0; i<numrhs; i++) {
239 double actRes = actual_resids[i]/rhs_norm[i];
240 std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl;
241 if (actRes > tol) badRes = true;
242 }
243 }
244
245 if (ret!=Belos::Converged || badRes) {
246 success = false;
247 if (proc_verbose)
248 std::cout << std::endl << "ERROR: Belos did not converge!" << std::endl;
249 } else {
250 success = true;
251 if (proc_verbose)
252 std::cout << std::endl << "SUCCESS: Belos converged!" << std::endl;
253 }
254 }
255 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
256
257 #ifdef EPETRA_MPI
258 MPI_Finalize();
259 #endif
260
261 return success ? EXIT_SUCCESS : EXIT_FAILURE;
262 }