Belos Package Browser (Single Doxygen Collection) Development
test_minres_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 "BelosMinresSolMgr.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 using namespace Teuchos;
00068 
00069 int main(int argc, char *argv[]) {
00070   //
00071 #ifdef HAVE_COMPLEX
00072   typedef std::complex<double> ST;
00073 #elif HAVE_COMPLEX_H
00074   typedef std::complex<double> ST;
00075 #else
00076   std::cout << "Not compiled with std::complex support." << std::endl;
00077   std::cout << "End Result: TEST FAILED" << std::endl;
00078   return -1;
00079 #endif
00080 
00081   typedef ScalarTraits<ST>                 SCT;
00082   typedef SCT::magnitudeType                MT;
00083   typedef Belos::MultiVec<ST>               MV;
00084   typedef Belos::Operator<ST>               OP;
00085   typedef Belos::MultiVecTraits<ST,MV>     MVT;
00086   typedef Belos::OperatorTraits<ST,MV,OP>  OPT;
00087   ST one  = SCT::one();
00088   ST zero = SCT::zero();
00089 
00090   int info = 0;
00091   int MyPID = 0;
00092   bool norm_failure = false;
00093 
00094   Teuchos::GlobalMPISession session(&argc, &argv, NULL);
00095   //
00096   using Teuchos::RCP;
00097   using Teuchos::rcp;
00098 
00099 bool success = false;
00100 bool verbose = false;
00101 try {
00102 bool proc_verbose = false;
00103   int frequency = -1;  // how often residuals are printed by solver
00104   int blocksize = 1;
00105   int numrhs = 1;
00106   std::string filename("mhd1280b.cua");
00107   MT tol = 1.0e-5;  // relative residual tolerance
00108 
00109   CommandLineProcessor cmdp(false,true);
00110   cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
00111   cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
00112   cmdp.setOption("filename",&filename,"Filename for Harwell-Boeing test matrix.");
00113   cmdp.setOption("tol",&tol,"Relative residual tolerance used by MINRES solver.");
00114   cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for.");
00115   if (cmdp.parse(argc,argv) != CommandLineProcessor::PARSE_SUCCESSFUL) {
00116     return -1;
00117   }
00118 
00119   proc_verbose = verbose && (MyPID==0);  /* Only print on the zero processor */
00120   if (proc_verbose) {
00121     std::cout << Belos::Belos_Version() << std::endl << std::endl;
00122   }
00123   if (!verbose)
00124     frequency = -1;  // reset frequency if test is not verbose
00125 
00126 
00127 #ifndef HAVE_BELOS_TRIUTILS
00128   std::cout << "This test requires Triutils. Please configure with --enable-triutils." << std::endl;
00129   if (MyPID==0) {
00130     std::cout << "End Result: TEST FAILED" << std::endl;
00131   }
00132   return -1;
00133 #endif
00134 
00135   // Get the data from the HB file
00136   int dim,dim2,nnz;
00137   MT *dvals;
00138   int *colptr,*rowind;
00139   ST *cvals;
00140   nnz = -1;
00141   info = readHB_newmat_double(filename.c_str(),&dim,&dim2,&nnz,
00142                               &colptr,&rowind,&dvals);
00143   if (info == 0 || nnz < 0) {
00144     if (MyPID==0) {
00145       std::cout << "Error reading '" << filename << "'" << std::endl;
00146       std::cout << "End Result: TEST FAILED" << std::endl;
00147     }
00148     return -1;
00149   }
00150   // Convert interleaved doubles to std::complex values
00151   cvals = new ST[nnz];
00152   for (int ii=0; ii<nnz; ii++) {
00153     cvals[ii] = ST(dvals[ii*2],dvals[ii*2+1]);
00154   }
00155   // Build the problem matrix
00156   RCP< MyBetterOperator<ST> > A
00157     = rcp( new MyBetterOperator<ST>(dim,colptr,nnz,rowind,cvals) );
00158   //
00159   // ********Other information used by block solver***********
00160   // *****************(can be user specified)******************
00161   //
00162   int maxits = dim/blocksize; // maximum number of iterations to run
00163   //
00164   ParameterList belosList;
00165   belosList.set( "Maximum Iterations", maxits );         // Maximum number of iterations allowed
00166   belosList.set( "Convergence Tolerance", tol );         // Relative convergence tolerance requested
00167   if (verbose) {
00168     belosList.set( "Verbosity", Belos::Errors + Belos::Warnings +
00169        Belos::TimingDetails + Belos::FinalSummary + Belos::StatusTestDetails );
00170     if (frequency > 0)
00171       belosList.set( "Output Frequency", frequency );
00172   }
00173   else
00174     belosList.set( "Verbosity", Belos::Errors + Belos::Warnings );
00175   //
00176   // Construct the right-hand side and solution multivectors.
00177   // NOTE:  The right-hand side will be constructed such that the solution is
00178   // a vectors of one.
00179   //
00180   RCP<MyMultiVec<ST> > soln = rcp( new MyMultiVec<ST>(dim,numrhs) );
00181   RCP<MyMultiVec<ST> > rhs = rcp( new MyMultiVec<ST>(dim,numrhs) );
00182   MVT::MvRandom( *soln );
00183   OPT::Apply( *A, *soln, *rhs );
00184   MVT::MvInit( *soln, zero );
00185   //
00186   //  Construct an unpreconditioned linear problem instance.
00187   //
00188   RCP<Belos::LinearProblem<ST,MV,OP> > problem =
00189     rcp( new Belos::LinearProblem<ST,MV,OP>( A, soln, rhs ) );
00190   bool set = problem->setProblem();
00191   if (set == false) {
00192     if (proc_verbose)
00193       std::cout << std::endl << "ERROR:  Belos::LinearProblem failed to set up correctly!" << std::endl;
00194     return -1;
00195   }
00196   //
00197   // *******************************************************************
00198   // *************Start the MINRES iteration***********************
00199   // *******************************************************************
00200   //
00201   Belos::MinresSolMgr<ST,MV,OP> solver( problem, rcp(&belosList,false) );
00202 
00203   //
00204   // **********Print out information about problem*******************
00205   //
00206   if (proc_verbose) {
00207     std::cout << std::endl << std::endl;
00208     std::cout << "Dimension of matrix: " << dim << std::endl;
00209     std::cout << "Number of right-hand sides: " << numrhs << std::endl;
00210     std::cout << "Block size used by solver: " << blocksize << std::endl;
00211     std::cout << "Max number of MINRES iterations: " << maxits << std::endl;
00212     std::cout << "Relative residual tolerance: " << tol << std::endl;
00213     std::cout << std::endl;
00214   }
00215   //
00216   // Perform solve
00217   //
00218   Belos::ReturnType ret = solver.solve();
00219   //
00220   // Compute actual residuals.
00221   //
00222   RCP<MyMultiVec<ST> > temp = rcp( new MyMultiVec<ST>(dim,numrhs) );
00223   OPT::Apply( *A, *soln, *temp );
00224   MVT::MvAddMv( one, *rhs, -one, *temp, *temp );
00225   std::vector<MT> norm_num(numrhs), norm_denom(numrhs);
00226   MVT::MvNorm( *temp, norm_num );
00227   MVT::MvNorm( *rhs, norm_denom );
00228   for (int i=0; i<numrhs; ++i) {
00229     if (proc_verbose)
00230       std::cout << "Relative residual "<<i<<" : " << norm_num[i] / norm_denom[i] << std::endl;
00231     if ( norm_num[i] / norm_denom[i] > tol ) {
00232       norm_failure = true;
00233     }
00234   }
00235 
00236   // Clean up.
00237   delete [] dvals;
00238   delete [] colptr;
00239   delete [] rowind;
00240   delete [] cvals;
00241 
00242 success = ret==Belos::Converged && !norm_failure;
00243 
00244 if (success) {
00245   if (proc_verbose)
00246     std::cout << "End Result: TEST PASSED" << std::endl;
00247 } else {
00248 if (proc_verbose)
00249   std::cout << "End Result: TEST FAILED" << std::endl;
00250 }
00251 }
00252 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
00253 
00254 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
00255 } // end test_minres_complex_hb.cpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines