Stratimikos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Thyra_Belos_StatusTest_UnitTests.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 
10 #include "Thyra_BelosLinearOpWithSolveFactory.hpp"
11 #include "Thyra_LinearOpWithSolveFactoryHelpers.hpp"
12 #include "Thyra_MultiVectorStdOps.hpp"
13 #include "Thyra_VectorBase.hpp"
14 #include "Thyra_VectorStdOps.hpp"
15 #include "Thyra_EpetraLinearOp.hpp"
16 #include "EpetraExt_readEpetraLinearSystem.h"
17 #include "Epetra_SerialComm.h"
19 #include "Teuchos_toString.hpp"
20 
22 
23 
24 namespace Thyra {
25 
26 
27 //
28 // Helper code
29 //
30 
31 
32 const std::string matrixFileName = "nos1.mtx";
33 
34 
35 RCP<const LinearOpBase<double> > getFwdLinearOp()
36 {
37  static RCP<const LinearOpBase<double> > fwdLinearOp;
38  if (is_null(fwdLinearOp)) {
39  Teuchos::RCP<Epetra_CrsMatrix> epetraCrsMatrix;
40  EpetraExt::readEpetraLinearSystem( matrixFileName, Epetra_SerialComm(), &epetraCrsMatrix );
41  fwdLinearOp = epetraLinearOp(epetraCrsMatrix);
42  }
43  return fwdLinearOp;
44 }
45 
46 
48 template<class Scalar>
49 class MockNormInfReductionFunctional : public ReductionFunctional<Scalar> {
50 protected:
51 
54 
57  reduceImpl(const VectorBase<Scalar> &v) const
58  { return norm_inf(v); }
59 
61  virtual bool isCompatibleImpl( const VectorBase<Scalar> &v ) const
62  { return true; }
63 
65 
66 };
67 
68 
73 template<class Scalar>
74 RCP<MockNormInfReductionFunctional<Scalar> >
76 {
78 }
79 
80 
83 template<class Scalar>
84 class MockMaxNormInfEpsReductionFunctional : public ReductionFunctional<Scalar> {
85 protected:
86 
89 
92  reduceImpl(const VectorBase<Scalar> &v) const
93  {
94  typedef typename ScalarTraits<Scalar>::magnitudeType ScalarMag;
95  return std::max(norm_inf(v), ScalarTraits<ScalarMag>::eps());
96  }
97 
99  virtual bool isCompatibleImpl( const VectorBase<Scalar> &v ) const
100  { return true; }
101 
103 
104 };
105 
106 
111 template<class Scalar>
112 RCP<MockMaxNormInfEpsReductionFunctional<Scalar> >
114 {
116 }
117 
118 
119 template<class Scalar>
120 void runGeneralSolveCriteriaBelosStatusTestCase(
121  const SolveCriteria<Scalar> &solveCriteria,
122  const Ptr<RCP<const VectorBase<Scalar> > > &x_out,
123  const Ptr<RCP<const VectorBase<Scalar> > > &r_out,
124  bool &success,
125  FancyOStream &out
126  )
127 {
128 
129  using Teuchos::describe; using Teuchos::optInArg; using Teuchos::rcpFromRef;
130  using Teuchos::toString;
131 
132  typedef ScalarTraits<Scalar> ST;
133 
134  // A) Set up the linear system
135 
136  const RCP<const LinearOpBase<Scalar> > fwdOp = getFwdLinearOp();
137  out << "\nfwdOp = " << describe(*fwdOp, Teuchos::VERB_MEDIUM) << "\n";
138  const RCP<VectorBase<Scalar> > b = createMember(fwdOp->range());
139  V_S(b.ptr(), ST::one());
140  const RCP<VectorBase<Scalar> > x = createMember(fwdOp->domain());
141 
142  // B) Print out the specialized SolveCriteria object
143 
144  out << "\nsolveCriteria:\n" << solveCriteria;
145 
146  // ToDo: Fill in the rest of the fields!
147 
148  // C) Solve the system with the given SolveCriteria object
149 
150  const int convergenceTestFrequency = 10;
151 
152  const RCP<ParameterList> pl = Teuchos::getParametersFromXmlString(
153  "<ParameterList name=\"Belos\">"
154  " <Parameter name=\"Solver Type\" type=\"string\" value=\"Pseudo Block GMRES\"/>"
155  " <Parameter name=\"Convergence Test Frequency\" type=\"int\" value=\""+toString(convergenceTestFrequency)+"\"/>"
156  " <ParameterList name=\"Solver Types\">"
157  " <ParameterList name=\"Pseudo Block GMRES\">"
158  " <Parameter name=\"Block Size\" type=\"int\" value=\"1\"/>"
159  " <Parameter name=\"Convergence Tolerance\" type=\"double\" value=\"1e-13\"/>"
160  " <Parameter name=\"Output Frequency\" type=\"int\" value=\""+toString(convergenceTestFrequency)+"\"/>"
161  " <Parameter name=\"Show Maximum Residual Norm Only\" type=\"bool\" value=\"1\"/>"
162  " <Parameter name=\"Maximum Iterations\" type=\"int\" value=\"400\"/>"
163  " <Parameter name=\"Verbosity\" type=\"int\" value=\"1\"/>"
164  " </ParameterList>"
165  " </ParameterList>"
166  "</ParameterList>"
167  );
168  out << "\n\npl:\n" << *pl;
169 
171  lowsFactory.setParameterList(pl);
172  lowsFactory.setOStream(rcpFromRef(out));
173  //lowsFactory.setVerbLevel(Teuchos::VERB_MEDIUM);
174  lowsFactory.setVerbLevel(Teuchos::VERB_HIGH);
175 
176  // NOTE: To get Belos ouptut to be quite, turn down the Belos "Verbosity"
177  // option above. To get just the StatusTest VerboseObject output, turn up
178  // lowsFactory output level.
179 
180 
181  const RCP<LinearOpWithSolveBase<Scalar> > lows = linearOpWithSolve<Scalar>(
182  lowsFactory, fwdOp);
183 
184  V_S(x.ptr(), ST::zero());
185  SolveStatus<Scalar> solveStatus = solve<Scalar>(*lows, NOTRANS, *b, x.ptr(),
186  optInArg(solveCriteria));
187  out << "\nsolveStatus:\n" << solveStatus;
188 
189  TEST_COMPARE( solveStatus.achievedTol, <=, solveCriteria.requestedTol );
190 
191  // D) Compute the actual residual and return x and r
192 
193  const RCP<VectorBase<Scalar> > r = b->clone_v();
194  fwdOp->apply(NOTRANS, *x, r.ptr(), ST::one(), -ST::one());
195 
196  *x_out = x;
197  *r_out = r;
198 
199 }
200 
201 
202 
203 //
204 // GeneralSolveCriteriaBelosStatusTest Unit Tests
205 //
206 
207 
208 TEUCHOS_UNIT_TEST( GeneralSolveCriteriaBelosStatusTest, norm_inf_r_over_norm_inf_r0 )
209 {
210 
211  using Teuchos::outArg;
212 
213  typedef double Scalar;
214  typedef ScalarTraits<Scalar> ST;
215  typedef ST::magnitudeType ScalarMag;
216 
217  SolveCriteria<Scalar> solveCriteria;
218  solveCriteria.solveMeasureType.numerator = SOLVE_MEASURE_NORM_RESIDUAL;
219  solveCriteria.numeratorReductionFunc = createMockNormReductionFunctional<Scalar>();
220  solveCriteria.solveMeasureType.denominator = SOLVE_MEASURE_NORM_SOLUTION;
221  solveCriteria.denominatorReductionFunc = createMockMaxNormInfEpsReductionFunctional<Scalar>();
222  solveCriteria.requestedTol = 0.9;
223 
224  RCP<const VectorBase<Scalar> > x, r;
225  runGeneralSolveCriteriaBelosStatusTestCase(solveCriteria, outArg(x), outArg(r),
226  success, out);
227 
228  out << "\nChecking convergence ...\n\n";
229 
230  const ScalarMag r_nrm_inf = norm_inf(*r);
231  const ScalarMag x_nrm_inf = norm_inf(*x);
232 
233  out << "||r||inf = " << r_nrm_inf << "\n";
234  out << "||x||inf = " << x_nrm_inf << "\n";
235 
236  TEST_COMPARE( r_nrm_inf / x_nrm_inf, <=, solveCriteria.requestedTol );
237 
238 }
239 
240 
241 TEUCHOS_UNIT_TEST( GeneralSolveCriteriaBelosStatusTest, norm_inf_r_over_1 )
242 {
243 
244  using Teuchos::outArg;
245 
246  typedef double Scalar;
247  typedef ScalarTraits<Scalar> ST;
248  typedef ST::magnitudeType ScalarMag;
249 
250  SolveCriteria<Scalar> solveCriteria;
251  solveCriteria.solveMeasureType.numerator = SOLVE_MEASURE_NORM_RESIDUAL;
252  solveCriteria.numeratorReductionFunc = createMockNormReductionFunctional<Scalar>();
253  solveCriteria.solveMeasureType.denominator = SOLVE_MEASURE_ONE;
254  solveCriteria.requestedTol = 0.9;
255 
256  RCP<const VectorBase<Scalar> > x, r;
257  runGeneralSolveCriteriaBelosStatusTestCase(solveCriteria, outArg(x), outArg(r),
258  success, out);
259 
260  out << "\nChecking convergence ...\n\n";
261 
262  const ScalarMag r_nrm_inf = norm_inf(*r);
263  const ScalarMag x_nrm_inf = norm_inf(*x);
264 
265  out << "||r||inf = " << r_nrm_inf << "\n";
266  out << "||x||inf = " << x_nrm_inf << "\n";
267 
268  TEST_COMPARE( r_nrm_inf, <=, solveCriteria.requestedTol );
269 
270 }
271 
272 
273 
274 } // namespace Thyra
virtual bool isCompatibleImpl(const VectorBase< Scalar > &v) const
const std::string toString(const EAdjointEpetraOp adjointEpetraOp)
bool is_null(const boost::shared_ptr< T > &p)
RCP< MockNormInfReductionFunctional< Scalar > > createMockNormReductionFunctional()
Non-member constructor.
virtual bool isCompatibleImpl(const VectorBase< Scalar > &v) const
TEUCHOS_UNIT_TEST(EpetraOperatorWrapper, basic)
void setParameterList(Teuchos::RCP< Teuchos::ParameterList > const &paramList)
Mock max(NormInf, eps) ReductionFunctional subclass used for unit testing.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
std::string toString(const HashSet< Key > &h)
Ptr< T > ptr() const
Mock NormInf ReductionFunctional subclass used for unit testing.
LinearOpWithSolveFactoryBase subclass implemented in terms of Belos.
virtual ScalarTraits< Scalar >::magnitudeType reduceImpl(const VectorBase< Scalar > &v) const
basic_FancyOStream< char > FancyOStream
RCP< MockMaxNormInfEpsReductionFunctional< Scalar > > createMockMaxNormInfEpsReductionFunctional()
Non-member constructor.
virtual ScalarTraits< Scalar >::magnitudeType reduceImpl(const VectorBase< Scalar > &v) const
#define TEST_COMPARE(v1, comp, v2)