Belos Package Browser (Single Doxygen Collection)  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
test_pseudo_tfqmr_complex_hb.cpp
Go to the documentation of this file.
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"
22 
23 #ifdef HAVE_MPI
24 #include <mpi.h>
25 #endif
26 
27 // I/O for Harwell-Boeing files
28 #ifdef HAVE_BELOS_TRIUTILS
29 #include "Trilinos_Util_iohb.h"
30 #endif
31 
32 #include "MyMultiVec.hpp"
33 #include "MyBetterOperator.hpp"
34 #include "MyOperator.hpp"
35 
36 using namespace Teuchos;
37 
38 int main(int argc, char *argv[]) {
39  //
40  typedef std::complex<double> ST;
41  typedef ScalarTraits<ST> SCT;
42  typedef SCT::magnitudeType MT;
43  typedef Belos::MultiVec<ST> MV;
44  typedef Belos::Operator<ST> OP;
45  typedef Belos::MultiVecTraits<ST,MV> MVT;
47  ST one = SCT::one();
48  ST zero = SCT::zero();
49 
50  int info = 0;
51  bool norm_failure = false;
52 
53  Teuchos::GlobalMPISession session(&argc, &argv, NULL);
54  //
55  int MyPID = session.getRank();
56 
57  using Teuchos::RCP;
58  using Teuchos::rcp;
59 
60  bool success = false;
61  bool verbose = false;
62  try {
63  bool proc_verbose = false;
64  int frequency = -1; // how often residuals are printed by solver
65  int blocksize = 1;
66  int numrhs = 1;
67  std::string filename("mhd1280b.cua");
68  MT tol = 1.0e-5; // relative residual tolerance
69 
70  CommandLineProcessor cmdp(false, true);
71  cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
72  cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
73  cmdp.setOption("filename",&filename,"Filename for Harwell-Boeing test matrix.");
74  cmdp.setOption("tol",&tol,"Relative residual tolerance used by pseudo-block TFQMR solver.");
75  cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for.");
76  if (cmdp.parse(argc,argv) != CommandLineProcessor::PARSE_SUCCESSFUL) {
77  return EXIT_FAILURE;
78  }
79 
80  proc_verbose = verbose && (MyPID==0); /* Only print on the zero processor */
81  if (proc_verbose) {
82  std::cout << Belos::Belos_Version() << std::endl << std::endl;
83  }
84  if (!verbose)
85  frequency = -1; // reset frequency if test is not verbose
86 
87 
88 #ifndef HAVE_BELOS_TRIUTILS
89  std::cout << "This test requires Triutils. Please configure with --enable-triutils." << std::endl;
90  if (MyPID==0) {
91  std::cout << "End Result: TEST FAILED" << std::endl;
92  }
93  return EXIT_FAILURE;
94 #endif
95 
96  // Get the data from the HB file
97  int dim,dim2,nnz;
98  MT *dvals;
99  int *colptr,*rowind;
100  ST *cvals;
101  nnz = -1;
102  info = readHB_newmat_double(filename.c_str(),&dim,&dim2,&nnz,
103  &colptr,&rowind,&dvals);
104  if (info == 0 || nnz < 0) {
105  if (MyPID==0) {
106  std::cout << "Error reading '" << filename << "'" << std::endl;
107  std::cout << "End Result: TEST FAILED" << std::endl;
108  }
109  return EXIT_FAILURE;
110  }
111  // Convert interleaved doubles to std::complex values
112  cvals = new ST[nnz];
113  for (int ii=0; ii<nnz; ii++) {
114  cvals[ii] = ST(dvals[ii*2],dvals[ii*2+1]);
115  }
116  // Build the problem matrix
118  = rcp( new MyBetterOperator<ST>(dim,colptr,nnz,rowind,cvals) );
119  //
120  // ********Other information used by block solver***********
121  // *****************(can be user specified)******************
122  //
123  int maxits = dim/blocksize; // maximum number of iterations to run
124  //
125  ParameterList belosList;
126  belosList.set( "Maximum Iterations", maxits ); // Maximum number of iterations allowed
127  belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
128  if (verbose) {
129  belosList.set( "Verbosity", Belos::Errors + Belos::Warnings +
131  if (frequency > 0)
132  belosList.set( "Output Frequency", frequency );
133  }
134  else
135  belosList.set( "Verbosity", Belos::Errors + Belos::Warnings );
136  //
137  // Construct the right-hand side and solution multivectors.
138  // NOTE: The right-hand side will be constructed such that the solution is
139  // a vectors of one.
140  //
141  RCP<MyMultiVec<ST> > soln = rcp( new MyMultiVec<ST>(dim,numrhs) );
142  RCP<MyMultiVec<ST> > rhs = rcp( new MyMultiVec<ST>(dim,numrhs) );
143  MVT::MvRandom( *soln );
144  OPT::Apply( *A, *soln, *rhs );
145  MVT::MvInit( *soln, zero );
146  //
147  // Construct an unpreconditioned linear problem instance.
148  //
150  rcp( new Belos::LinearProblem<ST,MV,OP>( A, soln, rhs ) );
151  bool set = problem->setProblem();
152  if (set == false) {
153  if (proc_verbose)
154  std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
155  return EXIT_FAILURE;
156  }
157  //
158  // *******************************************************************
159  // *************Start the TFQMR iteration***********************
160  // *******************************************************************
161  //
162  Belos::PseudoBlockTFQMRSolMgr<ST,MV,OP> solver( problem, rcp(&belosList,false) );
163 
164  //
165  // **********Print out information about problem*******************
166  //
167  if (proc_verbose) {
168  std::cout << std::endl << std::endl;
169  std::cout << "Dimension of matrix: " << dim << std::endl;
170  std::cout << "Number of right-hand sides: " << numrhs << std::endl;
171  std::cout << "Block size used by solver: " << blocksize << std::endl;
172  std::cout << "Max number of pseudo-block TFQMR iterations: " << maxits << std::endl;
173  std::cout << "Relative residual tolerance: " << tol << std::endl;
174  std::cout << std::endl;
175  }
176  //
177  // Perform solve
178  //
179  Belos::ReturnType ret = solver.solve();
180  //
181  // Compute actual residuals.
182  //
183  RCP<MyMultiVec<ST> > temp = rcp( new MyMultiVec<ST>(dim,numrhs) );
184  OPT::Apply( *A, *soln, *temp );
185  MVT::MvAddMv( one, *rhs, -one, *temp, *temp );
186  std::vector<MT> norm_num(numrhs), norm_denom(numrhs);
187  MVT::MvNorm( *temp, norm_num );
188  MVT::MvNorm( *rhs, norm_denom );
189  for (int i=0; i<numrhs; ++i) {
190  if (proc_verbose)
191  std::cout << "Relative residual "<<i<<" : " << norm_num[i] / norm_denom[i] << std::endl;
192  if ( norm_num[i] / norm_denom[i] > tol ) {
193  norm_failure = true;
194  }
195  }
196 
197  // Clean up.
198  delete [] dvals;
199  delete [] colptr;
200  delete [] rowind;
201  delete [] cvals;
202 
203  success = ret==Belos::Converged && !norm_failure;
204  if (success) {
205  if (proc_verbose)
206  std::cout << "End Result: TEST PASSED" << std::endl;
207  } else {
208  if (proc_verbose)
209  std::cout << "End Result: TEST FAILED" << std::endl;
210  }
211  }
212  TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
213 
214  return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
215 } // end test_tfqmr_complex_hb.cpp
std::string Belos_Version()
int main(int argc, char *argv[])
The Belos::PseudoBlockTFQMRSolMgr provides a solver manager for the pseudo-block TFQMR linear solver...
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Traits class which defines basic operations on multivectors.
Simple example of a user&#39;s defined Belos::MultiVec class.
Definition: MyMultiVec.hpp:33
std::string filename
Alternative run-time polymorphic interface for operators.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void setOption(const char option_true[], const char option_false[], bool *option_val, const char documentation[]=NULL)
#define TEUCHOS_STANDARD_CATCH_STATEMENTS(VERBOSE, ERR_STREAM, SUCCESS_FLAG)
ReturnType solve() override
This method performs possibly repeated calls to the underlying linear solver&#39;s iterate() routine unti...
A linear system to solve, and its associated information.
const double tol
Class which describes the linear problem to be solved by the iterative solver.
EParseCommandLineReturn parse(int argc, char *argv[], std::ostream *errout=&std::cerr) const
The Belos::PseudoBlockTFQMRSolMgr provides a powerful and fully-featured solver manager over the pseu...
ReturnType
Whether the Belos solve converged for all linear systems.
Definition: BelosTypes.hpp:123
Interface for multivectors used by Belos&#39; linear solvers.
Class which defines basic traits for the operator type.
Belos header file which uses auto-configuration information to include necessary C++ headers...
Simple example of a user&#39;s defined Belos::Operator class.