Debian Codegraph @

packages/belos/epetra/test/TFQMR/test_pseudo_ptfqmr_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 "BelosPseudoBlockTFQMRSolMgr.hpp"
18 #include "BelosEpetraUtils.h"
19 #include "Ifpack_IlukGraph.h"
20 #include "Ifpack_CrsRiluk.h"
21 #include "Epetra_CrsMatrix.h"
22 #include "Epetra_Map.h"
23 #include "Teuchos_CommandLineProcessor.hpp"
24 #include "Teuchos_ParameterList.hpp"
25 #include "Teuchos_StandardCatchMacros.hpp"
26 #include "Teuchos_Assert.hpp"
27
28 int main(int argc, char *argv[]) {
29 //
30 Teuchos::GlobalMPISession session(&argc, &argv, NULL);
31 //
32 typedef double ST;
33 typedef Teuchos::ScalarTraits<ST> SCT;
34 typedef SCT::magnitudeType MT;
35 typedef Epetra_MultiVector MV;
36 typedef Epetra_Operator OP;
37 typedef Belos::MultiVecTraits<ST,MV> MVT;
38 typedef Belos::OperatorTraits<ST,MV,OP> OPT;
39
40 using Teuchos::ParameterList;
41 using Teuchos::RCP;
42 using Teuchos::rcp;
43
44 bool success = false;
45 bool verbose = false;
46 try {
47 bool proc_verbose = false;
48 bool leftprec = true; // use left preconditioning to solve these linear systems
49 bool explicit_test = true;
50 int frequency = -1; // how often residuals are printed by solver
51 int numrhs = 1;
52 int maxiters = -1; // maximum iterations allowed
53 std::string filename("orsirr1.hb");
54 MT tol = 1.0e-5; // relative residual tolerance
55
56 Teuchos::CommandLineProcessor cmdp(false,true);
57 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
58 cmdp.setOption("left-prec","right-prec",&leftprec,"Left preconditioning or right.");
59 cmdp.setOption("explicit","implicit-only",&explicit_test,"Compute explicit residuals.");
60 cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
61 cmdp.setOption("filename",&filename,"Filename for Harwell-Boeing test matrix.");
62 cmdp.setOption("tol",&tol,"Relative residual tolerance used by GMRES solver.");
63 cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for.");
64 cmdp.setOption("maxiters",&maxiters,"Maximum number of iterations per linear system (-1 = adapted to problem size).");
65 if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
66 return -1;
67 }
68 if (!verbose)
69 frequency = -1; // reset frequency if test is not verbose
70 //
71 // Get the problem
72 //
73 int MyPID;
74 RCP<Epetra_CrsMatrix> A;
75 int return_val =Belos::Util::createEpetraProblem(filename,NULL,&A,NULL,NULL,&MyPID);
76 const Epetra_Map &Map = A->RowMap();
77 if(return_val != 0) return return_val;
78 proc_verbose = verbose && (MyPID==0); /* Only print on zero processor */
79 //
80 // *****Construct the Preconditioner*****
81 //
82 if (proc_verbose) std::cout << std::endl << std::endl;
83 if (proc_verbose) std::cout << "Constructing ILU preconditioner" << std::endl;
84 int Lfill = 2;
85 // if (argc > 2) Lfill = atoi(argv[2]);
86 if (proc_verbose) std::cout << "Using Lfill = " << Lfill << std::endl;
87 int Overlap = 2;
88 // if (argc > 3) Overlap = atoi(argv[3]);
89 if (proc_verbose) std::cout << "Using Level Overlap = " << Overlap << std::endl;
90 double Athresh = 0.0;
91 // if (argc > 4) Athresh = atof(argv[4]);
92 if (proc_verbose) std::cout << "Using Absolute Threshold Value of " << Athresh << std::endl;
93 double Rthresh = 1.0;
94 // if (argc >5) Rthresh = atof(argv[5]);
95 if (proc_verbose) std::cout << "Using Relative Threshold Value of " << Rthresh << std::endl;
96 //
97 Teuchos::RCP<Ifpack_IlukGraph> ilukGraph;
98 Teuchos::RCP<Ifpack_CrsRiluk> ilukFactors;
99 //
100 if (Lfill > -1) {
101 ilukGraph = Teuchos::rcp(new Ifpack_IlukGraph(A->Graph(), Lfill, Overlap));
102 int info = ilukGraph->ConstructFilledGraph();
103 TEUCHOS_ASSERT( info == 0 );
104 ilukFactors = Teuchos::rcp(new Ifpack_CrsRiluk(*ilukGraph));
105 int initerr = ilukFactors->InitValues(*A);
106 if (initerr != 0) std::cout << "InitValues error = " << initerr;
107 info = ilukFactors->Factor();
108 TEUCHOS_ASSERT( info == 0 );
109 }
110 //
111 bool transA = false;
112 double Cond_Est;
113 ilukFactors->Condest(transA, Cond_Est);
114 if (proc_verbose) {
115 std::cout << "Condition number estimate for this preconditoner = " << Cond_Est << std::endl;
116 std::cout << std::endl;
117 }
118
119 //
120 // Create the Belos preconditioned operator from the Ifpack preconditioner.
121 // NOTE: This is necessary because Belos expects an operator to apply the
122 // preconditioner with Apply() NOT ApplyInverse().
123 RCP<Belos::EpetraPrecOp> Prec = rcp( new Belos::EpetraPrecOp( ilukFactors ) );
124
125 //
126 // ********Other information used by block solver***********
127 // *****************(can be user specified)******************
128 //
129 const int NumGlobalElements = Map.NumGlobalElements();
130 if (maxiters == -1)
131 maxiters = NumGlobalElements - 1; // maximum number of iterations to run
132 //
133 ParameterList belosList;
134 belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed
135 belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
136 belosList.set( "Explicit Residual Test", explicit_test ); // Need to check for the explicit residual before returning
137 if (verbose) {
138 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings +
139 Belos::TimingDetails + Belos::StatusTestDetails );
140 if (frequency > 0)
141 belosList.set( "Output Frequency", frequency );
142 }
143 else
144 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings );
145 //
146 // *****Construct solution std::vector and random right-hand-sides *****
147 //
148 RCP<Epetra_MultiVector> X = rcp( new Epetra_MultiVector(Map, numrhs) );
149 RCP<Epetra_MultiVector> B = rcp( new Epetra_MultiVector(Map, numrhs) );
150 MVT::MvRandom( *X );
151 OPT::Apply( *A, *X, *B );
152 MVT::MvInit( *X, 0.0 );
153
154 Belos::LinearProblem<double,MV,OP> problem( A, X, B );
155 if (leftprec)
156 problem.setLeftPrec( Prec );
157 else
158 problem.setRightPrec( Prec );
159
160 bool set = problem.setProblem();
161 if (set == false) {
162 if (proc_verbose)
163 std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
164 return -1;
165 }
166 //
167 // *******************************************************************
168 // ************Start the Pseudo Block TFQMR iteration*****************
169 // *******************************************************************
170 //
171 Teuchos::RCP< Belos::PseudoBlockTFQMRSolMgr<double,MV,OP> > solver =
172 Teuchos::rcp( new Belos::PseudoBlockTFQMRSolMgr<double,MV,OP>( rcp(&problem,false), rcp(&belosList,false) ) );
173 //
174 // **********Print out information about problem*******************
175 //
176 if (proc_verbose) {
177 std::cout << std::endl << std::endl;
178 std::cout << "Dimension of matrix: " << NumGlobalElements << std::endl;
179 std::cout << "Number of right-hand sides: " << numrhs << std::endl;
180 std::cout << "Max number of Pseudo Block TFQMR iterations: " << maxiters << std::endl;
181 std::cout << "Relative residual tolerance: " << tol << std::endl;
182 std::cout << std::endl;
183 }
184 //
185 // Perform solve
186 //
187 Belos::ReturnType ret = solver->solve();
188
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 R(Map, numrhs);
196 OPT::Apply( *A, *X, R );
197 MVT::MvAddMv( -1.0, R, 1.0, *B, R );
198 MVT::MvNorm( R, 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 << "End Result: TEST PASSED" << std::endl;
214 } else {
215 if (proc_verbose)
216 std::cout << "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_ptfqmr.cpp