Debian Codegraph @

packages/belos/epetra/example/BlockGmres/PseudoBlockGmresEpetraExFile.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
13 // random right-hand-sides. The initial guesses are all set to zero.
14 //
15 // NOTE: No preconditioner is used in this example.
16 //
17 #include "BelosConfigDefs.hpp"
18 #include "BelosLinearProblem.hpp"
19 #include "BelosEpetraAdapter.hpp"
20 #include "BelosPseudoBlockGmresSolMgr.hpp"
21
22 #include "EpetraExt_readEpetraLinearSystem.h"
23 #include "Epetra_Map.h"
24 #ifdef EPETRA_MPI
25 #include "Epetra_MpiComm.h"
26 #else
27 #include "Epetra_SerialComm.h"
28 #endif
29 #include "Epetra_CrsMatrix.h"
30
31 #include "Teuchos_CommandLineProcessor.hpp"
32 #include "Teuchos_ParameterList.hpp"
33 #include "Teuchos_StandardCatchMacros.hpp"
34
35 int main(int argc, char *argv[]) {
36 //
37 int MyPID = 0;
38 #ifdef EPETRA_MPI
39 // Initialize MPI
40 MPI_Init(&argc,&argv);
41 Epetra_MpiComm Comm(MPI_COMM_WORLD);
42 MyPID = Comm.MyPID();
43 #else
44 Epetra_SerialComm Comm;
45 #endif
46 //
47 typedef double ST;
48 typedef Teuchos::ScalarTraits<ST> SCT;
49 typedef SCT::magnitudeType MT;
50 typedef Epetra_MultiVector MV;
51 typedef Epetra_Operator OP;
52 typedef Belos::MultiVecTraits<ST,MV> MVT;
53 typedef Belos::OperatorTraits<ST,MV,OP> OPT;
54
55 using Teuchos::ParameterList;
56 using Teuchos::RCP;
57 using Teuchos::rcp;
58
59 bool verbose = false;
60 bool success = true;
61 try {
62 bool proc_verbose = false;
63 int frequency = -1; // frequency of status test output.
64 int blocksize = 1; // blocksize
65 int numrhs = 1; // number of right-hand sides to solve for
66 int maxiters = -1; // maximum number of iterations allowed per linear system
67 int maxsubspace = 50; // maximum number of blocks the solver can use for the subspace
68 int maxrestarts = 15; // number of restarts allowed
69 std::string filename("orsirr1.hb");
70 MT tol = 1.0e-5; // relative residual tolerance
71
72 Teuchos::CommandLineProcessor cmdp(false,true);
73 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
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("block-size",&blocksize,"Block size used by GMRES.");
79 cmdp.setOption("max-iters",&maxiters,"Maximum number of iterations per linear system (-1 = adapted to problem/block size).");
80 cmdp.setOption("max-subspace",&maxsubspace,"Maximum number of blocks the solver can use for the subspace.");
81 cmdp.setOption("max-restarts",&maxrestarts,"Maximum number of restarts allowed for GMRES solver.");
82 if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
83 return -1;
84 }
85 if (!verbose)
86 frequency = -1; // reset frequency if test is not verbose
87 //
88 // Get the problem
89 //
90 RCP<Epetra_Map> Map;
91 RCP<Epetra_CrsMatrix> A;
92 RCP<Epetra_MultiVector> B, X;
93 RCP<Epetra_Vector> vecB, vecX;
94 EpetraExt::readEpetraLinearSystem(filename, Comm, &A, &Map, &vecX, &vecB);
95 A->OptimizeStorage();
96 proc_verbose = verbose && (MyPID==0); /* Only print on the zero processor */
97
98 // Check to see if the number of right-hand sides is the same as requested.
99 if (numrhs>1) {
100 X = rcp( new Epetra_MultiVector( *Map, numrhs ) );
101 B = rcp( new Epetra_MultiVector( *Map, numrhs ) );
102 X->Random();
103 OPT::Apply( *A, *X, *B );
104 X->PutScalar( 0.0 );
105 }
106 else {
107 X = Teuchos::rcp_implicit_cast<Epetra_MultiVector>(vecX);
108 B = Teuchos::rcp_implicit_cast<Epetra_MultiVector>(vecB);
109 }
110 //
111 // ********Other information used by block solver***********
112 // *****************(can be user specified)******************
113 //
114 const int NumGlobalElements = B->GlobalLength();
115 if (maxiters == -1)
116 maxiters = NumGlobalElements - 1; // maximum number of iterations to run
117 //
118 ParameterList belosList;
119 belosList.set( "Num Blocks", maxsubspace); // Maximum number of blocks in Krylov factorization
120 belosList.set( "Block Size", blocksize ); // Blocksize to be used by iterative solver
121 belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed
122 belosList.set( "Maximum Restarts", maxrestarts ); // Maximum number of restarts allowed
123 belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
124 if (verbose) {
125 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings +
126 Belos::TimingDetails + Belos::StatusTestDetails );
127 if (frequency > 0)
128 belosList.set( "Output Frequency", frequency );
129 }
130 else
131 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings );
132 //
133 // Construct an unpreconditioned linear problem instance.
134 //
135 Belos::LinearProblem<double,MV,OP> problem( A, X, B );
136 bool set = problem.setProblem();
137 if (set == false) {
138 if (proc_verbose)
139 std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
140 return -1;
141 }
142 //
143 // *******************************************************************
144 // *************Start the block Gmres iteration*************************
145 // *******************************************************************
146 //
147 // Create an iterative solver manager.
148 RCP< Belos::SolverManager<double,MV,OP> > newSolver
149 = rcp( new Belos::PseudoBlockGmresSolMgr<double,MV,OP>(rcp(&problem,false), rcp(&belosList,false)) );
150
151 //
152 // **********Print out information about problem*******************
153 //
154 if (proc_verbose) {
155 std::cout << std::endl << std::endl;
156 std::cout << "Dimension of matrix: " << NumGlobalElements << std::endl;
157 std::cout << "Number of right-hand sides: " << numrhs << std::endl;
158 std::cout << "Block size used by solver: " << blocksize << std::endl;
159 std::cout << "Max number of restarts allowed: " << maxrestarts << std::endl;
160 std::cout << "Max number of Gmres iterations per restart cycle: " << maxiters << std::endl;
161 std::cout << "Relative residual tolerance: " << tol << std::endl;
162 std::cout << std::endl;
163 }
164 //
165 // Perform solve
166 //
167 Belos::ReturnType ret = newSolver->solve();
168 //
169 // Compute actual residuals.
170 //
171 bool badRes = false;
172 std::vector<double> actual_resids( numrhs );
173 std::vector<double> rhs_norm( numrhs );
174 Epetra_MultiVector resid(*Map, numrhs);
175 OPT::Apply( *A, *X, resid );
176 MVT::MvAddMv( -1.0, resid, 1.0, *B, resid );
177 MVT::MvNorm( resid, actual_resids );
178 MVT::MvNorm( *B, rhs_norm );
179 if (proc_verbose) {
180 std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl;
181 for ( int i=0; i<numrhs; i++) {
182 double actRes = actual_resids[i]/rhs_norm[i];
183 std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl;
184 if (actRes > tol) badRes = true;
185 }
186 }
187
188 if (ret!=Belos::Converged || badRes) {
189 success = false;
190 if (proc_verbose)
191 std::cout << std::endl << "ERROR: Belos did not converge!" << std::endl;
192 } else {
193 success = true;
194 if (proc_verbose)
195 std::cout << std::endl << "SUCCESS: Belos converged!" << std::endl;
196 }
197 }
198 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
199
200 #ifdef EPETRA_MPI
201 MPI_Finalize();
202 #endif
203
204 return success ? EXIT_SUCCESS : EXIT_FAILURE;
205 }