Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_UnitTest_TimeEventList.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 #include "Tempus_TimeEventList.hpp"
12 
13 static double PI = M_PI;
14 
15 namespace Tempus_Unit_Test {
16 
17 using Teuchos::RCP;
18 using Teuchos::rcp;
19 using Teuchos::rcp_const_cast;
20 using Teuchos::rcp_dynamic_cast;
21 
22 // ************************************************************
23 // ************************************************************
24 TEUCHOS_UNIT_TEST(TimeEventList, Default_Construction)
25 {
26  auto te = rcp(new Tempus::TimeEventList<double>());
27 
28  TEST_COMPARE(te->getName(), ==, "TimeEventList");
29 
30  TEST_COMPARE(te->getTimeList().size(), ==, 0);
32  te->getRelTol(), std::numeric_limits<double>::epsilon() * 100.0, 1.0e-14);
34  te->getAbsTol(), std::numeric_limits<double>::epsilon() * 100.0, 1.0e-14);
35 
36  TEST_COMPARE(te->getLandOnExactly(), ==, true);
37 
38  // Check base class defaults (functions not implemented in TimeEventList).
39  TEST_COMPARE(te->isIndex(1), ==, false);
40  TEST_COMPARE(te->indexToNextEvent(1), ==, te->getDefaultIndex());
41  TEST_COMPARE(te->indexOfNextEvent(1), ==, te->getDefaultIndex());
42  TEST_COMPARE(te->eventInRangeIndex(1, 4), ==, false);
43 }
44 
45 // ************************************************************
46 // ************************************************************
47 TEUCHOS_UNIT_TEST(TimeEventList, Full_Construction)
48 {
49  std::vector<double> testVector;
50  testVector.push_back(-1.0);
51  testVector.push_back(0.0);
52  testVector.push_back(5.0);
53  testVector.push_back(2.0);
54  testVector.push_back(PI);
55 
56  auto te = rcp(
57  new Tempus::TimeEventList<double>(testVector, "TestName", true, 1.0e-14));
58 
59  TEST_COMPARE(te->getName(), ==, "TestName");
60  TEST_FLOATING_EQUALITY(te->getRelTol(), 1.0e-14, 1.0e-14);
61  TEST_COMPARE(te->getLandOnExactly(), ==, true);
62 
63  auto testList = te->getTimeList();
64  TEST_COMPARE(testList.size(), ==, 5);
65  TEST_FLOATING_EQUALITY(testList[0], -1.0, 1.0e-14);
66  TEST_FLOATING_EQUALITY(testList[1], 0.0, 1.0e-14);
67  TEST_FLOATING_EQUALITY(testList[2], 2.0, 1.0e-14);
68  TEST_FLOATING_EQUALITY(testList[3], PI, 1.0e-14);
69  TEST_FLOATING_EQUALITY(testList[4], 5.0, 1.0e-14);
70 }
71 
72 // ************************************************************
73 // ************************************************************
74 TEUCHOS_UNIT_TEST(TimeEventList, Basic_Accessors)
75 {
76  auto te = rcp(new Tempus::TimeEventList<double>());
77 
78  te->setName("TestName");
79  TEST_COMPARE(te->getName(), ==, "TestName");
80  te->setRelTol(0.1);
81  TEST_FLOATING_EQUALITY(te->getRelTol(), 0.1, 1.0e-14);
82  te->setRelTol(1.0e-14);
83  te->setLandOnExactly(false);
84  TEST_COMPARE(te->getLandOnExactly(), ==, false);
85 
86  // Test addTime.
87  te->addTime(0.0);
88  te->addTime(PI);
89  te->addTime(-1.0);
90  te->addTime(2.0);
91  te->addTime(5.0);
92 
93  // Add times that should not be duplicated.
94  te->addTime(0.0);
95  te->addTime(PI);
96 
97  auto testList = te->getTimeList();
98  TEST_COMPARE(testList.size(), ==, 5);
99  TEST_FLOATING_EQUALITY(testList[0], -1.0, 1.0e-14);
100  TEST_FLOATING_EQUALITY(testList[1], 0.0, 1.0e-14);
101  TEST_FLOATING_EQUALITY(testList[2], 2.0, 1.0e-14);
102  TEST_FLOATING_EQUALITY(testList[3], PI, 1.0e-14);
103  TEST_FLOATING_EQUALITY(testList[4], 5.0, 1.0e-14);
104 
105  // Test that two events within relative tolerance are added or not.
106  te->addTime(2.0 + 1.0e-14);
107  TEST_COMPARE(te->getTimeList().size(), ==, 5);
108  te->addTime(2.0 + 1.0e-13);
109  TEST_COMPARE(te->getTimeList().size(), ==, 6);
110 
111  // Test setTimeList()
112  te->clearTimeList();
113  te->setTimeList(testList);
114  TEST_COMPARE(testList.size(), ==, 5);
115  TEST_FLOATING_EQUALITY(testList[0], -1.0, 1.0e-14);
116  TEST_FLOATING_EQUALITY(testList[1], 0.0, 1.0e-14);
117  TEST_FLOATING_EQUALITY(testList[2], 2.0, 1.0e-14);
118  TEST_FLOATING_EQUALITY(testList[3], PI, 1.0e-14);
119  TEST_FLOATING_EQUALITY(testList[4], 5.0, 1.0e-14);
120 }
121 
122 // ************************************************************
123 // ************************************************************
124 TEUCHOS_UNIT_TEST(TimeEventList, isTime)
125 {
126  auto te = rcp(new Tempus::TimeEventList<double>());
127  te->addTime(0.0);
128  te->addTime(PI);
129  te->addTime(-1.0);
130  te->addTime(2.0);
131  te->addTime(5.0);
132  te->setRelTol(1.0e-14);
133 
134  // Test isTime.
135  // Around first event.
136  TEST_COMPARE(te->isTime(-10.0e-14), ==, false); // Just outside tolerance.
137  TEST_COMPARE(te->isTime(-0.1e-14), ==, true); // Just inside tolerance.
138  TEST_COMPARE(te->isTime(0.0), ==, true); // Right on timeEvent.
139  TEST_COMPARE(te->isTime(0.1e-14), ==, true); // Just inside tolerance.
140  TEST_COMPARE(te->isTime(10.0e-14), ==, false); // Just outside tolerance.
141 
142  // Around mid event.
143  TEST_COMPARE(te->isTime(PI + -10.0e-14), ==,
144  false); // Just outside tolerance.
145  TEST_COMPARE(te->isTime(PI + -0.1e-14), ==, true); // Just inside tolerance.
146  TEST_COMPARE(te->isTime(PI + 0.0), ==, true); // Right on timeEvent.
147  TEST_COMPARE(te->isTime(PI + 0.1e-14), ==, true); // Just inside tolerance.
148  TEST_COMPARE(te->isTime(PI + 10.0e-14), ==,
149  false); // Just outside tolerance.
150 
151  // Around last event.
152  TEST_COMPARE(te->isTime(5.0 + -10.0e-14), ==,
153  false); // Just outside tolerance.
154  TEST_COMPARE(te->isTime(5.0 + -0.1e-14), ==, true); // Just inside tolerance.
155  TEST_COMPARE(te->isTime(5.0 + 0.0), ==, true); // Right on timeEvent.
156  TEST_COMPARE(te->isTime(5.0 + 0.1e-14), ==, true); // Just inside tolerance.
157  TEST_COMPARE(te->isTime(5.0 + 10.0e-14), ==,
158  false); // Just outside tolerance.
159 }
160 
161 // ************************************************************
162 // ************************************************************
163 TEUCHOS_UNIT_TEST(TimeEventList, timeToNextEvent)
164 {
165  std::vector<double> testList;
166  testList.push_back(0.0);
167  testList.push_back(PI);
168  testList.push_back(-1.0);
169  testList.push_back(2.0);
170  testList.push_back(5.0);
171 
172  auto te = rcp(
173  new Tempus::TimeEventList<double>(testList, "testList", true, 1.0e-14));
174 
175  // Test timeToNextEvent.
176  // Around first event.
177  TEST_FLOATING_EQUALITY(te->timeToNextEvent(-1.0 + -10.0e-14), 1.0e-13,
178  1.0e-02);
179  TEST_FLOATING_EQUALITY(te->timeToNextEvent(-1.0 + -0.1e-14), 1.0 + 0.1e-14,
180  1.0e-01);
181  TEST_FLOATING_EQUALITY(te->timeToNextEvent(-1.0 + 0.0), 1.0 + 0.0, 1.0e-02);
182  TEST_FLOATING_EQUALITY(te->timeToNextEvent(-1.0 + 0.1e-14), 1.0 - 0.1e-14,
183  1.0e-02);
184  TEST_FLOATING_EQUALITY(te->timeToNextEvent(-1.0 + 10.0e-14), 1.0 - 1.0e-13,
185  1.0e-14);
186 
187  // Around mid event.
188  TEST_FLOATING_EQUALITY(te->timeToNextEvent(PI + -10.0e-14), 1.0e-13, 1.0e-02);
189  TEST_FLOATING_EQUALITY(te->timeToNextEvent(PI + -0.1e-14), 5.0 - PI + 0.1e-14,
190  1.0e-01);
191  TEST_FLOATING_EQUALITY(te->timeToNextEvent(PI + 0.0), 5.0 - PI + 0.0,
192  1.0e-02);
193  TEST_FLOATING_EQUALITY(te->timeToNextEvent(PI + 0.1e-14), 5.0 - PI - 0.1e-14,
194  1.0e-01);
195  TEST_FLOATING_EQUALITY(te->timeToNextEvent(PI + 10.0e-14), 5.0 - PI - 1.0e-13,
196  1.0e-14);
197 
198  // Around last event.
199  TEST_FLOATING_EQUALITY(te->timeToNextEvent(5.0 + -10.0e-14), 1.0e-13,
200  1.0e-02);
201  TEST_FLOATING_EQUALITY(te->timeToNextEvent(5.0 + -0.1e-14),
202  te->getDefaultTime(), 1.0e-01);
203  TEST_FLOATING_EQUALITY(te->timeToNextEvent(5.0 + 0.0), te->getDefaultTime(),
204  1.0e-02);
205  TEST_FLOATING_EQUALITY(te->timeToNextEvent(5.0 + 0.1e-14),
206  te->getDefaultTime(), 1.0e-01);
207  TEST_FLOATING_EQUALITY(te->timeToNextEvent(5.0 + 10.0e-14),
208  te->getDefaultTime(), 1.0e-02);
209 }
210 
211 // ************************************************************
212 // ************************************************************
213 TEUCHOS_UNIT_TEST(TimeEventList, timeOfNextEvent)
214 {
215  std::vector<double> testList;
216  testList.push_back(0.0);
217  testList.push_back(PI);
218  testList.push_back(-1.0);
219  testList.push_back(2.0);
220  testList.push_back(5.0);
221 
222  auto te = rcp(
223  new Tempus::TimeEventList<double>(testList, "testList", true, 1.0e-14));
224 
225  // Test timeOfNextEvent.
226  // Around first event.
227  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(-1.0 + -10.0e-14), -1.0, 1.0e-14);
228  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(-1.0 + -0.1e-14), 0.0, 1.0e-14);
229  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(-1.0 + 0.0), 0.0, 1.0e-14);
230  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(-1.0 + 0.1e-14), 0.0, 1.0e-14);
231  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(-1.0 + 10.0e-14), 0.0, 1.0e-14);
232 
233  // Around mid event.
234  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(2.0 + -10.0e-14), 2.0, 1.0e-14);
235  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(2.0 + -0.1e-14), PI, 1.0e-14);
236  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(2.0 + 0.0), PI, 1.0e-14);
237  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(2.0 + 0.1e-14), PI, 1.0e-14);
238  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(2.0 + 10.0e-14), PI, 1.0e-14);
239 
240  // Around last event.
241  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(5.0 + -10.0e-14), 5.0, 1.0e-14);
242  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(5.0 + -0.1e-14),
243  te->getDefaultTime(), 1.0e-14);
244  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(5.0 + 0.0), te->getDefaultTime(),
245  1.0e-14);
246  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(5.0 + 0.1e-14),
247  te->getDefaultTime(), 1.0e-14);
248  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(5.0 + 10.0e-14),
249  te->getDefaultTime(), 1.0e-14);
250 }
251 
252 // ************************************************************
253 // ************************************************************
254 TEUCHOS_UNIT_TEST(TimeEventList, eventInRange)
255 {
256  std::vector<double> testList;
257  testList.push_back(0.0);
258  testList.push_back(PI);
259  testList.push_back(-1.0);
260  testList.push_back(2.0);
261  testList.push_back(5.0);
262 
263  auto te = rcp(
264  new Tempus::TimeEventList<double>(testList, "testList", true, 1.0e-14));
265 
266  // Test eventInRange.
267  // Right end.
268  TEST_COMPARE(te->eventInRange(-2.0, -1.0 + -10.0e-14), ==,
269  false); // Around first event.
270  TEST_COMPARE(te->eventInRange(-2.0, -1.0 + -0.1e-14), ==, true);
271  TEST_COMPARE(te->eventInRange(-2.0, -1.0 + 0.0), ==, true);
272  TEST_COMPARE(te->eventInRange(-2.0, -1.0 + 0.1e-14), ==, true);
273  TEST_COMPARE(te->eventInRange(-2.0, -1.0 + 10.0e-14), ==, true);
274 
275  TEST_COMPARE(te->eventInRange(3.0, PI + -10.0e-14), ==,
276  false); // Around mid event.
277  TEST_COMPARE(te->eventInRange(3.0, PI + -0.1e-14), ==, true);
278  TEST_COMPARE(te->eventInRange(3.0, PI + 0.0), ==, true);
279  TEST_COMPARE(te->eventInRange(3.0, PI + 0.1e-14), ==, true);
280  TEST_COMPARE(te->eventInRange(3.0, PI + 10.0e-14), ==, true);
281 
282  TEST_COMPARE(te->eventInRange(4.5, 5.0 + -10.0e-14), ==,
283  false); // Around last event.
284  TEST_COMPARE(te->eventInRange(4.5, 5.0 + -0.1e-14), ==, true);
285  TEST_COMPARE(te->eventInRange(4.5, 5.0 + 0.0), ==, true);
286  TEST_COMPARE(te->eventInRange(4.5, 5.0 + 0.1e-14), ==, true);
287  TEST_COMPARE(te->eventInRange(4.5, 5.0 + 10.0e-14), ==, true);
288 
289  // Left end.
290  TEST_COMPARE(te->eventInRange(-1.0 + -10.0e-14, -0.5), ==,
291  true); // Around first event.
292  TEST_COMPARE(te->eventInRange(-1.0 + -0.1e-14, -0.5), ==, false);
293  TEST_COMPARE(te->eventInRange(-1.0 + 0.0, -0.5), ==, false);
294  TEST_COMPARE(te->eventInRange(-1.0 + 0.1e-14, -0.5), ==, false);
295  TEST_COMPARE(te->eventInRange(-1.0 + 10.0e-14, -0.5), ==, false);
296 
297  TEST_COMPARE(te->eventInRange(PI + -10.0e-14, 3.5), ==,
298  true); // Around mid event.
299  TEST_COMPARE(te->eventInRange(PI + -0.1e-14, 3.5), ==, false);
300  TEST_COMPARE(te->eventInRange(PI + 0.0, 3.5), ==, false);
301  TEST_COMPARE(te->eventInRange(PI + 0.1e-14, 3.5), ==, false);
302  TEST_COMPARE(te->eventInRange(PI + 10.0e-14, 3.5), ==, false);
303 
304  TEST_COMPARE(te->eventInRange(5.0 + -10.0e-14, 6.0), ==,
305  true); // Around last event.
306  TEST_COMPARE(te->eventInRange(5.0 + -0.1e-14, 6.0), ==, false);
307  TEST_COMPARE(te->eventInRange(5.0 + 0.0, 6.0), ==, false);
308  TEST_COMPARE(te->eventInRange(5.0 + 0.1e-14, 6.0), ==, false);
309  TEST_COMPARE(te->eventInRange(5.0 + 10.0e-14, 6.0), ==, false);
310 }
311 
312 // ************************************************************
313 // ************************************************************
314 TEUCHOS_UNIT_TEST(TimeEventList, getValidParameters)
315 {
316  auto tel = rcp(new Tempus::TimeEventList<double>());
317 
318  auto pl = tel->getValidParameters();
319 
320  TEST_COMPARE(pl->get<std::string>("Type"), ==, "List");
321  TEST_COMPARE(pl->get<std::string>("Name"), ==, "TimeEventList");
322  TEST_FLOATING_EQUALITY(pl->get<double>("Relative Tolerance"),
323  std::numeric_limits<double>::epsilon() * 100.0,
324  1.0e-14);
325  TEST_COMPARE(pl->get<bool>("Land On Exactly"), ==, true);
326  TEST_COMPARE(pl->get<std::string>("Time List"), ==, "");
327 
328  { // Ensure that parameters are "used", excluding sublists.
329  std::ostringstream unusedParameters;
330  pl->unused(unusedParameters);
331  TEST_COMPARE(unusedParameters.str(), ==, "");
332  }
333 }
334 
335 // ************************************************************
336 // ************************************************************
338 {
339  // Construct parameterList similar to getValidParameters().
341  Teuchos::parameterList("Time Event List");
342 
343  pl->set("Name", "Unit Test Time Event List");
344  pl->set("Type", "List");
345  pl->set("Relative Tolerance", 1.0e-10);
346  pl->set("Land On Exactly", false);
347 
348  std::vector<double> times;
349  times.push_back(-0.1);
350  times.push_back(0.1);
351  times.push_back(0.5);
352  times.push_back(1.1);
353  std::ostringstream list;
354  for (std::size_t i = 0; i < times.size() - 1; ++i) list << times[i] << ", ";
355  list << times[times.size() - 1];
356  pl->set<std::string>("Time List", list.str());
357 
358  // Construct TimeEventList from ParameterList.
359  auto tel = Tempus::createTimeEventList<double>(pl);
360 
361  tel->describe(out, Teuchos::VERB_EXTREME);
362 
363  TEST_COMPARE(tel->getName(), ==, "Unit Test Time Event List");
364  TEST_COMPARE(tel->getType(), ==, "List");
365  TEST_FLOATING_EQUALITY(tel->getRelTol(), 1.0e-10, 1.0e-14);
366  TEST_COMPARE(tel->getLandOnExactly(), ==, false);
367  auto teList = tel->getTimeList();
368  TEST_FLOATING_EQUALITY(teList[0], -0.1, 1.0e-14);
369  TEST_FLOATING_EQUALITY(teList[1], 0.1, 1.0e-14);
370  TEST_FLOATING_EQUALITY(teList[2], 0.5, 1.0e-14);
371  TEST_FLOATING_EQUALITY(teList[3], 1.1, 1.0e-14);
372 }
373 
374 } // namespace Tempus_Unit_Test
#define TEST_COMPARE(v1, comp, v2)
#define TEST_FLOATING_EQUALITY(v1, v2, tol)
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
TEUCHOS_UNIT_TEST(BackwardEuler, Default_Construction)
TimeEventList specifies a list of time events.
static double PI
Teuchos::RCP< TimeEventList< Scalar > > createTimeEventList(Teuchos::RCP< Teuchos::ParameterList > pList)
Nonmember Constructor via ParameterList.