11 #include "Thyra_LinearOpWithSolveFactoryHelpers.hpp"
12 #include "Thyra_PreconditionerFactoryHelpers.hpp"
13 #include "Thyra_DefaultInverseLinearOp.hpp"
14 #include "Thyra_DefaultMultipliedLinearOp.hpp"
18 #include "Thyra_TestingTools.hpp"
19 #include "Thyra_LinearOpTester.hpp"
20 #include "EpetraExt_CrsMatrixIn.h"
21 #include "Epetra_CrsMatrix.h"
29 # include "Epetra_MpiComm.h"
31 # include "Epetra_SerialComm.h"
48 readEpetraCrsMatrixFromMatrixMarket(
49 const std::string fileName,
const Epetra_Comm &comm
52 Epetra_CrsMatrix *
A = 0;
54 0!=EpetraExt::MatrixMarketFileToCrsMatrix( fileName.c_str(), comm, A ),
56 "Error, could not read matrix file \""<<fileName<<
"\"!"
64 readEpetraCrsMatrixFromMatrixMarketAsLinearOp(
65 const std::string fileName,
const Epetra_Comm &comm,
66 const std::string &label
69 return Thyra::epetraLinearOp(
70 readEpetraCrsMatrixFromMatrixMarket(fileName,comm),label
78 int main(
int argc,
char* argv[])
81 using Teuchos::describe;
83 using Teuchos::rcp_dynamic_cast;
84 using Teuchos::rcp_const_cast;
88 using Teuchos::sublist;
89 using Teuchos::getParametersFromXmlFile;
92 using Thyra::initializePreconditionedOp;
93 using Thyra::initializeOp;
94 using Thyra::unspecifiedPrec;
96 typedef RCP<const Thyra::LinearOpBase<double> > LinearOpPtr;
97 typedef RCP<Thyra::VectorBase<double> > VectorPtr;
105 out = Teuchos::VerboseObjectBase::getDefaultOStream();
113 CommandLineProcessor clp(
false);
115 const int numVerbLevels = 6;
125 {
"default",
"none",
"low",
"medium",
"high",
"extreme" };
128 clp.setOption(
"verb-level", &verbLevel,
129 numVerbLevels, verbLevelValues, verbLevelNames,
130 "Verbosity level used for all objects."
133 std::string matrixFile =
".";
134 clp.setOption(
"matrix-file", &matrixFile,
138 std::string paramListFile =
"";
139 clp.setOption(
"param-list-file", ¶mListFile,
140 "Parameter list for preconditioner and solver blocks."
143 bool showParams =
false;
144 clp.setOption(
"show-params",
"no-show-params", &showParams,
145 "Show the parameter list or not."
148 bool testPrecIsLinearOp =
true;
149 clp.setOption(
"test-prec-is-linear-op",
"test-prec-is-linear-op", &testPrecIsLinearOp,
150 "Test if the preconditioner is a linear operator or not."
153 double solveTol = 1e-8;
154 clp.setOption(
"solve-tol", &solveTol,
155 "Tolerance for the solution to determine success or failure!"
159 "This example program shows how to use one linear solver (e.g. AztecOO)\n"
160 "as a preconditioner for another iterative solver (e.g. Belos).\n"
165 CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
166 if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL )
return parse_return;
170 *out <<
"\nA) Reading in the matrix ...\n";
174 Epetra_MpiComm comm(MPI_COMM_WORLD);
176 Epetra_SerialComm comm;
179 const LinearOpPtr A = readEpetraCrsMatrixFromMatrixMarketAsLinearOp(
180 matrixFile, comm,
"A");
181 *out <<
"\nA = " << describe(*A,verbLevel) <<
"\n";
183 const RCP<ParameterList> paramList = getParametersFromXmlFile(paramListFile);
185 *out <<
"\nRead in parameter list:\n\n";
186 paramList->print(*out, PLPrintOptions().indent(2).showTypes(
true));
190 *out <<
"\nB) Get the preconditioner as a forward solver\n";
193 const RCP<ParameterList> precParamList = sublist(paramList,
"Preconditioner Solver");
196 const RCP<const Thyra::LinearOpWithSolveFactoryBase<double> > precSolverStrategy
197 = createLinearSolveStrategy(precSolverBuilder);
200 const LinearOpPtr A_inv_prec = inverse<double>(*precSolverStrategy, A,
201 Thyra::SUPPORT_SOLVE_FORWARD_ONLY,
203 Thyra::IGNORE_SOLVE_FAILURE
205 *out <<
"\nA_inv_prec = " << describe(*A_inv_prec, verbLevel) <<
"\n";
207 if (testPrecIsLinearOp) {
208 *out <<
"\nTest that the preconditioner A_inv_prec is indeed a linear operator.\n";
209 Thyra::LinearOpTester<double> linearOpTester;
210 linearOpTester.check_adjoint(
false);
211 const bool linearOpCheck = linearOpTester.check(*A_inv_prec, out.
ptr());
212 if (!linearOpCheck) { success =
false; }
216 *out <<
"\nC) Create the forward solver using the created preconditioner ...\n";
219 const RCP<ParameterList> fwdSolverParamList = sublist(paramList,
"Forward Solver");
222 const RCP<const Thyra::LinearOpWithSolveFactoryBase<double> > fwdSolverSolverStrategy
223 = createLinearSolveStrategy(fwdSolverSolverBuilder);
225 const RCP<Thyra::LinearOpWithSolveBase<double> >
226 A_lows = fwdSolverSolverStrategy->createOp();
228 initializePreconditionedOp<double>( *fwdSolverSolverStrategy, A,
229 unspecifiedPrec(A_inv_prec), A_lows.ptr());
231 *out <<
"\nA_lows = " << describe(*A_lows, verbLevel) <<
"\n";
234 *out <<
"\nD) Solve the linear system for a random RHS ...\n";
237 VectorPtr x = createMember(A->domain());
238 VectorPtr b = createMember(A->range());
239 Thyra::randomize(-1.0, +1.0, b.ptr());
240 Thyra::assign(x.ptr(), 0.0);
242 Thyra::SolveStatus<double>
243 solveStatus = solve<double>( *A_lows, Thyra::NOTRANS, *b, x.ptr() );
245 *out <<
"\nSolve status:\n" << solveStatus;
247 *out <<
"\nSolution ||x|| = " << Thyra::norm(*x) <<
"\n";
250 *out <<
"\nParameter list after use:\n\n";
251 paramList->print(*out, PLPrintOptions().indent(2).showTypes(
true));
255 *out <<
"\nF) Checking the error in the solution of r=b-A*x ...\n";
258 VectorPtr Ax = Thyra::createMember(b->space());
259 Thyra::apply( *A, Thyra::NOTRANS, *x, Ax.ptr() );
260 VectorPtr r = Thyra::createMember(b->space());
261 Thyra::V_VmV<double>(r.ptr(), *b, *Ax);
264 Ax_nrm = Thyra::norm(*Ax),
265 r_nrm = Thyra::norm(*r),
266 b_nrm = Thyra::norm(*b),
267 r_nrm_over_b_nrm = r_nrm / b_nrm;
269 bool resid_tol_check = ( r_nrm_over_b_nrm <= solveTol );
270 if(!resid_tol_check) success =
false;
273 <<
"\n||A*x|| = " << Ax_nrm <<
"\n";
276 <<
"\n||A*x-b||/||b|| = " << r_nrm <<
"/" << b_nrm
277 <<
" = " << r_nrm_over_b_nrm <<
" <= " << solveTol
278 <<
" : " << Thyra::passfail(resid_tol_check) <<
"\n";
285 if(success) *out <<
"\nCongratulations! All of the tests checked out!\n";
286 else *out <<
"\nOh no! At least one of the tests failed!\n";
289 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
int main(int argc, char *argv[])
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void setParameterList(RCP< ParameterList > const ¶mList)
static void summarize(Ptr< const Comm< int > > comm, std::ostream &out=std::cout, const bool alwaysWriteLocal=false, const bool writeGlobalStats=true, const bool writeZeroTimers=true, const ECounterSetOp setOp=Intersection, const std::string &filter="", const bool ignoreZeroTimers=false)
#define TEUCHOS_STANDARD_CATCH_STATEMENTS(VERBOSE, ERR_STREAM, SUCCESS_FLAG)
ParameterList::PrintOptions PLPrintOptions
Concrete subclass of Thyra::LinearSolverBuilderBase for creating Thyra::LinearOpWithSolveFactoryBase ...