Debian Codegraph @

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