Debian Codegraph @

packages/belos/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 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 "BelosGCRODRSolMgr.hpp"
19 #include "Teuchos_CommandLineProcessor.hpp"
20 #include "Teuchos_ParameterList.hpp"
21
22 #ifdef HAVE_MPI
23 #include <mpi.h>
24 #endif
25
26 // I/O for Harwell-Boeing files
27 #ifdef HAVE_BELOS_TRIUTILS
28 #include "Trilinos_Util_iohb.h"
29 #endif
30
31 #include "MyMultiVec.hpp"
32 #include "MyBetterOperator.hpp"
33 #include "MyOperator.hpp"
34
35 using namespace Teuchos;
36
37 int main(int argc, char *argv[]) {
38 //
39 typedef std::complex<double> ST;
40 typedef ScalarTraits<ST> SCT;
41 typedef SCT::magnitudeType MT;
42 typedef Belos::MultiVec<ST> MV;
43 typedef Belos::Operator<ST> OP;
44 typedef Belos::MultiVecTraits<ST,MV> MVT;
45 typedef Belos::OperatorTraits<ST,MV,OP> OPT;
46 ST one = SCT::one();
47 ST zero = SCT::zero();
48
49 int info = 0;
50 bool norm_failure = false;
51
52 Teuchos::GlobalMPISession session(&argc, &argv, NULL);
53 int MyPID = session.getRank();
54
55 using Teuchos::RCP;
56 using Teuchos::rcp;
57
58 bool success = false;
59 bool verbose = false;
60 try {
61 bool proc_verbose = false;
62 int frequency = -1; // how often residuals are printed by solver
63 int blocksize = 1;
64 int numrhs = 1;
65 std::string filename("mhd1280b.cua");
66 MT tol = 1.0e-5; // relative residual tolerance
67
68 CommandLineProcessor cmdp(false,true);
69 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
70 cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
71 cmdp.setOption("filename",&filename,"Filename for Harwell-Boeing test matrix.");
72 cmdp.setOption("tol",&tol,"Relative residual tolerance used by GCRODR solver.");
73 cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for.");
74 if (cmdp.parse(argc,argv) != CommandLineProcessor::PARSE_SUCCESSFUL) {
75 return EXIT_FAILURE;
76 }
77
78 proc_verbose = verbose && (MyPID==0); /* Only print on the zero processor */
79 if (proc_verbose) {
80 std::cout << Belos::Belos_Version() << std::endl << std::endl;
81 }
82 if (!verbose)
83 frequency = -1; // reset frequency if test is not verbose
84
85 #ifndef HAVE_BELOS_TRIUTILS
86 std::cout << "This test requires Triutils. Please configure with --enable-triutils." << std::endl;
87 if (MyPID==0) {
88 std::cout << "End Result: TEST FAILED" << std::endl;
89 }
90 return EXIT_FAILURE;
91 #endif
92 // Get the data from the HB file
93 int dim,dim2,nnz;
94 MT *dvals;
95 int *colptr,*rowind;
96 ST *cvals;
97 nnz = -1;
98 info = readHB_newmat_double(filename.c_str(),&dim,&dim2,&nnz,
99 &colptr,&rowind,&dvals);
100 if (info == 0 || nnz < 0) {
101 if (MyPID==0) {
102 std::cout << "Error reading '" << filename << "'" << std::endl;
103 std::cout << "End Result: TEST FAILED" << std::endl;
104 }
105 return EXIT_FAILURE;
106 }
107 // Convert interleaved doubles to std::complex values
108 cvals = new ST[nnz];
109 for (int ii=0; ii<nnz; ii++) {
110 cvals[ii] = ST(dvals[ii*2],dvals[ii*2+1]);
111 }
112 // Build the problem matrix
113 RCP< MyBetterOperator<ST> > A
114 = rcp( new MyBetterOperator<ST>(dim,colptr,nnz,rowind,cvals) );
115 // ********Other information used by block solver***********
116 // *****************(can be user specified)******************
117 int maxits = dim/blocksize; // maximum number of iterations to run
118 int numBlocks = 100;
119 int numRecycledBlocks = 20;
120 int numIters1, numIters2, numIters3;
121 ParameterList belosList;
122 belosList.set( "Maximum Iterations", maxits ); // Maximum number of iterations allowed
123 belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
124 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings + Belos::StatusTestDetails + Belos::TimingDetails);
125 belosList.set( "Num Blocks", numBlocks );
126 belosList.set( "Num Recycled Blocks", numRecycledBlocks );
127 // Construct the right-hand side and solution multivectors.
128 // NOTE: The right-hand side will be constructed such that the solution is
129 // a vectors of one.
130 RCP<MyMultiVec<ST> > soln = rcp( new MyMultiVec<ST>(dim,numrhs) );
131 RCP<MyMultiVec<ST> > rhs = rcp( new MyMultiVec<ST>(dim,numrhs) );
132 MVT::MvRandom( *soln );
133 OPT::Apply( *A, *soln, *rhs );
134 MVT::MvInit( *soln, zero );
135 // Construct an unpreconditioned linear problem instance.
136 RCP<Belos::LinearProblem<ST,MV,OP> > problem =
137 rcp( new Belos::LinearProblem<ST,MV,OP>( A, soln, rhs ) );
138 bool set = problem->setProblem();
139 if (set == false) {
140 if (proc_verbose)
141 std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
142 return EXIT_FAILURE;
143 }
144 // *******************************************************************
145 // *************Start the GCRODR iteration***********************
146 // *******************************************************************
147 Belos::GCRODRSolMgr<ST,MV,OP> solver( problem, rcp(&belosList,false) );
148 // **********Print out information about problem*******************
149 if (proc_verbose) {
150 std::cout << std::endl << std::endl;
151 std::cout << "Dimension of matrix: " << dim << std::endl;
152 std::cout << "Number of right-hand sides: " << numrhs << std::endl;
153 std::cout << "Block size used by solver: " << blocksize << std::endl;
154 std::cout << "Max number of GCRODR iterations: " << maxits << std::endl;
155 std::cout << "Relative residual tolerance: " << tol << std::endl;
156 std::cout << std::endl;
157 }
158 // Perform solve
159 Belos::ReturnType ret = solver.solve();
160 // Get number of iterations
161 numIters1=solver.getNumIters();
162 // Compute actual residuals.
163 RCP<MyMultiVec<ST> > temp = rcp( new MyMultiVec<ST>(dim,numrhs) );
164 std::vector<MT> norm_num(numrhs), norm_denom(numrhs);
165 OPT::Apply( *A, *soln, *temp );
166 MVT::MvAddMv( one, *rhs, -one, *temp, *temp );
167 MVT::MvNorm( *temp, norm_num );
168 MVT::MvNorm( *rhs, norm_denom );
169 for (int i=0; i<numrhs; ++i) {
170 if (proc_verbose)
171 std::cout << "Relative residual "<<i<<" : " << norm_num[i] / norm_denom[i] << std::endl;
172 if ( norm_num[i] / norm_denom[i] > tol ) {
173 norm_failure = true;
174 }
175 }
176 // Resolve linear system with same rhs and recycled space
177 MVT::MvInit( *soln, zero );
178 solver.reset(Belos::Problem);
179 ret = solver.solve();
180 numIters2=solver.getNumIters();
181
182 // Resolve linear system (again) with same rhs and recycled space
183 MVT::MvInit( *soln, zero );
184 solver.reset(Belos::Problem);
185 ret = solver.solve();
186 numIters3=solver.getNumIters();
187 // Clean up.
188 delete [] dvals;
189 delete [] colptr;
190 delete [] rowind;
191 delete [] cvals;
192 // Test for failures
193 if ( ret!=Belos::Converged || norm_failure || numIters1 < numIters2 || numIters2 < numIters3 ) {
194 success = false;
195 if (proc_verbose)
196 std::cout << "End Result: TEST FAILED" << std::endl;
197 } else {
198 success = true;
199 if (proc_verbose)
200 std::cout << "End Result: TEST PASSED" << std::endl;
201 }
202 }
203 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
204
205 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
206 } // end test_gcrodr_complex_hb.cpp