10 #include "Tpetra_Core.hpp"
11 #include "Tpetra_CrsMatrix.hpp"
12 #include "Tpetra_Vector.hpp"
15 #include "Thyra_LinearOpTester.hpp"
16 #include "Thyra_LinearOpWithSolveTester.hpp"
17 #include "Thyra_LinearOpWithSolveFactoryHelpers.hpp"
20 #include "MatrixMarket_Tpetra.hpp"
29 template<
class Scalar,
class LO,
class GO,
class Node>
30 double tpetraNorm2(
const Tpetra::Vector<Scalar,LO,GO,Node> &v )
46 using Teuchos::rcp_dynamic_cast;
51 using Scalar = double;
52 using LocalOrdinal = Tpetra::Map<>::local_ordinal_type;
53 using GlobalOrdinal = Tpetra::Map<>::global_ordinal_type;
54 using NodeType = Tpetra::Map<>::node_type;
55 using CrsMatrix_t = Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, NodeType>;
56 using Vector_t = Tpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, NodeType>;
64 out = Teuchos::VerboseObjectBase::getDefaultOStream();
74 std::string matrixFile =
"";
75 std::string extraParamsFile =
"";
77 bool onlyPrintOptions =
false;
78 bool printXmlFormat =
false;
87 CommandLineProcessor clp(
false);
92 clp.setOption(
"matrix-file", &matrixFile,
93 "Defines the matrix and perhaps the RHS and LHS for a linear system to be solved." );
94 clp.setOption(
"tol", &tol,
95 "Tolerance to check against the scaled residual norm." );
96 clp.setOption(
"extra-params-file", &extraParamsFile,
97 "File containing extra parameters in XML format.");
98 clp.setOption(
"only-print-options",
"continue-after-printing-options",
100 "Only print options and stop or continue on" );
101 clp.setOption(
"print-xml-format",
"print-readable-format", &printXmlFormat,
102 "Print the valid options in XML format or in readable format." );
103 clp.setOption(
"show-doc",
"hide-doc", &showDoc,
104 "Print the valid options with or without documentation." );
105 clp.setOption(
"verb-level", &verbLevel, validVerbLevels.size(),
106 validVerbLevels.getRawPtr(), validVerbLevelsNamesRawStrings.getRawPtr(),
107 "The verbosity level." );
110 "Simple example for using TrilinosLinearSolver interface.\n"
112 "To print out just the valid options use --only-print-options with"
113 " --print-xml-format or --print-readable-format.\n"
115 "To solve a linear system read from a file use --matrix-file=\"<matrix>.mtx\""
116 " with options read from an XML file using"
117 " --linear-solver-params-file=\"<paramList>.xml\"\n"
120 CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
121 if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL )
return parse_return;
127 if(onlyPrintOptions) {
129 Teuchos::writeParameterListToXmlOStream(
133 *out,PLPrintOptions().indent(2).showTypes(
true).showDoc(showDoc) );
144 *out <<
"\nReading in Tpetra matrix A from the file"
145 <<
" \'"<<matrixFile<<
"\' ...\n";
147 RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
148 RCP<const CrsMatrix_t> tpetra_A =
149 Tpetra::MatrixMarket::Reader<CrsMatrix_t>::readSparseFile(matrixFile, comm);
151 *out <<
"\nGenerate a random RHS random vector ...\n";
152 auto tpetra_b =
rcp(
new Vector_t(tpetra_A->getRangeMap()));
153 tpetra_b->randomize();
155 *out <<
"\nGenerate LHS of zeros ...\n";
156 auto tpetra_x =
rcp(
new Vector_t(tpetra_A->getDomainMap()));
157 tpetra_x->putScalar(0.0);
159 *out <<
"\nPrinting statistics of the Tpetra linear system ...\n";
162 <<
"\n Tpetra::CrsMatrix tpetra_A of dimension "
163 << tpetra_A->getRangeMap()->getGlobalNumElements()
164 <<
" x " << tpetra_A->getDomainMap()->getGlobalNumElements() <<
"\n"
165 <<
" ||tpetra_A||_F = " << tpetra_A->getFrobeniusNorm() <<
"\n"
166 <<
" ||tpetra_b||2 = " << tpetraNorm2(*tpetra_b) <<
"\n"
167 <<
" ||tpetra_x||2 = " << tpetraNorm2(*tpetra_x) <<
"\n";
178 using TpetraVectorSpace_t =
181 RCP<const Thyra::LinearOpBase<double>>
A =
182 Thyra::createConstLinearOp<Scalar,LocalOrdinal,GlobalOrdinal,NodeType>(tpetra_A);
183 RCP<Thyra::VectorBase<Scalar>> x =
184 Thyra::tpetraVector(rcp_dynamic_cast<const TpetraVectorSpace_t>(A->domain()),
186 RCP<const Thyra::VectorBase<Scalar>> b =
187 Thyra::tpetraVector(rcp_dynamic_cast<const TpetraVectorSpace_t>(A->range()),
200 if(extraParamsFile.length()) {
201 Teuchos::updateParametersFromXmlFile(
"./"+extraParamsFile,
207 RCP<Thyra::LinearOpWithSolveFactoryBase<Scalar> > lowsFactory =
209 *out <<
"\nlowsFactory: " << describe(*lowsFactory, verbLevel);
212 lowsFactory->setOStream(out);
213 lowsFactory->setVerbLevel(verbLevel);
216 RCP<Thyra::LinearOpWithSolveBase<Scalar> > A_lows =
217 Thyra::linearOpWithSolve(*lowsFactory, A);
218 *out <<
"\nA: " << describe(*A, verbLevel);
219 *out <<
"\nA_lows: " << describe(*A_lows, verbLevel);
222 Thyra::SolveStatus<Scalar> status =
223 Thyra::solve<Scalar>(*A_lows, Thyra::NOTRANS, *b, x.ptr());
224 *out <<
"\nSolve status:\n" << status;
244 *out <<
"\nSolution ||tpetra_x||2 = " << tpetraNorm2(*tpetra_x) <<
"\n";
246 *out <<
"\nTesting the solution error ||b-A*x||/||b||:\n";
249 auto tpetra_r =
rcp(
new Vector_t(tpetra_b->getMap()));
250 tpetra_r->assign(*tpetra_b);
254 nrm_r = tpetraNorm2(*tpetra_r),
255 nrm_b = tpetraNorm2(*tpetra_b),
256 rel_err = ( nrm_r / nrm_b );
258 passed = (rel_err <= tol);
261 <<
"||b-A*x||/||b|| = " << nrm_r <<
"/" << nrm_b <<
" = " << rel_err
262 <<
" < tol = " << tol <<
" ? " << ( passed ?
"passed" :
"failed" ) <<
"\n";
264 if(!passed) success =
false;
271 int main(
int argc,
char* argv[])
275 out = Teuchos::VerboseObjectBase::getDefaultOStream();
277 bool success =
false;
284 if(success) *out <<
"\nCongratulations! All of the tests checked out!\n";
285 else *out <<
"\nOh no! At least one of the tests failed!\n";
287 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
RCP< ParameterList > getNonconstParameterList()
int main(int argc, char *argv[])
TEUCHOSCORE_LIB_DLL_EXPORT ArrayView< const EVerbosityLevel > getValidVerbLevels()
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)
bool readAndSolveLinearSystem(int argc, char *argv[])
#define TEUCHOS_STANDARD_CATCH_STATEMENTS(VERBOSE, ERR_STREAM, SUCCESS_FLAG)
ParameterList::PrintOptions PLPrintOptions
RCP< Thyra::LinearOpWithSolveFactoryBase< Scalar > > createLinearSolveStrategy(const std::string &linearSolveStrategyName) const
Concrete subclass of Thyra::LinearSolverBuilderBase for creating Thyra::LinearOpWithSolveFactoryBase ...
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...
TEUCHOSCORE_LIB_DLL_EXPORT ArrayView< const char *const > getValidVerbLevelsNamesRawStrings()
RCP< const ParameterList > getValidParameters() const