Debian Codegraph @

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