Debian Codegraph @

packages/belos/epetra/example/RCG/RCGEpetraExFile.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 HB file is used instead of random vectors.
12 // 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 "BelosRCGSolMgr.hpp"
20 #include "BelosEpetraUtils.h"
21 #include "Trilinos_Util.h"
22 #include "Epetra_CrsMatrix.h"
23 #include "Epetra_Map.h"
24 #include "Teuchos_CommandLineProcessor.hpp"
25 #include "Teuchos_ParameterList.hpp"
26 #include "Teuchos_StandardCatchMacros.hpp"
27
28 int main(int argc, char *argv[]) {
29 //
30 #ifdef EPETRA_MPI
31 // Initialize MPI
32 MPI_Init(&argc,&argv);
33 #endif
34 bool verbose = true;
35 bool success = true;
36 try {
37 //
38 using Teuchos::ParameterList;
39 using Teuchos::RCP;
40 using Teuchos::rcp;
41 //
42 // Get test parameters from command-line processor
43 //
44 bool proc_verbose = false;
45 int frequency = -1; // frequency of status test output.
46 std::string filename("bcsstk14.hb"); // default input filename
47 double tol = 1.0e-6; // relative residual tolerance
48 int numBlocks = 100; // maximum number of blocks the solver can use for the Krylov subspace
49 int recycleBlocks = 10; // maximum number of blocks the solver can use for the recycle space
50 int numrhs = 2; // number of right-hand sides to solve for
51 int maxiters = 4000; // maximum number of iterations allowed per linear system
52
53 Teuchos::CommandLineProcessor cmdp(false,true);
54 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
55 cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
56 cmdp.setOption("filename",&filename,"Filename for test matrix.");
57 cmdp.setOption("tol",&tol,"Relative residual tolerance used by the RCG solver.");
58 cmdp.setOption("max-subspace",&numBlocks,"Maximum number of vectors in search space (not including recycle space).");
59 cmdp.setOption("recycle",&recycleBlocks,"Number of vectors in recycle space.");
60 cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for.");
61 cmdp.setOption("max-iters",&maxiters,"Maximum number of iterations per linear system (-1 = adapted to problem/block size).");
62 if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
63 return -1;
64 }
65 if (!verbose)
66 frequency = -1; // reset frequency if test is not verbose
67 //
68 // Get the problem
69 //
70 int MyPID;
71 RCP<Epetra_CrsMatrix> A;
72 RCP<Epetra_MultiVector> B, X;
73 int return_val =Belos::Util::createEpetraProblem(filename,NULL,&A,&B,&X,&MyPID);
74 if(return_val != 0) return return_val;
75 proc_verbose = ( verbose && (MyPID==0) );
76 //
77 // Solve using Belos
78 //
79 typedef double ST;
80 typedef Epetra_Operator OP;
81 typedef Epetra_MultiVector MV;
82 typedef Belos::OperatorTraits<ST,MV,OP> OPT;
83 typedef Belos::MultiVecTraits<ST,MV> MVT;
84 //
85 // *****Construct initial guess and right-hand sides *****
86 //
87 if (numrhs != 1) {
88 X = rcp( new Epetra_MultiVector( A->Map(), numrhs ) );
89 MVT::MvInit( *X, 1.0 );
90 B = rcp( new Epetra_MultiVector( A->Map(), numrhs ) );
91 OPT::Apply( *A, *X, *B );
92 MVT::MvInit( *X, 0.0 );
93 }
94 else { // initialize exact solution to be vector of ones
95 MVT::MvInit( *X, 1.0 );
96 OPT::Apply( *A, *X, *B );
97 MVT::MvInit( *X, 0.0 );
98 }
99 //
100 // ********Other information used by block solver***********
101 // *****************(can be user specified)******************
102 //
103 const int NumGlobalElements = B->GlobalLength();
104 if (maxiters == -1)
105 maxiters = NumGlobalElements - 1; // maximum number of iterations to run
106 //
107 ParameterList belosList;
108 belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed
109 belosList.set( "Num Blocks", numBlocks); // Maximum number of blocks in Krylov space
110 belosList.set( "Num Recycled Blocks", recycleBlocks ); // Number of vectors in recycle space
111 belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
112 if (verbose) {
113 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings +
114 Belos::TimingDetails + Belos::FinalSummary + Belos::StatusTestDetails );
115 if (frequency > 0)
116 belosList.set( "Output Frequency", frequency );
117 }
118 else
119 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings );
120 //
121 // Construct an unpreconditioned linear problem instance.
122 //
123 Belos::LinearProblem<double,MV,OP> problem( A, X, B );
124 bool set = problem.setProblem();
125 if (set == false) {
126 if (proc_verbose)
127 std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
128 return -1;
129 }
130 //
131 // Create an iterative solver manager.
132 //
133 RCP< Belos::SolverManager<double,MV,OP> > newSolver
134 = rcp( new Belos::RCGSolMgr<double,MV,OP>(rcp(&problem,false), rcp(&belosList,false)) );
135 //
136 // **********Print out information about problem*******************
137 //
138 if (proc_verbose) {
139 std::cout << std::endl << std::endl;
140 std::cout << "Dimension of matrix: " << NumGlobalElements << std::endl;
141 std::cout << "Number of right-hand sides: " << numrhs << std::endl;
142 std::cout << "Max number of RCG iterations: " << maxiters << std::endl;
143 std::cout << "Max number of vectors in Krylov space: " << numBlocks << std::endl;
144 std::cout << "Number of vectors in recycle space: " << recycleBlocks << std::endl;
145 std::cout << "Relative residual tolerance: " << tol << std::endl;
146 std::cout << std::endl;
147 }
148 //
149 // Perform solve
150 //
151 Belos::ReturnType ret = newSolver->solve();
152 //
153 // Compute actual residuals.
154 //
155 bool badRes = false;
156 std::vector<double> actual_resids( numrhs );
157 std::vector<double> rhs_norm( numrhs );
158 Epetra_MultiVector resid(A->Map(), numrhs);
159 OPT::Apply( *A, *X, resid );
160 MVT::MvAddMv( -1.0, resid, 1.0, *B, resid );
161 MVT::MvNorm( resid, actual_resids );
162 MVT::MvNorm( *B, rhs_norm );
163 if (proc_verbose) {
164 std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl;
165 for ( int i=0; i<numrhs; i++) {
166 double actRes = actual_resids[i]/rhs_norm[i];
167 std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl;
168 if (actRes > tol) badRes = true;
169 }
170 }
171
172 if (ret!=Belos::Converged || badRes) {
173 success = false;
174 if (proc_verbose)
175 std::cout << std::endl << "ERROR: Belos did not converge!" << std::endl;
176 }
177 else {
178 success = true;
179 if (proc_verbose)
180 std::cout << std::endl << "SUCCESS: Belos converged!" << std::endl;
181 }
182 }
183 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
184
185 #ifdef EPETRA_MPI
186 MPI_Finalize();
187 #endif
188 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
189 }