10 #include "Stratimikos_DefaultLinearSolverBuilder.hpp"
12 #include "Teuchos_GlobalMPISession.hpp"
13 #include "Teuchos_VerboseObject.hpp"
14 #include "Teuchos_XMLParameterListHelpers.hpp"
15 #include "Teuchos_CommandLineProcessor.hpp"
16 #include "Teuchos_StandardCatchMacros.hpp"
18 # include "Epetra_MpiComm.h"
20 # include "Epetra_SerialComm.h"
30 double norm[1] = { -1.0 };
39 int main(
int argc,
char* argv[])
66 std::string matrixFile =
"";
67 std::string extraParamsFile =
"";
69 bool onlyPrintOptions =
false;
70 bool printXmlFormat =
false;
75 CommandLineProcessor clp(
false);
80 clp.setOption(
"matrix-file", &matrixFile
81 ,
"Defines the matrix and perhaps the RHS and LHS for a linear system to be solved." );
82 clp.setOption(
"tol", &tol,
"Tolerance to check against the scaled residual norm." );
83 clp.setOption(
"extra-params-file", &extraParamsFile,
"File containing extra parameters in XML format.");
84 clp.setOption(
"only-print-options",
"continue-after-printing-options", &onlyPrintOptions
85 ,
"Only print options and stop or continue on" );
86 clp.setOption(
"print-xml-format",
"print-readable-format", &printXmlFormat
87 ,
"Print the valid options in XML format or in readable format." );
88 clp.setOption(
"show-doc",
"hide-doc", &showDoc
89 ,
"Print the valid options with or without documentation." );
92 "Simple example for the use of the Stratimikos facade Stratimikos::DefaultLinearSolverBuilder.\n"
94 "To print out just the valid options use --matrix-file=\"\" --only-print-options with --print-xml-format"
95 " or --print-readable-format.\n"
97 "To solve a linear system read from a file use --matrix-file=\"SomeFile.mtx\""
98 " with options read from an XML file using --linear-solver-params-file=\"SomeFile.xml\"\n"
101 CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
102 if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL )
return parse_return;
108 if(onlyPrintOptions) {
110 Teuchos::writeParameterListToXmlOStream(
116 *out,PLPrintOptions().indent(2).showTypes(
true).showDoc(showDoc)
130 *out <<
"\nReading linear system in Epetra format from the file \'"<<matrixFile<<
"\' ...\n";
137 RCP<Epetra_CrsMatrix> epetra_A;
138 RCP<Epetra_Vector> epetra_x, epetra_b;
141 if(!epetra_b.get()) {
142 *out <<
"\nThe RHS b was not read in so generate a new random vector ...\n";
147 if(!epetra_x.get()) {
148 *out <<
"\nThe LHS x was not read in so generate a new zero vector ...\n";
150 epetra_x->PutScalar(0.0);
153 *out <<
"\nPrinting statistics of the Epetra linear system ...\n";
156 <<
"\n Epetra_CrsMatrix epetra_A of dimension "
157 << epetra_A->OperatorRangeMap().NumGlobalElements()
158 <<
" x " << epetra_A->OperatorDomainMap().NumGlobalElements()
159 <<
"\n ||epetraA||inf = " << epetra_A->NormInf()
160 <<
"\n ||epetra_b||2 = " << epetraNorm2(*epetra_b)
161 <<
"\n ||epetra_x||2 = " << epetraNorm2(*epetra_x)
174 RCP<const Thyra::LinearOpBase<double> > A = Thyra::epetraLinearOp( epetra_A );
197 if(extraParamsFile.length()) {
198 Teuchos::updateParametersFromXmlFile(
"./"+extraParamsFile,
204 RCP<Thyra::LinearOpWithSolveFactoryBase<double> > lowsFactory =
208 lowsFactory->setOStream(out);
212 RCP<Thyra::LinearOpWithSolveBase<double> > lows =
213 Thyra::linearOpWithSolve(*lowsFactory, A);
218 *out <<
"\nSolve status:\n" << status;
240 <<
"\nSolution ||epetra_x||2 = " << epetraNorm2(*epetra_x) <<
"\n";
242 *out <<
"\nTesting the solution error ||b-A*x||/||b|| computed through the Epetra objects ...\n";
248 epetra_A->Apply(*epetra_x,epetra_A_x);
249 epetra_r.Update(-1.0,epetra_A_x,1.0);
253 nrm_r = epetraNorm2(epetra_r),
254 nrm_b = epetraNorm2(*epetra_b),
255 rel_err = ( nrm_r / nrm_b );
257 passed = (rel_err <= tol);
260 <<
"||b-A*x||/||b|| = " << nrm_r <<
"/" << nrm_b <<
" = " << rel_err
261 <<
" < tol = " << tol <<
" ? " << ( passed ?
"passed" :
"failed" ) <<
"\n";
263 if(!passed) success =
false;
269 if(success) *out <<
"\nCongratulations! All of the tests checked out!\n";
270 else *out <<
"\nOh no! At least one of the tests failed!\n";
273 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
RCP< ParameterList > getNonconstParameterList()
void readParameters(std::ostream *out)
Force the parameters to be read from a file and/or an extra XML string.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
RCP< VectorBase< double > > create_Vector(const RCP< Epetra_Vector > &epetra_v, const RCP< const VectorSpaceBase< double > > &space=Teuchos::null)
static RCP< FancyOStream > getDefaultOStream()
void readEpetraLinearSystem(const std::string &fileName, const Epetra_Comm &comm, Teuchos::RCP< Epetra_CrsMatrix > *A=NULL, Teuchos::RCP< Epetra_Map > *map=NULL, Teuchos::RCP< Epetra_Vector > *x=NULL, Teuchos::RCP< Epetra_Vector > *b=NULL, Teuchos::RCP< Epetra_Vector > *xExact=NULL)
RCP< Thyra::LinearOpWithSolveFactoryBase< Scalar > > createLinearSolveStrategy(const std::string &linearSolveStrategyName) const
Concrete subclass of Thyra::LinearSolverBuilderBase for creating Thyra::LinearOpWithSolveFactoryBase ...
#define TEUCHOS_STANDARD_CATCH_STATEMENTS(VERBOSE, ERR_STREAM, SUCCESS_FLAG)
void writeParamsFile(const Thyra::LinearOpWithSolveFactoryBase< Scalar > &lowsFactory, const std::string &outputXmlFileName="") const
Write the parameters list for a LinearOpWithSolveFactoryBase object to a file after the parameters ar...
void setupCLP(Teuchos::CommandLineProcessor *clp)
Setup the command-line processor to read in the needed data to extra the parameters from...
RCP< const ParameterList > getValidParameters() const
#include "Stratimikos_DefaultLinearSolverBuilder.hpp"
#include "Teuchos_GlobalMPISession.hpp"
#include "Teuchos_VerboseObject.hpp"
#include "Teuchos_XMLParameterListHelpers.hpp"
#include "Teuchos_CommandLineProcessor.hpp"
#include "Teuchos_StandardCatchMacros.hpp"
#ifdef HAVE_MPI
# include "Epetra_MpiComm.h"
#else
# include "Epetra_SerialComm.h"
#endif
namespace {
{
double norm[1] = { -1.0 };
v.Norm2(&norm[0]);
return norm[0];
}
}
int main(int argc, char* argv[])
{
bool success = true;
bool verbose = true;
try {
std::string matrixFile = "";
std::string extraParamsFile = "";
double tol = 1e-5;
bool onlyPrintOptions = false;
bool printXmlFormat = false;
bool showDoc = true;
CommandLineProcessor clp(false);
clp.setOption( "matrix-file", &matrixFile
,"Defines the matrix and perhaps the RHS and LHS for a linear system to be solved." );
clp.setOption( "tol", &tol, "Tolerance to check against the scaled residual norm." );
clp.setOption( "extra-params-file", &extraParamsFile, "File containing extra parameters in XML format.");
clp.setOption( "only-print-options", "continue-after-printing-options", &onlyPrintOptions
,"Only print options and stop or continue on" );
clp.setOption( "print-xml-format", "print-readable-format", &printXmlFormat
,"Print the valid options in XML format or in readable format." );
clp.setOption( "show-doc", "hide-doc", &showDoc
,"Print the valid options with or without documentation." );
clp.setDocString(
"Simple example for the use of the Stratimikos facade Stratimikos::DefaultLinearSolverBuilder.\n"
"\n"
"To print out just the valid options use --matrix-file=\"\" --only-print-options with --print-xml-format"
" or --print-readable-format.\n"
"\n"
"To solve a linear system read from a file use --matrix-file=\"SomeFile.mtx\""
" with options read from an XML file using --linear-solver-params-file=\"SomeFile.xml\"\n"
);
CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) return parse_return;
if(onlyPrintOptions) {
if(printXmlFormat)
Teuchos::writeParameterListToXmlOStream(
,*out
);
else
*out,PLPrintOptions().indent(2).showTypes(true).showDoc(showDoc)
);
return 0;
}
*out << "\nReading linear system in Epetra format from the file \'"<<matrixFile<<"\' ...\n";
#ifdef HAVE_MPI
#else
#endif
RCP<Epetra_CrsMatrix> epetra_A;
RCP<Epetra_Vector> epetra_x, epetra_b;
if(!epetra_b.get()) {
*out << "\nThe RHS b was not read in so generate a new random vector ...\n";
epetra_b->Random();
}
if(!epetra_x.get()) {
*out << "\nThe LHS x was not read in so generate a new zero vector ...\n";
epetra_x->PutScalar(0.0);
}
*out << "\nPrinting statistics of the Epetra linear system ...\n";
*out
<< "\n Epetra_CrsMatrix epetra_A of dimension "
<< epetra_A->OperatorRangeMap().NumGlobalElements()
<< " x " << epetra_A->OperatorDomainMap().NumGlobalElements()
<< "\n ||epetraA||inf = " << epetra_A->NormInf()
<< "\n ||epetra_b||2 = " << epetraNorm2(*epetra_b)
<< "\n ||epetra_x||2 = " << epetraNorm2(*epetra_x)
<< "\n";
RCP<const Thyra::LinearOpBase<double> > A = Thyra::epetraLinearOp( epetra_A );
if(extraParamsFile.length()) {
Teuchos::updateParametersFromXmlFile( "./"+extraParamsFile,
}
RCP<Thyra::LinearOpWithSolveFactoryBase<double> > lowsFactory =
lowsFactory->setOStream(out);
RCP<Thyra::LinearOpWithSolveBase<double> > lows =
Thyra::linearOpWithSolve(*lowsFactory, A);
*out << "\nSolve status:\n" << status;
x = Teuchos::null;
*out
<< "\nSolution ||epetra_x||2 = " << epetraNorm2(*epetra_x) << "\n";
*out << "\nTesting the solution error ||b-A*x||/||b|| computed through the Epetra objects ...\n";
{
epetra_A->Apply(*epetra_x,epetra_A_x);
epetra_r.Update(-1.0,epetra_A_x,1.0);
}
const double
nrm_r = epetraNorm2(epetra_r),
nrm_b = epetraNorm2(*epetra_b),
rel_err = ( nrm_r / nrm_b );
const bool
passed = (rel_err <= tol);
*out
<< "||b-A*x||/||b|| = " << nrm_r << "/" << nrm_b << " = " << rel_err
<< " < tol = " << tol << " ? " << ( passed ? "passed" : "failed" ) << "\n";
if(!passed) success = false;
}
if (verbose) {
if(success) *out << "\nCongratulations! All of the tests checked out!\n";
else *out << "\nOh no! At least one of the tests failed!\n";
}
return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
}