Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Tempus_UnitTest_TimeEventComposite.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 
15 #include "Tempus_TimeEventRange.hpp"
16 #include "Tempus_TimeEventRangeIndex.hpp"
17 #include "Tempus_TimeEventList.hpp"
18 #include "Tempus_TimeEventListIndex.hpp"
19 
20 
21 #include "../TestUtils/Tempus_ConvergenceTestUtils.hpp"
22 
23 #include <cmath>
24 static double PI = M_PI;
25 
26 namespace Tempus_Unit_Test {
27 
28 using Teuchos::RCP;
29 using Teuchos::rcp;
30 using Teuchos::rcp_const_cast;
31 using Teuchos::rcp_dynamic_cast;
32 
33 
34 // TimeEventRanges for testing.
35 // ------------------------------------------------------------
36 Teuchos::RCP<Tempus::TimeEventRange<double> > getTestRange1()
37 {
38  auto teRange1 = rcp(new Tempus::TimeEventRange<double>(
39  "teRange1", 0.0, PI, 1.0, 1.0e-14, true));
40  return teRange1;
41 }
42 
43 Teuchos::RCP<Tempus::TimeEventRange<double> > getTestRange2()
44 {
45  auto teRange2 = rcp(new Tempus::TimeEventRange<double>(
46  "teRange2", -PI/2.0, PI/2.0, PI/4.0, 1.0e-14, true));
47  return teRange2;
48 }
49 
50 Teuchos::RCP<Tempus::TimeEventRange<double> > getTestRange3()
51 {
52  auto teRange3 = rcp(new Tempus::TimeEventRange<double>(
53  "teRange3", 4.0, 10.0, 4.0, 1.0e-14, true));
54  return teRange3;
55 }
56 
57 
58 // TimeEventLists for testing.
59 // ------------------------------------------------------------
60 Teuchos::RCP<Tempus::TimeEventList<double> > getTestList1()
61 {
62  std::vector<double> testList1;
63  testList1.push_back(-1.0);
64  testList1.push_back( 0.0);
65  testList1.push_back( 5.0);
66  testList1.push_back( 2.0);
67  testList1.push_back( PI);
68 
69  auto teList1 = rcp(new Tempus::TimeEventList<double>(
70  "teList1", testList1, 1.0e-14, true));
71  return teList1;
72 }
73 
74 Teuchos::RCP<Tempus::TimeEventList<double> > getTestList2()
75 {
76  std::vector<double> testList2;
77  testList2.push_back(-0.5);
78  testList2.push_back( 1.25);
79  testList2.push_back( 4.95);
80  testList2.push_back(12.34);
81 
82  auto teList2 = rcp(new Tempus::TimeEventList<double>(
83  "teList2", testList2, 1.0e-14, true));
84  return teList2;
85 }
86 
87 Teuchos::RCP<Tempus::TimeEventList<double> > getTestList3()
88 {
89  std::vector<double> testList3;
90  testList3.push_back(-5.0);
91  testList3.push_back(-PI);
92 
93  auto teList3 = rcp(new Tempus::TimeEventList<double>(
94  "teList3", testList3, 1.0e-14, true));
95  return teList3;
96 }
97 
98 
99 // TimeEventRangeIndices for testing.
100 // ------------------------------------------------------------
101 Teuchos::RCP<Tempus::TimeEventRangeIndex<double> > getTestRangeIndex1()
102 {
103  auto teRangeIndex1 = rcp(new Tempus::TimeEventRangeIndex<double>(
104  "teRangeIndex1", -1, 10, 3));
105  return teRangeIndex1;
106 }
107 
108 Teuchos::RCP<Tempus::TimeEventRangeIndex<double> > getTestRangeIndex2()
109 {
110  auto teRangeIndex2 = rcp(new Tempus::TimeEventRangeIndex<double>(
111  "teRangeIndex2", -5, 8, 4));
112  return teRangeIndex2;
113 }
114 
115 Teuchos::RCP<Tempus::TimeEventRangeIndex<double> > getTestRangeIndex3()
116 {
117  auto teRangeIndex3 = rcp(new Tempus::TimeEventRangeIndex<double>(
118  "teRangeIndex3", 10, 17, 5));
119  return teRangeIndex3;
120 }
121 
122 
123 // TimeEventRangeIndices for testing.
124 // ------------------------------------------------------------
125 Teuchos::RCP<Tempus::TimeEventListIndex<double> > getTestListIndex1()
126 {
127  std::vector<int> testListIndex1;
128  testListIndex1.push_back(-2);
129  testListIndex1.push_back( 0);
130  testListIndex1.push_back( 7);
131  testListIndex1.push_back( 3);
132  testListIndex1.push_back(-5);
133 
134  auto teListIndex1 = rcp(new Tempus::TimeEventListIndex<double>(
135  "teListIndex1", testListIndex1));
136  return teListIndex1;
137 }
138 
139 Teuchos::RCP<Tempus::TimeEventListIndex<double> > getTestListIndex2()
140 {
141  std::vector<int> testListIndex2;
142  testListIndex2.push_back( 2);
143 
144  auto teListIndex2 = rcp(new Tempus::TimeEventListIndex<double>(
145  "teListIndex2", testListIndex2));
146  return teListIndex2;
147 }
148 
149 Teuchos::RCP<Tempus::TimeEventListIndex<double> > getTestListIndex3()
150 {
151  std::vector<int> testListIndex3;
152  testListIndex3.push_back(14);
153  testListIndex3.push_back( 9);
154 
155  auto teListIndex3 = rcp(new Tempus::TimeEventListIndex<double>(
156  "teListIndex3", testListIndex3));
157 
158  return teListIndex3;
159 }
160 
161 
162 // ************************************************************
163 // ************************************************************
164 TEUCHOS_UNIT_TEST(TimeEventComposite, Default_Construction)
165 {
166  auto te = rcp(new Tempus::TimeEventComposite<double>());
167 
168  TEST_COMPARE(te->isTime(0.0), ==, false);
169  TEST_FLOATING_EQUALITY(te->timeToNextEvent(0.0), te->getDefaultTime(), 1.0e-14);
170  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(0.0), te->getDefaultTime(), 1.0e-14);
171  TEST_COMPARE(te->eventInRange(0.0, 1.0), ==, false);
172 
173  TEST_COMPARE(te->isIndex(0), ==, false);
174  TEST_COMPARE(te->indexToNextEvent(0), ==, te->getDefaultIndex());
175  TEST_COMPARE(te->indexOfNextEvent(0), ==, te->getDefaultIndex());
176  TEST_COMPARE(te->eventInRange(0, 10), ==, false);
177 }
178 
179 
180 // ************************************************************
181 // ************************************************************
182 TEUCHOS_UNIT_TEST(TimeEventComposite, One_TimeEventRange)
183 {
184  auto te = rcp(new Tempus::TimeEventComposite<double>());
185  auto teRange1 = getTestRange1();
186 
187  te->addTimeEvent(teRange1);
188 
189  TEST_COMPARE(te->isTime(0.0), ==, true );
190  TEST_FLOATING_EQUALITY(te->timeToNextEvent(0.0), 0.0, 1.0e-14);
191  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(1.1), 2.0, 1.0e-14);
192  TEST_COMPARE(te->eventInRange(0.0, 1.0), ==, true );
193 }
194 
195 
196 // ************************************************************
197 // ************************************************************
198 TEUCHOS_UNIT_TEST(TimeEventComposite, TwoOverlapping_TimeEventRanges)
199 {
200  auto te = rcp(new Tempus::TimeEventComposite<double>());
201  auto teRange1 = getTestRange1();
202  auto teRange2 = getTestRange2();
203 
204  te->addTimeEvent(teRange1);
205  te->addTimeEvent(teRange2);
206  //te->describe();
207 
208  TEST_COMPARE(te->isTime(0.0), ==, true );
209  TEST_FLOATING_EQUALITY(te->timeToNextEvent(0.1), PI/4.0-0.1, 1.0e-14);
210  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(1.1), PI/2.0, 1.0e-14);
211  TEST_COMPARE(te->eventInRange(0.0, 1.0), ==, true );
212 }
213 
214 
215 // ************************************************************
216 // ************************************************************
217 TEUCHOS_UNIT_TEST(TimeEventComposite, TwoSeparated_TimeEventRanges)
218 {
219  auto te = rcp(new Tempus::TimeEventComposite<double>());
220  auto teRange3 = getTestRange3();
221  auto teRange2 = getTestRange2();
222 
223  te->addTimeEvent(teRange3);
224  te->addTimeEvent(teRange2);
225  //te->describe();
226 
227  TEST_COMPARE(te->isTime(4.0), ==, true );
228  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(1.1), PI/2.0, 1.0e-14);
229  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(3.0), 4.0, 1.0e-14);
230  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(5.0), 8.0, 1.0e-14);
231  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(10.0), 8.0, 1.0e-14);
232  TEST_COMPARE(te->eventInRange(-3.0, -2.0), ==, false);
233  TEST_COMPARE(te->eventInRange(-1.0, 0.0), ==, true );
234  TEST_COMPARE(te->eventInRange( 1.0, 2.0), ==, true );
235  TEST_COMPARE(te->eventInRange( 2.0, 3.0), ==, false);
236  TEST_COMPARE(te->eventInRange( 5.0, 7.0), ==, false);
237  TEST_COMPARE(te->eventInRange( 7.0, 9.0), ==, true );
238  TEST_COMPARE(te->eventInRange( 9.0, 11.0), ==, false);
239 }
240 
241 
242 // ************************************************************
243 // ************************************************************
244 TEUCHOS_UNIT_TEST(TimeEventComposite, One_TimeEventList)
245 {
246  auto te = rcp(new Tempus::TimeEventComposite<double>());
247  auto teList1 = getTestList1();
248 
249  te->addTimeEvent(teList1);
250 
251  TEST_COMPARE(te->isTime(2.0), ==, true );
252  TEST_FLOATING_EQUALITY(te->timeToNextEvent(3.5), 1.5, 1.0e-14);
253  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(-2.0), -1.0, 1.0e-14);
254  TEST_COMPARE(te->eventInRange(4.99, 10.0), ==, true );
255 }
256 
257 
258 // ************************************************************
259 // ************************************************************
260 TEUCHOS_UNIT_TEST(TimeEventComposite, TwoOverlapping_TimeEventLists)
261 {
262  auto te = rcp(new Tempus::TimeEventComposite<double>());
263  auto teList1 = getTestList1();
264  auto teList2 = getTestList2();
265 
266  te->addTimeEvent(teList1);
267  te->addTimeEvent(teList2);
268  //te->describe();
269 
270  TEST_COMPARE(te->isTime(1.25), ==, true );
271  TEST_FLOATING_EQUALITY(te->timeToNextEvent(4.0), 0.95, 1.0e-14);
272  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(6.5), 12.34, 1.0e-14);
273  TEST_COMPARE(te->eventInRange(0.1, 1.0), ==, false);
274 }
275 
276 
277 // ************************************************************
278 // ************************************************************
279 TEUCHOS_UNIT_TEST(TimeEventComposite, TwoSeparated_TimeEventLists)
280 {
281  auto te = rcp(new Tempus::TimeEventComposite<double>());
282  auto teList3 = getTestList3();
283  auto teList2 = getTestList2();
284 
285  te->addTimeEvent(teList3);
286  te->addTimeEvent(teList2);
287  //te->describe();
288 
289  TEST_COMPARE(te->isTime(4.0), ==, false);
290  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(-8.9), -5.0, 1.0e-14);
291  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(-0.3), 1.25, 1.0e-14);
292  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(-4.0), -PI, 1.0e-14);
293  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(20.0), 12.34, 1.0e-14);
294  TEST_COMPARE(te->eventInRange(-6.0, -4.0), ==, true );
295  TEST_COMPARE(te->eventInRange(-3.0, 0.0), ==, true );
296  TEST_COMPARE(te->eventInRange( 2.0, 3.0), ==, false);
297  TEST_COMPARE(te->eventInRange( 4.9, 5.1), ==, true );
298  TEST_COMPARE(te->eventInRange(12.0, 12.4), ==, true );
299  TEST_COMPARE(te->eventInRange(14.0, 15.0), ==, false);
300 }
301 
302 
303 // ************************************************************
304 // ************************************************************
305 TEUCHOS_UNIT_TEST(TimeEventComposite, One_TimeEventRangeIndex)
306 {
307  auto te = rcp(new Tempus::TimeEventComposite<double>());
308  auto teRangeIndex1 = getTestRangeIndex1();
309 
310  te->addTimeEvent(teRangeIndex1);
311 
312  TEST_COMPARE(te->isIndex(5), ==, true );
313  TEST_COMPARE(te->indexToNextEvent(3), ==, 2);
314  TEST_COMPARE(te->indexOfNextEvent(3), ==, 5);
315  TEST_COMPARE(te->eventInRangeIndex(3, 9), ==, true );
316 }
317 
318 
319 // ************************************************************
320 // ************************************************************
321 TEUCHOS_UNIT_TEST(TimeEventComposite, TwoOverlapping_TimeEventRangeIndex)
322 {
323  auto te = rcp(new Tempus::TimeEventComposite<double>());
324  auto teRangeIndex1 = getTestRangeIndex1();
325  auto teRangeIndex2 = getTestRangeIndex2();
326 
327  te->addTimeEvent(teRangeIndex1);
328  te->addTimeEvent(teRangeIndex2);
329  //te->describe();
330 
331  TEST_COMPARE(te->isIndex(-1), ==, true );
332  TEST_COMPARE(te->indexToNextEvent(-2), ==, 1);
333  TEST_COMPARE(te->indexOfNextEvent( 2), ==, 2);
334  TEST_COMPARE(te->eventInRangeIndex(0, 1), ==, false);
335 }
336 
337 
338 // ************************************************************
339 // ************************************************************
340 TEUCHOS_UNIT_TEST(TimeEventComposite, TwoSeparate_TimeEventRangeIndex)
341 {
342  auto te = rcp(new Tempus::TimeEventComposite<double>());
343  auto teRangeIndex3 = getTestRangeIndex3();
344  auto teRangeIndex2 = getTestRangeIndex2();
345 
346  te->addTimeEvent(teRangeIndex3);
347  te->addTimeEvent(teRangeIndex2);
348  //te->describe();
349 
350  TEST_COMPARE(te->isIndex(15), ==, true );
351  TEST_COMPARE(te->indexOfNextEvent( 9), ==, 10);
352  TEST_COMPARE(te->indexOfNextEvent(-6), ==, -5);
353  TEST_COMPARE(te->indexOfNextEvent( 6), ==, 7);
354  TEST_COMPARE(te->indexOfNextEvent(16), ==, 15);
355  TEST_COMPARE(te->eventInRangeIndex(-3, -2), ==, false);
356  TEST_COMPARE(te->eventInRangeIndex(-1, 0), ==, true );
357  TEST_COMPARE(te->eventInRangeIndex( 1, 2), ==, false);
358  TEST_COMPARE(te->eventInRangeIndex( 7, 8), ==, true );
359  TEST_COMPARE(te->eventInRangeIndex( 8, 9), ==, false);
360  TEST_COMPARE(te->eventInRangeIndex(10, 13), ==, true );
361  TEST_COMPARE(te->eventInRangeIndex(14, 20), ==, true );
362 }
363 
364 
365 // ************************************************************
366 // ************************************************************
367 TEUCHOS_UNIT_TEST(TimeEventComposite, One_TimeEventListIndex)
368 {
369  auto te = rcp(new Tempus::TimeEventComposite<double>());
370  auto teListIndex1 = getTestListIndex1();
371 
372  te->addTimeEvent(teListIndex1);
373 
374  TEST_COMPARE(te->isIndex(3), ==, true );
375  TEST_COMPARE(te->indexToNextEvent(1), ==, 2);
376  TEST_COMPARE(te->indexOfNextEvent(4), ==, 7);
377  TEST_COMPARE(te->eventInRangeIndex(1, 3), ==, true );
378 }
379 
380 
381 // ************************************************************
382 // ************************************************************
383 TEUCHOS_UNIT_TEST(TimeEventComposite, TwoOverlapping_TimeEventListIndex)
384 {
385  auto te = rcp(new Tempus::TimeEventComposite<double>());
386  auto teListIndex1 = getTestListIndex1();
387  auto teListIndex2 = getTestListIndex2();
388 
389  te->addTimeEvent(teListIndex1);
390  te->addTimeEvent(teListIndex2);
391  //te->describe();
392 
393  TEST_COMPARE(te->isIndex(2), ==, true );
394  TEST_COMPARE(te->indexToNextEvent(0), ==, 0);
395  TEST_COMPARE(te->indexOfNextEvent(1), ==, 2);
396  TEST_COMPARE(te->eventInRangeIndex(-1, 3), ==, true );
397 }
398 
399 
400 // ************************************************************
401 // ************************************************************
402 TEUCHOS_UNIT_TEST(TimeEventComposite, TwoSeparate_TimeEventListIndex)
403 {
404  auto te = rcp(new Tempus::TimeEventComposite<double>());
405  auto teListIndex3 = getTestListIndex3();
406  auto teListIndex2 = getTestListIndex2();
407 
408  te->addTimeEvent(teListIndex3);
409  te->addTimeEvent(teListIndex2);
410  //te->describe();
411 
412  TEST_COMPARE(te->isIndex(14), ==, true );
413  TEST_COMPARE(te->indexOfNextEvent(2), ==, 2);
414  TEST_COMPARE(te->indexOfNextEvent(5), ==, 9);
415  TEST_COMPARE(te->indexOfNextEvent(19), ==, 14);
416  TEST_COMPARE(te->eventInRangeIndex( 0, 1), ==, false);
417  TEST_COMPARE(te->eventInRangeIndex( 3, 10), ==, true );
418  TEST_COMPARE(te->eventInRangeIndex(15, 20), ==, false);
419 }
420 
421 
422 // ************************************************************
423 // ************************************************************
424 TEUCHOS_UNIT_TEST(TimeEventComposite, OneOfEach_TimeEvent)
425 {
426  auto te = rcp(new Tempus::TimeEventComposite<double>());
427  auto teRange1 = getTestRange1();
428  auto teList1 = getTestList1();
429  auto teRangeIndex1 = getTestRangeIndex1();
430  auto teListIndex1 = getTestListIndex1();
431 
432  te->addTimeEvent(teRange1);
433  te->addTimeEvent(teList1);
434  te->addTimeEvent(teRangeIndex1);
435  te->addTimeEvent(teListIndex1);
436 
437  TEST_COMPARE(te->isTime (3.0), ==, true );
438  TEST_COMPARE(te->isTime (2.0), ==, true );
439  TEST_COMPARE(te->isIndex( 2), ==, true );
440  TEST_COMPARE(te->isIndex( 3), ==, true );
441 
442  TEST_FLOATING_EQUALITY(te->timeToNextEvent(-2.5), 1.5, 1.0e-14);
443  TEST_FLOATING_EQUALITY(te->timeToNextEvent( 0.5), 0.5, 1.0e-14);
444  TEST_FLOATING_EQUALITY(te->timeToNextEvent( 4.5), 0.5, 1.0e-14);
445  TEST_FLOATING_EQUALITY(te->timeToNextEvent( 7.5), -2.5, 1.0e-14);
446 
447  TEST_COMPARE(te->indexToNextEvent(-6), ==, 1);
448  TEST_COMPARE(te->indexToNextEvent( 1), ==, 1);
449  TEST_COMPARE(te->indexToNextEvent( 7), ==, 0);
450  TEST_COMPARE(te->indexToNextEvent( 9), ==, -1);
451 
452  TEST_FLOATING_EQUALITY(te->timeOfNextEvent( -PI), -1.0, 1.0e-14);
453  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(-0.5), 0.0, 1.0e-14);
454  TEST_FLOATING_EQUALITY(te->timeOfNextEvent( 2.5), 3.0, 1.0e-14);
455  TEST_FLOATING_EQUALITY(te->timeOfNextEvent( 7.5), 5.0, 1.0e-14);
456 
457  TEST_COMPARE(te->indexOfNextEvent(-6), ==, -5);
458  TEST_COMPARE(te->indexOfNextEvent( 1), ==, 2);
459  TEST_COMPARE(te->indexOfNextEvent( 7), ==, 7);
460  TEST_COMPARE(te->indexOfNextEvent( 9), ==, 8);
461 
462  TEST_COMPARE(te->eventInRange(-5.0, -2.0), ==, false);
463  TEST_COMPARE(te->eventInRange(-2.0, -0.5), ==, true );
464  TEST_COMPARE(te->eventInRange( 1.2, 1.8), ==, false);
465  TEST_COMPARE(te->eventInRange( 3.1, 4.0), ==, true );
466  TEST_COMPARE(te->eventInRange( 4.5, 6.0), ==, true );
467 
468  TEST_COMPARE(te->eventInRangeIndex(-8, -6), ==, false);
469  TEST_COMPARE(te->eventInRangeIndex( 1, 1), ==, false);
470  TEST_COMPARE(te->eventInRangeIndex( 5, 7), ==, true );
471  TEST_COMPARE(te->eventInRangeIndex( 8, 10), ==, true );
472  TEST_COMPARE(te->eventInRangeIndex(12, 14), ==, false);
473 
474 }
475 
476 
477 } // namespace Tempus_Test
Teuchos::RCP< Tempus::TimeEventRange< double > > getTestRange3()
TimeEventRangeIndex specifies a start, stop and stride index.
Teuchos::RCP< Tempus::TimeEventRange< double > > getTestRange2()
Teuchos::RCP< Tempus::TimeEventListIndex< double > > getTestListIndex2()
TimeEventListIndex specifies a list of index events.
Teuchos::RCP< Tempus::TimeEventRangeIndex< double > > getTestRangeIndex3()
Teuchos::RCP< Tempus::TimeEventList< double > > getTestList3()
Teuchos::RCP< Tempus::TimeEventRange< double > > getTestRange1()
TimeEventRange specifies a start, stop and stride time.
Teuchos::RCP< Tempus::TimeEventList< double > > getTestList1()
This composite TimeEvent loops over added TimeEvents.
TEUCHOS_UNIT_TEST(BackwardEuler, Default_Construction)
Teuchos::RCP< Tempus::TimeEventListIndex< double > > getTestListIndex3()
Teuchos::RCP< Tempus::TimeEventListIndex< double > > getTestListIndex1()
Teuchos::RCP< Tempus::TimeEventRangeIndex< double > > getTestRangeIndex1()
Teuchos::RCP< Tempus::TimeEventList< double > > getTestList2()
Teuchos::RCP< Tempus::TimeEventRangeIndex< double > > getTestRangeIndex2()
TimeEventList specifies a list of time events.