Belos Package Browser (Single Doxygen Collection)  Development
test_pseudo_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"
51 #include "Teuchos_CommandLineProcessor.hpp"
52 #include "Teuchos_ParameterList.hpp"
53 #include "Teuchos_StandardCatchMacros.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 blocksize = 1;
107  int numrhs = 1;
108  std::string filename("mhd1280b.cua");
109  MT tol = 1.0e-5; // relative residual tolerance
110 
111  CommandLineProcessor cmdp(false, true);
112  cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
113  cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
114  cmdp.setOption("filename",&filename,"Filename for Harwell-Boeing test matrix.");
115  cmdp.setOption("tol",&tol,"Relative residual tolerance used by pseudo-block TFQMR solver.");
116  cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for.");
117  if (cmdp.parse(argc,argv) != CommandLineProcessor::PARSE_SUCCESSFUL) {
118  return EXIT_FAILURE;
119  }
120 
121  proc_verbose = verbose && (MyPID==0); /* Only print on the zero processor */
122  if (proc_verbose) {
123  std::cout << Belos::Belos_Version() << std::endl << std::endl;
124  }
125  if (!verbose)
126  frequency = -1; // reset frequency if test is not verbose
127 
128 
129 #ifndef HAVE_BELOS_TRIUTILS
130  std::cout << "This test requires Triutils. Please configure with --enable-triutils." << std::endl;
131  if (MyPID==0) {
132  std::cout << "End Result: TEST FAILED" << std::endl;
133  }
134  return EXIT_FAILURE;
135 #endif
136 
137  // Get the data from the HB file
138  int dim,dim2,nnz;
139  MT *dvals;
140  int *colptr,*rowind;
141  ST *cvals;
142  nnz = -1;
143  info = readHB_newmat_double(filename.c_str(),&dim,&dim2,&nnz,
144  &colptr,&rowind,&dvals);
145  if (info == 0 || nnz < 0) {
146  if (MyPID==0) {
147  std::cout << "Error reading '" << filename << "'" << std::endl;
148  std::cout << "End Result: TEST FAILED" << std::endl;
149  }
150  return EXIT_FAILURE;
151  }
152  // Convert interleaved doubles to std::complex values
153  cvals = new ST[nnz];
154  for (int ii=0; ii<nnz; ii++) {
155  cvals[ii] = ST(dvals[ii*2],dvals[ii*2+1]);
156  }
157  // Build the problem matrix
158  RCP< MyBetterOperator<ST> > A
159  = rcp( new MyBetterOperator<ST>(dim,colptr,nnz,rowind,cvals) );
160  //
161  // ********Other information used by block solver***********
162  // *****************(can be user specified)******************
163  //
164  int maxits = dim/blocksize; // maximum number of iterations to run
165  //
166  ParameterList belosList;
167  belosList.set( "Maximum Iterations", maxits ); // Maximum number of iterations allowed
168  belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
169  if (verbose) {
170  belosList.set( "Verbosity", Belos::Errors + Belos::Warnings +
172  if (frequency > 0)
173  belosList.set( "Output Frequency", frequency );
174  }
175  else
176  belosList.set( "Verbosity", Belos::Errors + Belos::Warnings );
177  //
178  // Construct the right-hand side and solution multivectors.
179  // NOTE: The right-hand side will be constructed such that the solution is
180  // a vectors of one.
181  //
182  RCP<MyMultiVec<ST> > soln = rcp( new MyMultiVec<ST>(dim,numrhs) );
183  RCP<MyMultiVec<ST> > rhs = rcp( new MyMultiVec<ST>(dim,numrhs) );
184  MVT::MvRandom( *soln );
185  OPT::Apply( *A, *soln, *rhs );
186  MVT::MvInit( *soln, zero );
187  //
188  // Construct an unpreconditioned linear problem instance.
189  //
190  RCP<Belos::LinearProblem<ST,MV,OP> > problem =
191  rcp( new Belos::LinearProblem<ST,MV,OP>( A, soln, rhs ) );
192  bool set = problem->setProblem();
193  if (set == false) {
194  if (proc_verbose)
195  std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
196  return EXIT_FAILURE;
197  }
198  //
199  // *******************************************************************
200  // *************Start the TFQMR iteration***********************
201  // *******************************************************************
202  //
203  Belos::PseudoBlockTFQMRSolMgr<ST,MV,OP> solver( problem, rcp(&belosList,false) );
204 
205  //
206  // **********Print out information about problem*******************
207  //
208  if (proc_verbose) {
209  std::cout << std::endl << std::endl;
210  std::cout << "Dimension of matrix: " << dim << std::endl;
211  std::cout << "Number of right-hand sides: " << numrhs << std::endl;
212  std::cout << "Block size used by solver: " << blocksize << std::endl;
213  std::cout << "Max number of pseudo-block TFQMR iterations: " << maxits << std::endl;
214  std::cout << "Relative residual tolerance: " << tol << std::endl;
215  std::cout << std::endl;
216  }
217  //
218  // Perform solve
219  //
220  Belos::ReturnType ret = solver.solve();
221  //
222  // Compute actual residuals.
223  //
224  RCP<MyMultiVec<ST> > temp = rcp( new MyMultiVec<ST>(dim,numrhs) );
225  OPT::Apply( *A, *soln, *temp );
226  MVT::MvAddMv( one, *rhs, -one, *temp, *temp );
227  std::vector<MT> norm_num(numrhs), norm_denom(numrhs);
228  MVT::MvNorm( *temp, norm_num );
229  MVT::MvNorm( *rhs, norm_denom );
230  for (int i=0; i<numrhs; ++i) {
231  if (proc_verbose)
232  std::cout << "Relative residual "<<i<<" : " << norm_num[i] / norm_denom[i] << std::endl;
233  if ( norm_num[i] / norm_denom[i] > tol ) {
234  norm_failure = true;
235  }
236  }
237 
238  // Clean up.
239  delete [] dvals;
240  delete [] colptr;
241  delete [] rowind;
242  delete [] cvals;
243 
244  success = ret==Belos::Converged && !norm_failure;
245  if (success) {
246  if (proc_verbose)
247  std::cout << "End Result: TEST PASSED" << std::endl;
248  } else {
249  if (proc_verbose)
250  std::cout << "End Result: TEST FAILED" << std::endl;
251  }
252  }
253  TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
254 
255  return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
256 } // end test_tfqmr_complex_hb.cpp
Belos header file which uses auto-configuration information to include necessary C++ headers.
Class which describes the linear problem to be solved by the iterative solver.
The Belos::PseudoBlockTFQMRSolMgr provides a solver manager for the pseudo-block TFQMR linear solver.
A linear system to solve, and its associated information.
Traits class which defines basic operations on multivectors.
Interface for multivectors used by Belos' linear solvers.
Class which defines basic traits for the operator type.
Alternative run-time polymorphic interface for operators.
The Belos::PseudoBlockTFQMRSolMgr provides a powerful and fully-featured solver manager over the pseu...
ReturnType solve() override
This method performs possibly repeated calls to the underlying linear solver's iterate() routine unti...
Simple example of a user's defined Belos::Operator class.
Simple example of a user's defined Belos::MultiVec class.
Definition: MyMultiVec.hpp:66
@ StatusTestDetails
Definition: BelosTypes.hpp:261
@ FinalSummary
Definition: BelosTypes.hpp:259
@ TimingDetails
Definition: BelosTypes.hpp:260
@ Warnings
Definition: BelosTypes.hpp:256
ReturnType
Whether the Belos solve converged for all linear systems.
Definition: BelosTypes.hpp:155
@ Converged
Definition: BelosTypes.hpp:156
std::string Belos_Version()
int main(int argc, char *argv[])