Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Tempus_UnitTest_TimeEventRange.cpp
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 #include "Teuchos_UnitTestHarness.hpp"
10 #include "Teuchos_XMLParameterListHelpers.hpp"
11 #include "Teuchos_TimeMonitor.hpp"
12 #include "Teuchos_DefaultComm.hpp"
13 
14 #include "Tempus_TimeEventRange.hpp"
15 
16 #include "../TestUtils/Tempus_ConvergenceTestUtils.hpp"
17 
18 #include <cmath>
19 static double PI = M_PI;
20 
21 namespace Tempus_Unit_Test {
22 
23 using Teuchos::RCP;
24 using Teuchos::rcp;
25 using Teuchos::rcp_const_cast;
26 using Teuchos::rcp_dynamic_cast;
27 
28 
29 // ************************************************************
30 // ************************************************************
31 TEUCHOS_UNIT_TEST(TimeEventRange, Default_Construction)
32 {
33  auto te = rcp(new Tempus::TimeEventRange<double>());
34 
35  TEST_COMPARE(te->getName(), ==, "TimeEventRange");
36  te->setName("TestName");
37  TEST_COMPARE(te->getName(), ==, "TestName");
38 
39  TEST_FLOATING_EQUALITY(te->getTimeStart (), te->getDefaultTime(), 1.0e-14);
40  TEST_FLOATING_EQUALITY(te->getTimeStop (), te->getDefaultTime(), 1.0e-14);
41  TEST_FLOATING_EQUALITY(te->getTimeStride(), 0.0, 1.0e-14);
42  TEST_COMPARE(te->getNumEvents (), ==, 1);
43 
44  TEST_FLOATING_EQUALITY(te->getRelTol(), 1.0e-14, 1.0e-14);
45  TEST_COMPARE(te->getLandOnExactly(), ==, true);
46 }
47 
48 
49 // ************************************************************
50 // ************************************************************
51 TEUCHOS_UNIT_TEST(TimeEventRange, Construction_Stride)
52 {
53  auto te = rcp(new Tempus::TimeEventRange<double>(
54  "TestName", 0.0, PI, 1.0, 1.0e-14, true));
55 
56  TEST_COMPARE(te->getName(), ==, "TestName");
57 
58  // Test when everything is zero.
59  TEST_FLOATING_EQUALITY(te->getTimeStart (), 0.0 , 1.0e-14);
60  TEST_FLOATING_EQUALITY(te->getTimeStop (), PI, 1.0e-14);
61  TEST_FLOATING_EQUALITY(te->getTimeStride(), 1.0, 1.0e-14);
62  TEST_COMPARE(te->getNumEvents (), ==, 4);
63 
64  auto teRange2 = rcp(new Tempus::TimeEventRange<double>(
65  "teRange2", -PI/2.0, PI/2.0, PI/4.0, 1.0e-14, true));
66 
67  TEST_FLOATING_EQUALITY(teRange2->timeToNextEvent(0.1), PI/4.0-0.1, 1.0e-14);
68 
69 }
70 
71 
72 // ************************************************************
73 // ************************************************************
74 TEUCHOS_UNIT_TEST(TimeEventRange, Construction_NumEvents)
75 {
76  auto te = rcp(new Tempus::TimeEventRange<double>(
77  "TestName", 0.0, PI, 5, 1.0e-14, true));
78 
79  TEST_COMPARE(te->getName(), ==, "TestName");
80 
81  // Test when everything is zero.
82  TEST_FLOATING_EQUALITY(te->getTimeStart (), 0.0, 1.0e-14);
83  TEST_FLOATING_EQUALITY(te->getTimeStop (), PI, 1.0e-14);
84  TEST_FLOATING_EQUALITY(te->getTimeStride(), PI/4.0, 1.0e-14);
85  TEST_COMPARE(te->getNumEvents (), ==, 5);
86 }
87 
88 
89 // ************************************************************
90 // ************************************************************
91 TEUCHOS_UNIT_TEST(TimeEventRange, Basic_Accessors)
92 {
93  auto te = rcp(new Tempus::TimeEventRange<double>());
94 
95  te->setRelTol(0.1);
96  TEST_FLOATING_EQUALITY(te->getRelTol(), 0.1, 1.0e-14);
97  te->setRelTol(1.0e-14);
98  te->setLandOnExactly(false);
99  TEST_COMPARE(te->getLandOnExactly(), ==, false);
100  te->setLandOnExactly(true);
101 
102  // Reset start after stop.
103  te->setTimeStart(1.0);
104  TEST_FLOATING_EQUALITY(te->getTimeStart (), 1.0, 1.0e-14);
105  TEST_FLOATING_EQUALITY(te->getTimeStop (), 1.0, 1.0e-14);
106  TEST_FLOATING_EQUALITY(te->getTimeStride(), 0.0, 1.0e-14);
107  TEST_COMPARE(te->getNumEvents (), ==, 1);
108 
109  // Reset stop.
110  te->setTimeStop(4.0);
111  TEST_FLOATING_EQUALITY(te->getTimeStart (), 1.0, 1.0e-14);
112  TEST_FLOATING_EQUALITY(te->getTimeStop (), 4.0, 1.0e-14);
113  TEST_FLOATING_EQUALITY(te->getTimeStride(), 3.0, 1.0e-14);
114  TEST_COMPARE(te->getNumEvents (), ==, 2);
115 
116  // Reset stride.
117  te->setTimeStride(0.5);
118  TEST_FLOATING_EQUALITY(te->getTimeStart (), 1.0, 1.0e-14);
119  TEST_FLOATING_EQUALITY(te->getTimeStop (), 4.0, 1.0e-14);
120  TEST_FLOATING_EQUALITY(te->getTimeStride(), 0.5, 1.0e-14);
121  TEST_COMPARE(te->getNumEvents (), ==, 7);
122 }
123 
124 
125 // ************************************************************
126 // ************************************************************
127 TEUCHOS_UNIT_TEST(TimeEventRange, Stride)
128 {
129  auto te = rcp(new Tempus::TimeEventRange<double>());
130  te->setTimeStart(1.0);
131  te->setTimeStop(4.0);
132  te->setTimeStride(0.5);
133 
134  // Negative stride should be reset to stop_-start_.
135  te->setTimeStride(-0.5);
136  TEST_FLOATING_EQUALITY(te->getTimeStart (), 1.0, 1.0e-14);
137  TEST_FLOATING_EQUALITY(te->getTimeStop (), 4.0, 1.0e-14);
138  TEST_FLOATING_EQUALITY(te->getTimeStride(), 3.0, 1.0e-14);
139  TEST_COMPARE(te->getNumEvents (), ==, 2);
140 
141  // Large stride should be reset to stop_-start_.
142  te->setTimeStride(5.0);
143  TEST_FLOATING_EQUALITY(te->getTimeStart (), 1.0, 1.0e-14);
144  TEST_FLOATING_EQUALITY(te->getTimeStop (), 4.0, 1.0e-14);
145  TEST_FLOATING_EQUALITY(te->getTimeStride(), 3.0, 1.0e-14);
146  TEST_COMPARE(te->getNumEvents (), ==, 2);
147 
148  // Stride smaller than relative tolerance should be reset to stop_-start_.
149  te->setTimeStride(1.0e-14);
150  TEST_FLOATING_EQUALITY(te->getTimeStart (), 1.0, 1.0e-14);
151  TEST_FLOATING_EQUALITY(te->getTimeStop (), 4.0, 1.0e-14);
152  TEST_FLOATING_EQUALITY(te->getTimeStride(), 3.0, 1.0e-14);
153  TEST_COMPARE(te->getNumEvents (), ==, 2);
154 }
155 
156 
157 // ************************************************************
158 // ************************************************************
159 TEUCHOS_UNIT_TEST(TimeEventRange, setTimeRange)
160 {
161  auto te = rcp(new Tempus::TimeEventRange<double>());
162 
163  // Set with time range.
164  te->setTimeRange(0.0, PI, 1.0);
165  TEST_FLOATING_EQUALITY(te->getTimeStart (), 0.0, 1.0e-14);
166  TEST_FLOATING_EQUALITY(te->getTimeStop (), PI, 1.0e-14);
167  TEST_FLOATING_EQUALITY(te->getTimeStride(), 1.0, 1.0e-14);
168  TEST_COMPARE(te->getNumEvents (), ==, 4);
169 }
170 
171 
172 // ************************************************************
173 // ************************************************************
174 TEUCHOS_UNIT_TEST(TimeEventRange, isTime)
175 {
176  auto te = rcp(new Tempus::TimeEventRange<double>());
177  te->setTimeRange(0.0, PI, 1.0);
178 
179  // Test isTime.
180  // Around first event.
181  TEST_COMPARE(te->isTime(-10.0e-14), ==, false); // Just outside tolerance.
182  TEST_COMPARE(te->isTime( -1.0e-14), ==, true ); // Just inside tolerance.
183  TEST_COMPARE(te->isTime( 0.0 ), ==, true ); // Right on timeEvent.
184  TEST_COMPARE(te->isTime( 1.0e-14), ==, true ); // Just inside tolerance.
185  TEST_COMPARE(te->isTime( 10.0e-14), ==, false); // Just outside tolerance.
186 
187  // Around mid event.
188  TEST_COMPARE(te->isTime(1.0 + -10.0e-14), ==, false); // Just outside tolerance.
189  TEST_COMPARE(te->isTime(1.0 + -1.0e-14), ==, true ); // Just inside tolerance.
190  TEST_COMPARE(te->isTime(1.0 + 0.0 ), ==, true ); // Right on timeEvent.
191  TEST_COMPARE(te->isTime(1.0 + 1.0e-14), ==, true ); // Just inside tolerance.
192  TEST_COMPARE(te->isTime(1.0 + 10.0e-14), ==, false); // Just outside tolerance.
193 
194  // Around last event.
195  TEST_COMPARE(te->isTime(3.0 + -10.0e-14), ==, false); // Just outside tolerance.
196  TEST_COMPARE(te->isTime(3.0 + -1.0e-14), ==, true ); // Just inside tolerance.
197  TEST_COMPARE(te->isTime(3.0 + 0.0 ), ==, true ); // Right on timeEvent.
198  TEST_COMPARE(te->isTime(3.0 + 1.0e-14), ==, true ); // Just inside tolerance.
199  TEST_COMPARE(te->isTime(3.0 + 10.0e-14), ==, false); // Just outside tolerance.
200 }
201 
202 
203 // ************************************************************
204 // ************************************************************
205 TEUCHOS_UNIT_TEST(TimeEventRange, timeToNextEvent)
206 {
207  auto te = rcp(new Tempus::TimeEventRange<double>());
208  te->setTimeRange(0.0, PI, 1.0);
209 
210  // Test timeToNextEvent.
211  // Around first event.
212  TEST_FLOATING_EQUALITY(te->timeToNextEvent(-10.0e-14), 1.0e-13, 1.0e-14); // Just outside tolerance.
213  TEST_FLOATING_EQUALITY(te->timeToNextEvent( -1.0e-14), 1.0e-14, 1.0e-14); // Just inside tolerance.
214  TEST_FLOATING_EQUALITY(te->timeToNextEvent( 0.0 ), 0.0 , 1.0e-14); // Right on timeEvent.
215  TEST_FLOATING_EQUALITY(te->timeToNextEvent( 1.0e-14), -1.0e-14, 1.0e-14); // Just inside tolerance.
216  TEST_FLOATING_EQUALITY(te->timeToNextEvent( 10.0e-14), 1.0-1.0e-13, 1.0e-14); // Just outside tolerance.
217 
218  // Around mid event.
219  TEST_FLOATING_EQUALITY(te->timeToNextEvent(1.0+ -10.0e-14), 1.0e-13, 1.0e-02); // Just outside tolerance.
220  TEST_FLOATING_EQUALITY(te->timeToNextEvent(1.0+ -1.0e-14), 1.0e-14, 1.0e-01); // Just inside tolerance.
221  TEST_FLOATING_EQUALITY(te->timeToNextEvent(1.0+ 0.0 ), 0.0 , 1.0e-02); // Right on timeEvent.
222  TEST_FLOATING_EQUALITY(te->timeToNextEvent(1.0+ 1.0e-14), -1.0e-14, 1.0e-01); // Just inside tolerance.
223  TEST_FLOATING_EQUALITY(te->timeToNextEvent(1.0+ 10.0e-14), 1.0-1.0e-13, 1.0e-14); // Just outside tolerance.
224 
225  // Around last event.
226  TEST_FLOATING_EQUALITY(te->timeToNextEvent(3.0+ -10.0e-14), 1.0e-13, 1.0e-02); // Just outside tolerance.
227  TEST_FLOATING_EQUALITY(te->timeToNextEvent(3.0+ -1.0e-14), 1.0e-14, 1.0e-01); // Just inside tolerance.
228  TEST_FLOATING_EQUALITY(te->timeToNextEvent(3.0+ 0.0 ), 0.0 , 1.0e-02); // Right on timeEvent.
229  TEST_FLOATING_EQUALITY(te->timeToNextEvent(3.0+ 1.0e-14), -1.0e-14, 1.0e-01); // Just inside tolerance.
230  TEST_FLOATING_EQUALITY(te->timeToNextEvent(3.0+ 10.0e-14), -1.0e-13, 1.0e-02); // Just outside tolerance.
231 }
232 
233 
234 // ************************************************************
235 // ************************************************************
236 TEUCHOS_UNIT_TEST(TimeEventRange, timeOfNextEvent)
237 {
238  auto te = rcp(new Tempus::TimeEventRange<double>());
239  te->setTimeRange(0.0, PI, 1.0);
240 
241  // Test timeOfNextEvent.
242  // Around first event.
243  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(-10.0e-14), 0.0, 1.0e-14); // Just outside tolerance.
244  TEST_FLOATING_EQUALITY(te->timeOfNextEvent( -1.0e-14), 0.0, 1.0e-14); // Just inside tolerance.
245  TEST_FLOATING_EQUALITY(te->timeOfNextEvent( 0.0 ), 0.0, 1.0e-14); // Right on timeEvent.
246  TEST_FLOATING_EQUALITY(te->timeOfNextEvent( 1.0e-14), 0.0, 1.0e-14); // Just inside tolerance.
247  TEST_FLOATING_EQUALITY(te->timeOfNextEvent( 10.0e-14), 1.0, 1.0e-14); // Just outside tolerance.
248 
249  // Around mid event.
250  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(1.0+ -10.0e-14), 1.0, 1.0e-14); // Just outside tolerance.
251  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(1.0+ -1.0e-14), 1.0, 1.0e-14); // Just inside tolerance.
252  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(1.0+ 0.0 ), 1.0, 1.0e-14); // Right on timeEvent.
253  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(1.0+ 1.0e-14), 1.0, 1.0e-14); // Just inside tolerance.
254  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(1.0+ 10.0e-14), 2.0, 1.0e-14); // Just outside tolerance.
255 
256  // Around last event.
257  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(3.0+ -10.0e-14), 3.0, 1.0e-14); // Just outside tolerance.
258  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(3.0+ -1.0e-14), 3.0, 1.0e-14); // Just inside tolerance.
259  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(3.0+ 0.0 ), 3.0, 1.0e-14); // Right on timeEvent.
260  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(3.0+ 1.0e-14), 3.0, 1.0e-14); // Just inside tolerance.
261  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(3.0+ 10.0e-14), 3.0, 1.0e-14); // Just outside tolerance.
262 }
263 
264 
265 // ************************************************************
266 // ************************************************************
267 TEUCHOS_UNIT_TEST(TimeEventRange, eventInRange)
268 {
269  auto te = rcp(new Tempus::TimeEventRange<double>());
270  te->setTimeRange(0.0, PI, 1.0);
271 
272  // Test eventInRange.
273  // Right end.
274  // Around first event.
275  TEST_COMPARE(te->eventInRange(-1.0, -10.0e-14), ==, false); // Just outside tolerance.
276  TEST_COMPARE(te->eventInRange(-1.0, -1.0e-14), ==, true ); // Just inside tolerance.
277  TEST_COMPARE(te->eventInRange(-1.0, 0.0 ), ==, true ); // Right on timeEvent.
278  TEST_COMPARE(te->eventInRange(-1.0, 1.0e-14), ==, true ); // Just inside tolerance.
279  TEST_COMPARE(te->eventInRange(-1.0, 10.0e-14), ==, true ); // Just outside tolerance.
280 
281  // Around mid event.
282  TEST_COMPARE(te->eventInRange(0.5, 1.0 + -10.0e-14), ==, false); // Just outside tolerance.
283  TEST_COMPARE(te->eventInRange(0.5, 1.0 + -1.0e-14), ==, true ); // Just inside tolerance.
284  TEST_COMPARE(te->eventInRange(0.5, 1.0 + 0.0 ), ==, true ); // Right on timeEvent.
285  TEST_COMPARE(te->eventInRange(0.5, 1.0 + 1.0e-14), ==, true ); // Just inside tolerance.
286  TEST_COMPARE(te->eventInRange(0.5, 1.0 + 10.0e-14), ==, true ); // Just outside tolerance.
287 
288  // Around last event.
289  TEST_COMPARE(te->eventInRange(2.5, 3.0 + -10.0e-14), ==, false); // Just outside tolerance.
290  TEST_COMPARE(te->eventInRange(2.5, 3.0 + -1.0e-14), ==, true ); // Just inside tolerance.
291  TEST_COMPARE(te->eventInRange(2.5, 3.0 + 0.0 ), ==, true ); // Right on timeEvent.
292  TEST_COMPARE(te->eventInRange(2.5, 3.0 + 1.0e-14), ==, true ); // Just inside tolerance.
293  TEST_COMPARE(te->eventInRange(2.5, 3.0 + 10.0e-14), ==, true ); // Just outside tolerance.
294 
295  // Left end.
296  // Around first event.
297  TEST_COMPARE(te->eventInRange(-10.0e-14, 0.5), ==, true ); // Just outside tolerance.
298  TEST_COMPARE(te->eventInRange( -1.0e-14, 0.5), ==, true ); // Just inside tolerance.
299  TEST_COMPARE(te->eventInRange( 0.0 , 0.5), ==, true ); // Right on timeEvent.
300  TEST_COMPARE(te->eventInRange( 1.0e-14, 0.5), ==, true ); // Just inside tolerance.
301  TEST_COMPARE(te->eventInRange( 10.0e-14, 0.5), ==, false); // Just outside tolerance.
302 
303  // Around mid event.
304  TEST_COMPARE(te->eventInRange(1.0 + -10.0e-14, 1.5), ==, true ); // Just outside tolerance.
305  TEST_COMPARE(te->eventInRange(1.0 + -1.0e-14, 1.5), ==, true ); // Just inside tolerance.
306  TEST_COMPARE(te->eventInRange(1.0 + 0.0 , 1.5), ==, true ); // Right on timeEvent.
307  TEST_COMPARE(te->eventInRange(1.0 + 1.0e-14, 1.5), ==, true ); // Just inside tolerance.
308  TEST_COMPARE(te->eventInRange(1.0 + 10.0e-14, 1.5), ==, false); // Just outside tolerance.
309 
310  // Around last event.
311  TEST_COMPARE(te->eventInRange(3.0 + -10.0e-14, 4.0), ==, true ); // Just outside tolerance.
312  TEST_COMPARE(te->eventInRange(3.0 + -1.0e-14, 4.0), ==, true ); // Just inside tolerance.
313  TEST_COMPARE(te->eventInRange(3.0 + 0.0 , 4.0), ==, true ); // Right on timeEvent.
314  TEST_COMPARE(te->eventInRange(3.0 + 1.0e-14, 4.0), ==, true ); // Just inside tolerance.
315  TEST_COMPARE(te->eventInRange(3.0 + 10.0e-14, 4.0), ==, false); // Just outside tolerance.
316 }
317 
318 
319 } // namespace Tempus_Test
TimeEventRange specifies a start, stop and stride time.
TEUCHOS_UNIT_TEST(BackwardEuler, Default_Construction)