Debian Codegraph @

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