Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_UnitTest_TimeEventListIndex.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_TimeEventListIndex.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(TimeEventListIndex, Default_Construction)
24 {
25  auto te = rcp(new Tempus::TimeEventListIndex<double>());
26 
27  TEST_COMPARE(te->getName(), ==, "TimeEventListIndex");
28 
29  TEST_COMPARE(te->getIndexList().size(), ==, 0);
30 
31  TEST_COMPARE(te->isIndex(-1), ==, false);
32  TEST_COMPARE(te->isIndex(0), ==, false);
33  TEST_COMPARE(te->isIndex(1), ==, false);
34 
35  TEST_COMPARE(te->indexToNextEvent(0), ==, te->getDefaultIndex());
36  TEST_COMPARE(te->indexOfNextEvent(0), ==, te->getDefaultIndex());
37  TEST_COMPARE(te->eventInRangeIndex(2, -1), ==, false);
38 
39  // Check base class defaults (functions not implemented in
40  // TimeEventListIndex).
41  TEST_COMPARE(te->isTime(1.0), ==, false);
42  TEST_COMPARE(te->timeToNextEvent(1.0), ==, te->getDefaultTime());
43  TEST_COMPARE(te->timeOfNextEvent(1.0), ==, te->getDefaultTime());
44  TEST_COMPARE(te->eventInRange(1.0, 4.0), ==, false);
45 }
46 
47 // ************************************************************
48 // ************************************************************
49 TEUCHOS_UNIT_TEST(TimeEventListIndex, Construction)
50 {
51  std::vector<int> testVector;
52  testVector.push_back(-2);
53  testVector.push_back(0);
54  testVector.push_back(7);
55  testVector.push_back(3);
56  testVector.push_back(-5);
57 
58  auto te = rcp(new Tempus::TimeEventListIndex<double>(testVector, "TestName"));
59 
60  TEST_COMPARE(te->getName(), ==, "TestName");
61 
62  auto testList = te->getIndexList();
63  TEST_COMPARE(testList.size(), ==, 5);
64  TEST_COMPARE(testList[0], ==, -5);
65  TEST_COMPARE(testList[1], ==, -2);
66  TEST_COMPARE(testList[2], ==, 0);
67  TEST_COMPARE(testList[3], ==, 3);
68  TEST_COMPARE(testList[4], ==, 7);
69 
70  // Test adding a duplicate event index.
71  te->addIndex(3);
72  TEST_COMPARE(te->getIndexList().size(), ==, 5);
73  te->addIndex(1);
74  TEST_COMPARE(te->getIndexList().size(), ==, 6);
75 }
76 
77 // ************************************************************
78 // ************************************************************
79 TEUCHOS_UNIT_TEST(TimeEventListIndex, isIndex)
80 {
81  auto te = rcp(new Tempus::TimeEventListIndex<double>());
82 
83  te->setName("TestName");
84  TEST_COMPARE(te->getName(), ==, "TestName");
85 
86  // Test isIndex with one element.
87  te->addIndex(-5);
88  TEST_COMPARE(te->isIndex(-6), ==, false);
89  TEST_COMPARE(te->isIndex(-5), ==, true);
90  TEST_COMPARE(te->isIndex(-4), ==, false);
91 
92  // Test isIndex with two elements.
93  te->addIndex(1);
94  TEST_COMPARE(te->isIndex(0), ==, false);
95  TEST_COMPARE(te->isIndex(1), ==, true);
96  TEST_COMPARE(te->isIndex(2), ==, false);
97 
98  // Test addIndex.
99  te->addIndex(-2);
100  te->addIndex(4);
101  te->addIndex(-9);
102  auto testList = te->getIndexList();
103  TEST_COMPARE(testList.size(), ==, 5);
104  TEST_COMPARE(testList[0], ==, -9);
105  TEST_COMPARE(testList[1], ==, -5);
106  TEST_COMPARE(testList[2], ==, -2);
107  TEST_COMPARE(testList[3], ==, 1);
108  TEST_COMPARE(testList[4], ==, 4);
109 }
110 
111 // ************************************************************
112 // ************************************************************
113 TEUCHOS_UNIT_TEST(TimeEventListIndex, indexToNextEvent)
114 {
115  std::vector<int> testListIndex;
116  testListIndex.push_back(-5);
117  testListIndex.push_back(1);
118  testListIndex.push_back(-2);
119  testListIndex.push_back(4);
120  testListIndex.push_back(-9);
121 
122  auto te =
123  rcp(new Tempus::TimeEventListIndex<double>(testListIndex, "teListIndex"));
124 
125  // Test indexToNextEvent.
126  // Around first event.
127  TEST_COMPARE(te->indexToNextEvent(-12), ==, 3);
128  TEST_COMPARE(te->indexToNextEvent(-9), ==, 4);
129  TEST_COMPARE(te->indexToNextEvent(-8), ==, 3);
130 
131  // Around mid event.
132  TEST_COMPARE(te->indexToNextEvent(-4), ==, 2);
133  TEST_COMPARE(te->indexToNextEvent(-2), ==, 3);
134  TEST_COMPARE(te->indexToNextEvent(0), ==, 1);
135 
136  // Around last event.
137  TEST_COMPARE(te->indexToNextEvent(2), ==, 2);
138  TEST_COMPARE(te->indexToNextEvent(4), ==, te->getDefaultIndex() - 4);
139  TEST_COMPARE(te->indexToNextEvent(9), ==, te->getDefaultIndex() - 9);
140 }
141 
142 // ************************************************************
143 // ************************************************************
144 TEUCHOS_UNIT_TEST(TimeEventListIndex, indexOfNextEvent)
145 {
146  std::vector<int> testListIndex;
147  testListIndex.push_back(-5);
148  testListIndex.push_back(1);
149  testListIndex.push_back(-2);
150  testListIndex.push_back(4);
151  testListIndex.push_back(-9);
152 
153  auto te =
154  rcp(new Tempus::TimeEventListIndex<double>(testListIndex, "teListIndex"));
155 
156  // Test indexOfNextEvent.
157  // Around first event.
158  TEST_COMPARE(te->indexOfNextEvent(-12), ==, -9);
159  TEST_COMPARE(te->indexOfNextEvent(-9), ==, -5);
160  TEST_COMPARE(te->indexOfNextEvent(-8), ==, -5);
161 
162  // Around mid event.
163  TEST_COMPARE(te->indexOfNextEvent(-4), ==, -2);
164  TEST_COMPARE(te->indexOfNextEvent(-2), ==, 1);
165  TEST_COMPARE(te->indexOfNextEvent(0), ==, 1);
166 
167  // Around last event.
168  TEST_COMPARE(te->indexOfNextEvent(2), ==, 4);
169  TEST_COMPARE(te->indexOfNextEvent(4), ==, te->getDefaultIndex());
170  TEST_COMPARE(te->indexOfNextEvent(9), ==, te->getDefaultIndex());
171 }
172 
173 // ************************************************************
174 // ************************************************************
175 TEUCHOS_UNIT_TEST(TimeEventListIndex, eventInRangeIndex)
176 {
177  std::vector<int> testListIndex;
178  testListIndex.push_back(-5);
179  testListIndex.push_back(1);
180  testListIndex.push_back(-2);
181  testListIndex.push_back(4);
182  testListIndex.push_back(-9);
183 
184  auto te =
185  rcp(new Tempus::TimeEventListIndex<double>(testListIndex, "teListIndex"));
186 
187  // Test eventInRangeIndex.
188  // Right end.
189  TEST_COMPARE(te->eventInRangeIndex(-12.0, -10), ==,
190  false); // Around first event.
191  TEST_COMPARE(te->eventInRangeIndex(-12.0, -9), ==, true);
192  TEST_COMPARE(te->eventInRangeIndex(-12.0, -8), ==, true);
193 
194  TEST_COMPARE(te->eventInRangeIndex(-4, -3), ==, false); // Around mid event.
195  TEST_COMPARE(te->eventInRangeIndex(-4, -2), ==, true);
196  TEST_COMPARE(te->eventInRangeIndex(-4, -1), ==, true);
197 
198  TEST_COMPARE(te->eventInRangeIndex(3, 3), ==, false); // Around last event.
199  TEST_COMPARE(te->eventInRangeIndex(3, 4), ==, true);
200  TEST_COMPARE(te->eventInRangeIndex(3, 6), ==, true);
201 
202  // Left end.
203  TEST_COMPARE(te->eventInRangeIndex(-12, -7), ==,
204  true); // Around first event.
205  TEST_COMPARE(te->eventInRangeIndex(-9, -7), ==, false);
206  TEST_COMPARE(te->eventInRangeIndex(-8, -7), ==, 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(TimeEventListIndex, getValidParameters)
220 {
221  auto teli = rcp(new Tempus::TimeEventListIndex<double>());
222 
223  auto pl = teli->getValidParameters();
224 
225  TEST_COMPARE(pl->get<std::string>("Type"), ==, "List Index");
226  TEST_COMPARE(pl->get<std::string>("Name"), ==, "TimeEventListIndex");
227  TEST_COMPARE(pl->get<std::string>("Index List"), ==, "");
228 
229  { // Ensure that parameters are "used", excluding sublists.
230  std::ostringstream unusedParameters;
231  pl->unused(unusedParameters);
232  TEST_COMPARE(unusedParameters.str(), ==, "");
233  }
234 }
235 
236 // ************************************************************
237 // ************************************************************
239 {
240  // Construct parameterList similar to getValidParameters().
242  Teuchos::parameterList("Time Event List Index");
243 
244  pl->set("Name", "Unit Test Time Event List Index");
245  pl->set("Type", "List Index");
246 
247  std::vector<int> indices;
248  indices.push_back(-99);
249  indices.push_back(13);
250  indices.push_back(97);
251  indices.push_back(101);
252  std::ostringstream list;
253  for (std::size_t i = 0; i < indices.size() - 1; ++i)
254  list << indices[i] << ", ";
255  list << indices[indices.size() - 1];
256  pl->set<std::string>("Index List", list.str());
257 
258  // Construct TimeEventListIndex from ParameterList.
259  auto teli = Tempus::createTimeEventListIndex<double>(pl);
260 
261  teli->describe(out, Teuchos::VERB_EXTREME);
262 
263  TEST_COMPARE(teli->getName(), ==, "Unit Test Time Event List Index");
264  TEST_COMPARE(teli->getType(), ==, "List Index");
265  auto teList = teli->getIndexList();
266  TEST_COMPARE(teList[0], ==, -99);
267  TEST_COMPARE(teList[1], ==, 13);
268  TEST_COMPARE(teList[2], ==, 97);
269  TEST_COMPARE(teList[3], ==, 101);
270 }
271 
272 } // namespace Tempus_Unit_Test
TimeEventListIndex specifies a list of index events.
#define TEST_COMPARE(v1, comp, v2)
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)
Teuchos::RCP< TimeEventListIndex< Scalar > > createTimeEventListIndex(Teuchos::RCP< Teuchos::ParameterList > pList)
Nonmember Constructor via ParameterList.