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 #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   int info = 0;
00092   int MyPID = 0;
00093   bool norm_failure = false;
00094 
00095   Teuchos::GlobalMPISession session(&argc, &argv, NULL);
00096   //
00097   using Teuchos::RCP;
00098   using Teuchos::rcp;
00099 
00100   bool success = false;
00101   bool verbose = false;
00102   try {
00103     bool proc_verbose = false;
00104     int frequency = -1;  // how often residuals are printed by solver
00105     int blocksize = 1;
00106     int numrhs = 1;
00107     std::string filename("mhd1280b.cua");
00108     MT tol = 1.0e-5;  // relative residual tolerance
00109 
00110     CommandLineProcessor cmdp(false, true);
00111     cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
00112     cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
00113     cmdp.setOption("filename",&filename,"Filename for Harwell-Boeing test matrix.");
00114     cmdp.setOption("tol",&tol,"Relative residual tolerance used by TFQMR solver.");
00115     cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for.");
00116     if (cmdp.parse(argc,argv) != CommandLineProcessor::PARSE_SUCCESSFUL) {
00117       return EXIT_FAILURE;
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 EXIT_FAILURE;
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 EXIT_FAILURE;
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( "Maximum Iterations", maxits );         // Maximum number of iterations allowed
00167     belosList.set( "Convergence Tolerance", tol );         // Relative convergence tolerance requested
00168     if (verbose) {
00169       belosList.set( "Verbosity", Belos::Errors + Belos::Warnings +
00170           Belos::TimingDetails + Belos::FinalSummary + Belos::StatusTestDetails );
00171       if (frequency > 0)
00172         belosList.set( "Output Frequency", frequency );
00173     }
00174     else
00175       belosList.set( "Verbosity", Belos::Errors + Belos::Warnings );
00176     //
00177     // Construct the right-hand side and solution multivectors.
00178     // NOTE:  The right-hand side will be constructed such that the solution is
00179     // a vectors of one.
00180     //
00181     RCP<MyMultiVec<ST> > soln = rcp( new MyMultiVec<ST>(dim,numrhs) );
00182     RCP<MyMultiVec<ST> > rhs = rcp( new MyMultiVec<ST>(dim,numrhs) );
00183     MVT::MvRandom( *soln );
00184     OPT::Apply( *A, *soln, *rhs );
00185     MVT::MvInit( *soln, zero );
00186     //
00187     //  Construct an unpreconditioned linear problem instance.
00188     //
00189     RCP<Belos::LinearProblem<ST,MV,OP> > problem =
00190       rcp( new Belos::LinearProblem<ST,MV,OP>( A, soln, rhs ) );
00191     bool set = problem->setProblem();
00192     if (set == false) {
00193       if (proc_verbose)
00194         std::cout << std::endl << "ERROR:  Belos::LinearProblem failed to set up correctly!" << std::endl;
00195       return EXIT_FAILURE;
00196     }
00197     //
00198     // *******************************************************************
00199     // *************Start the TFQMR iteration***********************
00200     // *******************************************************************
00201     //
00202     Belos::TFQMRSolMgr<ST,MV,OP> solver( problem, rcp(&belosList,false) );
00203 
00204     //
00205     // **********Print out information about problem*******************
00206     //
00207     if (proc_verbose) {
00208       std::cout << std::endl << std::endl;
00209       std::cout << "Dimension of matrix: " << dim << std::endl;
00210       std::cout << "Number of right-hand sides: " << numrhs << std::endl;
00211       std::cout << "Block size used by solver: " << blocksize << std::endl;
00212       std::cout << "Max number of TFQMR iterations: " << maxits << std::endl;
00213       std::cout << "Relative residual tolerance: " << tol << std::endl;
00214       std::cout << std::endl;
00215     }
00216     //
00217     // Perform solve
00218     //
00219     Belos::ReturnType ret = solver.solve();
00220     //
00221     // Compute actual residuals.
00222     //
00223     RCP<MyMultiVec<ST> > temp = rcp( new MyMultiVec<ST>(dim,numrhs) );
00224     OPT::Apply( *A, *soln, *temp );
00225     MVT::MvAddMv( one, *rhs, -one, *temp, *temp );
00226     std::vector<MT> norm_num(numrhs), norm_denom(numrhs);
00227     MVT::MvNorm( *temp, norm_num );
00228     MVT::MvNorm( *rhs, norm_denom );
00229     for (int i=0; i<numrhs; ++i) {
00230       if (proc_verbose)
00231         std::cout << "Relative residual "<<i<<" : " << norm_num[i] / norm_denom[i] << std::endl;
00232       if ( norm_num[i] / norm_denom[i] > tol ) {
00233         norm_failure = true;
00234       }
00235     }
00236 
00237     // Clean up.
00238     delete [] dvals;
00239     delete [] colptr;
00240     delete [] rowind;
00241     delete [] cvals;
00242 
00243     success = ret==Belos::Converged && !norm_failure;
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_tfqmr_complex_hb.cpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines