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: Copyright (2017) Sandia Corporation
4 //
5 // Distributed under BSD 3-clause license (See accompanying file Copyright.txt)
6 // ****************************************************************************
7 // @HEADER
8 
11 #include "Teuchos_TimeMonitor.hpp"
12 #include "Teuchos_DefaultComm.hpp"
13 
14 #include "Tempus_TimeEventList.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(TimeEventList, Default_Construction)
32 {
33  auto te = rcp(new Tempus::TimeEventList<double>());
34 
35  TEST_COMPARE(te->getName(), ==, "TimeEventList");
36 
37  TEST_COMPARE(te->getTimeList().size(), ==, 0);
38  TEST_FLOATING_EQUALITY(te->getRelTol(), 1.0e-14, 1.0e-14);
39  TEST_COMPARE(te->getLandOnExactly(), ==, true);
40 
41  // Check base class defaults (functions not implemented in TimeEventList).
42  TEST_COMPARE(te->isIndex(1), ==, false);
43  TEST_COMPARE(te->indexToNextEvent(1), ==, te->getDefaultIndex());
44  TEST_COMPARE(te->indexOfNextEvent(1), ==, te->getDefaultIndex());
45  TEST_COMPARE(te->eventInRangeIndex(1,4), ==, false);
46 }
47 
48 
49 // ************************************************************
50 // ************************************************************
51 TEUCHOS_UNIT_TEST(TimeEventList, Construction)
52 {
53  std::vector<double> testVector;
54  testVector.push_back(-1.0);
55  testVector.push_back( 0.0);
56  testVector.push_back( 5.0);
57  testVector.push_back( 2.0);
58  testVector.push_back( PI);
59 
60  auto te = rcp(new Tempus::TimeEventList<double>(
61  "TestName", testVector, 1.0e-14, true));
62 
63  TEST_COMPARE(te->getName(), ==, "TestName");
64  TEST_FLOATING_EQUALITY(te->getRelTol(), 1.0e-14, 1.0e-14);
65  TEST_COMPARE(te->getLandOnExactly(), ==, true);
66 
67  auto testList = te->getTimeList();
68  TEST_COMPARE(testList.size(), ==, 5);
69  TEST_FLOATING_EQUALITY(testList[0], -1.0, 1.0e-14);
70  TEST_FLOATING_EQUALITY(testList[1], 0.0, 1.0e-14);
71  TEST_FLOATING_EQUALITY(testList[2], 2.0, 1.0e-14);
72  TEST_FLOATING_EQUALITY(testList[3], PI, 1.0e-14);
73  TEST_FLOATING_EQUALITY(testList[4], 5.0, 1.0e-14);
74 }
75 
76 
77 // ************************************************************
78 // ************************************************************
79 TEUCHOS_UNIT_TEST(TimeEventList, Basic_Accessors)
80 {
81  auto te = rcp(new Tempus::TimeEventList<double>());
82 
83  te->setName("TestName");
84  TEST_COMPARE(te->getName(), ==, "TestName");
85  te->setRelTol(0.1);
86  TEST_FLOATING_EQUALITY(te->getRelTol(), 0.1, 1.0e-14);
87  te->setRelTol(1.0e-14);
88  te->setLandOnExactly(false);
89  TEST_COMPARE(te->getLandOnExactly(), ==, false);
90 
91  // Test addTime.
92  te->addTime(0.0);
93  te->addTime(PI);
94  te->addTime(-1.0);
95  te->addTime( 2.0);
96  te->addTime( 5.0);
97 
98  // Add times that should not be duplicated.
99  te->addTime(0.0);
100  te->addTime(PI);
101 
102  auto testList = te->getTimeList();
103  TEST_COMPARE(testList.size(), ==, 5);
104  TEST_FLOATING_EQUALITY(testList[0], -1.0, 1.0e-14);
105  TEST_FLOATING_EQUALITY(testList[1], 0.0, 1.0e-14);
106  TEST_FLOATING_EQUALITY(testList[2], 2.0, 1.0e-14);
107  TEST_FLOATING_EQUALITY(testList[3], PI, 1.0e-14);
108  TEST_FLOATING_EQUALITY(testList[4], 5.0, 1.0e-14);
109 
110  // Test that two events within relative tolerance are added or not.
111  te->addTime( 2.0 + 1.0e-14);
112  TEST_COMPARE(te->getTimeList().size(), ==, 5);
113  te->addTime( 2.0 + 1.0e-13);
114  TEST_COMPARE(te->getTimeList().size(), ==, 6);
115 }
116 
117 
118 // ************************************************************
119 // ************************************************************
120 TEUCHOS_UNIT_TEST(TimeEventList, isTime)
121 {
122  auto te = rcp(new Tempus::TimeEventList<double>());
123  te->addTime( 0.0);
124  te->addTime( PI);
125  te->addTime(-1.0);
126  te->addTime( 2.0);
127  te->addTime( 5.0);
128 
129  // Test isTime.
130  // Around first event.
131  TEST_COMPARE(te->isTime(-10.0e-14), ==, false); // Just outside tolerance.
132  TEST_COMPARE(te->isTime( -1.0e-14), ==, true ); // Just inside tolerance.
133  TEST_COMPARE(te->isTime( 0.0 ), ==, true ); // Right on timeEvent.
134  TEST_COMPARE(te->isTime( 1.0e-14), ==, true ); // Just inside tolerance.
135  TEST_COMPARE(te->isTime( 10.0e-14), ==, false); // Just outside tolerance.
136 
137  // Around mid event.
138  TEST_COMPARE(te->isTime(PI + -10.0e-14), ==, false); // Just outside tolerance.
139  TEST_COMPARE(te->isTime(PI + -1.0e-14), ==, true ); // Just inside tolerance.
140  TEST_COMPARE(te->isTime(PI + 0.0 ), ==, true ); // Right on timeEvent.
141  TEST_COMPARE(te->isTime(PI + 1.0e-14), ==, true ); // Just inside tolerance.
142  TEST_COMPARE(te->isTime(PI + 10.0e-14), ==, false); // Just outside tolerance.
143 
144  // Around last event.
145  TEST_COMPARE(te->isTime(5.0 + -10.0e-14), ==, false); // Just outside tolerance.
146  TEST_COMPARE(te->isTime(5.0 + -1.0e-14), ==, true ); // Just inside tolerance.
147  TEST_COMPARE(te->isTime(5.0 + 0.0 ), ==, true ); // Right on timeEvent.
148  TEST_COMPARE(te->isTime(5.0 + 1.0e-14), ==, true ); // Just inside tolerance.
149  TEST_COMPARE(te->isTime(5.0 + 10.0e-14), ==, false); // Just outside tolerance.
150 }
151 
152 
153 // ************************************************************
154 // ************************************************************
155 TEUCHOS_UNIT_TEST(TimeEventList, timeToNextEvent)
156 {
157  std::vector<double> testList;
158  testList.push_back( 0.0);
159  testList.push_back( PI);
160  testList.push_back(-1.0);
161  testList.push_back( 2.0);
162  testList.push_back( 5.0);
163 
164  auto te = rcp(new Tempus::TimeEventList<double>(
165  "testList", testList, 1.0e-14, true));
166 
167  // Test timeToNextEvent.
168  // Around first event.
169  TEST_FLOATING_EQUALITY(te->timeToNextEvent(-1.0 + -10.0e-14), 1.0e-13, 1.0e-02);
170  TEST_FLOATING_EQUALITY(te->timeToNextEvent(-1.0 + -1.0e-14), 1.0e-14, 1.0e-02);
171  TEST_FLOATING_EQUALITY(te->timeToNextEvent(-1.0 + 0.0 ), 0.0 , 1.0e-02);
172  TEST_FLOATING_EQUALITY(te->timeToNextEvent(-1.0 + 1.0e-14), -1.0e-14, 1.0e-02);
173  TEST_FLOATING_EQUALITY(te->timeToNextEvent(-1.0 + 10.0e-14), 1.0-1.0e-13, 1.0e-14);
174 
175  // Around mid event.
176  TEST_FLOATING_EQUALITY(te->timeToNextEvent(PI + -10.0e-14), 1.0e-13, 1.0e-02);
177  TEST_FLOATING_EQUALITY(te->timeToNextEvent(PI + -1.0e-14), 1.0e-14, 1.0e-01);
178  TEST_FLOATING_EQUALITY(te->timeToNextEvent(PI + 0.0 ), 0.0 , 1.0e-02);
179  TEST_FLOATING_EQUALITY(te->timeToNextEvent(PI + 1.0e-14), -1.0e-14, 1.0e-01);
180  TEST_FLOATING_EQUALITY(te->timeToNextEvent(PI + 10.0e-14), 5.0-PI-1.0e-13, 1.0e-14);
181 
182  // Around last event.
183  TEST_FLOATING_EQUALITY(te->timeToNextEvent(5.0+ -10.0e-14), 1.0e-13, 1.0e-02);
184  TEST_FLOATING_EQUALITY(te->timeToNextEvent(5.0+ -1.0e-14), 1.0e-14, 1.0e-01);
185  TEST_FLOATING_EQUALITY(te->timeToNextEvent(5.0+ 0.0 ), 0.0 , 1.0e-02);
186  TEST_FLOATING_EQUALITY(te->timeToNextEvent(5.0+ 1.0e-14), -1.0e-14, 1.0e-01);
187  TEST_FLOATING_EQUALITY(te->timeToNextEvent(5.0+ 10.0e-14), -1.0e-13, 1.0e-02);
188 }
189 
190 
191 // ************************************************************
192 // ************************************************************
193 TEUCHOS_UNIT_TEST(TimeEventList, timeOfNextEvent)
194 {
195  std::vector<double> testList;
196  testList.push_back( 0.0);
197  testList.push_back( PI);
198  testList.push_back(-1.0);
199  testList.push_back( 2.0);
200  testList.push_back( 5.0);
201 
202  auto te = rcp(new Tempus::TimeEventList<double>(
203  "testList", testList, 1.0e-14, true));
204 
205  // Test timeOfNextEvent.
206  // Around first event.
207  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(-1.0 + -10.0e-14), -1.0, 1.0e-14);
208  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(-1.0 + -1.0e-14), -1.0, 1.0e-14);
209  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(-1.0 + 0.0 ), -1.0, 1.0e-14);
210  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(-1.0 + 1.0e-14), -1.0, 1.0e-14);
211  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(-1.0 + 10.0e-14), 0.0, 1.0e-14);
212 
213  // Around mid event.
214  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(2.0+ -10.0e-14), 2.0, 1.0e-14);
215  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(2.0+ -1.0e-14), 2.0, 1.0e-14);
216  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(2.0+ 0.0 ), 2.0, 1.0e-14);
217  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(2.0+ 1.0e-14), 2.0, 1.0e-14);
218  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(2.0+ 10.0e-14), PI, 1.0e-14);
219 
220  // Around last event.
221  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(5.0+ -10.0e-14), 5.0, 1.0e-14);
222  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(5.0+ -1.0e-14), 5.0, 1.0e-14);
223  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(5.0+ 0.0 ), 5.0, 1.0e-14);
224  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(5.0+ 1.0e-14), 5.0, 1.0e-14);
225  TEST_FLOATING_EQUALITY(te->timeOfNextEvent(5.0+ 10.0e-14), 5.0, 1.0e-14);
226 }
227 
228 
229 // ************************************************************
230 // ************************************************************
231 TEUCHOS_UNIT_TEST(TimeEventList, eventInRange)
232 {
233  std::vector<double> testList;
234  testList.push_back( 0.0);
235  testList.push_back( PI);
236  testList.push_back(-1.0);
237  testList.push_back( 2.0);
238  testList.push_back( 5.0);
239 
240  auto te = rcp(new Tempus::TimeEventList<double>(
241  "testList", testList, 1.0e-14, true));
242 
243  // Test eventInRange.
244  // Right end.
245  TEST_COMPARE(te->eventInRange(-2.0, -1.0 + -10.0e-14), ==, false); // Around first event.
246  TEST_COMPARE(te->eventInRange(-2.0, -1.0 + -1.0e-14), ==, true );
247  TEST_COMPARE(te->eventInRange(-2.0, -1.0 + 0.0 ), ==, true );
248  TEST_COMPARE(te->eventInRange(-2.0, -1.0 + 1.0e-14), ==, true );
249  TEST_COMPARE(te->eventInRange(-2.0, -1.0 + 10.0e-14), ==, true );
250 
251  TEST_COMPARE(te->eventInRange(3.0, PI + -10.0e-14), ==, false); // Around mid event.
252  TEST_COMPARE(te->eventInRange(3.0, PI + -1.0e-14), ==, true );
253  TEST_COMPARE(te->eventInRange(3.0, PI + 0.0 ), ==, true );
254  TEST_COMPARE(te->eventInRange(3.0, PI + 1.0e-14), ==, true );
255  TEST_COMPARE(te->eventInRange(3.0, PI + 10.0e-14), ==, true );
256 
257  TEST_COMPARE(te->eventInRange(4.5, 5.0 + -10.0e-14), ==, false); // Around last event.
258  TEST_COMPARE(te->eventInRange(4.5, 5.0 + -1.0e-14), ==, true );
259  TEST_COMPARE(te->eventInRange(4.5, 5.0 + 0.0 ), ==, true );
260  TEST_COMPARE(te->eventInRange(4.5, 5.0 + 1.0e-14), ==, true );
261  TEST_COMPARE(te->eventInRange(4.5, 5.0 + 10.0e-14), ==, true );
262 
263  // Left end.
264  TEST_COMPARE(te->eventInRange(-1.0 + -10.0e-14, -0.5), ==, true ); // Around first event.
265  TEST_COMPARE(te->eventInRange(-1.0 + -1.0e-14, -0.5), ==, true );
266  TEST_COMPARE(te->eventInRange(-1.0 + 0.0 , -0.5), ==, true );
267  TEST_COMPARE(te->eventInRange(-1.0 + 1.0e-14, -0.5), ==, true );
268  TEST_COMPARE(te->eventInRange(-1.0 + 10.0e-14, -0.5), ==, false );
269 
270  TEST_COMPARE(te->eventInRange(PI + -10.0e-14, 3.5), ==, true ); // Around mid event.
271  TEST_COMPARE(te->eventInRange(PI + -1.0e-14, 3.5), ==, true );
272  TEST_COMPARE(te->eventInRange(PI + 0.0 , 3.5), ==, true );
273  TEST_COMPARE(te->eventInRange(PI + 1.0e-14, 3.5), ==, true );
274  TEST_COMPARE(te->eventInRange(PI + 10.0e-14, 3.5), ==, false);
275 
276  TEST_COMPARE(te->eventInRange(5.0 + -10.0e-14, 6.0), ==, true ); // Around last event.
277  TEST_COMPARE(te->eventInRange(5.0 + -1.0e-14, 6.0), ==, true );
278  TEST_COMPARE(te->eventInRange(5.0 + 0.0 , 6.0), ==, true );
279  TEST_COMPARE(te->eventInRange(5.0 + 1.0e-14, 6.0), ==, true );
280  TEST_COMPARE(te->eventInRange(5.0 + 10.0e-14, 6.0), ==, false);
281 }
282 
283 
284 } // namespace Tempus_Test
#define TEST_COMPARE(v1, comp, v2)
#define TEST_FLOATING_EQUALITY(v1, v2, tol)
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