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: Time Integration and Sensitivity Analysis Package
4 //
5 // Copyright 2017 NTESS and the Tempus contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 //@HEADER
9 
10 #ifndef Tempus_NumericalUtils_hpp
11 #define Tempus_NumericalUtils_hpp
12 
13 #include "Teuchos_ScalarTraits.hpp"
14 
15 #include "Tempus_config.hpp"
16 
17 namespace Tempus {
18 
20 template <typename Scalar>
21 const Scalar numericalTol()
22 {
23  const Scalar numericalTol =
26  return numericalTol;
27 }
28 
30 template <typename Scalar>
31 bool approxZero(Scalar value,
33 {
35  return ST::magnitude(value) <= ST::magnitude(tol);
36 }
37 
39 template <typename Scalar>
40 bool approxEqualAbsTol(Scalar a, Scalar b, Scalar absTol)
41 {
42  return Teuchos::ScalarTraits<Scalar>::magnitude(a - b) < std::abs(absTol);
43 }
44 
46 template <typename Scalar>
47 bool approxEqual(Scalar a, Scalar b, Scalar relTol = numericalTol<Scalar>())
48 {
49  const Scalar min = std::min(std::abs(a), std::abs(b));
50  Scalar absTol = min * std::abs(relTol);
51  if (absTol == Scalar(0.0)) absTol = Teuchos::ScalarTraits<Scalar>::sfmin();
52  return approxEqualAbsTol(a, b, absTol);
53 }
54 
57 template <typename Scalar>
58 bool approxEqualScale(Scalar a, Scalar b, Scalar scale,
59  Scalar relTol = numericalTol<Scalar>())
60 {
61  return approxEqualAbsTol(a, b, scale * relTol);
62 }
63 
64 } // namespace Tempus
65 
66 #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 >())