Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_NumericalUtils.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_NumericalUtils_hpp
10 #define Tempus_NumericalUtils_hpp
11 
12 #include "Teuchos_ScalarTraits.hpp"
13 
14 #include "Tempus_config.hpp"
15 
16 namespace Tempus {
17 
19 template <typename Scalar>
20 const Scalar numericalTol()
21 {
22  const Scalar numericalTol =
25  return numericalTol;
26 }
27 
29 template <typename Scalar>
30 bool approxZero(Scalar value,
32 {
34  return ST::magnitude(value) <= ST::magnitude(tol);
35 }
36 
38 template <typename Scalar>
39 bool approxEqualAbsTol(Scalar a, Scalar b, Scalar absTol)
40 {
41  return Teuchos::ScalarTraits<Scalar>::magnitude(a - b) < std::abs(absTol);
42 }
43 
45 template <typename Scalar>
46 bool approxEqual(Scalar a, Scalar b, Scalar relTol = numericalTol<Scalar>())
47 {
48  const Scalar min = std::min(std::abs(a), std::abs(b));
49  Scalar absTol = min * std::abs(relTol);
50  if (absTol == Scalar(0.0)) absTol = Teuchos::ScalarTraits<Scalar>::sfmin();
51  return approxEqualAbsTol(a, b, absTol);
52 }
53 
56 template <typename Scalar>
57 bool approxEqualScale(Scalar a, Scalar b, Scalar scale,
58  Scalar relTol = numericalTol<Scalar>())
59 {
60  return approxEqualAbsTol(a, b, scale * relTol);
61 }
62 
63 } // namespace Tempus
64 
65 #endif // Tempus_NumericalUtils_hpp
static magnitudeType eps()
const Scalar numericalTol()
Numerical Tolerance (approx. max. significant digits minus two)
static magnitudeType sfmin()
bool approxEqual(Scalar a, Scalar b, Scalar relTol=numericalTol< Scalar >())
Test if values are approximately equal within the relative tolerance.
static magnitudeType magnitude(T a)
bool approxEqualAbsTol(Scalar a, Scalar b, Scalar absTol)
Test if values are approximately equal within the absolute tolerance.
bool approxZero(Scalar value, Scalar tol=Teuchos::ScalarTraits< Scalar >::sfmin())
Test if value is approximately zero within tolerance.
bool approxEqualScale(Scalar a, Scalar b, Scalar scale, Scalar relTol=numericalTol< Scalar >())