Debian Codegraph @

packages/belos/tpetra/test/BiCGStab/test_bicgstab_hb.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 only be a Harwell-Boeing (*.hb)
11 // matrix. The right-hand side is generated using a random solution vector that has
12 // the matrix applied to it. The initial guesses are all set to zero.
13 //
14 #include "BelosConfigDefs.hpp"
15 #include "BelosLinearProblem.hpp"
16 #include "BelosTpetraAdapter.hpp"
17 #include "BelosBiCGStabSolMgr.hpp"
18 #include "BelosTpetraTestFramework.hpp"
19
20 #include <Teuchos_CommandLineProcessor.hpp>
21 #include <Teuchos_ParameterList.hpp>
22 #include <Teuchos_GlobalMPISession.hpp>
23 #include "Teuchos_StandardCatchMacros.hpp"
24 #include <Tpetra_Core.hpp>
25 #include <Tpetra_CrsMatrix.hpp>
26
27 int main (int argc, char *argv[])
28 {
29 using Teuchos::ParameterList;
30 using Teuchos::RCP;
31 using Teuchos::rcp;
32 using std::cout;
33 using std::endl;
34
35 typedef Tpetra::MultiVector<>::scalar_type ST;
36 typedef Teuchos::ScalarTraits<ST> SCT;
37 typedef SCT::magnitudeType MT;
38 typedef Tpetra::Operator<ST> OP;
39 typedef Tpetra::MultiVector<ST> MV;
40 typedef Belos::OperatorTraits<ST,MV,OP> OPT;
41 typedef Belos::MultiVecTraits<ST,MV> MVT;
42
43 Teuchos::GlobalMPISession mpisess(&argc,&argv,&cout);
44
45 const ST one = SCT::one();
46
47 RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
48 int MyPID = rank(*comm);
49
50 bool verbose = false;
51 bool success = true;
52
53 // This "try" relates to TEUCHOS_STANDARD_CATCH_STATEMENTS near the
54 // bottom of main(). That macro has the corresponding "catch".
55 try {
56 bool proc_verbose = false;
57 int frequency = -1; // frequency of status test output.
58 int numrhs = 1; // number of right-hand sides to solve for
59 int maxiters = -1; // maximum number of iterations allowed per linear system
60 std::string filename ("cage4.hb");
61 MT tol = 1.0e-5; // relative residual tolerance
62
63 Teuchos::CommandLineProcessor cmdp(false,true);
64 cmdp.setOption ("verbose", "quiet", &verbose, "Whether to print messages "
65 "and results.");
66 cmdp.setOption ("frequency", &frequency, "Frequency of solver output "
67 "(1 means every iteration; -1 means never).");
68 cmdp.setOption ("filename", &filename, "Test matrix filename. "
69 "Allowed file extensions: *.hb, *.mtx, *.triU, *.triS");
70 cmdp.setOption ("tol", &tol, "Relative residual tolerance for solver.");
71 cmdp.setOption ("num-rhs", &numrhs, "Number of right-hand sides to solve.");
72 cmdp.setOption ("max-iters", &maxiters, "Maximum number of iterations per "
73 "linear system (-1 = adapted to problem/block size).");
74 if (cmdp.parse (argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
75 return -1;
76 }
77 if (! verbose) {
78 frequency = -1; // reset frequency if test is not verbose
79 }
80
81 proc_verbose = ( verbose && (MyPID==0) );
82
83 //
84 // *************Get the problem*********************
85 //
86 Belos::Tpetra::HarwellBoeingReader<Tpetra::CrsMatrix<ST> > reader( comm );
87 RCP<Tpetra::CrsMatrix<ST> > A = reader.readFromFile( filename );
88 RCP<const Tpetra::Map<> > map = A->getDomainMap();
89
90 // Create initial vectors
91 RCP<MV> B, X;
92 X = rcp( new MV(map,numrhs) );
93 MVT::MvRandom( *X );
94 B = rcp( new MV(map,numrhs) );
95 OPT::Apply( *A, *X, *B );
96 MVT::MvInit( *X, 0.0 );
97
98 //
99 // Create parameter list for the Belos solver
100 //
101 const int NumGlobalElements = B->getGlobalLength ();
102 if (maxiters == -1) {
103 maxiters = NumGlobalElements - 1; // maximum number of iterations to run
104 }
105 RCP<ParameterList> belosList (new ParameterList ("Belos"));
106 belosList->set ("Maximum Iterations", maxiters);
107 belosList->set ("Convergence Tolerance", tol);
108 if (verbose) {
109 belosList->set ("Verbosity", Belos::Errors + Belos::Warnings +
110 Belos::TimingDetails + Belos::StatusTestDetails);
111 if (frequency > 0) {
112 belosList->set ("Output Frequency", frequency);
113 }
114 }
115 else {
116 belosList->set ("Verbosity", Belos::Errors + Belos::Warnings +
117 Belos::FinalSummary);
118 }
119 //
120 // Construct a preconditioned linear problem
121 //
122 RCP<Belos::LinearProblem<ST,MV,OP> > problem
123 = rcp (new Belos::LinearProblem<ST,MV,OP> (A, X, B));
124 bool set = problem->setProblem ();
125 if (! set) {
126 if (proc_verbose) {
127 cout << endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << endl;
128 }
129 return -1;
130 }
131
132 // Create a Belos solver.
133 RCP<Belos::SolverManager<ST,MV,OP> > solver
134 = rcp (new Belos::BiCGStabSolMgr<ST,MV,OP> (problem, belosList));
135
136 if (proc_verbose) {
137 cout << endl << endl;
138 cout << "Dimension of matrix: " << NumGlobalElements << endl;
139 cout << "Number of right-hand sides: " << numrhs << endl;
140 cout << "Max number of BiCGStab iterations: " << maxiters << endl;
141 cout << "Relative residual tolerance: " << tol << endl;
142 cout << endl;
143 }
144
145 // Ask Belos to solve the linear system.
146 Belos::ReturnType ret = solver->solve();
147
148 //
149 // Compute actual residuals.
150 //
151 bool badRes = false;
152 std::vector<MT> actual_resids( numrhs );
153 std::vector<MT> rhs_norm( numrhs );
154 MV resid(map, numrhs);
155 OPT::Apply( *A, *X, resid );
156 MVT::MvAddMv( -one, resid, one, *B, resid );
157 MVT::MvNorm( resid, actual_resids );
158 MVT::MvNorm( *B, rhs_norm );
159 if (proc_verbose) {
160 std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl;
161 }
162 for ( int i=0; i<numrhs; i++) {
163 MT actRes = actual_resids[i]/rhs_norm[i];
164 if (proc_verbose) {
165 std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl;
166 }
167 if (actRes > tol) badRes = true;
168 }
169
170 if ( ret!=Belos::Converged || badRes) {
171 if (proc_verbose) {
172 std::cout << "\nEnd Result: TEST FAILED" << std::endl;
173 }
174 return -1;
175 }
176 //
177 // Default return value
178 //
179 if (proc_verbose) {
180 std::cout << "\nEnd Result: TEST PASSED" << std::endl;
181 }
182
183 }
184 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
185
186 return success ? EXIT_SUCCESS : EXIT_FAILURE;
187 }