Stratimikos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
test_single_belos_thyra_solver.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Stratimikos: Thyra-based strategies for linear solvers
4 //
5 // Copyright 2006 NTESS and the Stratimikos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
11 
12 #ifndef SUN_CXX
13 
14 #include "Thyra_BelosLinearOpWithSolveFactory.hpp"
15 #include "Thyra_LinearOpWithSolveFactoryHelpers.hpp"
16 #include "Thyra_EpetraLinearOp.hpp"
17 #include "Thyra_LinearOpTester.hpp"
18 #include "Thyra_LinearOpWithSolveBase.hpp"
19 #include "Thyra_LinearOpWithSolveTester.hpp"
20 #include "Thyra_MultiVectorStdOps.hpp"
21 #include "Thyra_VectorStdOps.hpp"
22 #include "EpetraExt_readEpetraLinearSystem.h"
23 #include "Epetra_SerialComm.h"
25 #ifdef HAVE_BELOS_IFPACK
27 #endif
28 
29 #endif // SUN_CXX
30 
31 bool Thyra::test_single_belos_thyra_solver(
32  const std::string matrixFile
33  ,const bool testTranspose
34  ,const bool usePreconditioner
35  ,const int numRhs
36  ,const int numRandomVectors
37  ,const double maxFwdError
38  ,const double maxResid
39  ,const double maxSolutionError
40  ,const bool showAllTests
41  ,const bool dumpAll
42  ,Teuchos::ParameterList *belosLOWSFPL
43  ,Teuchos::ParameterList *precPL
44  ,Teuchos::FancyOStream *out_arg
45  )
46 {
47  using Teuchos::rcp;
48  using Teuchos::OSTab;
49  bool result, success = true;
50 
52 
53  try {
54 
55 #ifndef SUN_CXX
56 
57  if(out.get()) {
58  *out << "\n***"
59  << "\n*** Testing Thyra::BelosLinearOpWithSolveFactory (and Thyra::BelosLinearOpWithSolve)"
60  << "\n***\n"
61  << "\nEchoing input options:"
62  << "\n matrixFile = " << matrixFile
63  << "\n testTranspose = " << testTranspose
64  << "\n usePreconditioner = " << usePreconditioner
65  << "\n numRhs = " << numRhs
66  << "\n numRandomVectors = " << numRandomVectors
67  << "\n maxFwdError = " << maxFwdError
68  << "\n maxResid = " << maxResid
69  << "\n showAllTests = " << showAllTests
70  << "\n dumpAll = " << dumpAll
71  << std::endl;
72  }
73 
74  if(out.get()) *out << "\nA) Reading in an epetra matrix A from the file \'"<<matrixFile<<"\' ...\n";
75 
76  Epetra_SerialComm comm;
78  EpetraExt::readEpetraLinearSystem( matrixFile, comm, &epetra_A );
79 
80  Teuchos::RCP<const LinearOpBase<double> > A = epetraLinearOp(epetra_A);
81 
82  if(out.get() && dumpAll) *out << "\ndescribe(A) =\n" << describe(*A,Teuchos::VERB_EXTREME);
83 
84  if(out.get()) *out << "\nB) Creating a BelosLinearOpWithSolveFactory object opFactory ...\n";
85 
87  lowsFactory;
88  {
90  belosLowsFactory = Teuchos::rcp(new BelosLinearOpWithSolveFactory<double>());
91  lowsFactory = belosLowsFactory;
92  }
93 
94  if(out.get()) {
95  *out << "\nlowsFactory.getValidParameters() before setting preconditioner factory:\n";
96  lowsFactory->getValidParameters()->print(OSTab(out).o(),0,true,false);
97  }
98 
99  if(usePreconditioner) {
100 #ifdef HAVE_BELOS_IFPACK
101  if(out.get()) {
102  *out << "\nSetting an Ifpack preconditioner factory ...\n";
103  }
104  RCP<PreconditionerFactoryBase<double> >
105  precFactory = Teuchos::rcp(new IfpackPreconditionerFactory());
106  if (precPL)
107  precFactory->setParameterList(rcp(precPL,false));
108  lowsFactory->setPreconditionerFactory(precFactory,"Ifpack");
109 #else
110  TEUCHOS_TEST_FOR_EXCEPT(usePreconditioner);
111 #endif
112  }
113 
114  if(out.get()) {
115  *out << "\nlowsFactory.getValidParameters() after setting preconditioner factory:\n";
116  lowsFactory->getValidParameters()->print(OSTab(out).o(),0,true,false);
117  *out << "\nbelosLOWSFPL before setting parameters:\n";
118  belosLOWSFPL->print(OSTab(out).o(),0,true);
119  }
120 
121  lowsFactory->setParameterList(Teuchos::rcp(belosLOWSFPL,false));
122 
123  if(out.get()) {
124  *out << "\nbelosLOWSFPL after setting parameters:\n";
125  belosLOWSFPL->print(OSTab(out).o(),0,true);
126  }
127 
128  if(out.get()) *out << "\nC) Creating a BelosLinearOpWithSolve object nsA from A ...\n";
129 
130  Teuchos::RCP<LinearOpWithSolveBase<double> > nsA = lowsFactory->createOp();
131  Thyra::initializeOp<double>(*lowsFactory, A, nsA.ptr());
132 
133  if(out.get()) *out << "\nD) Testing the LinearOpBase interface of nsA ...\n";
134 
135  LinearOpTester<double> linearOpTester;
136  linearOpTester.check_adjoint(testTranspose);
137  linearOpTester.num_rhs(numRhs);
138  linearOpTester.num_random_vectors(numRandomVectors);
139  linearOpTester.set_all_error_tol(maxFwdError);
140  linearOpTester.set_all_warning_tol(1e-2*maxFwdError);
141  linearOpTester.show_all_tests(showAllTests);
142  linearOpTester.dump_all(dumpAll);
143  Thyra::seed_randomize<double>(0);
144  result = linearOpTester.check(*nsA,Teuchos::Ptr<Teuchos::FancyOStream>(out.get()));
145  if(!result) success = false;
146 
147  if(out.get()) *out << "\nE) Testing the LinearOpWithSolveBase interface of nsA ...\n";
148 
149  LinearOpWithSolveTester<double> linearOpWithSolveTester;
150  linearOpWithSolveTester.num_rhs(numRhs);
151  linearOpWithSolveTester.turn_off_all_tests();
152  linearOpWithSolveTester.check_forward_default(true);
153  linearOpWithSolveTester.check_forward_residual(true);
154  if(testTranspose) {
155  linearOpWithSolveTester.check_adjoint_default(true);
156  linearOpWithSolveTester.check_adjoint_residual(true);
157  }
158  else {
159  linearOpWithSolveTester.check_adjoint_default(false);
160  linearOpWithSolveTester.check_adjoint_residual(false);
161  }
162  linearOpWithSolveTester.set_all_solve_tol(maxResid);
163  linearOpWithSolveTester.set_all_slack_error_tol(maxResid);
164  linearOpWithSolveTester.set_all_slack_warning_tol(1e+1*maxResid);
165  linearOpWithSolveTester.forward_default_residual_error_tol(2*maxResid);
166  linearOpWithSolveTester.forward_default_solution_error_error_tol(maxSolutionError);
167  linearOpWithSolveTester.adjoint_default_residual_error_tol(2*maxResid);
168  linearOpWithSolveTester.adjoint_default_solution_error_error_tol(maxSolutionError);
169  linearOpWithSolveTester.show_all_tests(showAllTests);
170  linearOpWithSolveTester.dump_all(dumpAll);
171  Thyra::seed_randomize<double>(0);
172  result = linearOpWithSolveTester.check(*nsA,out.get());
173  if(!result) success = false;
174 
175  if(out.get()) {
176  *out << "\nbelosLOWSFPL after solving:\n";
177  belosLOWSFPL->print(OSTab(out).o(),0,true);
178  }
179 
180 #else // SUN_CXX
181 
182  if(out.get()) *out << "\nTest failed since is was not even compiled since SUN_CXX was defined!\n";
183  success = false;
184 
185 #endif // SUN_CXX
186 
187  }
188  catch( const std::exception &excpt ) {
189  if(out.get()) *out << std::flush;
190  std::cerr << "*** Caught standard exception : " << excpt.what() << std::endl;
191  success = false;
192  }
193 
194  return success;
195 
196 }
T * get() const
basic_OSTab< char > OSTab
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Ptr< T > ptr() const
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)