Debian Codegraph @

packages/belos/epetra/example/BlockCG/BlockCGEpetraExFile.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 "BelosBlockCGSolMgr.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
60 bool verbose = false;
61 bool success = true;
62 try {
63 bool proc_verbose = false;
64 int frequency = -1; // frequency of status test output.
65 int blocksize = 1; // blocksize
66 int numrhs = 1; // number of right-hand sides to solve for
67 int maxiters = -1; // maximum number of iterations allowed per linear system
68 std::string filename("bcsstk14.hb");
69 MT tol = 1.0e-5; // relative residual tolerance
70
71 Teuchos::CommandLineProcessor cmdp(false,true);
72 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
73 cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
74 cmdp.setOption("filename",&filename,"Filename for test matrix. Acceptable file extensions: *.hb,*.mtx,*.triU,*.triS");
75 cmdp.setOption("tol",&tol,"Relative residual tolerance used by CG solver.");
76 cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for.");
77 cmdp.setOption("block-size",&blocksize,"Block size used by CG.");
78 cmdp.setOption("max-iters",&maxiters,"Maximum number of iterations per linear system (-1 = adapted to problem/block size).");
79 if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
80 return -1;
81 }
82 if (!verbose)
83 frequency = -1; // reset frequency if test is not verbose
84 //
85 // Get the problem
86 //
87 RCP<Epetra_Map> Map;
88 RCP<Epetra_CrsMatrix> A;
89 RCP<Epetra_MultiVector> B, X;
90 RCP<Epetra_Vector> vecB, vecX;
91 EpetraExt::readEpetraLinearSystem(filename, Comm, &A, &Map, &vecX, &vecB);
92 A->OptimizeStorage();
93 proc_verbose = verbose && (MyPID==0); /* Only print on the zero processor */
94
95 // Check to see if the number of right-hand sides is the same as requested.
96 if (numrhs>1) {
97 X = rcp( new Epetra_MultiVector( *Map, numrhs ) );
98 B = rcp( new Epetra_MultiVector( *Map, numrhs ) );
99 X->Random();
100 OPT::Apply( *A, *X, *B );
101 X->PutScalar( 0.0 );
102 }
103 else {
104 X = Teuchos::rcp_implicit_cast<Epetra_MultiVector>(vecX);
105 B = Teuchos::rcp_implicit_cast<Epetra_MultiVector>(vecB);
106 }
107 //
108 // ********Other information used by block solver***********
109 // *****************(can be user specified)******************
110 //
111 const int NumGlobalElements = B->GlobalLength();
112 if (maxiters == -1)
113 maxiters = NumGlobalElements/blocksize - 1; // maximum number of iterations to run
114 //
115 ParameterList belosList;
116 belosList.set( "Block Size", blocksize ); // Blocksize to be used by iterative solver
117 belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed
118 belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
119 if (verbose) {
120 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings +
121 Belos::TimingDetails + Belos::StatusTestDetails );
122 if (frequency > 0)
123 belosList.set( "Output Frequency", frequency );
124 }
125 else
126 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings );
127 //
128 // Construct an unpreconditioned linear problem instance.
129 //
130 Belos::LinearProblem<double,MV,OP> problem( A, X, B );
131 bool set = problem.setProblem();
132 if (set == false) {
133 if (proc_verbose)
134 std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
135 return -1;
136 }
137 //
138 // Create an iterative solver manager.
139 //
140 RCP< Belos::SolverManager<double,MV,OP> > newSolver
141 = rcp( new Belos::BlockCGSolMgr<double,MV,OP>(rcp(&problem,false), rcp(&belosList,false)) );
142 //
143 // **********Print out information about problem*******************
144 //
145 if (proc_verbose) {
146 std::cout << std::endl << std::endl;
147 std::cout << "Dimension of matrix: " << NumGlobalElements << std::endl;
148 std::cout << "Number of right-hand sides: " << numrhs << std::endl;
149 std::cout << "Max number of iterations allowed: " << maxiters << std::endl;
150 std::cout << "Block size used by solver: " << blocksize << std::endl;
151 std::cout << "Relative residual tolerance: " << tol << std::endl;
152 std::cout << std::endl;
153 }
154 //
155 // Perform solve
156 //
157 Belos::ReturnType ret = newSolver->solve();
158 //
159 // Get the number of iterations for this solve.
160 //
161 int numIters = newSolver->getNumIters();
162 if (proc_verbose)
163 std::cout << "Number of iterations performed for this solve: " << numIters << std::endl;
164 //
165 // Compute actual residuals.
166 //
167 bool badRes = false;
168 std::vector<double> actual_resids( numrhs );
169 std::vector<double> rhs_norm( numrhs );
170 Epetra_MultiVector resid(*Map, numrhs);
171 OPT::Apply( *A, *X, resid );
172 MVT::MvAddMv( -1.0, resid, 1.0, *B, resid );
173 MVT::MvNorm( resid, actual_resids );
174 MVT::MvNorm( *B, rhs_norm );
175 if (proc_verbose) {
176 std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl;
177 for ( int i=0; i<numrhs; i++) {
178 double actRes = actual_resids[i]/rhs_norm[i];
179 std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl;
180 if (actRes > tol) badRes = true;
181 }
182 }
183
184 if (ret!=Belos::Converged || badRes) {
185 success = false;
186 if (proc_verbose)
187 std::cout << std::endl << "ERROR: Belos did not converge!" << std::endl;
188 } else {
189 success = true;
190 if (proc_verbose)
191 std::cout << std::endl << "SUCCESS: Belos converged!" << std::endl;
192 }
193 }
194 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
195
196 #ifdef EPETRA_MPI
197 MPI_Finalize();
198 #endif
199
200 return success ? EXIT_SUCCESS : EXIT_FAILURE;
201 }