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