1 #include "Tpetra_Core.hpp"
2 #include "Tpetra_CrsMatrix.hpp"
3 #include "Tpetra_Vector.hpp"
6 #include "Thyra_LinearOpTester.hpp"
7 #include "Thyra_LinearOpWithSolveTester.hpp"
8 #include "Thyra_LinearOpWithSolveFactoryHelpers.hpp"
11 #include "MatrixMarket_Tpetra.hpp"
20 template<
class Scalar,
class LO,
class GO,
class Node>
21 double tpetraNorm2(
const Tpetra::Vector<Scalar,LO,GO,Node> &v )
37 using Teuchos::rcp_dynamic_cast;
42 using Scalar = double;
43 using LocalOrdinal = Tpetra::Map<>::local_ordinal_type;
44 using GlobalOrdinal = Tpetra::Map<>::global_ordinal_type;
45 using NodeType = Tpetra::Map<>::node_type;
46 using CrsMatrix_t = Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, NodeType>;
47 using Vector_t = Tpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, NodeType>;
55 out = Teuchos::VerboseObjectBase::getDefaultOStream();
65 std::string matrixFile =
"";
66 std::string extraParamsFile =
"";
68 bool onlyPrintOptions =
false;
69 bool printXmlFormat =
false;
78 CommandLineProcessor clp(
false);
83 clp.setOption(
"matrix-file", &matrixFile,
84 "Defines the matrix and perhaps the RHS and LHS for a linear system to be solved." );
85 clp.setOption(
"tol", &tol,
86 "Tolerance to check against the scaled residual norm." );
87 clp.setOption(
"extra-params-file", &extraParamsFile,
88 "File containing extra parameters in XML format.");
89 clp.setOption(
"only-print-options",
"continue-after-printing-options",
91 "Only print options and stop or continue on" );
92 clp.setOption(
"print-xml-format",
"print-readable-format", &printXmlFormat,
93 "Print the valid options in XML format or in readable format." );
94 clp.setOption(
"show-doc",
"hide-doc", &showDoc,
95 "Print the valid options with or without documentation." );
96 clp.setOption(
"verb-level", &verbLevel, validVerbLevels.size(),
97 validVerbLevels.getRawPtr(), validVerbLevelsNamesRawStrings.getRawPtr(),
98 "The verbosity level." );
101 "Simple example for using TrilinosLinearSolver interface.\n"
103 "To print out just the valid options use --only-print-options with"
104 " --print-xml-format or --print-readable-format.\n"
106 "To solve a linear system read from a file use --matrix-file=\"<matrix>.mtx\""
107 " with options read from an XML file using"
108 " --linear-solver-params-file=\"<paramList>.xml\"\n"
111 CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
112 if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL )
return parse_return;
118 if(onlyPrintOptions) {
120 Teuchos::writeParameterListToXmlOStream(
124 *out,PLPrintOptions().indent(2).showTypes(
true).showDoc(showDoc) );
135 *out <<
"\nReading in Tpetra matrix A from the file"
136 <<
" \'"<<matrixFile<<
"\' ...\n";
138 RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
139 RCP<const CrsMatrix_t> tpetra_A =
140 Tpetra::MatrixMarket::Reader<CrsMatrix_t>::readSparseFile(matrixFile, comm);
142 *out <<
"\nGenerate a random RHS random vector ...\n";
143 auto tpetra_b =
rcp(
new Vector_t(tpetra_A->getRangeMap()));
144 tpetra_b->randomize();
146 *out <<
"\nGenerate LHS of zeros ...\n";
147 auto tpetra_x =
rcp(
new Vector_t(tpetra_A->getDomainMap()));
148 tpetra_x->putScalar(0.0);
150 *out <<
"\nPrinting statistics of the Tpetra linear system ...\n";
153 <<
"\n Tpetra::CrsMatrix tpetra_A of dimension "
154 << tpetra_A->getRangeMap()->getGlobalNumElements()
155 <<
" x " << tpetra_A->getDomainMap()->getGlobalNumElements() <<
"\n"
156 <<
" ||tpetra_A||_F = " << tpetra_A->getFrobeniusNorm() <<
"\n"
157 <<
" ||tpetra_b||2 = " << tpetraNorm2(*tpetra_b) <<
"\n"
158 <<
" ||tpetra_x||2 = " << tpetraNorm2(*tpetra_x) <<
"\n";
169 using TpetraVectorSpace_t =
172 RCP<const Thyra::LinearOpBase<double>>
A =
173 Thyra::createConstLinearOp<Scalar,LocalOrdinal,GlobalOrdinal,NodeType>(tpetra_A);
174 RCP<Thyra::VectorBase<Scalar>> x =
175 Thyra::tpetraVector(rcp_dynamic_cast<const TpetraVectorSpace_t>(A->domain()),
177 RCP<const Thyra::VectorBase<Scalar>> b =
178 Thyra::tpetraVector(rcp_dynamic_cast<const TpetraVectorSpace_t>(A->range()),
191 if(extraParamsFile.length()) {
192 Teuchos::updateParametersFromXmlFile(
"./"+extraParamsFile,
198 RCP<Thyra::LinearOpWithSolveFactoryBase<Scalar> > lowsFactory =
200 *out <<
"\nlowsFactory: " << describe(*lowsFactory, verbLevel);
203 lowsFactory->setOStream(out);
204 lowsFactory->setVerbLevel(verbLevel);
207 RCP<Thyra::LinearOpWithSolveBase<Scalar> > A_lows =
208 Thyra::linearOpWithSolve(*lowsFactory, A);
209 *out <<
"\nA: " << describe(*A, verbLevel);
210 *out <<
"\nA_lows: " << describe(*A_lows, verbLevel);
213 Thyra::SolveStatus<Scalar> status =
214 Thyra::solve<Scalar>(*A_lows, Thyra::NOTRANS, *b, x.ptr());
215 *out <<
"\nSolve status:\n" << status;
235 *out <<
"\nSolution ||tpetra_x||2 = " << tpetraNorm2(*tpetra_x) <<
"\n";
237 *out <<
"\nTesting the solution error ||b-A*x||/||b||:\n";
240 auto tpetra_r =
rcp(
new Vector_t(tpetra_b->getMap()));
241 tpetra_r->assign(*tpetra_b);
245 nrm_r = tpetraNorm2(*tpetra_r),
246 nrm_b = tpetraNorm2(*tpetra_b),
247 rel_err = ( nrm_r / nrm_b );
249 passed = (rel_err <= tol);
252 <<
"||b-A*x||/||b|| = " << nrm_r <<
"/" << nrm_b <<
" = " << rel_err
253 <<
" < tol = " << tol <<
" ? " << ( passed ?
"passed" :
"failed" ) <<
"\n";
255 if(!passed) success =
false;
262 int main(
int argc,
char* argv[])
266 out = Teuchos::VerboseObjectBase::getDefaultOStream();
268 bool success =
false;
275 if(success) *out <<
"\nCongratulations! All of the tests checked out!\n";
276 else *out <<
"\nOh no! At least one of the tests failed!\n";
278 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