Debian Codegraph @

packages/belos/epetra/test/BlockGmres/test_bl_gmres_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 problem is being used instead of multiple
12 // random right-hand-sides. 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 "BelosBlockGmresSolMgr.hpp"
20 #include "BelosPseudoBlockGmresSolMgr.hpp"
21 #include "BelosEpetraUtils.h"
22 #include "Epetra_CrsMatrix.h"
23 #include "Teuchos_CommandLineProcessor.hpp"
24 #include "Teuchos_ParameterList.hpp"
25 #include "Teuchos_StandardCatchMacros.hpp"
26 #include "Teuchos_GlobalMPISession.hpp"
27 #include "Epetra_Map.h"
28
29 int main(int argc, char *argv[]) {
30 //
31 Teuchos::GlobalMPISession session(&argc, &argv, NULL);
32 //
33 typedef double ST;
34 typedef Teuchos::ScalarTraits<ST> SCT;
35 typedef SCT::magnitudeType MT;
36 typedef Epetra_MultiVector MV;
37 typedef Epetra_Operator OP;
38 typedef Belos::MultiVecTraits<ST,MV> MVT;
39 typedef Belos::OperatorTraits<ST,MV,OP> OPT;
40
41 using Teuchos::ParameterList;
42 using Teuchos::RCP;
43 using Teuchos::rcp;
44
45 bool debug = false;
46 bool verbose = false;
47 bool success = true;
48 try {
49 bool proc_verbose = false;
50 bool pseudo = false; // use pseudo block GMRES to solve this linear system.
51 int frequency = -1;
52 int blocksize = 1;
53 int numrhs = 1;
54 int maxrestarts = 15; // number of restarts allowed
55 int maxiters = -1; // maximum number of iterations allowed per linear system
56 std::string filename("orsirr1.hb");
57 MT tol = 1.0e-5; // relative residual tolerance
58
59 Teuchos::CommandLineProcessor cmdp(false,true);
60 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
61 cmdp.setOption("debug","no-debug",&debug,"Print debug messages.");
62 cmdp.setOption("pseudo","regular",&pseudo,"Use pseudo-block GMRES to solve the linear systems.");
63 cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
64 cmdp.setOption("filename",&filename,"Filename for Harwell-Boeing test matrix.");
65 cmdp.setOption("tol",&tol,"Relative residual tolerance used by GMRES solver.");
66 cmdp.setOption("max-restarts",&maxrestarts,"Maximum number of restarts allowed for GMRES solver.");
67 cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for.");
68 cmdp.setOption("blocksize",&blocksize,"Block size used by GMRES.");
69 cmdp.setOption("maxiters",&maxiters,"Maximum number of iterations per linear system (-1 = adapted to problem/block size).");
70 if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
71 return -1;
72 }
73 if (!verbose)
74 frequency = -1; // reset frequency if test is not verbose
75 //
76 // Get the problem
77 //
78 int MyPID;
79 RCP<Epetra_CrsMatrix> A;
80 RCP<Epetra_MultiVector> B, X;
81 int return_val =Belos::Util::createEpetraProblem(filename,NULL,&A,&B,&X,&MyPID);
82 if(return_val != 0) return return_val;
83 const Epetra_Map &Map = A->RowMap();
84 proc_verbose = verbose && (MyPID==0); /* Only print on the zero processor */
85 //
86 // ********Other information used by block solver***********
87 // *****************(can be user specified)******************
88 //
89 const int NumGlobalElements = B->GlobalLength();
90 if (maxiters == -1)
91 maxiters = NumGlobalElements/blocksize - 1; // maximum number of iterations to run
92 //
93 ParameterList belosList;
94 belosList.set( "Num Blocks", maxiters ); // Maximum number of blocks in Krylov factorization
95 belosList.set( "Block Size", blocksize ); // Blocksize to be used by iterative solver
96 belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed
97 belosList.set( "Maximum Restarts", maxrestarts ); // Maximum number of restarts allowed
98 belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
99 if (verbose) {
100 int verbosity = Belos::Errors + Belos::Warnings +
101 Belos::TimingDetails + Belos::StatusTestDetails;
102 if (debug)
103 verbosity += Belos::Debug + Belos::OrthoDetails;
104 belosList.set( "Verbosity", verbosity );
105 if (frequency > 0)
106 belosList.set( "Output Frequency", frequency );
107 }
108 else
109 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings );
110 //
111 //
112 // Construct an unpreconditioned linear problem instance.
113 //
114 if (numrhs > 1) {
115 X = rcp( new Epetra_MultiVector(Map, numrhs) );
116 X->PutScalar( 0.0 );
117 B = rcp( new Epetra_MultiVector(Map, numrhs) );
118 B->Random();
119 }
120 Belos::LinearProblem<double,MV,OP> problem( A, X, B );
121 bool set = problem.setProblem();
122 if (set == false) {
123 if (proc_verbose)
124 std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
125 return -1;
126 }
127 //
128 // *******************************************************************
129 // *************Start the block Gmres iteration*************************
130 // *******************************************************************
131 //
132 Teuchos::RCP< Belos::SolverManager<double,MV,OP> > solver;
133 if (pseudo)
134 solver = Teuchos::rcp( new Belos::PseudoBlockGmresSolMgr<double,MV,OP>( rcp(&problem,false), rcp(&belosList,false) ) );
135 else
136 solver = Teuchos::rcp( new Belos::BlockGmresSolMgr<double,MV,OP>( rcp(&problem,false), rcp(&belosList,false) ) );
137 //
138 // **********Print out information about problem*******************
139 //
140 if (proc_verbose) {
141 std::cout << std::endl << std::endl;
142 std::cout << "Dimension of matrix: " << NumGlobalElements << std::endl;
143 std::cout << "Number of right-hand sides: " << numrhs << std::endl;
144 std::cout << "Block size used by solver: " << blocksize << std::endl;
145 std::cout << "Number of restarts allowed: " << maxrestarts << std::endl;
146 std::cout << "Max number of Gmres iterations per restart cycle: " << maxiters << std::endl;
147 std::cout << "Relative residual tolerance: " << tol << std::endl;
148 std::cout << std::endl;
149 }
150 //
151 // Perform solve
152 //
153 Belos::ReturnType ret = solver->solve();
154
155 //
156 // Compute actual residuals.
157 //
158 std::vector<double> actual_resids( numrhs );
159 std::vector<double> rhs_norm( numrhs );
160 Epetra_MultiVector resid(Map, numrhs);
161 OPT::Apply( *A, *X, resid );
162 MVT::MvAddMv( -1.0, resid, 1.0, *B, resid );
163 MVT::MvNorm( resid, actual_resids );
164 MVT::MvNorm( *B, rhs_norm );
165 if (proc_verbose) {
166 std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl;
167 for ( int i=0; i<numrhs; i++) {
168 std::cout<<"Problem "<<i<<" : \t"<< actual_resids[i]/rhs_norm[i] <<std::endl;
169 }
170 }
171
172 success = ret==Belos::Converged;
173
174 if (success) {
175 if (proc_verbose)
176 std::cout << "End Result: TEST PASSED" << std::endl;
177 } else {
178 if (proc_verbose)
179 std::cout << "End Result: TEST FAILED" << std::endl;
180 }
181 }
182 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
183
184 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
185 } // end test_bl_gmres_hb.cpp