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 /*
2 // @HEADER
3 // ***********************************************************************
4 //
5 // Stratimikos: Thyra-based strategies for linear solvers
6 // Copyright (2006) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov)
39 //
40 // ***********************************************************************
41 // @HEADER
42 */
43 
44 #include "Thyra_BelosLinearOpWithSolveFactory.hpp"
45 #include "Thyra_LinearOpWithSolveFactoryHelpers.hpp"
46 #include "Thyra_MultiVectorStdOps.hpp"
47 #include "Thyra_VectorBase.hpp"
48 #include "Thyra_VectorStdOps.hpp"
49 #include "Thyra_EpetraLinearOp.hpp"
50 #include "EpetraExt_readEpetraLinearSystem.h"
51 #include "Epetra_SerialComm.h"
53 #include "Teuchos_toString.hpp"
54 
56 
57 
58 namespace Thyra {
59 
60 
61 //
62 // Helper code
63 //
64 
65 
66 const std::string matrixFileName = "nos1.mtx";
67 
68 
69 RCP<const LinearOpBase<double> > getFwdLinearOp()
70 {
71  static RCP<const LinearOpBase<double> > fwdLinearOp;
72  if (is_null(fwdLinearOp)) {
73  Teuchos::RCP<Epetra_CrsMatrix> epetraCrsMatrix;
74  EpetraExt::readEpetraLinearSystem( matrixFileName, Epetra_SerialComm(), &epetraCrsMatrix );
75  fwdLinearOp = epetraLinearOp(epetraCrsMatrix);
76  }
77  return fwdLinearOp;
78 }
79 
80 
82 template<class Scalar>
83 class MockNormInfReductionFunctional : public ReductionFunctional<Scalar> {
84 protected:
85 
88 
91  reduceImpl(const VectorBase<Scalar> &v) const
92  { return norm_inf(v); }
93 
95  virtual bool isCompatibleImpl( const VectorBase<Scalar> &v ) const
96  { return true; }
97 
99 
100 };
101 
102 
107 template<class Scalar>
108 RCP<MockNormInfReductionFunctional<Scalar> >
110 {
112 }
113 
114 
117 template<class Scalar>
118 class MockMaxNormInfEpsReductionFunctional : public ReductionFunctional<Scalar> {
119 protected:
120 
123 
125  virtual typename ScalarTraits<Scalar>::magnitudeType
126  reduceImpl(const VectorBase<Scalar> &v) const
127  {
128  typedef typename ScalarTraits<Scalar>::magnitudeType ScalarMag;
129  return std::max(norm_inf(v), ScalarTraits<ScalarMag>::eps());
130  }
131 
133  virtual bool isCompatibleImpl( const VectorBase<Scalar> &v ) const
134  { return true; }
135 
137 
138 };
139 
140 
145 template<class Scalar>
146 RCP<MockMaxNormInfEpsReductionFunctional<Scalar> >
148 {
150 }
151 
152 
153 template<class Scalar>
154 void runGeneralSolveCriteriaBelosStatusTestCase(
155  const SolveCriteria<Scalar> &solveCriteria,
156  const Ptr<RCP<const VectorBase<Scalar> > > &x_out,
157  const Ptr<RCP<const VectorBase<Scalar> > > &r_out,
158  bool &success,
159  FancyOStream &out
160  )
161 {
162 
163  using Teuchos::describe; using Teuchos::optInArg; using Teuchos::rcpFromRef;
164  using Teuchos::toString;
165 
166  typedef ScalarTraits<Scalar> ST;
167  typedef typename ST::magnitudeType ScalarMag;
168 
169  // A) Set up the linear system
170 
171  const RCP<const LinearOpBase<Scalar> > fwdOp = getFwdLinearOp();
172  out << "\nfwdOp = " << describe(*fwdOp, Teuchos::VERB_MEDIUM) << "\n";
173  const RCP<VectorBase<Scalar> > b = createMember(fwdOp->range());
174  V_S(b.ptr(), ST::one());
175  const RCP<VectorBase<Scalar> > x = createMember(fwdOp->domain());
176 
177  // B) Print out the specialized SolveCriteria object
178 
179  out << "\nsolveCriteria:\n" << solveCriteria;
180 
181  // ToDo: Fill in the rest of the fields!
182 
183  // C) Solve the system with the given SolveCriteria object
184 
185  const int convergenceTestFrequency = 10;
186 
187  const RCP<ParameterList> pl = Teuchos::getParametersFromXmlString(
188  "<ParameterList name=\"Belos\">"
189  " <Parameter name=\"Solver Type\" type=\"string\" value=\"Pseudo Block GMRES\"/>"
190  " <Parameter name=\"Convergence Test Frequency\" type=\"int\" value=\""+toString(convergenceTestFrequency)+"\"/>"
191  " <ParameterList name=\"Solver Types\">"
192  " <ParameterList name=\"Pseudo Block GMRES\">"
193  " <Parameter name=\"Block Size\" type=\"int\" value=\"1\"/>"
194  " <Parameter name=\"Convergence Tolerance\" type=\"double\" value=\"1e-13\"/>"
195  " <Parameter name=\"Output Frequency\" type=\"int\" value=\""+toString(convergenceTestFrequency)+"\"/>"
196  " <Parameter name=\"Show Maximum Residual Norm Only\" type=\"bool\" value=\"1\"/>"
197  " <Parameter name=\"Maximum Iterations\" type=\"int\" value=\"400\"/>"
198  " <Parameter name=\"Verbosity\" type=\"int\" value=\"1\"/>"
199  " </ParameterList>"
200  " </ParameterList>"
201  "</ParameterList>"
202  );
203  out << "\n\npl:\n" << *pl;
204 
206  lowsFactory.setParameterList(pl);
207  lowsFactory.setOStream(rcpFromRef(out));
208  //lowsFactory.setVerbLevel(Teuchos::VERB_MEDIUM);
209  lowsFactory.setVerbLevel(Teuchos::VERB_HIGH);
210 
211  // NOTE: To get Belos ouptut to be quite, turn down the Belos "Verbosity"
212  // option above. To get just the StatusTest VerboseObject output, turn up
213  // lowsFactory output level.
214 
215 
216  const RCP<LinearOpWithSolveBase<Scalar> > lows = linearOpWithSolve<Scalar>(
217  lowsFactory, fwdOp);
218 
219  V_S(x.ptr(), ST::zero());
220  SolveStatus<Scalar> solveStatus = solve<Scalar>(*lows, NOTRANS, *b, x.ptr(),
221  optInArg(solveCriteria));
222  out << "\nsolveStatus:\n" << solveStatus;
223 
224  TEST_COMPARE( solveStatus.achievedTol, <=, solveCriteria.requestedTol );
225 
226  // D) Compute the actual residual and return x and r
227 
228  const RCP<VectorBase<Scalar> > r = b->clone_v();
229  fwdOp->apply(NOTRANS, *x, r.ptr(), ST::one(), -ST::one());
230 
231  *x_out = x;
232  *r_out = r;
233 
234 }
235 
236 
237 
238 //
239 // GeneralSolveCriteriaBelosStatusTest Unit Tests
240 //
241 
242 
243 TEUCHOS_UNIT_TEST( GeneralSolveCriteriaBelosStatusTest, norm_inf_r_over_norm_inf_r0 )
244 {
245 
246  using Teuchos::outArg;
247 
248  typedef double Scalar;
249  typedef ScalarTraits<Scalar> ST;
250  typedef ST::magnitudeType ScalarMag;
251 
252  SolveCriteria<Scalar> solveCriteria;
253  solveCriteria.solveMeasureType.numerator = SOLVE_MEASURE_NORM_RESIDUAL;
254  solveCriteria.numeratorReductionFunc = createMockNormReductionFunctional<Scalar>();
255  solveCriteria.solveMeasureType.denominator = SOLVE_MEASURE_NORM_SOLUTION;
256  solveCriteria.denominatorReductionFunc = createMockMaxNormInfEpsReductionFunctional<Scalar>();
257  solveCriteria.requestedTol = 0.9;
258 
259  RCP<const VectorBase<Scalar> > x, r;
260  runGeneralSolveCriteriaBelosStatusTestCase(solveCriteria, outArg(x), outArg(r),
261  success, out);
262 
263  out << "\nChecking convergence ...\n\n";
264 
265  const ScalarMag r_nrm_inf = norm_inf(*r);
266  const ScalarMag x_nrm_inf = norm_inf(*x);
267 
268  out << "||r||inf = " << r_nrm_inf << "\n";
269  out << "||x||inf = " << x_nrm_inf << "\n";
270 
271  TEST_COMPARE( r_nrm_inf / x_nrm_inf, <=, solveCriteria.requestedTol );
272 
273 }
274 
275 
276 TEUCHOS_UNIT_TEST( GeneralSolveCriteriaBelosStatusTest, norm_inf_r_over_1 )
277 {
278 
279  using Teuchos::outArg;
280 
281  typedef double Scalar;
282  typedef ScalarTraits<Scalar> ST;
283  typedef ST::magnitudeType ScalarMag;
284 
285  SolveCriteria<Scalar> solveCriteria;
286  solveCriteria.solveMeasureType.numerator = SOLVE_MEASURE_NORM_RESIDUAL;
287  solveCriteria.numeratorReductionFunc = createMockNormReductionFunctional<Scalar>();
288  solveCriteria.solveMeasureType.denominator = SOLVE_MEASURE_ONE;
289  solveCriteria.requestedTol = 0.9;
290 
291  RCP<const VectorBase<Scalar> > x, r;
292  runGeneralSolveCriteriaBelosStatusTestCase(solveCriteria, outArg(x), outArg(r),
293  success, out);
294 
295  out << "\nChecking convergence ...\n\n";
296 
297  const ScalarMag r_nrm_inf = norm_inf(*r);
298  const ScalarMag x_nrm_inf = norm_inf(*x);
299 
300  out << "||r||inf = " << r_nrm_inf << "\n";
301  out << "||x||inf = " << x_nrm_inf << "\n";
302 
303  TEST_COMPARE( r_nrm_inf, <=, solveCriteria.requestedTol );
304 
305 }
306 
307 
308 
309 } // 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
double ST
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)