46 #include "Thyra_LinearOpWithSolveFactoryHelpers.hpp" 47 #include "Thyra_PreconditionerFactoryHelpers.hpp" 48 #include "Thyra_LinearOpWithSolveFactoryExamples.hpp" 49 #include "Thyra_DefaultScaledAdjointLinearOp.hpp" 50 #include "Thyra_EpetraLinearOp.hpp" 51 #include "Thyra_LinearOpTester.hpp" 52 #include "Thyra_LinearOpWithSolveBase.hpp" 53 #include "Thyra_LinearOpWithSolveTester.hpp" 54 #include "EpetraExt_readEpetraLinearSystem.h" 55 #include "Epetra_SerialComm.h" 57 #ifdef HAVE_AZTECOO_IFPACK 61 # include "Epetra_MpiComm.h" 67 const std::string matrixFile
68 ,
const bool testTranspose
69 ,
const int numRandomVectors
70 ,
const double maxFwdError
71 ,
const double maxResid
72 ,
const double maxSolutionError
73 ,
const bool showAllTests
83 bool result, success =
true;
95 <<
"\n*** Testing Thyra::AztecOOLinearOpWithSolveFactory (and Thyra::AztecOOLinearOpWithSolve)" 97 <<
"\nEchoing input options:" 98 <<
"\n matrixFile = " << matrixFile
99 <<
"\n testTranspose = " << testTranspose
100 <<
"\n numRandomVectors = " << numRandomVectors
101 <<
"\n maxFwdError = " << maxFwdError
102 <<
"\n maxResid = " << maxResid
103 <<
"\n showAllTests = " << showAllTests
104 <<
"\n dumpAll = " << dumpAll
108 const bool useAztecPrec = (
111 aztecooLOWSFPL->
sublist(
"Forward Solve")
112 .sublist(
"AztecOO Settings")
113 .get(
"Aztec Preconditioner",
"none")!=
"none" 118 *out <<
"\nUsing aztec preconditioning so we will not test adjoint solves using internal preconditioning ...\n";
121 if(out.
get()) *out <<
"\nA) Reading in an epetra matrix A from the file \'"<<matrixFile<<
"\' ...\n";
124 Epetra_MpiComm comm(MPI_COMM_WORLD);
126 Epetra_SerialComm comm;
129 EpetraExt::readEpetraLinearSystem( matrixFile, comm, &epetra_A );
135 if(out.
get()) *out <<
"\nB) Creating a AztecOOLinearOpWithSolveFactory object opFactory ...\n";
140 *out <<
"\nlowsFactory.getValidParameters() initially:\n";
141 lowsFactory->getValidParameters()->print(
OSTab(out).o(),PLPrintOptions().showTypes(
true).showDoc(
true));
143 aztecooLOWSFPL->
sublist(
"Forward Solve").set(
"Tolerance",maxResid);
144 aztecooLOWSFPL->
sublist(
"Adjoint Solve").set(
"Tolerance",maxResid);
146 aztecooLOWSFPL->
set(
"Output Every RHS",
bool(
true));
149 *out <<
"\naztecooLOWSFPL before setting parameters:\n";
152 if(aztecooLOWSFPL) lowsFactory->setParameterList(
Teuchos::rcp(aztecooLOWSFPL,
false));
154 if(out.
get()) *out <<
"\nC) Creating a AztecOOLinearOpWithSolve object nsA from A ...\n";
157 nsA = lowsFactory->createOp();
159 Thyra::initializeOp<double>(*lowsFactory, A, nsA.
ptr());
161 if(out.
get()) *out <<
"\nD) Testing the LinearOpBase interface of nsA ...\n";
163 LinearOpTester<double> linearOpTester;
164 linearOpTester.check_adjoint(testTranspose);
165 linearOpTester.num_random_vectors(numRandomVectors);
166 linearOpTester.set_all_error_tol(maxFwdError);
167 linearOpTester.set_all_warning_tol(1e-2*maxFwdError);
168 linearOpTester.show_all_tests(showAllTests);
169 linearOpTester.dump_all(dumpAll);
170 Thyra::seed_randomize<double>(0);
171 result = linearOpTester.check(*nsA,out());
172 if(!result) success =
false;
174 if(out.
get()) *out <<
"\nE) Testing the LinearOpWithSolveBase interface of nsA ...\n";
176 LinearOpWithSolveTester<double> linearOpWithSolveTester;
177 linearOpWithSolveTester.turn_off_all_tests();
178 linearOpWithSolveTester.check_forward_default(
true);
179 linearOpWithSolveTester.check_forward_residual(
true);
180 if(testTranspose && useAztecPrec) {
181 linearOpWithSolveTester.check_adjoint_default(
true);
182 linearOpWithSolveTester.check_adjoint_residual(
true);
185 linearOpWithSolveTester.check_adjoint_default(
false);
186 linearOpWithSolveTester.check_adjoint_residual(
false);
188 linearOpWithSolveTester.set_all_solve_tol(maxResid);
189 linearOpWithSolveTester.set_all_slack_error_tol(maxResid);
190 linearOpWithSolveTester.set_all_slack_warning_tol(10.0*maxResid);
191 linearOpWithSolveTester.forward_default_residual_error_tol(2.5*maxResid);
192 linearOpWithSolveTester.forward_default_solution_error_error_tol(maxSolutionError);
193 linearOpWithSolveTester.adjoint_default_residual_error_tol(2.5*maxResid);
194 linearOpWithSolveTester.adjoint_default_solution_error_error_tol(maxSolutionError);
195 linearOpWithSolveTester.show_all_tests(showAllTests);
196 linearOpWithSolveTester.dump_all(dumpAll);
197 Thyra::seed_randomize<double>(0);
198 result = linearOpWithSolveTester.check(*nsA,out.
get());
199 if(!result) success =
false;
201 if(out.
get()) *out <<
"\nF) Uninitialize nsA, create preconditioner for diagonal scaled by 0.99 and then reinitialize nsA reusing the old preconditioner ...\n";
204 Thyra::uninitializeOp<double>(*lowsFactory, nsA.
ptr());
207 Epetra_Vector diag(epetra_A->RowMap());
208 epetra_A->ExtractDiagonalCopy(diag);
210 epetra_A->ReplaceDiagonalValues(diag);
212 Thyra::initializeOp<double>(*lowsFactory, A, nsA.
ptr());
215 Thyra::uninitializeOp<double>(*lowsFactory, nsA.
ptr());
218 Epetra_Vector diag(epetra_A->RowMap());
219 epetra_A->ExtractDiagonalCopy(diag);
221 epetra_A->ReplaceDiagonalValues(diag);
223 initializeAndReuseOp<double>(*lowsFactory, A, nsA.
ptr());
225 if(out.
get()) *out <<
"\nG) Testing the LinearOpWithSolveBase interface of nsA ...\n";
227 Thyra::seed_randomize<double>(0);
228 result = linearOpWithSolveTester.check(*nsA,out.
get());
229 if(!result) success =
false;
233 if(out.
get()) *out <<
"\nH) Reinitialize (A,A,PRECONDITIONER_INPUT_TYPE_AS_MATRIX) => nsA ...\n";
235 initializeApproxPreconditionedOp<double>(*lowsFactory, A, A, nsA.
ptr());
237 if(out.
get()) *out <<
"\nI) Testing the LinearOpWithSolveBase interface of nsA ...\n";
239 Thyra::seed_randomize<double>(0);
240 result = linearOpWithSolveTester.check(*nsA,out.
get());
241 if(!result) success =
false;
243 if(testTranspose && useAztecPrec) {
244 linearOpWithSolveTester.check_adjoint_default(
true);
245 linearOpWithSolveTester.check_adjoint_residual(
true);
248 linearOpWithSolveTester.check_adjoint_default(
false);
249 linearOpWithSolveTester.check_adjoint_residual(
false);
255 if(out.
get()) *out <<
"\nSkipping testing steps H and I since we are not using aztec preconditioning and therefore will not test with an external preconditioner matrix!\n";
263 #ifdef HAVE_AZTECOO_IFPACK 268 linearOpWithSolveTester.check_adjoint_default(
true);
269 linearOpWithSolveTester.check_adjoint_residual(
true);
272 if(out.
get()) *out <<
"\nJ) Create an ifpack preconditioner precA for A ...\n";
277 *out <<
"\nprecFactory.description() = " << precFactory->description() << std::endl;
278 *out <<
"\nprecFactory.getValidParameters() =\n";
279 precFactory->getValidParameters()->print(
OSTab(out).o(),0,
true,
false);
284 ifpackPFPL->
set(
"Prec Type",
"ILUT");
285 ifpackPFPL->
set(
"Overlap",
int(1));
287 *out <<
"\nifpackPFPL before setting parameters =\n";
291 precFactory->setParameterList(ifpackPFPL);
294 precA = precFactory->createPrec();
295 Thyra::initializePrec<double>(*precFactory,A,&*precA);
298 *out <<
"\nifpackPFPL after setting parameters =\n";
300 *out <<
"\nprecFactory.description() = " << precFactory->description() << std::endl;
303 if(out.
get()) *out <<
"\nprecA.description() = " << precA->description() << std::endl;
306 if(out.
get()) *out <<
"\nK) Reinitialize (A,precA->getUnspecifiedPrecOp(),PRECONDITIONER_INPUT_TYPE_AS_OPERATOR) => nsA ...\n";
308 Thyra::initializePreconditionedOp<double>(*lowsFactory,A,precA,&*nsA);
310 if(out.
get()) *out <<
"\nL) Testing the LinearOpWithSolveBase interface of nsA ...\n";
312 Thyra::seed_randomize<double>(0);
313 result = linearOpWithSolveTester.check(*nsA,out.
get());
314 if(!result) success =
false;
316 if(testTranspose && useAztecPrec) {
317 linearOpWithSolveTester.check_adjoint_default(
true);
318 linearOpWithSolveTester.check_adjoint_residual(
true);
321 linearOpWithSolveTester.check_adjoint_default(
false);
322 linearOpWithSolveTester.check_adjoint_residual(
false);
328 if(out.
get()) *out <<
"\nSkipping testing steps J, K, and L since we are not using aztec preconditioning and therefore will not test with an ifpack preconditioner!\n";
332 #else // HAVE_AZTECOO_IFPACK 334 if(out.
get()) *out <<
"\nSkipping testing steps J, K, and L since they require ifpack and ifpack has not been enabled!\n";
336 #endif // HAVE_AZTECOO_IFPACK 339 if(out.
get()) *out <<
"\nM) Scale the epetra_A object by 2.5, and then reinitialize nsA with epetra_A ...\n";
341 Thyra::uninitializeOp<double>(*lowsFactory, nsA.
ptr());
343 epetra_A->Scale(2.5);
344 initializeOp<double>(*lowsFactory, A, nsA.
ptr());
346 if(out.
get()) *out <<
"\nN) Testing the LinearOpWithSolveBase interface of nsA ...\n";
348 Thyra::seed_randomize<double>(0);
349 result = linearOpWithSolveTester.check(*nsA,out.
get());
350 if(!result) success =
false;
352 if(out.
get()) *out <<
"\nO) Create a scaled (by 2.5) copy epetra_A2 of epetra_A, and then reinitialize nsA with epetra_A2 ...\n";
355 epetra_A2 =
Teuchos::rcp(
new Epetra_CrsMatrix(*epetra_A));
356 epetra_A2->Scale(2.5);
358 A2 = Thyra::epetraLinearOp(epetra_A2);
359 initializeOp<double>(*lowsFactory, A2, nsA.
ptr());
364 if(out.
get()) *out <<
"\nP) Testing the LinearOpWithSolveBase interface of nsA ...\n";
366 Thyra::seed_randomize<double>(0);
367 result = linearOpWithSolveTester.check(*nsA,out.
get());
368 if(!result) success =
false;
372 if(out.
get()) *out <<
"\nQ) Create an implicitly scaled (by 2.5) and transposed matrix A3 = scale(2.5,transpose(A)) and initialize nsA2 ...\n";
375 A3 = scale<double>(2.5,transpose<double>(A));
377 nsA2 = linearOpWithSolve(*lowsFactory,A3);
379 if(out.
get()) *out <<
"\nR) Testing the LinearOpWithSolveBase interface of nsA2 ...\n";
381 Thyra::seed_randomize<double>(0);
382 result = linearOpWithSolveTester.check(*nsA2,out.
get());
383 if(!result) success =
false;
385 if(out.
get()) *out <<
"\nS) Testing that LinearOpBase interfaces of transpose(nsA) == nsA2 ...\n";
387 result = linearOpTester.compare(
388 *transpose(Teuchos::rcp_implicit_cast<
const LinearOpBase<double> >(nsA)),*nsA2
391 if(!result) success =
false;
396 if(out.
get()) *out <<
"\nSkipping testing steps Q, R, and S because we are using internal AztecOO preconditioners!\n";
400 if(out.
get()) *out <<
"\nT) Running example use cases ...\n";
402 nonExternallyPreconditionedLinearSolveUseCases(
403 *A,*lowsFactory,testTranspose,*out
406 if(precFactory.
get()) {
407 externallyPreconditionedLinearSolveUseCases(
408 *A,*lowsFactory,*precFactory,
false,
true,*out
414 if(out.
get()) *out <<
"\nTest failed since is was not even compiled since SUN_CXX was defined!\n";
420 catch(
const std::exception &excpt ) {
421 std::cerr <<
"\n*** Caught standard exception : " << excpt.what() << std::endl;
Concrete preconditioner factory subclass based on Ifpack.
LinearOpWithSolveFactoryBase subclass implemented in terms of AztecOO.
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
RCP< ParameterList > sublist(const RCP< ParameterList > ¶mList, const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
basic_OSTab< char > OSTab
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
ParameterList::PrintOptions PLPrintOptions
bool test_single_aztecoo_thyra_solver(const std::string matrixFile, const bool testTranspose, const int numRandomVectors, const double maxFwdError, const double maxResid, const double maxSolutionError, const bool showAllTests, const bool dumpAll, Teuchos::ParameterList *paramList, Teuchos::FancyOStream *out)
Testing function for a single aztecoo solver with a single matrix.