Belos Package Browser (Single Doxygen Collection) Development
test_tfqmr_complex_hb.cpp
Go to the documentation of this file.
00001 //@HEADER
00002 // ************************************************************************
00003 //
00004 //                 Belos: Block Linear Solvers Package
00005 //                  Copyright 2004 Sandia Corporation
00006 //
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00038 //
00039 // ************************************************************************
00040 //@HEADER
00041 //
00042 // This driver reads a problem from a Harwell-Boeing (HB) file.
00043 // The right-hand-side from the HB file is used instead of random vectors.
00044 // The initial guesses are all set to zero. 
00045 //
00046 // NOTE: No preconditioner is used in this case. 
00047 //
00048 #include "BelosConfigDefs.hpp"
00049 #include "BelosLinearProblem.hpp"
00050 #include "BelosTFQMRSolMgr.hpp"
00051 #include "Teuchos_CommandLineProcessor.hpp"
00052 #include "Teuchos_ParameterList.hpp"
00053 
00054 #ifdef HAVE_MPI
00055 #include <mpi.h>
00056 #endif
00057 
00058 // I/O for Harwell-Boeing files
00059 #ifdef HAVE_BELOS_TRIUTILS
00060 #include "iohb.h"
00061 #endif
00062 
00063 #include "MyMultiVec.hpp"
00064 #include "MyBetterOperator.hpp"
00065 #include "MyOperator.hpp"
00066 
00067 namespace Belos {
00068   class MPIFinalize {
00069   public:
00070     ~MPIFinalize() {
00071 #ifdef HAVE_MPI
00072       MPI_Finalize();
00073 #endif
00074     }
00075   };
00076 }
00077 
00078 using namespace Teuchos;
00079 
00080 int main(int argc, char *argv[]) {
00081   //
00082 #ifdef HAVE_COMPLEX
00083   typedef std::complex<double> ST;
00084 #elif HAVE_COMPLEX_H
00085   typedef std::complex<double> ST;
00086 #else
00087   std::cout << "Not compiled with std::complex support." << std::endl;
00088   std::cout << "End Result: TEST FAILED" << std::endl;
00089   return -1;
00090 #endif
00091 
00092   typedef ScalarTraits<ST>                 SCT;
00093   typedef SCT::magnitudeType                MT;
00094   typedef Belos::MultiVec<ST>               MV;
00095   typedef Belos::Operator<ST>               OP;
00096   typedef Belos::MultiVecTraits<ST,MV>     MVT;
00097   typedef Belos::OperatorTraits<ST,MV,OP>  OPT;
00098   ST one  = SCT::one();
00099   ST zero = SCT::zero();  
00100 
00101   int info = 0;
00102   int MyPID = 0;
00103   bool norm_failure = false;
00104 
00105 #ifdef HAVE_MPI 
00106   // Initialize MPI
00107   MPI_Init(&argc,&argv);
00108   MPI_Comm_rank(MPI_COMM_WORLD, &MyPID);
00109   Belos::MPIFinalize mpiFinalize; // Will call finalize with *any* return
00110   (void)mpiFinalize;
00111 #endif
00112   //
00113   using Teuchos::RCP;
00114   using Teuchos::rcp;
00115 
00116   bool verbose = false, proc_verbose = false;
00117   int frequency = -1;  // how often residuals are printed by solver
00118   int blocksize = 1;
00119   int numrhs = 1;
00120   std::string filename("mhd1280b.cua");
00121   MT tol = 1.0e-5;  // relative residual tolerance
00122 
00123   CommandLineProcessor cmdp(false,true);
00124   cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
00125   cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
00126   cmdp.setOption("filename",&filename,"Filename for Harwell-Boeing test matrix.");
00127   cmdp.setOption("tol",&tol,"Relative residual tolerance used by TFQMR solver.");
00128   cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for.");
00129   if (cmdp.parse(argc,argv) != CommandLineProcessor::PARSE_SUCCESSFUL) {
00130     return -1;
00131   }
00132   
00133   proc_verbose = verbose && (MyPID==0);  /* Only print on the zero processor */
00134   if (proc_verbose) {
00135     std::cout << Belos::Belos_Version() << std::endl << std::endl;
00136   }
00137   if (!verbose)
00138     frequency = -1;  // reset frequency if test is not verbose
00139   
00140   
00141 #ifndef HAVE_BELOS_TRIUTILS
00142   std::cout << "This test requires Triutils. Please configure with --enable-triutils." << std::endl;
00143   if (MyPID==0) {
00144     std::cout << "End Result: TEST FAILED" << std::endl;  
00145   }
00146   return -1;
00147 #endif
00148   
00149   // Get the data from the HB file
00150   int dim,dim2,nnz;
00151   MT *dvals;
00152   int *colptr,*rowind;
00153   ST *cvals;
00154   nnz = -1;
00155   info = readHB_newmat_double(filename.c_str(),&dim,&dim2,&nnz,
00156                               &colptr,&rowind,&dvals);
00157   if (info == 0 || nnz < 0) {
00158     if (MyPID==0) {
00159       std::cout << "Error reading '" << filename << "'" << std::endl;
00160       std::cout << "End Result: TEST FAILED" << std::endl;
00161     }
00162     return -1;
00163   }
00164   // Convert interleaved doubles to std::complex values
00165   cvals = new ST[nnz];
00166   for (int ii=0; ii<nnz; ii++) {
00167     cvals[ii] = ST(dvals[ii*2],dvals[ii*2+1]);
00168   }
00169   // Build the problem matrix
00170   RCP< MyBetterOperator<ST> > A 
00171     = rcp( new MyBetterOperator<ST>(dim,colptr,nnz,rowind,cvals) );
00172   //
00173   // ********Other information used by block solver***********
00174   // *****************(can be user specified)******************
00175   //
00176   int maxits = dim/blocksize; // maximum number of iterations to run
00177   //
00178   ParameterList belosList;
00179   belosList.set( "Maximum Iterations", maxits );         // Maximum number of iterations allowed
00180   belosList.set( "Convergence Tolerance", tol );         // Relative convergence tolerance requested
00181   if (verbose) {
00182     belosList.set( "Verbosity", Belos::Errors + Belos::Warnings + 
00183        Belos::TimingDetails + Belos::FinalSummary + Belos::StatusTestDetails );
00184     if (frequency > 0)
00185       belosList.set( "Output Frequency", frequency );
00186   }
00187   else
00188     belosList.set( "Verbosity", Belos::Errors + Belos::Warnings );
00189   //
00190   // Construct the right-hand side and solution multivectors.
00191   // NOTE:  The right-hand side will be constructed such that the solution is
00192   // a vectors of one.
00193   //
00194   RCP<MyMultiVec<ST> > soln = rcp( new MyMultiVec<ST>(dim,numrhs) );
00195   RCP<MyMultiVec<ST> > rhs = rcp( new MyMultiVec<ST>(dim,numrhs) );
00196   MVT::MvRandom( *soln );
00197   OPT::Apply( *A, *soln, *rhs );
00198   MVT::MvInit( *soln, zero );
00199   //
00200   //  Construct an unpreconditioned linear problem instance.
00201   //
00202   RCP<Belos::LinearProblem<ST,MV,OP> > problem = 
00203     rcp( new Belos::LinearProblem<ST,MV,OP>( A, soln, rhs ) );
00204   bool set = problem->setProblem();
00205   if (set == false) {
00206     if (proc_verbose)
00207       std::cout << std::endl << "ERROR:  Belos::LinearProblem failed to set up correctly!" << std::endl;
00208     return -1;
00209   }
00210   //
00211   // *******************************************************************
00212   // *************Start the TFQMR iteration***********************
00213   // *******************************************************************
00214   //
00215   Belos::TFQMRSolMgr<ST,MV,OP> solver( problem, rcp(&belosList,false) );
00216 
00217   //
00218   // **********Print out information about problem*******************
00219   //
00220   if (proc_verbose) {
00221     std::cout << std::endl << std::endl;
00222     std::cout << "Dimension of matrix: " << dim << std::endl;
00223     std::cout << "Number of right-hand sides: " << numrhs << std::endl;
00224     std::cout << "Block size used by solver: " << blocksize << std::endl;
00225     std::cout << "Max number of TFQMR iterations: " << maxits << std::endl; 
00226     std::cout << "Relative residual tolerance: " << tol << std::endl;
00227     std::cout << std::endl;
00228   }
00229   //
00230   // Perform solve
00231   //
00232   Belos::ReturnType ret = solver.solve();
00233   //
00234   // Compute actual residuals.
00235   //
00236   RCP<MyMultiVec<ST> > temp = rcp( new MyMultiVec<ST>(dim,numrhs) );
00237   OPT::Apply( *A, *soln, *temp );
00238   MVT::MvAddMv( one, *rhs, -one, *temp, *temp );
00239   std::vector<MT> norm_num(numrhs), norm_denom(numrhs);
00240   MVT::MvNorm( *temp, norm_num );
00241   MVT::MvNorm( *rhs, norm_denom );
00242   for (int i=0; i<numrhs; ++i) {
00243     if (proc_verbose) 
00244       std::cout << "Relative residual "<<i<<" : " << norm_num[i] / norm_denom[i] << std::endl;
00245     if ( norm_num[i] / norm_denom[i] > tol ) {
00246       norm_failure = true;
00247     }
00248   }
00249   
00250   // Clean up.
00251   delete [] dvals;
00252   delete [] colptr;
00253   delete [] rowind;
00254   delete [] cvals;
00255 
00256   if ( ret!=Belos::Converged || norm_failure ) {
00257     if (proc_verbose)
00258       std::cout << "End Result: TEST FAILED" << std::endl;  
00259     return -1;
00260   }
00261   //
00262   // Default return value
00263   //
00264   if (proc_verbose)
00265     std::cout << "End Result: TEST PASSED" << std::endl;
00266   return 0;
00267 
00268   //
00269 } // end test_tfqmr_complex_hb.cpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines