Debian Codegraph @

packages/belos/tpetra/test/GCRODR/test_gcrodr_complex_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 Harwell-Boeing (HB) file.
11 // The right-hand-side corresponds to a randomly generated solution.
12 // The initial guesses are all set to zero.
13 //
14 // NOTE: No preconditioner is used in this case.
15 //
16
17 #include "BelosConfigDefs.hpp"
18 #include "BelosLinearProblem.hpp"
19 #include "BelosTpetraAdapter.hpp"
20 #include "BelosGCRODRSolMgr.hpp"
21 #include "BelosTpetraTestFramework.hpp"
22
23 #include <Tpetra_Core.hpp>
24 #include <Tpetra_CrsMatrix.hpp>
25
26 #include <Teuchos_CommandLineProcessor.hpp>
27 #include <Teuchos_ParameterList.hpp>
28 #include <Teuchos_GlobalMPISession.hpp>
29
30 template <typename ScalarType>
31 int run(int argc, char *argv[])
32 {
33 using Teuchos::Comm;
34 using Teuchos::RCP;
35 using Teuchos::rcpFromRef;
36 using Teuchos::tuple;
37
38 using BST = typename Tpetra::MultiVector<ScalarType>::scalar_type;
39 using ST = typename std::complex<BST>;
40
41 using SCT = typename Teuchos::ScalarTraits<ST>;
42 using MT = typename SCT::magnitudeType;
43
44 using OP = typename Tpetra::Operator<ST>;
45 using MV = typename Tpetra::MultiVector<ST>;
46 using OPT = typename Belos::OperatorTraits<ST,MV,OP>;
47 using MVT = typename Belos::MultiVecTraits<ST,MV>;
48
49 using tcrsmatrix_t = Tpetra::CrsMatrix<ST>;
50
51 Teuchos::GlobalMPISession mpisess(&argc, &argv, &std::cout);
52
53 const ST one = SCT::one();
54
55 RCP<const Comm<int> > comm = Tpetra::getDefaultComm();
56 int MyPID = rank(*comm);
57
58 // Get test parameters from command-line processor
59 bool verbose = false, proc_verbose = false, debug = false;
60 int frequency = -1; // how often residuals are printed by solver
61 int numrhs = 1; // total number of right-hand sides to solve for
62 std::string filename("mhd1280b.cua");
63 MT tol = 1.0e-5; // relative residual tolerance
64
65 Teuchos::CommandLineProcessor cmdp(false,true);
66 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
67 cmdp.setOption("debug","nodebug",&debug,"Run debugging checks.");
68 cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
69 cmdp.setOption("tol",&tol,"Relative residual tolerance used by GCRODR solver.");
70 cmdp.setOption("filename",&filename,"Filename for Harwell-Boeing test matrix.");
71 cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for.");
72 if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
73 return -1;
74 }
75 if (debug) {
76 verbose = true;
77 }
78 if (!verbose) {
79 frequency = -1; // reset frequency if test is not verbose
80 }
81
82 proc_verbose = ( verbose && (MyPID==0) );
83
84 if (proc_verbose) {
85 std::cout << Belos::Belos_Version() << std::endl << std::endl;
86 }
87
88 Belos::Tpetra::HarwellBoeingReader<tcrsmatrix_t> reader( comm );
89 RCP<tcrsmatrix_t> A = reader.readFromFile( filename );
90 RCP<const Tpetra::Map<> > map = A->getDomainMap();
91
92 // Create initial vectors
93 RCP<MV> B, X;
94 X = rcp( new MV(map,numrhs) );
95 MVT::MvRandom( *X );
96 B = rcp( new MV(map,numrhs) );
97 OPT::Apply( *A, *X, *B );
98 MVT::MvInit( *X, 0.0 );
99
100 // ********Other information used by block solver***********
101 // *****************(can be user specified)******************
102 const int NumGlobalElements = B->getGlobalLength();
103 int numIters1, numIters2, numIters3;
104 int maxits = NumGlobalElements; // maximum number of iterations to run
105 int numBlocks = 100;
106 int numRecycledBlocks = 20;
107 Teuchos::ParameterList belosList;
108 belosList.set( "Maximum Iterations", maxits ); // Maximum number of iterations allowed
109 belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
110 belosList.set( "Num Blocks", numBlocks );
111 belosList.set( "Num Recycled Blocks", numRecycledBlocks );
112
113 int verbLevel = Belos::Errors + Belos::Warnings;
114 if (debug) {
115 verbLevel += Belos::Debug;
116 }
117 if (verbose) {
118 verbLevel += Belos::TimingDetails + Belos::FinalSummary + Belos::StatusTestDetails;
119 }
120 belosList.set( "Verbosity", verbLevel );
121 if (verbose) {
122 if (frequency > 0) {
123 belosList.set( "Output Frequency", frequency );
124 }
125 }
126
127 // Construct an unpreconditioned linear problem instance.
128 Belos::LinearProblem<ST,MV,OP> problem( A, X, B );
129 bool set = problem.setProblem();
130 if (set == false) {
131 if (proc_verbose)
132 std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
133 return -1;
134 }
135
136 // Start the GCRODR iteration
137 Belos::GCRODRSolMgr<ST,MV,OP> solver( rcpFromRef(problem), rcpFromRef(belosList) );
138
139 // Print out information about problem
140 if (proc_verbose) {
141 std::cout << std::endl << std::endl;
142 std::cout << "Dimension of matrix: " << NumGlobalElements << std::endl;
143 std::cout << "Number of right-hand sides: " << numrhs << std::endl;
144 std::cout << "Max number of GCRODR iterations: " << maxits << std::endl;
145 std::cout << "Relative residual tolerance: " << tol << std::endl;
146 std::cout << std::endl;
147 }
148
149 // Perform solve
150 Belos::ReturnType ret = solver.solve();
151 numIters1=solver.getNumIters();
152
153 // Compute actual residuals.
154 bool badRes = false;
155 std::vector<MT> actual_resids( numrhs );
156 std::vector<MT> rhs_norm( numrhs );
157 MV resid(map, numrhs);
158 OPT::Apply( *A, *X, resid );
159 MVT::MvAddMv( -one, resid, one, *B, resid );
160 MVT::MvNorm( resid, actual_resids );
161 MVT::MvNorm( *B, rhs_norm );
162 if (proc_verbose) {
163 std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl;
164 }
165 for ( int i=0; i<numrhs; i++) {
166 MT actRes = actual_resids[i]/rhs_norm[i];
167 if (proc_verbose) {
168 std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl;
169 }
170 if (actRes > tol) badRes = true;
171 }
172
173 if (proc_verbose) { std::cout << "First solve took " << numIters1 << " iterations." << std::endl; }
174
175 // Resolve linear system with same rhs and recycled space
176 MVT::MvInit( *X, SCT::zero() );
177 solver.reset(Belos::Problem);
178 ret = solver.solve();
179 numIters2=solver.getNumIters();
180
181 if (proc_verbose) { std::cout << "Second solve took " << numIters2 << " iterations." << std::endl; }
182
183 // Resolve linear system (again) with same rhs and recycled space
184 MVT::MvInit( *X, SCT::zero() );
185 solver.reset(Belos::Problem);
186 ret = solver.solve();
187 numIters3=solver.getNumIters();
188
189 if (proc_verbose) { std::cout << "Third solve took " << numIters3 << " iterations." << std::endl; }
190
191 if ( ret!=Belos::Converged || badRes || numIters1 < numIters2 || numIters2 < numIters3 ) {
192 if (proc_verbose) {
193 std::cout << "\nEnd Result: TEST FAILED" << std::endl;
194 }
195 return -1;
196 }
197
198 // Default return value
199 if (proc_verbose) {
200 std::cout << "\nEnd Result: TEST PASSED" << std::endl;
201 }
202 return 0;
203
204 } // end test_gcrodr_complex_hb.cpp
205
206 int main(int argc, char *argv[]) {
207 return run<double>(argc, argv);
208 // return run<float>(argc, argv);
209 }
210