Debian Codegraph @

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