Stratimikos Package Browser (Single Doxygen Collection)  Version of the Day
test_single_aztecoo_thyra_solver.cpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Stratimikos: Thyra-based strategies for linear solvers
5 // Copyright (2006) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
42 
43 #ifndef SUN_CXX
44 
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
59 #endif
60 #ifdef HAVE_MPI
61 # include "Epetra_MpiComm.h"
62 #endif
63 
64 #endif // SUN_CXX
65 
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
74  ,const bool dumpAll
75  ,Teuchos::ParameterList *aztecooLOWSFPL
76  ,Teuchos::FancyOStream *out_arg
77  )
78 {
79  using Teuchos::rcp;
80  using Teuchos::RCP;
81  using Teuchos::OSTab;
83  bool result, success = true;
84 
86  out = Teuchos::rcp(out_arg,false);
87 
88  try {
89 
90 #ifndef SUN_CXX
91 
92  if(out.get()) {
93  *out
94  << "\n***"
95  << "\n*** Testing Thyra::AztecOOLinearOpWithSolveFactory (and Thyra::AztecOOLinearOpWithSolve)"
96  << "\n***\n"
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
105  << std::endl;
106  }
107 
108  const bool useAztecPrec = (
109  aztecooLOWSFPL
110  &&
111  aztecooLOWSFPL->sublist("Forward Solve")
112  .sublist("AztecOO Settings")
113  .get("Aztec Preconditioner","none")!="none"
114  );
115 
116  if(out.get()) {
117  if(useAztecPrec)
118  *out << "\nUsing aztec preconditioning so we will not test adjoint solves using internal preconditioning ...\n";
119  }
120 
121  if(out.get()) *out << "\nA) Reading in an epetra matrix A from the file \'"<<matrixFile<<"\' ...\n";
122 
123 #ifdef HAVE_MPI
124  Epetra_MpiComm comm(MPI_COMM_WORLD);
125 #else
126  Epetra_SerialComm comm;
127 #endif
128  RCP<Epetra_CrsMatrix> epetra_A;
129  EpetraExt::readEpetraLinearSystem( matrixFile, comm, &epetra_A );
130 
131  RCP<const LinearOpBase<double> > A = Thyra::epetraLinearOp(epetra_A);
132 
133  if(out.get() && dumpAll) *out << "\ndescribe(A) =\n" << describe(*A,Teuchos::VERB_EXTREME);
134 
135  if(out.get()) *out << "\nB) Creating a AztecOOLinearOpWithSolveFactory object opFactory ...\n";
136 
138  lowsFactory = Teuchos::rcp(new AztecOOLinearOpWithSolveFactory());
139  if(out.get()) {
140  *out << "\nlowsFactory.getValidParameters() initially:\n";
141  lowsFactory->getValidParameters()->print(OSTab(out).o(),PLPrintOptions().showTypes(true).showDoc(true));
142  }
143  aztecooLOWSFPL->sublist("Forward Solve").set("Tolerance",maxResid);
144  aztecooLOWSFPL->sublist("Adjoint Solve").set("Tolerance",maxResid);
145  if(showAllTests) {
146  aztecooLOWSFPL->set("Output Every RHS",bool(true));
147  }
148  if(out.get()) {
149  *out << "\naztecooLOWSFPL before setting parameters:\n";
150  aztecooLOWSFPL->print(OSTab(out).o(),0,true);
151  }
152  if(aztecooLOWSFPL) lowsFactory->setParameterList(Teuchos::rcp(aztecooLOWSFPL,false));
153 
154  if(out.get()) *out << "\nC) Creating a AztecOOLinearOpWithSolve object nsA from A ...\n";
155 
157  nsA = lowsFactory->createOp();
158 
159  Thyra::initializeOp<double>(*lowsFactory, A, nsA.ptr());
160 
161  if(out.get()) *out << "\nD) Testing the LinearOpBase interface of nsA ...\n";
162 
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;
173 
174  if(out.get()) *out << "\nE) Testing the LinearOpWithSolveBase interface of nsA ...\n";
175 
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);
183  }
184  else {
185  linearOpWithSolveTester.check_adjoint_default(false);
186  linearOpWithSolveTester.check_adjoint_residual(false);
187  }
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;
200 
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";
202 
203  // Scale the diagonal of the matrix and then create the preconditioner for it
204  Thyra::uninitializeOp<double>(*lowsFactory, nsA.ptr());
205  // Above is not required but a good idea since we are changing the matrix
206  {
207  Epetra_Vector diag(epetra_A->RowMap());
208  epetra_A->ExtractDiagonalCopy(diag);
209  diag.Scale(0.5);
210  epetra_A->ReplaceDiagonalValues(diag);
211  }
212  Thyra::initializeOp<double>(*lowsFactory, A, nsA.ptr());
213 
214  // Scale the matrix back again and then reuse the preconditioner
215  Thyra::uninitializeOp<double>(*lowsFactory, nsA.ptr());
216  // Above is not required but a good idea since we are changing the matrix
217  {
218  Epetra_Vector diag(epetra_A->RowMap());
219  epetra_A->ExtractDiagonalCopy(diag);
220  diag.Scale(1.0/0.5);
221  epetra_A->ReplaceDiagonalValues(diag);
222  }
223  initializeAndReuseOp<double>(*lowsFactory, A, nsA.ptr());
224 
225  if(out.get()) *out << "\nG) Testing the LinearOpWithSolveBase interface of nsA ...\n";
226 
227  Thyra::seed_randomize<double>(0);
228  result = linearOpWithSolveTester.check(*nsA,out.get());
229  if(!result) success = false;
230 
231  if(useAztecPrec) {
232 
233  if(out.get()) *out << "\nH) Reinitialize (A,A,PRECONDITIONER_INPUT_TYPE_AS_MATRIX) => nsA ...\n";
234 
235  initializeApproxPreconditionedOp<double>(*lowsFactory, A, A, nsA.ptr());
236 
237  if(out.get()) *out << "\nI) Testing the LinearOpWithSolveBase interface of nsA ...\n";
238 
239  Thyra::seed_randomize<double>(0);
240  result = linearOpWithSolveTester.check(*nsA,out.get());
241  if(!result) success = false;
242 
243  if(testTranspose && useAztecPrec) {
244  linearOpWithSolveTester.check_adjoint_default(true);
245  linearOpWithSolveTester.check_adjoint_residual(true);
246  }
247  else {
248  linearOpWithSolveTester.check_adjoint_default(false);
249  linearOpWithSolveTester.check_adjoint_residual(false);
250  }
251 
252  }
253  else {
254 
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";
256 
257  }
258 
259 
261  precFactory;
262 
263 #ifdef HAVE_AZTECOO_IFPACK
264 
265  if(useAztecPrec) {
266 
267  if(testTranspose) {
268  linearOpWithSolveTester.check_adjoint_default(true);
269  linearOpWithSolveTester.check_adjoint_residual(true);
270  }
271 
272  if(out.get()) *out << "\nJ) Create an ifpack preconditioner precA for A ...\n";
273 
274  precFactory = Teuchos::rcp(new IfpackPreconditionerFactory());
275 
276  if(out.get()) {
277  *out << "\nprecFactory.description() = " << precFactory->description() << std::endl;
278  *out << "\nprecFactory.getValidParameters() =\n";
279  precFactory->getValidParameters()->print(OSTab(out).o(),0,true,false);
280  }
281 
283  ifpackPFPL = Teuchos::rcp(new Teuchos::ParameterList("IfpackPreconditionerFactory"));
284  ifpackPFPL->set("Prec Type","ILUT");
285  ifpackPFPL->set("Overlap",int(1));
286  if(out.get()) {
287  *out << "\nifpackPFPL before setting parameters =\n";
288  ifpackPFPL->print(OSTab(out).o(),0,true);
289  }
290 
291  precFactory->setParameterList(ifpackPFPL);
292 
294  precA = precFactory->createPrec();
295  Thyra::initializePrec<double>(*precFactory,A,&*precA);
296 
297  if(out.get()) {
298  *out << "\nifpackPFPL after setting parameters =\n";
299  ifpackPFPL->print(OSTab(out).o(),0,true);
300  *out << "\nprecFactory.description() = " << precFactory->description() << std::endl;
301  }
302 
303  if(out.get()) *out << "\nprecA.description() = " << precA->description() << std::endl;
304  if(out.get() && dumpAll) *out << "\ndescribe(precA) =\n" << describe(*precA,Teuchos::VERB_EXTREME);
305 
306  if(out.get()) *out << "\nK) Reinitialize (A,precA->getUnspecifiedPrecOp(),PRECONDITIONER_INPUT_TYPE_AS_OPERATOR) => nsA ...\n";
307 
308  Thyra::initializePreconditionedOp<double>(*lowsFactory,A,precA,&*nsA);
309 
310  if(out.get()) *out << "\nL) Testing the LinearOpWithSolveBase interface of nsA ...\n";
311 
312  Thyra::seed_randomize<double>(0);
313  result = linearOpWithSolveTester.check(*nsA,out.get());
314  if(!result) success = false;
315 
316  if(testTranspose && useAztecPrec) {
317  linearOpWithSolveTester.check_adjoint_default(true);
318  linearOpWithSolveTester.check_adjoint_residual(true);
319  }
320  else {
321  linearOpWithSolveTester.check_adjoint_default(false);
322  linearOpWithSolveTester.check_adjoint_residual(false);
323  }
324 
325  }
326  else {
327 
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";
329 
330  }
331 
332 #else // HAVE_AZTECOO_IFPACK
333 
334  if(out.get()) *out << "\nSkipping testing steps J, K, and L since they require ifpack and ifpack has not been enabled!\n";
335 
336 #endif // HAVE_AZTECOO_IFPACK
337 
338 
339  if(out.get()) *out << "\nM) Scale the epetra_A object by 2.5, and then reinitialize nsA with epetra_A ...\n";
340 
341  Thyra::uninitializeOp<double>(*lowsFactory, nsA.ptr());
342  // Not required but a good idea since we are changing the matrix
343  epetra_A->Scale(2.5);
344  initializeOp<double>(*lowsFactory, A, nsA.ptr());
345 
346  if(out.get()) *out << "\nN) Testing the LinearOpWithSolveBase interface of nsA ...\n";
347 
348  Thyra::seed_randomize<double>(0);
349  result = linearOpWithSolveTester.check(*nsA,out.get());
350  if(!result) success = false;
351 
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";
353 
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());
360  // Note that it was okay not to uninitialize nsA first here since A, which
361  // was used to initialize nsA last, was not changed and therefore the
362  // state of nsA was fine throughout
363 
364  if(out.get()) *out << "\nP) Testing the LinearOpWithSolveBase interface of nsA ...\n";
365 
366  Thyra::seed_randomize<double>(0);
367  result = linearOpWithSolveTester.check(*nsA,out.get());
368  if(!result) success = false;
369 
370  if(!useAztecPrec) {
371 
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";
373 
375  A3 = scale<double>(2.5,transpose<double>(A));
377  nsA2 = linearOpWithSolve(*lowsFactory,A3);
378 
379  if(out.get()) *out << "\nR) Testing the LinearOpWithSolveBase interface of nsA2 ...\n";
380 
381  Thyra::seed_randomize<double>(0);
382  result = linearOpWithSolveTester.check(*nsA2,out.get());
383  if(!result) success = false;
384 
385  if(out.get()) *out << "\nS) Testing that LinearOpBase interfaces of transpose(nsA) == nsA2 ...\n";
386 
387  result = linearOpTester.compare(
388  *transpose(Teuchos::rcp_implicit_cast<const LinearOpBase<double> >(nsA)),*nsA2
389  ,out()
390  );
391  if(!result) success = false;
392 
393  }
394  else {
395 
396  if(out.get()) *out << "\nSkipping testing steps Q, R, and S because we are using internal AztecOO preconditioners!\n";
397 
398  }
399 
400  if(out.get()) *out << "\nT) Running example use cases ...\n";
401 
402  nonExternallyPreconditionedLinearSolveUseCases(
403  *A,*lowsFactory,testTranspose,*out
404  );
405 
406  if(precFactory.get()) {
407  externallyPreconditionedLinearSolveUseCases(
408  *A,*lowsFactory,*precFactory,false,true,*out
409  );
410  }
411 
412 #else // SUN_CXX
413 
414  if(out.get()) *out << "\nTest failed since is was not even compiled since SUN_CXX was defined!\n";
415  success = false;
416 
417 #endif // SUN_CXX
418 
419  }
420  catch( const std::exception &excpt ) {
421  std::cerr << "\n*** Caught standard exception : " << excpt.what() << std::endl;
422  success = false;
423  }
424 
425  return success;
426 
427 }
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 > &paramList, const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
T * get() const
basic_OSTab< char > OSTab
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Ptr< T > ptr() const
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.