Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_UnitTest_SolutionHistory.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_SolutionHistory.hpp"
14 
15 #include "../TestModels/DahlquistTestModel.hpp"
16 
17 namespace Tempus_Unit_Test {
18 
20 using Teuchos::RCP;
21 using Teuchos::rcp;
22 using Teuchos::rcp_const_cast;
23 using Teuchos::rcp_dynamic_cast;
24 using Teuchos::sublist;
25 
26 // ************************************************************
27 // ************************************************************
28 TEUCHOS_UNIT_TEST(SolutionHistory, Default_Construction)
29 {
30  auto sh = rcp(new Tempus::SolutionHistory<double>());
31  TEUCHOS_TEST_FOR_EXCEPT(sh->isInitialized()); // Should NOT be initialized
32  // as no State is set.
33 
34  auto model = rcp(new Tempus_Test::SinCosModel<double>());
35  auto inArgsIC = model->getNominalValues();
36  auto icSolution =
37  rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
38  auto icState = Tempus::createSolutionStateX<double>(icSolution);
39  sh->addState(icState);
40  sh->initialize();
41  TEUCHOS_TEST_FOR_EXCEPT(!sh->isInitialized());
42 
43  // Test the get functions (i.e., defaults).
44  TEST_COMPARE(sh->getNumStates(), ==, 1);
45  TEST_COMPARE(sh->getInterpolator()->order(), ==, 0);
46  TEST_COMPARE(sh->getStorageType(), ==, Tempus::STORAGE_TYPE_UNDO);
47  TEST_COMPARE(sh->getStorageLimit(), ==, 2);
48 
49  // Test the set functions.
50  sh->setName("Testing");
51  TEUCHOS_TEST_FOR_EXCEPT(!sh->isInitialized());
52  sh->setStorageType(Tempus::STORAGE_TYPE_STATIC);
53  sh->initialize();
54  TEUCHOS_TEST_FOR_EXCEPT(!sh->isInitialized());
55  sh->setStorageTypeString("Static");
56  sh->initialize();
57  TEUCHOS_TEST_FOR_EXCEPT(!sh->isInitialized());
58  sh->setStorageLimit(99);
59  sh->initialize();
60  TEUCHOS_TEST_FOR_EXCEPT(!sh->isInitialized());
61 
62  TEST_COMPARE(sh->getName(), ==, "Testing");
63  TEST_COMPARE(sh->getStorageType(), ==, Tempus::STORAGE_TYPE_STATIC);
64  TEST_COMPARE(sh->getStorageTypeString(), ==, "Static");
65  TEST_COMPARE(sh->getStorageLimit(), ==, 99);
66 
67  TEST_FLOATING_EQUALITY(sh->minTime(), 0.0, 1.0e-14);
68  TEST_FLOATING_EQUALITY(sh->maxTime(), 0.0, 1.0e-14);
69 }
70 
71 // ************************************************************
72 // ************************************************************
73 TEUCHOS_UNIT_TEST(SolutionHistory, Full_Construction)
74 {
75  std::string name = "Unit Test";
76 
77  auto history = rcp(new std::vector<RCP<Tempus::SolutionState<double> > >);
78  auto model = rcp(new Tempus_Test::SinCosModel<double>());
79  auto inArgsIC = model->getNominalValues();
80  auto icSolution =
81  rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
83  Tempus::createSolutionStateX<double>(icSolution);
84  history->push_back(icState);
85 
87 
89  int storageLimit = 99;
90 
91  auto sh = rcp(new Tempus::SolutionHistory<double>(name, history, interpolator,
92  storageType, storageLimit));
93  TEUCHOS_TEST_FOR_EXCEPT(!sh->isInitialized());
94 
95  TEST_COMPARE(sh->getNumStates(), ==, 1);
96  TEST_COMPARE(sh->getInterpolator()->order(), ==, 0);
97  TEST_COMPARE(sh->getStorageType(), ==, Tempus::STORAGE_TYPE_STATIC);
98  TEST_COMPARE(sh->getStorageLimit(), ==, 99);
99 }
100 
101 // ************************************************************
102 // ************************************************************
103 TEUCHOS_UNIT_TEST(SolutionHistory, Create_Construction)
104 {
105  auto sh_temp = rcp(new Tempus::SolutionHistory<double>());
106  RCP<ParameterList> pl = sh_temp->getNonconstParameterList();
107 
108  pl->setName("Unit Test");
109  pl->set<std::string>("Storage Type", "Static");
110  pl->set<int>("Storage Limit", 99);
111 
112  pl->sublist("Interpolator").set("Interpolator Type", "Lagrange");
113  pl->sublist("Interpolator").set("Order", 1);
114 
115  auto sh = Tempus::createSolutionHistoryPL<double>(pl);
116  TEUCHOS_TEST_FOR_EXCEPT(sh->isInitialized()); // Should NOT be initialized
117  // as no State is set.
118 
119  TEST_COMPARE(sh->getNumStates(), ==, 0);
120  TEST_COMPARE(sh->getInterpolator()->order(), ==, 1);
121  TEST_COMPARE(sh->getStorageType(), ==, Tempus::STORAGE_TYPE_STATIC);
122  TEST_COMPARE(sh->getStorageLimit(), ==, 99);
123 }
124 
125 // ************************************************************
126 // ************************************************************
127 TEUCHOS_UNIT_TEST(SolutionHistory, addState_With_Keep_Newest)
128 {
129  // Setup SolutionHistory for testing.
130  auto sh = rcp(new Tempus::SolutionHistory<double>());
131  TEUCHOS_TEST_FOR_EXCEPT(sh->isInitialized()); // Should NOT be initialized
132  // as no State is set.
133 
134  auto model = rcp(new Tempus_Test::DahlquistTestModel<double>(-1.0, false));
135  auto inArgsIC = model->getNominalValues();
136  auto icSoln = rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
137  auto state0 = Tempus::createSolutionStateX<double>(icSoln);
138  state0->setTime(0.0);
139  state0->setIndex(0);
140 
141  sh->setStorageTypeString("Keep Newest");
142  sh->addState(state0);
143  sh->initialize();
144  TEUCHOS_TEST_FOR_EXCEPT(!sh->isInitialized());
145 
146  TEST_COMPARE(sh->getNumStates(), ==, 1);
147  TEST_FLOATING_EQUALITY(sh->getCurrentTime(), 0.0, 1.0e-14);
148  TEST_COMPARE(sh->getCurrentIndex(), ==, 0);
149  TEST_FLOATING_EQUALITY(sh->minTime(), 0.0, 1.0e-14);
150  TEST_FLOATING_EQUALITY(sh->maxTime(), 0.0, 1.0e-14);
151 
152  TEUCHOS_ASSERT(sh->getCurrentState() != Teuchos::null);
153  TEUCHOS_ASSERT(sh->getStateTimeIndexN(false) != Teuchos::null);
154  TEUCHOS_ASSERT(sh->getStateTimeIndexNM1(false) == Teuchos::null);
155  TEUCHOS_ASSERT(sh->getStateTimeIndexNM2(false) == Teuchos::null);
156  TEUCHOS_ASSERT(sh->getStateTimeIndex(0, false) != Teuchos::null);
157 
158  TEST_COMPARE(sh->getStateTimeIndexN()->getIndex(), ==, 0);
159  TEST_FLOATING_EQUALITY(sh->getStateTimeIndexN()->getTime(), 0.0, 1.0e-14);
160 
161  // ---------------------------------------------------------------------------
162 
163  // Second State -- should replace first state.
164  auto state1 = Tempus::createSolutionStateX<double>(icSoln);
165  state1->setTime(1.0);
166  state1->setIndex(1);
167  sh->addState(state1);
168 
169  TEST_COMPARE(sh->getNumStates(), ==, 1); // Only 1 state!
170  TEST_FLOATING_EQUALITY(sh->getCurrentTime(), 1.0, 1.0e-14);
171  TEST_COMPARE(sh->getCurrentIndex(), ==, 1);
172  TEST_FLOATING_EQUALITY(sh->minTime(), 1.0, 1.0e-14);
173  TEST_FLOATING_EQUALITY(sh->maxTime(), 1.0, 1.0e-14);
174 
175  TEUCHOS_ASSERT(sh->getCurrentState() != Teuchos::null);
176  TEUCHOS_ASSERT(sh->getStateTimeIndexN(false) != Teuchos::null);
177  TEUCHOS_ASSERT(sh->getStateTimeIndexNM1(false) == Teuchos::null);
178  TEUCHOS_ASSERT(sh->getStateTimeIndexNM2(false) == Teuchos::null);
179  TEUCHOS_ASSERT(sh->getStateTimeIndex(0, false) == Teuchos::null);
180  TEUCHOS_ASSERT(sh->getStateTimeIndex(1, false) != Teuchos::null);
181 
182  TEST_COMPARE(sh->getStateTimeIndexN()->getIndex(), ==, 1);
183  TEST_FLOATING_EQUALITY(sh->getStateTimeIndexN()->getTime(), 1.0, 1.0e-14);
184 
185  // ---------------------------------------------------------------------------
186 
187  // Third State -- old state should not be added.
188  auto state2 = Tempus::createSolutionStateX<double>(icSoln);
189  state2->setTime(-1.0);
190  state2->setIndex(-1);
191  sh->addState(state2);
192 
193  // Still second state.
194  TEST_COMPARE(sh->getNumStates(), ==, 1); // Only 1 state!
195  TEST_FLOATING_EQUALITY(sh->getCurrentTime(), 1.0, 1.0e-14);
196  TEST_COMPARE(sh->getCurrentIndex(), ==, 1);
197  TEST_FLOATING_EQUALITY(sh->minTime(), 1.0, 1.0e-14);
198  TEST_FLOATING_EQUALITY(sh->maxTime(), 1.0, 1.0e-14);
199 
200  TEUCHOS_ASSERT(sh->getCurrentState() != Teuchos::null);
201  TEUCHOS_ASSERT(sh->getStateTimeIndexN(false) != Teuchos::null);
202  TEUCHOS_ASSERT(sh->getStateTimeIndexNM1(false) == Teuchos::null);
203  TEUCHOS_ASSERT(sh->getStateTimeIndexNM2(false) == Teuchos::null);
204  TEUCHOS_ASSERT(sh->getStateTimeIndex(0, false) == Teuchos::null);
205  TEUCHOS_ASSERT(sh->getStateTimeIndex(1, false) != Teuchos::null);
206 
207  TEST_COMPARE(sh->getStateTimeIndexN()->getIndex(), ==, 1);
208  TEST_FLOATING_EQUALITY(sh->getStateTimeIndexN()->getTime(), 1.0, 1.0e-14);
209 
210  // ---------------------------------------------------------------------------
211 }
212 
213 // ************************************************************
214 // ************************************************************
215 TEUCHOS_UNIT_TEST(SolutionHistory, addState_With_Undo)
216 {
217  // Setup SolutionHistory for testing.
218  auto sh = rcp(new Tempus::SolutionHistory<double>());
219  TEUCHOS_TEST_FOR_EXCEPT(sh->isInitialized()); // Should NOT be initialized
220  // as no State is set.
221 
222  auto model = rcp(new Tempus_Test::DahlquistTestModel<double>(-1.0, false));
223  auto inArgsIC = model->getNominalValues();
224  auto icSoln = rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
225  auto state0 = Tempus::createSolutionStateX<double>(icSoln);
226  state0->setTime(0.0);
227  state0->setIndex(0);
228  sh->addState(state0);
229  sh->initialize();
230  TEUCHOS_TEST_FOR_EXCEPT(!sh->isInitialized());
231 
232  TEST_COMPARE(sh->getNumStates(), ==, 1);
233  TEST_FLOATING_EQUALITY(sh->getCurrentTime(), 0.0, 1.0e-14);
234  TEST_COMPARE(sh->getCurrentIndex(), ==, 0);
235  TEST_FLOATING_EQUALITY(sh->minTime(), 0.0, 1.0e-14);
236  TEST_FLOATING_EQUALITY(sh->maxTime(), 0.0, 1.0e-14);
237 
238  TEUCHOS_ASSERT(sh->getCurrentState() != Teuchos::null);
239  TEUCHOS_ASSERT(sh->getStateTimeIndexN(false) != Teuchos::null);
240  TEUCHOS_ASSERT(sh->getStateTimeIndexNM1(false) == Teuchos::null);
241  TEUCHOS_ASSERT(sh->getStateTimeIndexNM2(false) == Teuchos::null);
242  TEUCHOS_ASSERT(sh->getStateTimeIndex(0, false) != Teuchos::null);
243 
244  TEST_COMPARE(sh->getStateTimeIndexN()->getIndex(), ==, 0);
245  TEST_FLOATING_EQUALITY(sh->getStateTimeIndexN()->getTime(), 0.0, 1.0e-14);
246 
247  // ---------------------------------------------------------------------------
248 
249  // Second State
250  auto state1 = Tempus::createSolutionStateX<double>(icSoln);
251  state1->setTime(1.0);
252  state1->setIndex(1);
253  sh->addState(state1);
254 
255  TEST_COMPARE(sh->getNumStates(), ==, 2);
256  TEST_FLOATING_EQUALITY(sh->getCurrentTime(), 1.0, 1.0e-14);
257  TEST_COMPARE(sh->getCurrentIndex(), ==, 1);
258  TEST_FLOATING_EQUALITY(sh->minTime(), 0.0, 1.0e-14);
259  TEST_FLOATING_EQUALITY(sh->maxTime(), 1.0, 1.0e-14);
260 
261  TEUCHOS_ASSERT(sh->getCurrentState() != Teuchos::null);
262  TEUCHOS_ASSERT(sh->getStateTimeIndexN(false) != Teuchos::null);
263  TEUCHOS_ASSERT(sh->getStateTimeIndexNM1(false) != Teuchos::null);
264  TEUCHOS_ASSERT(sh->getStateTimeIndexNM2(false) == Teuchos::null);
265  TEUCHOS_ASSERT(sh->getStateTimeIndex(0, false) != Teuchos::null);
266  TEUCHOS_ASSERT(sh->getStateTimeIndex(1, false) != Teuchos::null);
267 
268  TEST_COMPARE(sh->getStateTimeIndexN()->getIndex(), ==, 1);
269  TEST_COMPARE(sh->getStateTimeIndexNM1()->getIndex(), ==, 0);
270  TEST_FLOATING_EQUALITY(sh->getStateTimeIndexN()->getTime(), 1.0, 1.0e-14);
271  TEST_FLOATING_EQUALITY(sh->getStateTimeIndexNM1()->getTime(), 0.0, 1.0e-14);
272 
273  // ---------------------------------------------------------------------------
274 
275  // Third State -- should be added and first state dropped.
276  auto state2 = Tempus::createSolutionStateX<double>(icSoln);
277  state2->setTime(2.0);
278  state2->setIndex(2);
279  sh->addState(state2);
280 
281  TEST_COMPARE(sh->getNumStates(), ==, 2); // Only 2 states!
282  TEST_FLOATING_EQUALITY(sh->getCurrentTime(), 2.0, 1.0e-14);
283  TEST_COMPARE(sh->getCurrentIndex(), ==, 2);
284  TEST_FLOATING_EQUALITY(sh->minTime(), 1.0, 1.0e-14);
285  TEST_FLOATING_EQUALITY(sh->maxTime(), 2.0, 1.0e-14);
286 
287  TEUCHOS_ASSERT(sh->getCurrentState() != Teuchos::null);
288  TEUCHOS_ASSERT(sh->getStateTimeIndexN(false) != Teuchos::null);
289  TEUCHOS_ASSERT(sh->getStateTimeIndexNM1(false) != Teuchos::null);
290  TEUCHOS_ASSERT(sh->getStateTimeIndexNM2(false) == Teuchos::null);
291  TEUCHOS_ASSERT(sh->getStateTimeIndex(1, false) != Teuchos::null);
292  TEUCHOS_ASSERT(sh->getStateTimeIndex(2, false) != Teuchos::null);
293 
294  TEST_COMPARE(sh->getStateTimeIndexN()->getIndex(), ==, 2);
295  TEST_COMPARE(sh->getStateTimeIndexNM1()->getIndex(), ==, 1);
296  TEST_FLOATING_EQUALITY(sh->getStateTimeIndexN()->getTime(), 2.0, 1.0e-14);
297  TEST_FLOATING_EQUALITY(sh->getStateTimeIndexNM1()->getTime(), 1.0, 1.0e-14);
298 
299  // ---------------------------------------------------------------------------
300 
301  // Fourth State -- old state should not be added.
302  sh->addState(state0);
303 
304  // Still third and second states.
305  TEST_COMPARE(sh->getNumStates(), ==, 2); // Only 2 states!
306  TEST_FLOATING_EQUALITY(sh->getCurrentTime(), 2.0, 1.0e-14);
307  TEST_COMPARE(sh->getCurrentIndex(), ==, 2);
308  TEST_FLOATING_EQUALITY(sh->minTime(), 1.0, 1.0e-14);
309  TEST_FLOATING_EQUALITY(sh->maxTime(), 2.0, 1.0e-14);
310 
311  TEUCHOS_ASSERT(sh->getCurrentState() != Teuchos::null);
312  TEUCHOS_ASSERT(sh->getStateTimeIndexN(false) != Teuchos::null);
313  TEUCHOS_ASSERT(sh->getStateTimeIndexNM1(false) != Teuchos::null);
314  TEUCHOS_ASSERT(sh->getStateTimeIndexNM2(false) == Teuchos::null);
315  TEUCHOS_ASSERT(sh->getStateTimeIndex(1, false) != Teuchos::null);
316  TEUCHOS_ASSERT(sh->getStateTimeIndex(2, false) != Teuchos::null);
317 
318  TEST_COMPARE(sh->getStateTimeIndexN()->getIndex(), ==, 2);
319  TEST_COMPARE(sh->getStateTimeIndexNM1()->getIndex(), ==, 1);
320  TEST_FLOATING_EQUALITY(sh->getStateTimeIndexN()->getTime(), 2.0, 1.0e-14);
321  TEST_FLOATING_EQUALITY(sh->getStateTimeIndexNM1()->getTime(), 1.0, 1.0e-14);
322 
323  // ---------------------------------------------------------------------------
324 }
325 
326 // ************************************************************
327 // ************************************************************
328 TEUCHOS_UNIT_TEST(SolutionHistory, addState_With_Static)
329 {
330  // Setup SolutionHistory for testing.
331  auto sh = rcp(new Tempus::SolutionHistory<double>());
332  TEUCHOS_TEST_FOR_EXCEPT(sh->isInitialized()); // Should NOT be initialized
333  // as no State is set.
334  sh->setStorageTypeString("Static");
335  sh->setStorageLimit(7);
336 
337  auto model = rcp(new Tempus_Test::DahlquistTestModel<double>(-1.0, false));
338  auto inArgsIC = model->getNominalValues();
339 
340  // Sequential insertion.
341  // ---------------------------------------------------------------------------
342  for (size_t i = 0; i < 13; ++i) {
343  auto icSoln = rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
344  auto stateI = Tempus::createSolutionStateX<double>(icSoln);
345  stateI->setTime(i * 0.9);
346  stateI->setTimeStep(0.9);
347  stateI->setIndex(i);
348  sh->addState(stateI);
349  }
350  sh->initialize();
351  TEUCHOS_TEST_FOR_EXCEPT(!sh->isInitialized());
352 
353  sh->describe(out, Teuchos::VERB_MEDIUM);
354 
355  TEST_COMPARE(sh->getNumStates(), ==, 7);
356  TEST_FLOATING_EQUALITY(sh->getCurrentTime(), 10.8, 1.0e-14);
357  TEST_COMPARE(sh->getCurrentIndex(), ==, 12);
358  TEST_FLOATING_EQUALITY(sh->minTime(), 5.4, 1.0e-14);
359  TEST_FLOATING_EQUALITY(sh->maxTime(), 10.8, 1.0e-14);
360 
361  TEUCHOS_ASSERT(sh->getCurrentState() != Teuchos::null);
362  TEUCHOS_ASSERT(sh->getStateTimeIndexN() != Teuchos::null);
363  TEUCHOS_ASSERT(sh->getStateTimeIndexNM1() != Teuchos::null);
364  TEUCHOS_ASSERT(sh->getStateTimeIndexNM2() != Teuchos::null);
365  TEUCHOS_ASSERT(sh->getStateTimeIndex(7) != Teuchos::null);
366 
367  TEST_COMPARE(sh->getStateTimeIndexN()->getIndex(), ==, 12);
368  TEST_FLOATING_EQUALITY(sh->getStateTimeIndexN()->getTime(), 10.8, 1.0e-14);
369 
370  TEST_COMPARE(sh->getStateTimeIndexNM1()->getIndex(), ==, 11);
371  TEST_FLOATING_EQUALITY(sh->getStateTimeIndexNM1()->getTime(), 9.9, 1.0e-14);
372 
373  TEST_COMPARE(sh->getStateTimeIndexNM2()->getIndex(), ==, 10);
374  TEST_FLOATING_EQUALITY(sh->getStateTimeIndexNM2()->getTime(), 9.0, 1.0e-14);
375 
376  TEST_COMPARE(sh->getStateTimeIndex(7)->getIndex(), ==, 7);
377  TEST_FLOATING_EQUALITY(sh->getStateTimeIndex(7)->getTime(), 6.3, 1.0e-14);
378 
379  // ---------------------------------------------------------------------------
380 
381  // "Random" insertion.
382  // ---------------------------------------------------------------------------
383  sh->clear();
384  for (size_t i = 0; i < 3; ++i) {
385  auto icSoln = rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
386  auto stateI = Tempus::createSolutionStateX<double>(icSoln);
387  stateI->setTime(2 * i * 0.9 + 6.3);
388  stateI->setTimeStep(0.9);
389  stateI->setIndex(2 * i + 7);
390  sh->addState(stateI);
391  }
392  for (size_t i = 0; i < 4; ++i) {
393  auto icSoln = rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
394  auto stateI = Tempus::createSolutionStateX<double>(icSoln);
395  stateI->setTime(2 * i * 0.9 + 5.4);
396  stateI->setTimeStep(0.9);
397  stateI->setIndex(2 * i + 6);
398  sh->addState(stateI);
399  }
400  sh->initialize();
401  TEUCHOS_TEST_FOR_EXCEPT(!sh->isInitialized());
402 
403  sh->describe(out, Teuchos::VERB_MEDIUM);
404 
405  TEST_COMPARE(sh->getNumStates(), ==, 7);
406  TEST_FLOATING_EQUALITY(sh->getCurrentTime(), 10.8, 1.0e-14);
407  TEST_COMPARE(sh->getCurrentIndex(), ==, 12);
408  TEST_FLOATING_EQUALITY(sh->minTime(), 5.4, 1.0e-14);
409  TEST_FLOATING_EQUALITY(sh->maxTime(), 10.8, 1.0e-14);
410 
411  TEUCHOS_ASSERT(sh->getCurrentState() != Teuchos::null);
412  TEUCHOS_ASSERT(sh->getStateTimeIndexN() != Teuchos::null);
413  TEUCHOS_ASSERT(sh->getStateTimeIndexNM1() != Teuchos::null);
414  TEUCHOS_ASSERT(sh->getStateTimeIndexNM2() != Teuchos::null);
415  TEUCHOS_ASSERT(sh->getStateTimeIndex(7) != Teuchos::null);
416 
417  TEST_COMPARE(sh->getStateTimeIndexN()->getIndex(), ==, 12);
418  TEST_FLOATING_EQUALITY(sh->getStateTimeIndexN()->getTime(), 10.8, 1.0e-14);
419 
420  TEST_COMPARE(sh->getStateTimeIndexNM1()->getIndex(), ==, 11);
421  TEST_FLOATING_EQUALITY(sh->getStateTimeIndexNM1()->getTime(), 9.9, 1.0e-14);
422 
423  TEST_COMPARE(sh->getStateTimeIndexNM2()->getIndex(), ==, 10);
424  TEST_FLOATING_EQUALITY(sh->getStateTimeIndexNM2()->getTime(), 9.0, 1.0e-14);
425 
426  TEST_COMPARE(sh->getStateTimeIndex(7)->getIndex(), ==, 7);
427  TEST_FLOATING_EQUALITY(sh->getStateTimeIndex(7)->getTime(), 6.3, 1.0e-14);
428 
429  // ---------------------------------------------------------------------------
430 }
431 
432 // ************************************************************
433 // ************************************************************
434 TEUCHOS_UNIT_TEST(SolutionHistory, removeState)
435 {
436  // Setup SolutionHistory for testing.
437  auto sh = rcp(new Tempus::SolutionHistory<double>());
438  TEUCHOS_TEST_FOR_EXCEPT(sh->isInitialized()); // Should NOT be initialized
439  // as no State is set.
440  sh->setStorageTypeString("Static");
441  sh->setStorageLimit(7);
442 
443  auto model = rcp(new Tempus_Test::DahlquistTestModel<double>(-1.0, false));
444  auto inArgsIC = model->getNominalValues();
445 
446  // ---------------------------------------------------------------------------
447  for (size_t i = 0; i < 13; ++i) {
448  auto icSoln = rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
449  auto stateI = Tempus::createSolutionStateX<double>(icSoln);
450  stateI->setTime(i * 0.9);
451  stateI->setTimeStep(0.9);
452  stateI->setIndex(i);
453  sh->addState(stateI);
454  }
455  sh->initialize();
456  TEUCHOS_TEST_FOR_EXCEPT(!sh->isInitialized());
457 
458  sh->describe(out, Teuchos::VERB_MEDIUM);
459 
460  sh->removeState(sh->getStateTimeIndex(6));
461  sh->removeState(7.2);
462  sh->removeState(sh->getStateTimeIndex(10));
463  sh->removeState(10.8);
464 
465  sh->describe(out, Teuchos::VERB_MEDIUM);
466 
467  TEST_COMPARE(sh->getNumStates(), ==, 3);
468  TEST_FLOATING_EQUALITY(sh->getCurrentTime(), 9.9, 1.0e-14);
469  TEST_COMPARE(sh->getCurrentIndex(), ==, 11);
470  TEST_FLOATING_EQUALITY(sh->minTime(), 6.3, 1.0e-14);
471  TEST_FLOATING_EQUALITY(sh->maxTime(), 9.9, 1.0e-14);
472 
473  TEUCHOS_ASSERT(sh->getCurrentState() != Teuchos::null);
474  TEUCHOS_ASSERT(sh->getStateTimeIndexN() != Teuchos::null);
475  TEUCHOS_ASSERT(sh->getStateTimeIndexNM1() == Teuchos::null);
476  TEUCHOS_ASSERT(sh->getStateTimeIndexNM2() != Teuchos::null);
477  TEUCHOS_ASSERT(sh->getStateTimeIndex(7) != Teuchos::null);
478 
479  TEST_COMPARE(sh->getStateTimeIndexN()->getIndex(), ==, 11);
480  TEST_FLOATING_EQUALITY(sh->getStateTimeIndexN()->getTime(), 9.9, 1.0e-14);
481 
482  TEST_COMPARE(sh->getStateTimeIndexNM2()->getIndex(), ==, 9);
483  TEST_FLOATING_EQUALITY(sh->getStateTimeIndexNM2()->getTime(), 8.1, 1.0e-14);
484 
485  // ---------------------------------------------------------------------------
486 }
487 
488 // ************************************************************
489 // ************************************************************
490 TEUCHOS_UNIT_TEST(SolutionHistory, initWorkingState_Passing)
491 {
492  // Setup SolutionHistory for testing.
493  auto sh = rcp(new Tempus::SolutionHistory<double>());
494  TEUCHOS_TEST_FOR_EXCEPT(sh->isInitialized()); // Should NOT be initialized
495  // as no State is set.
496 
497  auto model = rcp(new Tempus_Test::DahlquistTestModel<double>(-1.0, false));
498  auto inArgsIC = model->getNominalValues();
499  auto icSoln = rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
500  auto state0 = Tempus::createSolutionStateX<double>(icSoln);
501  state0->setTime(0.0);
502  state0->setTimeStep(1.0);
503  state0->setIndex(0);
504  sh->addState(state0);
505  sh->initialize();
506  TEUCHOS_TEST_FOR_EXCEPT(!sh->isInitialized());
507 
508  // State before initializing workingState
509  // with sh->getWorkingState() == Teuchos::null, i.e., workingState
510  // was promoted from last time step or initial condition.
511  TEST_COMPARE(sh->getNumStates(), ==, 1);
512  TEUCHOS_ASSERT(sh->getCurrentState() != Teuchos::null);
513  TEUCHOS_ASSERT(sh->getCurrentState() == sh->getStateTimeIndexN());
514  TEUCHOS_ASSERT(sh->getWorkingState() == Teuchos::null);
515 
516  sh->initWorkingState();
517 
518  // State after initializing workingState.
519  TEST_COMPARE(sh->getNumStates(), ==, 2);
520  TEST_FLOATING_EQUALITY(sh->getCurrentTime(), 0.0, 1.0e-14);
521  TEST_COMPARE(sh->getCurrentIndex(), ==, 0);
522  TEST_FLOATING_EQUALITY(sh->minTime(), 0.0, 1.0e-14);
523  TEST_FLOATING_EQUALITY(sh->maxTime(), 1.0, 1.0e-14);
524 
525  TEUCHOS_ASSERT(sh->getCurrentState() != Teuchos::null);
526  TEUCHOS_ASSERT(sh->getCurrentState() == sh->getStateTimeIndexNM1());
527  TEUCHOS_ASSERT(sh->getWorkingState() != Teuchos::null);
528  TEUCHOS_ASSERT(sh->getWorkingState() == sh->getStateTimeIndexN());
529 
530  TEST_COMPARE(sh->getCurrentState()->getIndex(), ==, 0);
531  TEST_FLOATING_EQUALITY(sh->getCurrentState()->getTime(), 0.0, 1.0e-14);
532 
533  TEST_COMPARE(sh->getWorkingState()->getIndex(), ==, 1);
534  TEST_FLOATING_EQUALITY(sh->getWorkingState()->getTime(), 1.0, 1.0e-14);
535 
536  // ---------------------------------------------------------------------------
537 }
538 
539 // ************************************************************
540 // ************************************************************
541 TEUCHOS_UNIT_TEST(SolutionHistory, initWorkingState_Failing)
542 {
543  // Setup SolutionHistory for testing.
544  auto sh = rcp(new Tempus::SolutionHistory<double>());
545  TEUCHOS_TEST_FOR_EXCEPT(sh->isInitialized()); // Should NOT be initialized
546  // as no State is set.
547 
548  auto model = rcp(new Tempus_Test::DahlquistTestModel<double>(-1.0, false));
549  auto inArgsIC = model->getNominalValues();
550  auto icSoln = rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
551  auto state0 = Tempus::createSolutionStateX<double>(icSoln);
552  state0->setTime(0.0);
553  state0->setTimeStep(1.0);
554  state0->setIndex(0);
555  sh->addState(state0);
556  sh->initialize();
557  TEUCHOS_TEST_FOR_EXCEPT(!sh->isInitialized());
558 
559  auto currentSoln = icSoln->clone_v();
560  Thyra::V_S(currentSoln.ptr(), std::numeric_limits<double>::quiet_NaN());
561  auto state1 = Tempus::createSolutionStateX<double>(currentSoln);
562  state1->setTime(1.0);
563  state1->setTimeStep(1.0);
564  state1->setIndex(1);
565  sh->addWorkingState(state1);
566  sh->getWorkingState()->setSolutionStatus(Tempus::Status::FAILED);
567 
568  // State before initializing workingState
569  // with sh->getWorkingState() != Teuchos::null, i.e., workingState
570  // was NOT promoted from last time step or initial condition.
571  TEST_COMPARE(sh->getNumStates(), ==, 2);
572  TEST_FLOATING_EQUALITY(sh->getCurrentTime(), 0.0, 1.0e-14);
573  TEST_COMPARE(sh->getCurrentIndex(), ==, 0);
574  TEST_FLOATING_EQUALITY(sh->minTime(), 0.0, 1.0e-14);
575  TEST_FLOATING_EQUALITY(sh->maxTime(), 1.0, 1.0e-14);
576 
577  TEUCHOS_ASSERT(sh->getCurrentState() != Teuchos::null);
578  TEUCHOS_ASSERT(sh->getCurrentState() == sh->getStateTimeIndexNM1());
579  TEUCHOS_ASSERT(get_ele(*(sh->getCurrentState()->getX()), 0) == 1.0);
580 
581  TEUCHOS_ASSERT(sh->getWorkingState() != Teuchos::null);
582  TEUCHOS_ASSERT(sh->getWorkingState() == sh->getStateTimeIndexN());
583  TEUCHOS_ASSERT(std::isnan(get_ele(*(sh->getWorkingState()->getX()), 0))); // !!!
584 
585  TEST_COMPARE(sh->getCurrentState()->getIndex(), ==, 0);
586  TEST_FLOATING_EQUALITY(sh->getCurrentState()->getTime(), 0.0, 1.0e-14);
587 
588  TEST_COMPARE(sh->getWorkingState()->getIndex(), ==, 1);
589  TEST_FLOATING_EQUALITY(sh->getWorkingState()->getTime(), 1.0, 1.0e-14);
590 
591  sh->initWorkingState();
592 
593  // State after initializing workingState.
594  // Should be unchanged except the workingState->getX() is reset
595  // to currentState->getX().
596  TEST_COMPARE(sh->getNumStates(), ==, 2);
597  TEST_FLOATING_EQUALITY(sh->getCurrentTime(), 0.0, 1.0e-14);
598  TEST_COMPARE(sh->getCurrentIndex(), ==, 0);
599  TEST_FLOATING_EQUALITY(sh->minTime(), 0.0, 1.0e-14);
600  TEST_FLOATING_EQUALITY(sh->maxTime(), 1.0, 1.0e-14);
601 
602  TEUCHOS_ASSERT(sh->getCurrentState() != Teuchos::null);
603  TEUCHOS_ASSERT(sh->getCurrentState() == sh->getStateTimeIndexNM1());
604  TEUCHOS_ASSERT(get_ele(*(sh->getCurrentState()->getX()), 0) == 1.0);
605 
606  TEUCHOS_ASSERT(sh->getWorkingState() != Teuchos::null);
607  TEUCHOS_ASSERT(sh->getWorkingState() == sh->getStateTimeIndexN());
608  TEUCHOS_ASSERT(get_ele(*(sh->getWorkingState()->getX()), 0) == 1.0); // !!!
609 
610  TEST_COMPARE(sh->getCurrentState()->getIndex(), ==, 0);
611  TEST_FLOATING_EQUALITY(sh->getCurrentState()->getTime(), 0.0, 1.0e-14);
612 
613  TEST_COMPARE(sh->getWorkingState()->getIndex(), ==, 1);
614  TEST_FLOATING_EQUALITY(sh->getWorkingState()->getTime(), 1.0, 1.0e-14);
615 
616  // ---------------------------------------------------------------------------
617 }
618 
619 // ************************************************************
620 // ************************************************************
621 TEUCHOS_UNIT_TEST(SolutionHistory, promoteWorkingState_Passing)
622 {
623  // Setup SolutionHistory for testing.
624  auto sh = rcp(new Tempus::SolutionHistory<double>());
625  TEUCHOS_TEST_FOR_EXCEPT(sh->isInitialized()); // Should NOT be initialized
626  // as no State is set.
627 
628  auto model = rcp(new Tempus_Test::DahlquistTestModel<double>(-1.0, false));
629  auto inArgsIC = model->getNominalValues();
630  auto icSoln = rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
631  auto state0 = Tempus::createSolutionStateX<double>(icSoln);
632  state0->setTime(0.0);
633  state0->setTimeStep(1.0);
634  state0->setIndex(0);
635  sh->addState(state0);
636  sh->initialize();
637  TEUCHOS_TEST_FOR_EXCEPT(!sh->isInitialized());
638  sh->initWorkingState();
639  sh->getWorkingState()->setSolutionStatus(Tempus::Status::PASSED);
640 
641  // State before promoting workingState.
642  // with workingState PASSing.
643  TEST_COMPARE(sh->getNumStates(), ==, 2);
644  TEUCHOS_ASSERT(sh->getCurrentState() != Teuchos::null);
645  TEUCHOS_ASSERT(sh->getCurrentState() == sh->getStateTimeIndexNM1());
646  TEUCHOS_ASSERT(sh->getWorkingState() != Teuchos::null);
647  TEUCHOS_ASSERT(sh->getWorkingState() == sh->getStateTimeIndexN());
648 
649  TEST_COMPARE(sh->getCurrentState()->getIndex(), ==, 0);
650  TEST_FLOATING_EQUALITY(sh->getCurrentState()->getTime(), 0.0, 1.0e-14);
651 
652  TEST_COMPARE(sh->getWorkingState()->getIndex(), ==, 1);
653  TEST_FLOATING_EQUALITY(sh->getWorkingState()->getTime(), 1.0, 1.0e-14);
654 
655  sh->promoteWorkingState();
656 
657  // State after promoting workingState.
658  TEST_COMPARE(sh->getNumStates(), ==, 2);
659  TEUCHOS_ASSERT(sh->getCurrentState() != Teuchos::null);
660  TEUCHOS_ASSERT(sh->getCurrentState() == sh->getStateTimeIndexN());
661  TEUCHOS_ASSERT(sh->getWorkingState() == Teuchos::null);
662 
663  // Current state is now at t=1.0!
664  TEST_COMPARE(sh->getCurrentState()->getIndex(), ==, 1);
665  TEST_FLOATING_EQUALITY(sh->getCurrentState()->getTime(), 1.0, 1.0e-14);
666 
667  // ---------------------------------------------------------------------------
668 }
669 
670 // ************************************************************
671 // ************************************************************
672 TEUCHOS_UNIT_TEST(SolutionHistory, promoteWorkingState_Failing)
673 {
674  // Setup SolutionHistory for testing.
675  auto sh = rcp(new Tempus::SolutionHistory<double>());
676  TEUCHOS_TEST_FOR_EXCEPT(sh->isInitialized()); // Should NOT be initialized
677  // as no State is set.
678 
679  auto model = rcp(new Tempus_Test::DahlquistTestModel<double>(-1.0, false));
680  auto inArgsIC = model->getNominalValues();
681  auto icSoln = rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
682  auto state0 = Tempus::createSolutionStateX<double>(icSoln);
683  state0->setTime(0.0);
684  state0->setTimeStep(1.0);
685  state0->setIndex(0);
686  sh->addState(state0);
687  sh->initialize();
688  TEUCHOS_TEST_FOR_EXCEPT(!sh->isInitialized());
689  sh->initWorkingState();
690  sh->getWorkingState()->setSolutionStatus(Tempus::Status::FAILED);
691 
692  // State before promoting workingState.
693  // with workingState FAILing.
694  TEST_COMPARE(sh->getNumStates(), ==, 2);
695  TEUCHOS_ASSERT(sh->getCurrentState() != Teuchos::null);
696  TEUCHOS_ASSERT(sh->getCurrentState() == sh->getStateTimeIndexNM1());
697  TEUCHOS_ASSERT(sh->getWorkingState() != Teuchos::null);
698  TEUCHOS_ASSERT(sh->getWorkingState() == sh->getStateTimeIndexN());
699 
700  TEST_COMPARE(sh->getCurrentState()->getIndex(), ==, 0);
701  TEST_FLOATING_EQUALITY(sh->getCurrentState()->getTime(), 0.0, 1.0e-14);
702 
703  TEST_COMPARE(sh->getWorkingState()->getIndex(), ==, 1);
704  TEST_FLOATING_EQUALITY(sh->getWorkingState()->getTime(), 1.0, 1.0e-14);
705 
706  sh->promoteWorkingState();
707 
708  // State after promoting workingState.
709  // Should be unchanged as we are trying to redo FAILing time step.
710  TEST_COMPARE(sh->getNumStates(), ==, 2);
711  TEUCHOS_ASSERT(sh->getCurrentState() != Teuchos::null);
712  TEUCHOS_ASSERT(sh->getCurrentState() == sh->getStateTimeIndexNM1());
713  TEUCHOS_ASSERT(sh->getWorkingState() != Teuchos::null);
714  TEUCHOS_ASSERT(sh->getWorkingState() == sh->getStateTimeIndexN());
715 
716  // Current state is still at t=0.0!
717  TEST_COMPARE(sh->getCurrentState()->getIndex(), ==, 0);
718  TEST_FLOATING_EQUALITY(sh->getCurrentState()->getTime(), 0.0, 1.0e-14);
719 
720  // Working state is still at t=1.0!
721  TEST_COMPARE(sh->getWorkingState()->getIndex(), ==, 1);
722  TEST_FLOATING_EQUALITY(sh->getWorkingState()->getTime(), 1.0, 1.0e-14);
723 
724  // ---------------------------------------------------------------------------
725 }
726 
727 } // namespace Tempus_Unit_Test
Keep the 2 newest states for undo.
The classic Dahlquist Test Problem.
#define TEST_COMPARE(v1, comp, v2)
#define TEST_FLOATING_EQUALITY(v1, v2, tol)
Sine-Cosine model problem from Rythmos. This is a canonical Sine-Cosine differential equation with a...
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
static Teuchos::RCP< Interpolator< Scalar > > createInterpolator(std::string interpolatorType="")
Create default interpolator from interpolator type (e.g., &quot;Linear&quot;).
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
TEUCHOS_UNIT_TEST(BackwardEuler, Default_Construction)
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
Keep a fix number of states.
ParameterList & sublist(const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
ParameterList & setName(const std::string &name)
#define TEUCHOS_ASSERT(assertion_test)
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
Solution state for integrators and steppers.