Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_Stepper_ErrorNorm_impl.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ****************************************************************************
3 // Tempus: Copyright (2017) Sandia Corporation
4 //
5 // Distributed under BSD 3-clause license (See accompanying file Copyright.txt)
6 // ****************************************************************************
7 // @HEADER
8 
9 #ifndef Tempus_Stepper_ErrorNorm_impl_hpp
10 #define Tempus_Stepper_ErrorNorm_impl_hpp
11 
13 #include "Thyra_DefaultSerialDenseLinearOpWithSolve_decl.hpp"
14 #include "Thyra_MultiVectorStdOps_decl.hpp"
15 #include "Thyra_VectorSpaceBase_decl.hpp"
16 #include "Thyra_VectorStdOps_decl.hpp"
17 
19 
20 namespace Tempus {
21 
22 template <class Scalar>
24  : relTol_(1.0e-4), abssTol_(1.0e-4)
25 {
26 }
27 
28 template <class Scalar>
30  const Scalar absTol)
31  : relTol_(relTol), abssTol_(absTol)
32 {
33 }
34 
35 template <class Scalar>
38  const Teuchos::RCP<const Thyra::VectorBase<Scalar>> &xNext,
39  const Teuchos::RCP<const Thyra::VectorBase<Scalar>> &err)
40 {
41  if (errorWeightVector_ == Teuchos::null)
42  errorWeightVector_ = Thyra::createMember(x->space());
43 
44  if (u_ == Teuchos::null) u_ = Thyra::createMember(x->space());
45 
46  if (uNext_ == Teuchos::null) uNext_ = Thyra::createMember(x->space());
47 
48  // Compute: Atol + max(|u^n|, |u^{n+1}| ) * Rtol
49  Thyra::abs(*x, u_.ptr());
50  Thyra::abs(*xNext, uNext_.ptr());
51  Thyra::pair_wise_max_update(relTol_, *u_, uNext_.ptr());
52  if (!approxZero(abssTol_)) {
53  Thyra::add_scalar(abssTol_, uNext_.ptr());
54  }
55  else {
56  Scalar absTol = Thyra::norm_2(*uNext_) * numericalTol<Scalar>();
57  if (approxZero(absTol)) absTol = numericalTol<Scalar>();
58  Thyra::add_scalar(absTol, uNext_.ptr());
59  }
60 
61  Thyra::assign(errorWeightVector_.ptr(),
63  Thyra::ele_wise_divide(Teuchos::as<Scalar>(1.0), *err, *uNext_,
64  errorWeightVector_.ptr());
65 
66  const auto space_dim = err->space()->dim();
67  Scalar err_norm = std::abs(Thyra::norm(*errorWeightVector_) / space_dim);
68  return err_norm;
69 }
70 
71 template <class Scalar>
74 {
75  if (scratchVector_ == Teuchos::null)
76  scratchVector_ = Thyra::createMember(x->space());
77 
78  Thyra::assign(scratchVector_.ptr(), *x); // | U |
79  Thyra::abs(*x, scratchVector_.ptr());
80  Thyra::Vt_S(scratchVector_.ptr(), relTol_);
81  if (!approxZero(abssTol_)) {
82  Thyra::Vp_S(scratchVector_.ptr(), abssTol_);
83  }
84  else {
85  Scalar absTol = Thyra::norm_2(*scratchVector_) * numericalTol<Scalar>();
86  if (approxZero(absTol)) absTol = numericalTol<Scalar>();
87  Thyra::add_scalar(absTol, scratchVector_.ptr());
88  }
89  Thyra::ele_wise_divide(Teuchos::as<Scalar>(1.0), *x, *scratchVector_,
90  scratchVector_.ptr());
91  Scalar err = Thyra::norm_inf(*scratchVector_);
92  return err;
93 }
94 
95 } // namespace Tempus
96 
97 #endif
Scalar computeWRMSNorm(const Teuchos::RCP< const Thyra::VectorBase< Scalar >> &x, const Teuchos::RCP< const Thyra::VectorBase< Scalar >> &xNext, const Teuchos::RCP< const Thyra::VectorBase< Scalar >> &err)
Compute the weigthed root mean square norm.
bool approxZero(Scalar value, Scalar tol=Teuchos::ScalarTraits< Scalar >::sfmin())
Test if value is approximately zero within tolerance.
Scalar errorNorm(const Teuchos::RCP< const Thyra::VectorBase< Scalar >> &x)
Compute the error Norm.