Debian Codegraph @

packages/belos/epetra/example/BlockGmres/BlockFlexGmresEpetraExFile.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 success = false;
61 bool verbose = false;
62 try {
63 bool proc_verbose = 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 maxrestarts = 15; // maximum number of restarts allowed
68 int maxiters = -1; // maximum number of iterations allowed per linear system
69 int maxsubspace = 25; // maximum number of blocks the solver can use for the subspace
70 std::string filename("orsirr1.hb");
71 MT tol = 1.0e-5; // relative residual tolerance
72
73 // Specify whether to use RHS as initial guess. If false, use zero.
74 bool useRHSAsInitialGuess = false;
75
76 Teuchos::CommandLineProcessor cmdp(false,true);
77 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
78 cmdp.setOption("use-rhs","use-zero",&useRHSAsInitialGuess,"Use RHS as initial guess.");
79 cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
80 cmdp.setOption("filename",&filename,"Filename for test matrix. Acceptable file extensions: *.hb,*.mtx,*.triU,*.triS");
81 cmdp.setOption("tol",&tol,"Relative residual tolerance used by GMRES solver.");
82 cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for.");
83 cmdp.setOption("block-size",&blocksize,"Block size used by GMRES.");
84 cmdp.setOption("max-iters",&maxiters,"Maximum number of iterations per linear system (-1 = adapted to problem/block size).");
85 cmdp.setOption("max-subspace",&maxsubspace,"Maximum number of blocks the solver can use for the subspace.");
86 cmdp.setOption("max-restarts",&maxrestarts,"Maximum number of restarts allowed for GMRES solver.");
87 if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
88 return EXIT_FAILURE;
89 }
90
91 if (!verbose)
92 frequency = -1; // reset frequency if test is not verbose
93
94 //
95 // *************Get the problem*********************
96 //
97 RCP<Epetra_Map> Map;
98 RCP<Epetra_CrsMatrix> A;
99 RCP<Epetra_MultiVector> B, X;
100 RCP<Epetra_Vector> vecB, vecX;
101 EpetraExt::readEpetraLinearSystem(filename, Comm, &A, &Map, &vecX, &vecB);
102 A->OptimizeStorage();
103 proc_verbose = verbose && (MyPID==0); /* Only print on the zero processor */
104
105 // Check to see if the number of right-hand sides is the same as requested.
106 if (numrhs>1) {
107 X = rcp( new Epetra_MultiVector( *Map, numrhs ) );
108 B = rcp( new Epetra_MultiVector( *Map, numrhs ) );
109 X->Random();
110 OPT::Apply( *A, *X, *B );
111 X->PutScalar( 0.0 );
112 }
113 else {
114 X = Teuchos::rcp_implicit_cast<Epetra_MultiVector>(vecX);
115 B = Teuchos::rcp_implicit_cast<Epetra_MultiVector>(vecB);
116 }
117
118 // If requested, use a copy of B as initial guess
119 if (useRHSAsInitialGuess)
120 {
121 X->Update(1.0, *B, 0.0);
122 }
123
124 //
125 // ************Construct preconditioner*************
126 //
127 ParameterList ifpackList;
128
129 // allocates an IFPACK factory. No data is associated
130 // to this object (only method Create()).
131 Ifpack Factory;
132
133 // create the preconditioner. For valid PrecType values,
134 // please check the documentation
135 std::string PrecType = "ILU"; // incomplete LU
136 int OverlapLevel = 1; // must be >= 0. If Comm.NumProc() == 1,
137 // it is ignored.
138
139 RCP<Ifpack_Preconditioner> Prec = Teuchos::rcp( Factory.Create(PrecType, &*A, OverlapLevel) );
140 assert(Prec != Teuchos::null);
141
142 // specify parameters for ILU
143 ifpackList.set("fact: level-of-fill", 1);
144 // the combine mode is on the following:
145 // "Add", "Zero", "Insert", "InsertAdd", "Average", "AbsMax"
146 // Their meaning is as defined in file Epetra_CombineMode.h
147 ifpackList.set("schwarz: combine mode", "Add");
148 // sets the parameters
149 IFPACK_CHK_ERR(Prec->SetParameters(ifpackList));
150
151 // initialize the preconditioner. At this point the matrix must
152 // have been FillComplete()'d, but actual values are ignored.
153 IFPACK_CHK_ERR(Prec->Initialize());
154
155 // Builds the preconditioners, by looking for the values of
156 // the matrix.
157 IFPACK_CHK_ERR(Prec->Compute());
158
159 // Create the Belos preconditioned operator from the Ifpack preconditioner.
160 // NOTE: This is necessary because Belos expects an operator to apply the
161 // preconditioner with Apply() NOT ApplyInverse().
162 RCP<Belos::EpetraPrecOp> belosPrec = rcp( new Belos::EpetraPrecOp( Prec ) );
163
164 //
165 // *****Create parameter list for the block GMRES solver manager*****
166 //
167 const int NumGlobalElements = B->GlobalLength();
168 if (maxiters == -1)
169 maxiters = NumGlobalElements/blocksize - 1; // maximum number of iterations to run
170 //
171 ParameterList belosList;
172 belosList.set( "Flexible Gmres", true ); // Flexible Gmres will be used to solve this problem
173 belosList.set( "Num Blocks", maxsubspace ); // Maximum number of blocks in Krylov factorization
174 belosList.set( "Block Size", blocksize ); // Blocksize to be used by iterative solver
175 belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed
176 belosList.set( "Maximum Restarts", maxrestarts ); // Maximum number of restarts allowed
177 belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
178 if (numrhs > 1) {
179 belosList.set( "Show Maximum Residual Norm Only", true ); // Show only the maximum residual norm
180 }
181 if (verbose) {
182 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings +
183 Belos::TimingDetails + Belos::StatusTestDetails );
184 if (frequency > 0)
185 belosList.set( "Output Frequency", frequency );
186 }
187 else
188 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings );
189 //
190 // *******Construct a preconditioned linear problem********
191 //
192 RCP<Belos::LinearProblem<double,MV,OP> > problem
193 = rcp( new Belos::LinearProblem<double,MV,OP>( A, X, B ) );
194 problem->setRightPrec( belosPrec );
195
196 bool set = problem->setProblem();
197 if (set == false) {
198 if (proc_verbose)
199 std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
200 return EXIT_FAILURE;
201 }
202
203 // Create an iterative solver manager.
204 RCP< Belos::SolverManager<double,MV,OP> > solver
205 = rcp( new Belos::BlockGmresSolMgr<double,MV,OP>(problem, rcp(&belosList,false)));
206
207 //
208 // *******************************************************************
209 // *************Start the block Gmres iteration*************************
210 // *******************************************************************
211 //
212 if (proc_verbose) {
213 std::cout << std::endl << std::endl;
214 std::cout << "Dimension of matrix: " << NumGlobalElements << std::endl;
215 std::cout << "Number of right-hand sides: " << numrhs << std::endl;
216 std::cout << "Block size used by solver: " << blocksize << std::endl;
217 std::cout << "Number of restarts allowed: " << maxrestarts << std::endl;
218 std::cout << "Max number of Gmres iterations per restart cycle: " << maxiters << std::endl;
219 std::cout << "Relative residual tolerance: " << tol << std::endl;
220 std::cout << std::endl;
221 }
222 //
223 // Perform solve
224 //
225 Belos::ReturnType ret = solver->solve();
226 //
227 // Compute actual residuals.
228 //
229 bool badRes = false;
230 std::vector<double> actual_resids( numrhs );
231 std::vector<double> rhs_norm( numrhs );
232 Epetra_MultiVector resid(*Map, numrhs);
233 OPT::Apply( *A, *X, resid );
234 MVT::MvAddMv( -1.0, resid, 1.0, *B, resid );
235 MVT::MvNorm( resid, actual_resids );
236 MVT::MvNorm( *B, rhs_norm );
237 if (proc_verbose) {
238 std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl;
239 for ( int i=0; i<numrhs; i++) {
240 double actRes = actual_resids[i]/rhs_norm[i];
241 std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl;
242 if (actRes > tol) badRes = true;
243 }
244 }
245
246 success = ret==Belos::Converged && !badRes;
247 if (success) {
248 if (proc_verbose)
249 std::cout << "End Result: TEST PASSED" << std::endl;
250 } else {
251 if (proc_verbose)
252 std::cout << "End Result: TEST FAILED" << 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 }