Stratimikos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
test_single_belos_thyra_solver.cpp
Go to the documentation of this file.
00001 /*
00002 // @HEADER
00003 // ***********************************************************************
00004 // 
00005 //         Stratimikos: Thyra-based strategies for linear solvers
00006 //                Copyright (2006) Sandia Corporation
00007 // 
00008 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00009 // license for use of this work by or on behalf of the U.S. Government.
00010 // 
00011 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 
00039 // 
00040 // ***********************************************************************
00041 // @HEADER
00042 */
00043 
00044 #include "test_single_belos_thyra_solver.hpp"
00045 
00046 #ifndef SUN_CXX
00047 
00048 #include "Thyra_BelosLinearOpWithSolveFactory.hpp"
00049 #include "Thyra_LinearOpWithSolveFactoryHelpers.hpp"
00050 #include "Thyra_EpetraLinearOp.hpp"
00051 #include "Thyra_LinearOpTester.hpp"
00052 #include "Thyra_LinearOpWithSolveBase.hpp"
00053 #include "Thyra_LinearOpWithSolveTester.hpp"
00054 #include "Thyra_MultiVectorStdOps.hpp"
00055 #include "Thyra_VectorStdOps.hpp"
00056 #include "EpetraExt_readEpetraLinearSystem.h"
00057 #include "Epetra_SerialComm.h"
00058 #include "Teuchos_ParameterList.hpp"
00059 #ifdef HAVE_BELOS_IFPACK
00060 #  include "Thyra_IfpackPreconditionerFactory.hpp"
00061 #endif
00062 
00063 #endif // SUN_CXX
00064 
00065 bool Thyra::test_single_belos_thyra_solver(
00066   const std::string                       matrixFile
00067   ,const bool                             testTranspose
00068   ,const bool                             usePreconditioner
00069   ,const int                              numRhs
00070   ,const int                              numRandomVectors
00071   ,const double                           maxFwdError
00072   ,const double                           maxResid
00073   ,const double                           maxSolutionError
00074   ,const bool                             showAllTests
00075   ,const bool                             dumpAll
00076   ,Teuchos::ParameterList                 *belosLOWSFPL
00077   ,Teuchos::ParameterList                 *precPL
00078   ,Teuchos::FancyOStream                  *out_arg
00079   )
00080 {
00081   using Teuchos::rcp;
00082   using Teuchos::OSTab;
00083   bool result, success = true;
00084 
00085   Teuchos::RCP<Teuchos::FancyOStream> out = Teuchos::rcp(out_arg,false);
00086 
00087   try {
00088 
00089 #ifndef SUN_CXX
00090 
00091     if(out.get()) {
00092       *out << "\n***"
00093            << "\n*** Testing Thyra::BelosLinearOpWithSolveFactory (and Thyra::BelosLinearOpWithSolve)"
00094            << "\n***\n"
00095            << "\nEchoing input options:"
00096            << "\n  matrixFile             = " << matrixFile
00097            << "\n  testTranspose          = " << testTranspose
00098            << "\n  usePreconditioner      = " << usePreconditioner
00099            << "\n  numRhs                 = " << numRhs
00100            << "\n  numRandomVectors       = " << numRandomVectors
00101            << "\n  maxFwdError            = " << maxFwdError
00102            << "\n  maxResid               = " << maxResid
00103            << "\n  showAllTests           = " << showAllTests
00104            << "\n  dumpAll                = " << dumpAll
00105            << std::endl;
00106     }
00107 
00108     if(out.get()) *out << "\nA) Reading in an epetra matrix A from the file \'"<<matrixFile<<"\' ...\n";
00109   
00110     Epetra_SerialComm comm;
00111     Teuchos::RCP<Epetra_CrsMatrix> epetra_A;
00112     EpetraExt::readEpetraLinearSystem( matrixFile, comm, &epetra_A );
00113 
00114     Teuchos::RCP<const LinearOpBase<double> > A = epetraLinearOp(epetra_A);
00115 
00116     if(out.get() && dumpAll) *out << "\ndescribe(A) =\n" << describe(*A,Teuchos::VERB_EXTREME);
00117 
00118     if(out.get()) *out << "\nB) Creating a BelosLinearOpWithSolveFactory object opFactory ...\n";
00119 
00120     Teuchos::RCP<LinearOpWithSolveFactoryBase<double> >
00121       lowsFactory;
00122     {
00123       Teuchos::RCP<BelosLinearOpWithSolveFactory<double> >
00124         belosLowsFactory = Teuchos::rcp(new BelosLinearOpWithSolveFactory<double>());
00125       lowsFactory = belosLowsFactory;
00126     }
00127 
00128     if(out.get()) {
00129       *out << "\nlowsFactory.getValidParameters() before setting preconditioner factory:\n";
00130       lowsFactory->getValidParameters()->print(OSTab(out).o(),0,true,false);
00131     }
00132 
00133     if(usePreconditioner) {
00134 #ifdef HAVE_BELOS_IFPACK
00135       if(out.get()) {
00136         *out << "\nSetting an Ifpack preconditioner factory ...\n";
00137       }
00138       RCP<PreconditionerFactoryBase<double> >
00139         precFactory = Teuchos::rcp(new IfpackPreconditionerFactory());
00140       if (precPL)
00141         precFactory->setParameterList(rcp(precPL,false));
00142       lowsFactory->setPreconditionerFactory(precFactory,"Ifpack");
00143 #else
00144       TEUCHOS_TEST_FOR_EXCEPT(usePreconditioner);
00145 #endif
00146     }
00147     
00148     if(out.get()) {
00149       *out << "\nlowsFactory.getValidParameters() after setting preconditioner factory:\n";
00150       lowsFactory->getValidParameters()->print(OSTab(out).o(),0,true,false);
00151       *out << "\nbelosLOWSFPL before setting parameters:\n";
00152       belosLOWSFPL->print(OSTab(out).o(),0,true);
00153     }
00154 
00155     lowsFactory->setParameterList(Teuchos::rcp(belosLOWSFPL,false));
00156 
00157     if(out.get()) {
00158       *out << "\nbelosLOWSFPL after setting parameters:\n";
00159       belosLOWSFPL->print(OSTab(out).o(),0,true);
00160     }
00161 
00162     if(out.get()) *out << "\nC) Creating a BelosLinearOpWithSolve object nsA from A ...\n";
00163 
00164     Teuchos::RCP<LinearOpWithSolveBase<double> > nsA = lowsFactory->createOp();
00165     Thyra::initializeOp<double>(*lowsFactory,  A, nsA.ptr());
00166 
00167     if(out.get()) *out << "\nD) Testing the LinearOpBase interface of nsA ...\n";
00168 
00169     LinearOpTester<double> linearOpTester;
00170     linearOpTester.check_adjoint(testTranspose);
00171     linearOpTester.num_rhs(numRhs);
00172     linearOpTester.num_random_vectors(numRandomVectors);
00173     linearOpTester.set_all_error_tol(maxFwdError);
00174     linearOpTester.set_all_warning_tol(1e-2*maxFwdError);
00175     linearOpTester.show_all_tests(showAllTests);
00176     linearOpTester.dump_all(dumpAll);
00177     Thyra::seed_randomize<double>(0);
00178     result = linearOpTester.check(*nsA,Teuchos::Ptr<Teuchos::FancyOStream>(out.get()));
00179     if(!result) success = false;
00180 
00181     if(out.get()) *out << "\nE) Testing the LinearOpWithSolveBase interface of nsA ...\n";
00182     
00183     LinearOpWithSolveTester<double> linearOpWithSolveTester;
00184     linearOpWithSolveTester.num_rhs(numRhs);
00185     linearOpWithSolveTester.turn_off_all_tests();
00186     linearOpWithSolveTester.check_forward_default(true);
00187     linearOpWithSolveTester.check_forward_residual(true);
00188     if(testTranspose) {
00189       linearOpWithSolveTester.check_adjoint_default(true);
00190       linearOpWithSolveTester.check_adjoint_residual(true);
00191     }
00192     else {
00193       linearOpWithSolveTester.check_adjoint_default(false);
00194       linearOpWithSolveTester.check_adjoint_residual(false);
00195     }
00196     linearOpWithSolveTester.set_all_solve_tol(maxResid);
00197     linearOpWithSolveTester.set_all_slack_error_tol(maxResid);
00198     linearOpWithSolveTester.set_all_slack_warning_tol(1e+1*maxResid);
00199     linearOpWithSolveTester.forward_default_residual_error_tol(2*maxResid);
00200     linearOpWithSolveTester.forward_default_solution_error_error_tol(maxSolutionError);
00201     linearOpWithSolveTester.adjoint_default_residual_error_tol(2*maxResid);
00202     linearOpWithSolveTester.adjoint_default_solution_error_error_tol(maxSolutionError);
00203     linearOpWithSolveTester.show_all_tests(showAllTests);
00204     linearOpWithSolveTester.dump_all(dumpAll);
00205     Thyra::seed_randomize<double>(0);
00206     result = linearOpWithSolveTester.check(*nsA,out.get());
00207     if(!result) success = false;
00208 
00209     if(out.get()) {
00210       *out << "\nbelosLOWSFPL after solving:\n";
00211       belosLOWSFPL->print(OSTab(out).o(),0,true);
00212     }
00213     
00214 #else // SUN_CXX
00215     
00216     if(out.get()) *out << "\nTest failed since is was not even compiled since SUN_CXX was defined!\n";
00217     success = false;
00218 
00219 #endif // SUN_CXX
00220 
00221   }
00222   catch( const std::exception &excpt ) {
00223     if(out.get()) *out << std::flush;
00224     std::cerr << "*** Caught standard exception : " << excpt.what() << std::endl;
00225     success = false;
00226   }
00227    
00228   return success;
00229     
00230 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines