Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_UnitTest_NumericalUtils.cpp
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 
11 
13 
14 namespace Tempus_Unit_Test {
15 
19 
20 static double PI = M_PI;
21 static double eps = 1.0e-14;
22 
23 // ************************************************************
24 // ************************************************************
26 {
27  double zero = 0.0;
28  double one = 1.0;
29 
30  std::vector<double> scales;
31  scales.push_back(1.0);
32  scales.push_back(100.0);
33  scales.push_back(0.01);
34  scales.push_back(1.0e-28);
35  scales.push_back(1.0e+28);
36 
37  std::vector<double> numbers;
38  numbers.push_back(one);
39  numbers.push_back(PI);
40 
41  for (auto& s : scales) {
42  out << " *****************************" << std::endl;
43  out << " s, eps = " << s << ", " << eps << std::endl;
44 
45  TEST_COMPARE(approxEqual(zero * s, (zero - 10.0 * eps) * s), ==, false);
46  TEST_COMPARE(approxEqual(zero * s, (zero - eps) * s), ==, false);
47  TEST_COMPARE(approxEqual(zero * s, (zero)*s), ==, true);
48  TEST_COMPARE(approxEqual(zero * s, (zero + eps) * s), ==, false);
49  TEST_COMPARE(approxEqual(zero * s, (zero + 10.0 * eps) * s), ==, false);
50 
51  // Swap
52  TEST_COMPARE(approxEqual((zero - 10.0 * eps) * s, zero * s), ==, false);
53  TEST_COMPARE(approxEqual((zero - eps) * s, zero * s), ==, false);
54  TEST_COMPARE(approxEqual((zero)*s, zero * s), ==, true);
55  TEST_COMPARE(approxEqual((zero + eps) * s, zero * s), ==, false);
56  TEST_COMPARE(approxEqual((zero + 10.0 * eps) * s, zero * s), ==, false);
57 
58  for (auto& n : numbers) {
59  out << " n = " << n << std::endl;
60 
61  TEST_COMPARE(approxEqual(n * s, (n - 10.0 * eps) * s), ==, false);
62  TEST_COMPARE(approxEqual(n * s, (n - eps) * s), ==, true);
63  TEST_COMPARE(approxEqual(n * s, (n)*s), ==, true);
64  TEST_COMPARE(approxEqual(n * s, (n + eps) * s), ==, true);
65  TEST_COMPARE(approxEqual(n * s, (n + 10.0 * eps) * s), ==, false);
66 
67  // Swap
68  TEST_COMPARE(approxEqual((n - 10.0 * eps) * s, n * s), ==, false);
69  TEST_COMPARE(approxEqual((n - eps) * s, n * s), ==, true);
70  TEST_COMPARE(approxEqual((n)*s, n * s), ==, true);
71  TEST_COMPARE(approxEqual((n + eps) * s, n * s), ==, true);
72  TEST_COMPARE(approxEqual((n + 10.0 * eps) * s, n * s), ==, false);
73  }
74  out << " *****************************" << std::endl;
75  }
76 }
77 
78 // ************************************************************
79 // ************************************************************
81 {
82  double numTol = Tempus::numericalTol<double>();
83  double zero = 0.0;
84  double one = 1.0;
85 
86  std::vector<double> scales;
87  scales.push_back(1.0);
88  scales.push_back(100.0);
89  scales.push_back(0.01);
90  scales.push_back(1.0e-28);
91  scales.push_back(1.0e+28);
92 
93  std::vector<double> numbers;
94  numbers.push_back(zero);
95  numbers.push_back(one);
96  numbers.push_back(PI);
97 
98  for (auto& s : scales) {
99  for (auto& n : numbers) {
100  out << " *****************************" << std::endl;
101  out << " n, s, eps = " << n << ", " << s << ", " << eps << std::endl;
102 
103  TEST_COMPARE(approxEqualAbsTol(n * s, (n - 10.0 * eps) * s, numTol * s),
104  ==, false);
105  TEST_COMPARE(approxEqualAbsTol(n * s, (n - eps) * s, numTol * s), ==,
106  true);
107  TEST_COMPARE(approxEqualAbsTol(n * s, (n)*s, numTol * s), ==, true);
108  TEST_COMPARE(approxEqualAbsTol(n * s, (n + eps) * s, numTol * s), ==,
109  true);
110  TEST_COMPARE(approxEqualAbsTol(n * s, (n + 10.0 * eps) * s, numTol * s),
111  ==, false);
112 
113  // Swap
114  TEST_COMPARE(approxEqualAbsTol((n - 10.0 * eps) * s, n * s, numTol * s),
115  ==, false);
116  TEST_COMPARE(approxEqualAbsTol((n - eps) * s, n * s, numTol * s), ==,
117  true);
118  TEST_COMPARE(approxEqualAbsTol((n)*s, n * s, numTol * s), ==, true);
119  TEST_COMPARE(approxEqualAbsTol((n + eps) * s, n * s, numTol * s), ==,
120  true);
121  TEST_COMPARE(approxEqualAbsTol((n + 10.0 * eps) * s, n * s, numTol * s),
122  ==, false);
123 
124  out << " *****************************" << std::endl;
125  }
126  }
127 }
128 
129 // ************************************************************
130 // ************************************************************
132 {
133  double zero = 0.0;
134  double one = 1.0;
135 
136  std::vector<double> scales;
137  scales.push_back(1.0);
138  scales.push_back(100.0);
139  scales.push_back(0.01);
140  scales.push_back(1.0e-28);
141  scales.push_back(1.0e+28);
142 
143  std::vector<double> numbers;
144  numbers.push_back(zero);
145  numbers.push_back(one);
146  numbers.push_back(PI);
147 
148  for (auto& s : scales) {
149  for (auto& n : numbers) {
150  out << " *****************************" << std::endl;
151  out << " n, s, eps = " << n << ", " << s << ", " << eps << std::endl;
152 
153  TEST_COMPARE(approxEqualScale(n * s, (n - 10.0 * eps) * s, s), ==, false);
154  TEST_COMPARE(approxEqualScale(n * s, (n - eps) * s, s), ==, true);
155  TEST_COMPARE(approxEqualScale(n * s, (n)*s, s), ==, true);
156  TEST_COMPARE(approxEqualScale(n * s, (n + eps) * s, s), ==, true);
157  TEST_COMPARE(approxEqualScale(n * s, (n + 10.0 * eps) * s, s), ==, false);
158 
159  // Swap
160  TEST_COMPARE(approxEqualScale((n - 10.0 * eps) * s, n * s, s), ==, false);
161  TEST_COMPARE(approxEqualScale((n - eps) * s, n * s, s), ==, true);
162  TEST_COMPARE(approxEqualScale((n)*s, n * s, s), ==, true);
163  TEST_COMPARE(approxEqualScale((n + eps) * s, n * s, s), ==, true);
164  TEST_COMPARE(approxEqualScale((n + 10.0 * eps) * s, n * s, s), ==, false);
165 
166  out << " *****************************" << std::endl;
167  }
168  }
169 }
170 
171 } // namespace Tempus_Unit_Test
#define TEST_COMPARE(v1, comp, v2)
bool approxEqual(Scalar a, Scalar b, Scalar relTol=numericalTol< Scalar >())
Test if values are approximately equal within the relative tolerance.
TEUCHOS_UNIT_TEST(BackwardEuler, Default_Construction)
bool approxEqualAbsTol(Scalar a, Scalar b, Scalar absTol)
Test if values are approximately equal within the absolute tolerance.
bool approxEqualScale(Scalar a, Scalar b, Scalar scale, Scalar relTol=numericalTol< Scalar >())