Debian Codegraph @

packages/belos/epetra/test/RCG/test_prec_rcg_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 Harwell-Boeing (HB) file.
11 // The right-hand-side from the HB file is used instead of random vectors.
12 // The initial guesses are all set to zero.
13 //
14 // NOTE: No preconditioner is used in this case.
15 //
16 #include "BelosConfigDefs.hpp"
17 #include "BelosLinearProblem.hpp"
18 #include "BelosEpetraAdapter.hpp"
19 #include "BelosRCGSolMgr.hpp"
20 #include "BelosEpetraUtils.h"
21 #include "Trilinos_Util.h"
22 #include "Epetra_CrsMatrix.h"
23 #include "Epetra_Map.h"
24 #include "Teuchos_CommandLineProcessor.hpp"
25 #include "Teuchos_ParameterList.hpp"
26 #include "Teuchos_StandardCatchMacros.hpp"
27 #include "Ifpack.h"
28
29 int main(int argc, char *argv[]) {
30 //
31 #ifdef EPETRA_MPI
32 // Initialize MPI
33 MPI_Init(&argc,&argv);
34 #endif
35 //
36 using Teuchos::ParameterList;
37 using Teuchos::RCP;
38 using Teuchos::rcp;
39
40 bool success = false;
41 bool verbose = false;
42 try {
43 //
44 // Get test parameters from command-line processor
45 //
46 bool proc_verbose = false;
47 bool leftprec = true; // left preconditioning or right.
48 int frequency = -1; // frequency of status test output.
49 std::string filename("gr_30_30.hb"); // default input filename
50 double tol = 1.0e-10; // relative residual tolerance
51 int numBlocks = 30; // maximum number of blocks the solver can use for the Krylov subspace
52 int recycleBlocks = 3; // maximum number of blocks the solver can use for the recycle space
53 int numrhs = 1; // number of right-hand sides to solve for
54 int maxiters = -1; // maximum number of iterations allowed per linear system
55
56 Teuchos::CommandLineProcessor cmdp(false,true);
57 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
58 cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
59 cmdp.setOption("left-prec","right-prec",&leftprec,"Left preconditioning or right.");
60 cmdp.setOption("filename",&filename,"Filename for test matrix.");
61 cmdp.setOption("tol",&tol,"Relative residual tolerance used by the RCG solver.");
62 cmdp.setOption("max-subspace",&numBlocks,"Maximum number of vectors in search space (not including recycle space).");
63 cmdp.setOption("recycle",&recycleBlocks,"Number of vectors in recycle space.");
64 cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for.");
65 cmdp.setOption("max-iters",&maxiters,"Maximum number of iterations per linear system (-1 = adapted to problem/block size).");
66 if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
67 return -1;
68 }
69 if (!verbose)
70 frequency = -1; // reset frequency if test is not verbose
71 //
72 // Get the problem
73 //
74 int MyPID;
75 RCP<Epetra_CrsMatrix> A;
76 RCP<Epetra_MultiVector> B, X;
77 int return_val =Belos::Util::createEpetraProblem(filename,NULL,&A,&B,&X,&MyPID);
78 if(return_val != 0) return return_val;
79 proc_verbose = ( verbose && (MyPID==0) );
80 //
81 // Solve using Belos
82 //
83 typedef double ST;
84 typedef Epetra_Operator OP;
85 typedef Epetra_MultiVector MV;
86 typedef Belos::OperatorTraits<ST,MV,OP> OPT;
87 typedef Belos::MultiVecTraits<ST,MV> MVT;
88 //
89 // *****Construct initial guess and right-hand sides *****
90 //
91 if (numrhs != 1) {
92 X = rcp( new Epetra_MultiVector( A->Map(), numrhs ) );
93 X->Random();
94 B = rcp( new Epetra_MultiVector( A->Map(), numrhs ) );
95 OPT::Apply( *A, *X, *B );
96 MVT::MvInit( *X, 0.0 );
97 }
98 else { // initialize exact solution to be vector of ones
99 MVT::MvInit( *X, 1.0 );
100 OPT::Apply( *A, *X, *B );
101 MVT::MvInit( *X, 0.0 );
102 }
103 //
104 // ************Construct preconditioner*************
105 //
106 ParameterList ifpackList;
107
108 // allocates an IFPACK factory. No data is associated
109 // to this object (only method Create()).
110 Ifpack Factory;
111
112 // create the preconditioner. For valid PrecType values,
113 // please check the documentation
114 std::string PrecType = "ICT"; // incomplete Cholesky
115 int OverlapLevel = 0; // must be >= 0. If Comm.NumProc() == 1,
116 // it is ignored.
117
118 RCP<Ifpack_Preconditioner> Prec = Teuchos::rcp( Factory.Create(PrecType, &*A, OverlapLevel) );
119 assert(Prec != Teuchos::null);
120
121 // specify parameters for ICT
122 ifpackList.set("fact: drop tolerance", 1e-9);
123 ifpackList.set("fact: ict level-of-fill", 1.0);
124 // the combine mode is on the following:
125 // "Add", "Zero", "Insert", "InsertAdd", "Average", "AbsMax"
126 // Their meaning is as defined in file Epetra_CombineMode.h
127 ifpackList.set("schwarz: combine mode", "Add");
128 // sets the parameters
129 IFPACK_CHK_ERR(Prec->SetParameters(ifpackList));
130
131 // initialize the preconditioner. At this point the matrix must
132 // have been FillComplete()'d, but actual values are ignored.
133 IFPACK_CHK_ERR(Prec->Initialize());
134
135 // Builds the preconditioners, by looking for the values of
136 // the matrix.
137 IFPACK_CHK_ERR(Prec->Compute());
138
139 // Create the Belos preconditioned operator from the Ifpack preconditioner.
140 // NOTE: This is necessary because Belos expects an operator to apply the
141 // preconditioner with Apply() NOT ApplyInverse().
142 RCP<Belos::EpetraPrecOp> belosPrec = rcp( new Belos::EpetraPrecOp( Prec ) );
143
144 //
145 // ************Other information used by solver**************
146 // *****************(can be user specified)******************
147 //
148 const int NumGlobalElements = B->GlobalLength();
149 if (maxiters == -1)
150 maxiters = NumGlobalElements - 1; // maximum number of iterations to run
151 //
152 ParameterList belosList;
153 belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed
154 belosList.set( "Num Blocks", numBlocks); // Maximum number of blocks in Krylov space
155 belosList.set( "Num Recycled Blocks", recycleBlocks ); // Number of vectors in recycle space
156 belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
157 if (verbose) {
158 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings +
159 Belos::TimingDetails + Belos::FinalSummary + Belos::StatusTestDetails );
160 if (frequency > 0)
161 belosList.set( "Output Frequency", frequency );
162 }
163 else
164 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings );
165 //
166 // Construct a preconditioned linear problem instance.
167 //
168 Belos::LinearProblem<double,MV,OP> problem( A, X, B );
169 if (leftprec) {
170 problem.setLeftPrec( belosPrec );
171 }
172 else {
173 problem.setRightPrec( belosPrec );
174 }
175
176 bool set = problem.setProblem();
177 if (set == false) {
178 if (proc_verbose)
179 std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
180 return -1;
181 }
182 //
183 // Create an iterative solver manager.
184 //
185 RCP< Belos::SolverManager<double,MV,OP> > newSolver
186 = rcp( new Belos::RCGSolMgr<double,MV,OP>(rcp(&problem,false), rcp(&belosList,false)) );
187 //
188 // **********Print out information about problem*******************
189 //
190 if (proc_verbose) {
191 std::cout << std::endl << std::endl;
192 std::cout << "Dimension of matrix: " << NumGlobalElements << std::endl;
193 std::cout << "Number of right-hand sides: " << numrhs << std::endl;
194 std::cout << "Max number of RCG iterations: " << maxiters << std::endl;
195 std::cout << "Max number of vectors in Krylov space: " << numBlocks << std::endl;
196 std::cout << "Number of vectors in recycle space: " << recycleBlocks << std::endl;
197 std::cout << "Relative residual tolerance: " << tol << std::endl;
198 std::cout << std::endl;
199 }
200 //
201 // Perform solve
202 //
203 Belos::ReturnType ret = newSolver->solve();
204 //
205 // Compute actual residuals.
206 //
207 bool badRes = false;
208 std::vector<double> actual_resids( numrhs );
209 std::vector<double> rhs_norm( numrhs );
210 Epetra_MultiVector resid(A->Map(), numrhs);
211 OPT::Apply( *A, *X, resid );
212 MVT::MvAddMv( -1.0, resid, 1.0, *B, resid );
213 MVT::MvNorm( resid, actual_resids );
214 MVT::MvNorm( *B, rhs_norm );
215 if (proc_verbose) {
216 std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl;
217 for ( int i=0; i<numrhs; i++) {
218 double actRes = actual_resids[i]/rhs_norm[i];
219 std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl;
220 if (actRes > tol) badRes = true;
221 }
222 }
223
224 success = ret==Belos::Converged && !badRes;
225
226 if (success) {
227 if (proc_verbose)
228 std::cout << std::endl << "End Result: TEST PASSED" << std::endl;
229 } else {
230 if (proc_verbose)
231 std::cout << std::endl << "End Result: TEST FAILED" << std::endl;
232 }
233 }
234 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
235
236 #ifdef EPETRA_MPI
237 MPI_Finalize();
238 #endif
239
240 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
241 } // end test_rcg_hb.cpp