Debian Codegraph @

packages/belos/epetra/example/GCRODR/PrecGCRODREpetraExFile.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 "BelosGCRODRSolMgr.hpp"
20 #include "BelosEpetraUtils.h"
21 #include "Ifpack_IlukGraph.h"
22 #include "Ifpack_CrsRiluk.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 #include "Teuchos_CommandLineProcessor.hpp"
31 #include "Teuchos_ParameterList.hpp"
32 #include "Teuchos_StandardCatchMacros.hpp"
33 #include "Teuchos_Assert.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 bool leftprec = true; // left preconditioning or right.
65 int frequency = -1; // frequency of status test output.
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 = 250; // maximum number of blocks the solver can use for the subspace
69 int recycle = 50; // maximum size of recycle space
70 int maxrestarts = 15; // maximum number of restarts allowed
71 std::string filename("sherman5.hb");
72 std::string ortho("IMGS");
73 MT tol = 1.0e-10; // relative residual tolerance
74
75 Teuchos::CommandLineProcessor cmdp(false,true);
76 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
77 cmdp.setOption("debug","nondebug",&debug, "Print debugging information from solver.");
78 cmdp.setOption("left-prec","right-prec",&leftprec,"Left preconditioning or right.");
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("max-iters",&maxiters,"Maximum number of iterations per linear system (-1 = adapted to problem/block size).");
84 cmdp.setOption("max-subspace",&maxsubspace,"Maximum number of blocks the solver can use for the subspace.");
85 cmdp.setOption("recycle",&recycle,"Number of vectors in recycle space.");
86 cmdp.setOption("max-cycles",&maxrestarts,"Maximum number of cycles allowed for GCRO-DR solver.");
87 cmdp.setOption("ortho-type",&ortho,"Orthogonalization type. Must be one of DGKS, ICGS, IMGS.");
88 if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
89 return -1;
90 }
91 if (!verbose)
92 frequency = -1; // reset frequency if test is not verbose
93
94 //
95 // *************Get the problem*********************
96 //
97 RCP<Epetra_CrsMatrix> A;
98 RCP<Epetra_MultiVector> B, X;
99 int return_val =Belos::Util::createEpetraProblem(filename,NULL,&A,NULL,NULL,&MyPID);
100 const Epetra_Map &Map = A->RowMap();
101 if(return_val != 0) return return_val;
102 proc_verbose = verbose && (MyPID==0); /* Only print on zero processor */
103 X = rcp( new Epetra_MultiVector( Map, numrhs ) );
104 B = rcp( new Epetra_MultiVector( Map, numrhs ) );
105 X->Random();
106 OPT::Apply( *A, *X, *B );
107 X->PutScalar( 0.0 );
108 //
109 // ************Construct preconditioner*************
110 //
111 if (proc_verbose) std::cout << std::endl << std::endl;
112 if (proc_verbose) std::cout << "Constructing ILU preconditioner" << std::endl;
113 int Lfill = 2;
114 if (proc_verbose) std::cout << "Using Lfill = " << Lfill << std::endl;
115 int Overlap = 2;
116 if (proc_verbose) std::cout << "Using Level Overlap = " << Overlap << std::endl;
117 double Athresh = 0.0;
118 if (proc_verbose) std::cout << "Using Absolute Threshold Value of " << Athresh << std::endl;
119 double Rthresh = 1.0;
120 if (proc_verbose) std::cout << "Using Relative Threshold Value of " << Rthresh << std::endl;
121 //
122 Teuchos::RCP<Ifpack_IlukGraph> ilukGraph;
123 Teuchos::RCP<Ifpack_CrsRiluk> ilukFactors;
124 //
125 ilukGraph = Teuchos::rcp(new Ifpack_IlukGraph(A->Graph(), Lfill, Overlap));
126 int info = ilukGraph->ConstructFilledGraph();
127 TEUCHOS_ASSERT( info == 0 );
128 ilukFactors = Teuchos::rcp(new Ifpack_CrsRiluk(*ilukGraph));
129 int initerr = ilukFactors->InitValues(*A);
130 if (initerr != 0) std::cout << "InitValues error = " << initerr;
131 info = ilukFactors->Factor();
132 TEUCHOS_ASSERT( info == 0 );
133 bool transA = false;
134 double Cond_Est;
135 ilukFactors->Condest(transA, Cond_Est);
136 if (proc_verbose) {
137 std::cout << "Condition number estimate for this preconditoner = " << Cond_Est << std::endl;
138 std::cout << std::endl;
139 }
140
141 // Create the Belos preconditioned operator from the Ifpack preconditioner.
142 // NOTE: This is necessary because Belos expects an operator to apply the
143 // preconditioner with Apply() NOT ApplyInverse().
144 RCP<Belos::EpetraPrecOp> belosPrec = rcp( new Belos::EpetraPrecOp( ilukFactors ) );
145
146 //
147 // ********Other information used by block solver***********
148 // *****************(can be user specified)******************
149 //
150 const int NumGlobalElements = B->GlobalLength();
151 if (maxiters == -1)
152 maxiters = NumGlobalElements - 1; // maximum number of iterations to run
153 //
154 ParameterList belosList;
155 belosList.set( "Num Blocks", maxsubspace ); // Maximum number of blocks in Krylov factorization
156 belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed
157 belosList.set( "Maximum Restarts", maxrestarts ); // Maximum number of restarts allowed
158 belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
159 belosList.set( "Num Recycled Blocks", recycle ); // Number of vectors in recycle space
160 belosList.set( "Orthogonalization", ortho ); // Orthogonalization type
161 if (numrhs > 1) {
162 belosList.set( "Show Maximum Residual Norm Only", true ); // Show only the maximum residual norm
163 }
164 int verbosity = Belos::Errors + Belos::Warnings;
165 if (verbose) {
166 verbosity += Belos::TimingDetails + Belos::StatusTestDetails;
167 if (frequency > 0)
168 belosList.set( "Output Frequency", frequency );
169 }
170 if (debug) {
171 verbosity += Belos::Debug;
172 }
173 belosList.set( "Verbosity", verbosity );
174 //
175 // *******Construct a preconditioned linear problem********
176 //
177 RCP<Belos::LinearProblem<double,MV,OP> > problem
178 = rcp( new Belos::LinearProblem<double,MV,OP>( A, X, B ) );
179 if (leftprec) {
180 problem->setLeftPrec( belosPrec );
181 }
182 else {
183 problem->setRightPrec( belosPrec );
184 }
185 bool set = problem->setProblem();
186 if (set == false) {
187 if (proc_verbose)
188 std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
189 return -1;
190 }
191
192 // Create an iterative solver manager.
193 RCP< Belos::SolverManager<double,MV,OP> > solver
194 = rcp( new Belos::GCRODRSolMgr<double,MV,OP>(problem, rcp(&belosList,false)));
195
196 //
197 // *******************************************************************
198 // *************Start the block Gmres iteration*************************
199 // *******************************************************************
200 //
201 if (proc_verbose) {
202 std::cout << std::endl << std::endl;
203 std::cout << "Dimension of matrix: " << NumGlobalElements << std::endl;
204 std::cout << "Number of right-hand sides: " << numrhs << std::endl;
205 std::cout << "Number of restarts allowed: " << maxrestarts << std::endl;
206 std::cout << "Max number of Gmres iterations per restart cycle: " << maxiters << std::endl;
207 std::cout << "Relative residual tolerance: " << tol << std::endl;
208 std::cout << std::endl;
209 }
210 //
211 // Perform solve
212 //
213 Belos::ReturnType ret = solver->solve();
214 //
215 // Compute actual residuals.
216 //
217 bool badRes = false;
218 std::vector<double> actual_resids( numrhs );
219 std::vector<double> rhs_norm( numrhs );
220 Epetra_MultiVector resid(Map, numrhs);
221 OPT::Apply( *A, *X, resid );
222 MVT::MvAddMv( -1.0, resid, 1.0, *B, resid );
223 MVT::MvNorm( resid, actual_resids );
224 MVT::MvNorm( *B, rhs_norm );
225 if (proc_verbose) {
226 std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl;
227 for ( int i=0; i<numrhs; i++) {
228 double actRes = actual_resids[i]/rhs_norm[i];
229 std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl;
230 if (actRes > tol) badRes = true;
231 }
232 }
233
234 if (ret!=Belos::Converged || badRes) {
235 success = false;
236 if (proc_verbose)
237 std::cout << std::endl << "ERROR: Belos did not converge!" << std::endl;
238 } else {
239 success = true;
240 if (proc_verbose)
241 std::cout << std::endl << "SUCCESS: Belos converged!" << std::endl;
242 }
243 }
244 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
245
246 #ifdef EPETRA_MPI
247 MPI_Finalize();
248 #endif
249
250 return success ? EXIT_SUCCESS : EXIT_FAILURE;
251 }