Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_UnitTest_TimeEventRangeIndex.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 
12 #include "Tempus_TimeEventRangeIndex.hpp"
13 
14 namespace Tempus_Unit_Test {
15 
16 using Teuchos::RCP;
17 using Teuchos::rcp;
18 using Teuchos::rcp_const_cast;
19 using Teuchos::rcp_dynamic_cast;
20 
21 // ************************************************************
22 // ************************************************************
23 TEUCHOS_UNIT_TEST(TimeEventRangeIndex, Default_Construction)
24 {
26 
27  TEST_COMPARE(te->getName(), ==, "TimeEventRangeIndex (0; 0; 1)");
28 
29  // Test when everything is zero.
30  TEST_COMPARE(te->getIndexStart(), ==, 0);
31  TEST_COMPARE(te->getIndexStop(), ==, 0);
32  TEST_COMPARE(te->getIndexStride(), ==, 1);
33  TEST_COMPARE(te->getNumEvents(), ==, 1);
34 
35  // Check base class defaults (functions not implemented in
36  // TimeEventRangeIndex).
37  TEST_COMPARE(te->isTime(1.0), ==, false);
38  TEST_COMPARE(te->timeToNextEvent(1.0), ==, te->getDefaultTime());
39  TEST_COMPARE(te->timeOfNextEvent(1.0), ==, te->getDefaultTime());
40  TEST_COMPARE(te->eventInRange(1.0, 4.0), ==, false);
41 }
42 
43 // ************************************************************
44 // ************************************************************
45 TEUCHOS_UNIT_TEST(TimeEventRangeIndex, Construction)
46 {
47  auto te = rcp(new Tempus::TimeEventRangeIndex<double>(-1, 10, 2, "TestName"));
48 
49  TEST_COMPARE(te->getName(), ==, "TestName");
50 
51  // Test when everything is zero.
52  TEST_COMPARE(te->getIndexStart(), ==, -1);
53  TEST_COMPARE(te->getIndexStop(), ==, 10);
54  TEST_COMPARE(te->getIndexStride(), ==, 2);
55  TEST_COMPARE(te->getNumEvents(), ==, 6);
56 }
57 
58 // ************************************************************
59 // ************************************************************
60 TEUCHOS_UNIT_TEST(TimeEventRangeIndex, Basic_Accesors)
61 {
63 
64  te->setName("TestName");
65  TEST_COMPARE(te->getName(), ==, "TestName");
66 
67  // Reset start after stop.
68  te->setIndexStart(1);
69  TEST_COMPARE(te->getIndexStart(), ==, 1);
70  TEST_COMPARE(te->getIndexStop(), ==, 1);
71  TEST_COMPARE(te->getIndexStride(), ==, 1);
72  TEST_COMPARE(te->getNumEvents(), ==, 1);
73 
74  // Reset stop.
75  te->setIndexStop(5);
76  TEST_COMPARE(te->getIndexStart(), ==, 1);
77  TEST_COMPARE(te->getIndexStop(), ==, 5);
78  TEST_COMPARE(te->getIndexStride(), ==, 1);
79  TEST_COMPARE(te->getNumEvents(), ==, 5);
80 
81  // Reset stride.
82  te->setIndexStride(2);
83  TEST_COMPARE(te->getIndexStart(), ==, 1);
84  TEST_COMPARE(te->getIndexStop(), ==, 5);
85  TEST_COMPARE(te->getIndexStride(), ==, 2);
86  TEST_COMPARE(te->getNumEvents(), ==, 3);
87 
88  // Set with index range.
89  te->setIndexRange(-5, 5, 3);
90  TEST_COMPARE(te->getIndexStart(), ==, -5);
91  TEST_COMPARE(te->getIndexStop(), ==, 5);
92  TEST_COMPARE(te->getIndexStride(), ==, 3);
93  TEST_COMPARE(te->getNumEvents(), ==, 4);
94 }
95 
96 // ************************************************************
97 // ************************************************************
98 TEUCHOS_UNIT_TEST(TimeEventRangeIndex, Stride)
99 {
100  auto te = rcp(new Tempus::TimeEventRangeIndex<double>());
101  te->setIndexStart(1);
102  te->setIndexStop(5);
103  TEST_COMPARE(te->getIndexStart(), ==, 1);
104  TEST_COMPARE(te->getIndexStop(), ==, 5);
105 
106  // Negative stride should be reset to stop_-start_.
107  te->setIndexStride(-1);
108  TEST_COMPARE(te->getIndexStride(), ==, 1);
109  TEST_COMPARE(te->getNumEvents(), ==, 5);
110 
111  // Large stride should be reset to stop_-start_.
112  te->setIndexStride(5);
113  TEST_COMPARE(te->getIndexStride(), ==, 4);
114  TEST_COMPARE(te->getNumEvents(), ==, 2);
115 }
116 
117 // ************************************************************
118 // ************************************************************
119 TEUCHOS_UNIT_TEST(TimeEventRangeIndex, isIndex)
120 {
121  auto te = rcp(new Tempus::TimeEventRangeIndex<double>());
122  te->setIndexRange(-5, 5, 3);
123 
124  // Test isIndex.
125  TEST_COMPARE(te->isIndex(-6), ==, false); // Around first event.
126  TEST_COMPARE(te->isIndex(-5), ==, true);
127  TEST_COMPARE(te->isIndex(-4), ==, false);
128 
129  TEST_COMPARE(te->isIndex(0), ==, false); // Around mid event.
130  TEST_COMPARE(te->isIndex(1), ==, true);
131  TEST_COMPARE(te->isIndex(2), ==, false);
132 
133  TEST_COMPARE(te->isIndex(3), ==, false); // Around last event.
134  TEST_COMPARE(te->isIndex(4), ==, true);
135  TEST_COMPARE(te->isIndex(5), ==, false);
136 }
137 
138 // ************************************************************
139 // ************************************************************
140 TEUCHOS_UNIT_TEST(TimeEventRangeIndex, indexToNextEvent)
141 {
142  auto te = rcp(new Tempus::TimeEventRangeIndex<double>());
143  te->setIndexRange(-5, 5, 3);
144 
145  // Test indexToNextEvent.
146  TEST_COMPARE(te->indexToNextEvent(-9), ==, 4); // Around first event.
147  TEST_COMPARE(te->indexToNextEvent(-5), ==, 3);
148  TEST_COMPARE(te->indexToNextEvent(-4), ==, 2);
149 
150  TEST_COMPARE(te->indexToNextEvent(-1), ==, 2); // Around mid event.
151  TEST_COMPARE(te->indexToNextEvent(1), ==, 3);
152  TEST_COMPARE(te->indexToNextEvent(3), ==, 1);
153 
154  TEST_COMPARE(te->indexToNextEvent(2), ==, 2); // Around last event.
155  TEST_COMPARE(te->indexToNextEvent(4), ==, te->getDefaultIndex() - 4);
156  TEST_COMPARE(te->indexToNextEvent(8), ==, te->getDefaultIndex() - 8);
157 }
158 
159 // ************************************************************
160 // ************************************************************
161 TEUCHOS_UNIT_TEST(TimeEventRangeIndex, indexOfNextEvent)
162 {
163  auto te = rcp(new Tempus::TimeEventRangeIndex<double>());
164  te->setIndexRange(-5, 5, 3);
165 
166  // Test indexOfNextEvent.
167  TEST_COMPARE(te->indexOfNextEvent(-9), ==, -5); // Around first event.
168  TEST_COMPARE(te->indexOfNextEvent(-5), ==, -2);
169  TEST_COMPARE(te->indexOfNextEvent(-4), ==, -2);
170 
171  TEST_COMPARE(te->indexOfNextEvent(-1), ==, 1); // Around mid event.
172  TEST_COMPARE(te->indexOfNextEvent(1), ==, 4);
173  TEST_COMPARE(te->indexOfNextEvent(3), ==, 4);
174 
175  TEST_COMPARE(te->indexOfNextEvent(2), ==, 4); // Around last event.
176  TEST_COMPARE(te->indexOfNextEvent(4), ==, te->getDefaultIndex());
177  TEST_COMPARE(te->indexOfNextEvent(8), ==, te->getDefaultIndex());
178 }
179 
180 // ************************************************************
181 // ************************************************************
182 TEUCHOS_UNIT_TEST(TimeEventRangeIndex, eventInRangeIndex)
183 {
184  auto te = rcp(new Tempus::TimeEventRangeIndex<double>());
185  te->setIndexRange(-5, 5, 3);
186 
187  // Test eventInRangeIndex.
188  // Right end.
189  TEST_COMPARE(te->eventInRangeIndex(-9, -6), ==,
190  false); // Around first event.
191  TEST_COMPARE(te->eventInRangeIndex(-9, -5), ==, true);
192  TEST_COMPARE(te->eventInRangeIndex(-9, -4), ==, true);
193 
194  TEST_COMPARE(te->eventInRangeIndex(-1, 0), ==, false); // Around mid event.
195  TEST_COMPARE(te->eventInRangeIndex(-1, 1), ==, true);
196  TEST_COMPARE(te->eventInRangeIndex(-1, 2), ==, true);
197 
198  TEST_COMPARE(te->eventInRangeIndex(2, 3), ==, false); // Around last event.
199  TEST_COMPARE(te->eventInRangeIndex(2, 4), ==, true);
200  TEST_COMPARE(te->eventInRangeIndex(2, 5), ==, true);
201 
202  // Left end.
203  TEST_COMPARE(te->eventInRangeIndex(-6.0, -3), ==,
204  true); // Around first event.
205  TEST_COMPARE(te->eventInRangeIndex(-5.0, -3), ==, false);
206  TEST_COMPARE(te->eventInRangeIndex(-4.0, -3), ==, false);
207 
208  TEST_COMPARE(te->eventInRangeIndex(-3, 0), ==, true); // Around mid event.
209  TEST_COMPARE(te->eventInRangeIndex(-2, 0), ==, false);
210  TEST_COMPARE(te->eventInRangeIndex(-1, 0), ==, false);
211 
212  TEST_COMPARE(te->eventInRangeIndex(3, 8), ==, true); // Around last event.
213  TEST_COMPARE(te->eventInRangeIndex(4, 8), ==, false);
214  TEST_COMPARE(te->eventInRangeIndex(5, 8), ==, false);
215 }
216 
217 // ************************************************************
218 // ************************************************************
219 TEUCHOS_UNIT_TEST(TimeEventRangeIndex, getValidParameters)
220 {
221  auto teri = rcp(new Tempus::TimeEventRangeIndex<double>());
222 
223  auto pl = teri->getValidParameters();
224 
225  TEST_COMPARE(pl->get<std::string>("Type"), ==, "Range Index");
226  TEST_COMPARE(pl->get<std::string>("Name"), ==,
227  "TimeEventRangeIndex (0; 0; 1)");
228  TEST_COMPARE(pl->get<int>("Start Index"), ==, 0);
229  TEST_COMPARE(pl->get<int>("Stop Index"), ==, 0);
230  TEST_COMPARE(pl->get<int>("Stride Index"), ==, 1);
231 
232  { // Ensure that parameters are "used", excluding sublists.
233  std::ostringstream unusedParameters;
234  pl->unused(unusedParameters);
235  TEST_COMPARE(unusedParameters.str(), ==, "");
236  }
237 }
238 
239 // ************************************************************
240 // ************************************************************
242 {
243  // Construct parameterList similar to getValidParameters().
245  Teuchos::parameterList("Time Event Range Index");
246 
247  pl->set("Name", "Unit Test Time Event Range Index");
248  pl->set("Type", "Range Index");
249  pl->set("Start Index", -1);
250  pl->set("Stop Index", 11);
251  pl->set("Stride Index", 2);
252 
253  // Construct TimeEventRangeIndex from ParameterList.
254  auto teri = Tempus::createTimeEventRangeIndex<double>(pl);
255 
256  teri->describe(out, Teuchos::VERB_EXTREME);
257 
258  TEST_COMPARE(teri->getName(), ==, "Unit Test Time Event Range Index");
259  TEST_COMPARE(teri->getType(), ==, "Range Index");
260  TEST_COMPARE(teri->getIndexStart(), ==, -1);
261  TEST_COMPARE(teri->getIndexStop(), ==, 11);
262  TEST_COMPARE(teri->getIndexStride(), ==, 2);
263  TEST_COMPARE(teri->getNumEvents(), ==, 7);
264 }
265 
266 } // namespace Tempus_Unit_Test
TimeEventRangeIndex specifies a start, stop and stride index.
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
#define TEST_COMPARE(v1, comp, v2)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Teuchos::RCP< TimeEventRange< Scalar > > createTimeEventRange(Teuchos::RCP< Teuchos::ParameterList > pList)
Nonmember Constructor via ParameterList.
TEUCHOS_UNIT_TEST(BackwardEuler, Default_Construction)