Belos Package Browser (Single Doxygen Collection)  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
test_tfqmr_complex_hb.cpp
Go to the documentation of this file.
1 //@HEADER
2 // ************************************************************************
3 //
4 // Belos: Block Linear Solvers Package
5 // Copyright 2004 Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ************************************************************************
40 //@HEADER
41 //
42 // This driver reads a problem from a Harwell-Boeing (HB) file.
43 // The right-hand-side from the HB file is used instead of random vectors.
44 // The initial guesses are all set to zero.
45 //
46 // NOTE: No preconditioner is used in this case.
47 //
48 #include "BelosConfigDefs.hpp"
49 #include "BelosLinearProblem.hpp"
50 #include "BelosTFQMRSolMgr.hpp"
54 
55 #ifdef HAVE_MPI
56 #include <mpi.h>
57 #endif
58 
59 // I/O for Harwell-Boeing files
60 #ifdef HAVE_BELOS_TRIUTILS
61 #include "Trilinos_Util_iohb.h"
62 #endif
63 
64 #include "MyMultiVec.hpp"
65 #include "MyBetterOperator.hpp"
66 #include "MyOperator.hpp"
67 
68 using namespace Teuchos;
69 
70 int main(int argc, char *argv[]) {
71  //
72 #ifdef HAVE_COMPLEX
73  typedef std::complex<double> ST;
74 #elif HAVE_COMPLEX_H
75  typedef std::complex<double> ST;
76 #else
77  std::cout << "Not compiled with std::complex support." << std::endl;
78  std::cout << "End Result: TEST FAILED" << std::endl;
79  return EXIT_FAILURE;
80 #endif
81 
82  typedef ScalarTraits<ST> SCT;
83  typedef SCT::magnitudeType MT;
84  typedef Belos::MultiVec<ST> MV;
85  typedef Belos::Operator<ST> OP;
86  typedef Belos::MultiVecTraits<ST,MV> MVT;
88  ST one = SCT::one();
89  ST zero = SCT::zero();
90 
91  int info = 0;
92  bool norm_failure = false;
93 
94  Teuchos::GlobalMPISession session(&argc, &argv, NULL);
95  //
96  int MyPID = session.getRank();
97 
98  using Teuchos::RCP;
99  using Teuchos::rcp;
100 
101  bool success = false;
102  bool verbose = false;
103  try {
104  bool proc_verbose = false;
105  int frequency = -1; // how often residuals are printed by solver
106  int numrhs = 1;
107  std::string filename("mhd1280b.cua");
108  MT tol = 1.0e-5; // relative residual tolerance
109 
110  CommandLineProcessor cmdp(false, true);
111  cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
112  cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
113  cmdp.setOption("filename",&filename,"Filename for Harwell-Boeing test matrix.");
114  cmdp.setOption("tol",&tol,"Relative residual tolerance used by TFQMR solver.");
115  cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for.");
116  if (cmdp.parse(argc,argv) != CommandLineProcessor::PARSE_SUCCESSFUL) {
117  return EXIT_FAILURE;
118  }
119 
120  proc_verbose = verbose && (MyPID==0); /* Only print on the zero processor */
121  if (proc_verbose) {
122  std::cout << Belos::Belos_Version() << std::endl << std::endl;
123  }
124  if (!verbose)
125  frequency = -1; // reset frequency if test is not verbose
126 
127 
128 #ifndef HAVE_BELOS_TRIUTILS
129  std::cout << "This test requires Triutils. Please configure with --enable-triutils." << std::endl;
130  if (MyPID==0) {
131  std::cout << "End Result: TEST FAILED" << std::endl;
132  }
133  return EXIT_FAILURE;
134 #endif
135 
136  // Get the data from the HB file
137  int dim,dim2,nnz;
138  MT *dvals;
139  int *colptr,*rowind;
140  ST *cvals;
141  nnz = -1;
142  info = readHB_newmat_double(filename.c_str(),&dim,&dim2,&nnz,
143  &colptr,&rowind,&dvals);
144  if (info == 0 || nnz < 0) {
145  if (MyPID==0) {
146  std::cout << "Error reading '" << filename << "'" << std::endl;
147  std::cout << "End Result: TEST FAILED" << std::endl;
148  }
149  return EXIT_FAILURE;
150  }
151  // Convert interleaved doubles to std::complex values
152  cvals = new ST[nnz];
153  for (int ii=0; ii<nnz; ii++) {
154  cvals[ii] = ST(dvals[ii*2],dvals[ii*2+1]);
155  }
156  // Build the problem matrix
158  = rcp( new MyBetterOperator<ST>(dim,colptr,nnz,rowind,cvals) );
159  //
160  // ********Other information used by block solver***********
161  // *****************(can be user specified)******************
162  //
163  int maxits = dim; // maximum number of iterations to run
164  //
165  ParameterList belosList;
166  belosList.set( "Maximum Iterations", maxits ); // Maximum number of iterations allowed
167  belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
168  if (verbose) {
169  belosList.set( "Verbosity", Belos::Errors + Belos::Warnings +
171  if (frequency > 0)
172  belosList.set( "Output Frequency", frequency );
173  }
174  else
175  belosList.set( "Verbosity", Belos::Errors + Belos::Warnings );
176  //
177  // Construct the right-hand side and solution multivectors.
178  // NOTE: The right-hand side will be constructed such that the solution is
179  // a vectors of one.
180  //
181  RCP<MyMultiVec<ST> > soln = rcp( new MyMultiVec<ST>(dim,numrhs) );
182  RCP<MyMultiVec<ST> > rhs = rcp( new MyMultiVec<ST>(dim,numrhs) );
183  MVT::MvRandom( *soln );
184  OPT::Apply( *A, *soln, *rhs );
185  MVT::MvInit( *soln, zero );
186  //
187  // Construct an unpreconditioned linear problem instance.
188  //
190  rcp( new Belos::LinearProblem<ST,MV,OP>( A, soln, rhs ) );
191  bool set = problem->setProblem();
192  if (set == false) {
193  if (proc_verbose)
194  std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
195  return EXIT_FAILURE;
196  }
197  //
198  // *******************************************************************
199  // *************Start the TFQMR iteration***********************
200  // *******************************************************************
201  //
202  Belos::TFQMRSolMgr<ST,MV,OP> solver( problem, rcp(&belosList,false) );
203 
204  //
205  // **********Print out information about problem*******************
206  //
207  if (proc_verbose) {
208  std::cout << std::endl << std::endl;
209  std::cout << "Dimension of matrix: " << dim << std::endl;
210  std::cout << "Number of right-hand sides: " << numrhs << std::endl;
211  std::cout << "Max number of TFQMR iterations: " << maxits << std::endl;
212  std::cout << "Relative residual tolerance: " << tol << std::endl;
213  std::cout << std::endl;
214  }
215  //
216  // Perform solve
217  //
218  Belos::ReturnType ret = solver.solve();
219  //
220  // Compute actual residuals.
221  //
222  RCP<MyMultiVec<ST> > temp = rcp( new MyMultiVec<ST>(dim,numrhs) );
223  OPT::Apply( *A, *soln, *temp );
224  MVT::MvAddMv( one, *rhs, -one, *temp, *temp );
225  std::vector<MT> norm_num(numrhs), norm_denom(numrhs);
226  MVT::MvNorm( *temp, norm_num );
227  MVT::MvNorm( *rhs, norm_denom );
228  for (int i=0; i<numrhs; ++i) {
229  if (proc_verbose)
230  std::cout << "Relative residual "<<i<<" : " << norm_num[i] / norm_denom[i] << std::endl;
231  if ( norm_num[i] / norm_denom[i] > tol ) {
232  norm_failure = true;
233  }
234  }
235 
236  // Clean up.
237  delete [] dvals;
238  delete [] colptr;
239  delete [] rowind;
240  delete [] cvals;
241 
242  success = ret==Belos::Converged && !norm_failure;
243  if (success) {
244  if (proc_verbose)
245  std::cout << "End Result: TEST PASSED" << std::endl;
246  } else {
247  if (proc_verbose)
248  std::cout << "End Result: TEST FAILED" << std::endl;
249  }
250  }
251  TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
252 
253  return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
254 } // end test_tfqmr_complex_hb.cpp
std::string Belos_Version()
int main(int argc, char *argv[])
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
ReturnType solve() override
This method performs possibly repeated calls to the underlying linear solver&#39;s iterate() routine unti...
Traits class which defines basic operations on multivectors.
Simple example of a user&#39;s defined Belos::MultiVec class.
Definition: MyMultiVec.hpp:65
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)
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
ReturnType
Whether the Belos solve converged for all linear systems.
Definition: BelosTypes.hpp:155
The Belos::TFQMRSolMgr provides a powerful and fully-featured solver manager over the TFQMR linear so...
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...
The Belos::TFQMRSolMgr provides a solver manager for the TFQMR linear solver.
Simple example of a user&#39;s defined Belos::Operator class.