Debian Codegraph @

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