Debian Codegraph @

packages/belos/epetra/example/BlockGmres/BlockGmresEpetraGaleriEx.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 #include "BelosBlockGmresSolMgr.hpp"
11 #include "BelosPseudoBlockGmresSolMgr.hpp"
12 #include "BelosEpetraAdapter.hpp"
13 #include "Epetra_CrsMatrix.h"
14 #include "Epetra_MultiVector.h"
15
16 // The Trilinos package Galeri has many example problems.
17 #include "Galeri_Maps.h"
18 #include "Galeri_CrsMatrices.h"
19
20 #include <Teuchos_CommandLineProcessor.hpp>
21 #include <Teuchos_GlobalMPISession.hpp>
22 #include <Teuchos_oblackholestream.hpp>
23 #include <Teuchos_TypeNameTraits.hpp>
24 #include <Teuchos_StandardCatchMacros.hpp>
25
26 // Include selected communicator class required by Epetra objects
27 #ifdef EPETRA_MPI
28 # include "Epetra_MpiComm.h"
29 #else
30 # include "Epetra_SerialComm.h"
31 #endif // EPETRA_MPI
32
33 // ****************************************************************************
34 // BEGIN MAIN ROUTINE
35 // ****************************************************************************
36
37 int
38 main (int argc, char *argv[])
39 {
40 int MyPID = 0;
41
42 // Belos solvers have the following template parameters:
43 //
44 // - Scalar: The type of dot product results.
45 // - MV: The type of (multi)vectors.
46 // - OP: The type of operators (functions from multivector to
47 // multivector). A matrix (like Epetra_CrsMatrix) is an example
48 // of an operator; an Ifpack preconditioner is another example.
49 //
50 // Here, Scalar is double, MV is Epetra_MultiVector, and OP is
51 // Epetra_Operator.
52 typedef double ST;
53 typedef Teuchos::ScalarTraits<ST> SCT;
54 typedef SCT::magnitudeType MT;
55 typedef Epetra_MultiVector MV;
56 typedef Epetra_Operator OP;
57 typedef Belos::MultiVecTraits<ST,MV> MVT;
58 typedef Belos::OperatorTraits<ST,MV,OP> OPT;
59
60 using Teuchos::ParameterList;
61 using Teuchos::RCP;
62 using Teuchos::rcp;
63
64 #ifdef EPETRA_MPI
65 MPI_Init (&argc, &argv);
66 Epetra_MpiComm Comm (MPI_COMM_WORLD);
67 #else
68 Epetra_SerialComm Comm;
69 #endif // EPETRA_MPI
70
71 bool verbose = false;
72 bool success = true;
73 try {
74 bool proc_verbose = false;
75 bool debug = false;
76 bool pseudo = false; // use pseudo-block or block gmres
77 int frequency = -1; // frequency of status test output
78 int blocksize = 1; // blocksize
79 int numrhs = 1; // number of right-hand sides to solve for
80 int maxiters = -1; // maximum number of iterations allowed per linear system
81 int maxsubspace = 50; // maximum number of blocks the solver can use for the subspace
82 int maxrestarts = 15; // number of restarts allowed
83 int nx = 10; // number of discretization points in each direction
84 MT tol = 1.0e-5; // relative residual tolerance
85 std::string ortho = "DGKS";// orthogonalization method
86
87 Teuchos::CommandLineProcessor cmdp(false,true);
88 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
89 cmdp.setOption("debug","nondebug",&debug,"Print debugging information from solver.");
90 cmdp.setOption("pseudo","block",&pseudo,"Use pseudo-block or block GMRES solver.");
91 cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
92 cmdp.setOption("tol",&tol,"Relative residual tolerance used by GMRES solver.");
93 cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for.");
94 cmdp.setOption("block-size",&blocksize,"Block size used by GMRES.");
95 cmdp.setOption("max-iters",&maxiters,"Maximum number of iterations per linear system (-1 = adapted to problem/block size).");
96 cmdp.setOption("max-subspace",&maxsubspace,"Maximum number of blocks the solver can use for the subspace.");
97 cmdp.setOption("max-restarts",&maxrestarts,"Maximum number of restarts allowed for GMRES solver.");
98 cmdp.setOption("nx",&nx,"Number of discretization points in each direction of 3D Laplacian.");
99 cmdp.setOption("ortho",&ortho,"Orthogonalization being used by GMRES solver.");
100 if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
101 return -1;
102 }
103 if (!verbose)
104 frequency = -1; // reset frequency if test is not verbose
105
106 //
107 // Set up the test problem.
108 //
109 // We use Trilinos' Galeri package to construct a test problem.
110 // Here, we use a discretization of the 2-D Laplacian operator.
111 // The global mesh size is nx * nx.
112 //
113 Teuchos::ParameterList GaleriList;
114 GaleriList.set ("n", nx * nx * nx);
115 GaleriList.set ("nx", nx);
116 GaleriList.set ("ny", nx);
117 GaleriList.set ("nz", nx);
118 RCP<Epetra_Map> Map = rcp (Galeri::CreateMap ("Linear", Comm, GaleriList));
119 RCP<Epetra_RowMatrix> A =
120 rcp (Galeri::CreateCrsMatrix ("Laplace3D", &*Map, GaleriList));
121
122 proc_verbose = verbose && (MyPID==0); /* Only print on the zero processor */
123
124 // Create RHS using random solution vector
125 RCP<MV> B = rcp (new MV (*Map, numrhs));
126 RCP<MV> X = rcp (new MV (*Map, numrhs));
127 RCP<MV> Xexact = rcp (new MV (*Map, numrhs));
128 Xexact->Random ();
129
130 A->Apply( *Xexact, *B );
131
132 //
133 // ********Other information used by block solver***********
134 // *****************(can be user specified)******************
135 //
136 const int NumGlobalElements = B->GlobalLength();
137 if (maxiters == -1)
138 maxiters = NumGlobalElements/blocksize - 1; // maximum number of iterations to run
139 //
140 ParameterList belosList;
141 belosList.set( "Num Blocks", maxsubspace); // Maximum number of blocks in Krylov factorization
142 belosList.set( "Block Size", blocksize ); // Blocksize to be used by iterative solver
143 belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed
144 belosList.set( "Maximum Restarts", maxrestarts ); // Maximum number of restarts allowed
145 belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
146 belosList.set( "Orthogonalization", ortho ); // Orthogonalization used by iterative solver
147 int verbosity = Belos::Errors + Belos::Warnings;
148 if (verbose) {
149 verbosity += Belos::TimingDetails + Belos::StatusTestDetails;
150 if (frequency > 0)
151 belosList.set( "Output Frequency", frequency );
152 }
153 if (debug) {
154 verbosity += Belos::Debug;
155 }
156 belosList.set( "Verbosity", verbosity );
157 //
158 // Construct an unpreconditioned linear problem instance.
159 //
160 Belos::LinearProblem<double,MV,OP> problem( A, X, B );
161 bool set = problem.setProblem();
162 if (set == false) {
163 if (proc_verbose)
164 std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
165 return -1;
166 }
167 //
168 //
169 // *******************************************************************
170 // *************Start the block Gmres iteration*************************
171 // *******************************************************************
172 //
173 // Create an iterative solver manager.
174 RCP< Belos::SolverManager<double,MV,OP> > newSolver;
175 if (pseudo && (blocksize == 1))
176 newSolver = rcp( new Belos::PseudoBlockGmresSolMgr<double,MV,OP>(rcp(&problem,false), rcp(&belosList,false)));
177 else
178 newSolver = rcp( new Belos::BlockGmresSolMgr<double,MV,OP>(rcp(&problem,false), rcp(&belosList,false)));
179
180 //
181 // **********Print out information about problem*******************
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 << "Block size used by solver: " << blocksize << std::endl;
188 std::cout << "Max number of restarts allowed: " << maxrestarts << std::endl;
189 std::cout << "Max number of Gmres iterations per linear system: " << maxiters << std::endl;
190 std::cout << "Relative residual tolerance: " << tol << std::endl;
191 std::cout << std::endl;
192 }
193 //
194 // Perform solve
195 //
196 Belos::ReturnType ret = newSolver->solve();
197 //
198 // Get the number of iterations for this solve.
199 //
200 int numIters = newSolver->getNumIters();
201 if (proc_verbose)
202 std::cout << "Number of iterations performed for this solve: " << numIters << std::endl;
203 //
204 // Compute actual residuals.
205 //
206 bool badRes = false;
207 std::vector<double> actual_resids( numrhs );
208 std::vector<double> rhs_norm( numrhs );
209 Epetra_MultiVector resid(*Map, numrhs);
210 OPT::Apply( *A, *X, resid );
211 MVT::MvAddMv( -1.0, resid, 1.0, *B, resid );
212 MVT::MvNorm( resid, actual_resids );
213 MVT::MvNorm( *B, rhs_norm );
214 if (proc_verbose) {
215 std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl;
216 for ( int i=0; i<numrhs; i++) {
217 double actRes = actual_resids[i]/rhs_norm[i];
218 std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl;
219 if (actRes > tol) badRes = true;
220 }
221 }
222
223 if (ret!=Belos::Converged || badRes) {
224 success = false;
225 if (proc_verbose)
226 std::cout << "End Result: TEST FAILED" << std::endl;
227 } else {
228 if (proc_verbose)
229 std::cout << "End Result: TEST PASSED" << std::endl;
230 }
231 }
232 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
233
234 #ifdef EPETRA_MPI
235 MPI_Finalize();
236 #endif
237
238 return success ? EXIT_SUCCESS : EXIT_FAILURE;
239 }