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: Copyright (2017) Sandia Corporation
4 //
5 // Distributed under BSD 3-clause license (See accompanying file Copyright.txt)
6 // ****************************************************************************
7 // @HEADER
8 
10 
11 #include "Tempus_SolutionHistory.hpp"
13 
14 #include "../TestModels/DahlquistTestModel.hpp"
15 
16 
17 namespace Tempus_Unit_Test {
18 
19 using Teuchos::RCP;
20 using Teuchos::rcp;
21 using Teuchos::rcp_const_cast;
22 using Teuchos::rcp_dynamic_cast;
24 using Teuchos::sublist;
25 
26 
27 // ************************************************************
28 // ************************************************************
29 TEUCHOS_UNIT_TEST(SolutionHistory, Default_Construction)
30 {
31  auto sh = rcp(new Tempus::SolutionHistory<double>());
32  TEUCHOS_TEST_FOR_EXCEPT(sh->isInitialized()); // Should NOT be initialized
33  // as no State is set.
34 
35  auto model = rcp(new Tempus_Test::SinCosModel<double>());
36  auto inArgsIC = model->getNominalValues();
37  auto icSolution = 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"); TEUCHOS_TEST_FOR_EXCEPT(!sh->isInitialized());
51  sh->setStorageType (Tempus::STORAGE_TYPE_STATIC); sh->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!sh->isInitialized());
52  sh->setStorageTypeString ("Static"); sh->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!sh->isInitialized());
53  sh->setStorageLimit(99); sh->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!sh->isInitialized());
54 
55  TEST_COMPARE( sh->getName () , ==, "Testing");
56  TEST_COMPARE( sh->getStorageType () , ==, Tempus::STORAGE_TYPE_STATIC);
57  TEST_COMPARE( sh->getStorageTypeString(), ==, "Static");
58  TEST_COMPARE( sh->getStorageLimit() , ==, 99);
59 
60  TEST_FLOATING_EQUALITY( sh->minTime(), 0.0, 1.0e-14);
61  TEST_FLOATING_EQUALITY( sh->maxTime(), 0.0, 1.0e-14);
62 
63 }
64 
65 
66 // ************************************************************
67 // ************************************************************
68 TEUCHOS_UNIT_TEST(SolutionHistory, Full_Construction)
69 {
70  std::string name = "Unit Test";
71 
72  auto history = rcp(new std::vector<RCP<Tempus::SolutionState<double> > >);;
73  auto model = rcp(new Tempus_Test::SinCosModel<double>());
74  auto inArgsIC = model->getNominalValues();
75  auto icSolution = rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
77  icState = Tempus::createSolutionStateX<double>(icSolution);
78  history->push_back(icState);
79 
81 
83  int storageLimit = 99;
84 
86  name, history, interpolator, storageType, storageLimit));
87  TEUCHOS_TEST_FOR_EXCEPT(!sh->isInitialized());
88 
89  TEST_COMPARE ( sh->getNumStates(), ==, 1);
90  TEST_COMPARE ( sh->getInterpolator()->order(), ==, 0);
91  TEST_COMPARE ( sh->getStorageType(), ==, Tempus::STORAGE_TYPE_STATIC);
92  TEST_COMPARE ( sh->getStorageLimit(), ==, 99);
93 
94 }
95 
96 
97 // ************************************************************
98 // ************************************************************
99 TEUCHOS_UNIT_TEST(SolutionHistory, Create_Construction)
100 {
101  auto sh_temp = rcp(new Tempus::SolutionHistory<double>());
102  RCP<ParameterList> pl = sh_temp->getNonconstParameterList();
103 
104  pl->setName("Unit Test");
105  pl->set<std::string>("Storage Type", "Static" );
106  pl->set<int> ("Storage Limit", 99);
107 
108  pl->sublist("Interpolator").set("Interpolator Type", "Lagrange");
109  pl->sublist("Interpolator").set("Order", 1);
110 
111  auto sh = Tempus::createSolutionHistoryPL<double>(pl);
112  TEUCHOS_TEST_FOR_EXCEPT(sh->isInitialized()); // Should NOT be initialized
113  // as no State is set.
114 
115  TEST_COMPARE ( sh->getNumStates(), ==, 0);
116  TEST_COMPARE ( sh->getInterpolator()->order(), ==, 1);
117  TEST_COMPARE ( sh->getStorageType(), ==, Tempus::STORAGE_TYPE_STATIC);
118  TEST_COMPARE ( sh->getStorageLimit(), ==, 99);
119 
120 }
121 
122 
123 // ************************************************************
124 // ************************************************************
125 TEUCHOS_UNIT_TEST(SolutionHistory, addState_With_Keep_Newest)
126 {
127  // Setup SolutionHistory for testing.
128  auto sh = rcp(new Tempus::SolutionHistory<double>());
129  TEUCHOS_TEST_FOR_EXCEPT(sh->isInitialized()); // Should NOT be initialized
130  // as no State is set.
131 
132  auto model = rcp(new Tempus_Test::DahlquistTestModel<double>(-1.0, false));
133  auto inArgsIC = model->getNominalValues();
134  auto icSoln = rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
135  auto state0 = Tempus::createSolutionStateX<double>(icSoln);
136  state0->setTime (0.0);
137  state0->setIndex(0);
138 
139  sh->setStorageTypeString ("Keep Newest");
140  sh->addState(state0);
141  sh->initialize();
142  TEUCHOS_TEST_FOR_EXCEPT(!sh->isInitialized());
143 
144  TEST_COMPARE ( sh->getNumStates(), ==, 1);
145  TEST_FLOATING_EQUALITY( sh->getCurrentTime(), 0.0, 1.0e-14);
146  TEST_COMPARE ( sh->getCurrentIndex(), ==, 0);
147  TEST_FLOATING_EQUALITY( sh->minTime(), 0.0, 1.0e-14);
148  TEST_FLOATING_EQUALITY( sh->maxTime(), 0.0, 1.0e-14);
149 
150  TEUCHOS_ASSERT(sh->getCurrentState () != Teuchos::null);
151  TEUCHOS_ASSERT(sh->getStateTimeIndexN (false) != Teuchos::null);
152  TEUCHOS_ASSERT(sh->getStateTimeIndexNM1(false) == Teuchos::null);
153  TEUCHOS_ASSERT(sh->getStateTimeIndexNM2(false) == Teuchos::null);
154  TEUCHOS_ASSERT(sh->getStateTimeIndex (0, false) != Teuchos::null);
155 
156  TEST_COMPARE( sh->getStateTimeIndexN ()->getIndex(), ==, 0);
157  TEST_FLOATING_EQUALITY( sh->getStateTimeIndexN ()->getTime(), 0.0, 1.0e-14);
158 
159  // ---------------------------------------------------------------------------
160 
161  // Second State -- should replace first state.
162  auto state1 = Tempus::createSolutionStateX<double>(icSoln);
163  state1->setTime (1.0);
164  state1->setIndex(1);
165  sh->addState(state1);
166 
167  TEST_COMPARE ( sh->getNumStates(), ==, 1); // Only 1 state!
168  TEST_FLOATING_EQUALITY( sh->getCurrentTime(), 1.0, 1.0e-14);
169  TEST_COMPARE ( sh->getCurrentIndex(), ==, 1);
170  TEST_FLOATING_EQUALITY( sh->minTime(), 1.0, 1.0e-14);
171  TEST_FLOATING_EQUALITY( sh->maxTime(), 1.0, 1.0e-14);
172 
173  TEUCHOS_ASSERT(sh->getCurrentState () != Teuchos::null);
174  TEUCHOS_ASSERT(sh->getStateTimeIndexN (false) != Teuchos::null);
175  TEUCHOS_ASSERT(sh->getStateTimeIndexNM1(false) == Teuchos::null);
176  TEUCHOS_ASSERT(sh->getStateTimeIndexNM2(false) == Teuchos::null);
177  TEUCHOS_ASSERT(sh->getStateTimeIndex (0, false) == Teuchos::null);
178  TEUCHOS_ASSERT(sh->getStateTimeIndex (1, false) != Teuchos::null);
179 
180  TEST_COMPARE( sh->getStateTimeIndexN ()->getIndex(), ==, 1);
181  TEST_FLOATING_EQUALITY( sh->getStateTimeIndexN ()->getTime(), 1.0, 1.0e-14);
182 
183  // ---------------------------------------------------------------------------
184 
185  // Third State -- old state should not be added.
186  auto state2 = Tempus::createSolutionStateX<double>(icSoln);
187  state2->setTime (-1.0);
188  state2->setIndex(-1);
189  sh->addState(state2);
190 
191  // Still second state.
192  TEST_COMPARE ( sh->getNumStates(), ==, 1); // Only 1 state!
193  TEST_FLOATING_EQUALITY( sh->getCurrentTime(), 1.0, 1.0e-14);
194  TEST_COMPARE ( sh->getCurrentIndex(), ==, 1);
195  TEST_FLOATING_EQUALITY( sh->minTime(), 1.0, 1.0e-14);
196  TEST_FLOATING_EQUALITY( sh->maxTime(), 1.0, 1.0e-14);
197 
198  TEUCHOS_ASSERT(sh->getCurrentState () != Teuchos::null);
199  TEUCHOS_ASSERT(sh->getStateTimeIndexN (false) != Teuchos::null);
200  TEUCHOS_ASSERT(sh->getStateTimeIndexNM1(false) == Teuchos::null);
201  TEUCHOS_ASSERT(sh->getStateTimeIndexNM2(false) == Teuchos::null);
202  TEUCHOS_ASSERT(sh->getStateTimeIndex (0, false) == Teuchos::null);
203  TEUCHOS_ASSERT(sh->getStateTimeIndex (1, false) != Teuchos::null);
204 
205  TEST_COMPARE( sh->getStateTimeIndexN ()->getIndex(), ==, 1);
206  TEST_FLOATING_EQUALITY( sh->getStateTimeIndexN ()->getTime(), 1.0, 1.0e-14);
207 
208  // ---------------------------------------------------------------------------
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 // ************************************************************
329 // ************************************************************
330 TEUCHOS_UNIT_TEST(SolutionHistory, addState_With_Static)
331 {
332  // Setup SolutionHistory for testing.
333  auto sh = rcp(new Tempus::SolutionHistory<double>());
334  TEUCHOS_TEST_FOR_EXCEPT(sh->isInitialized()); // Should NOT be initialized
335  // as no State is set.
336  sh->setStorageTypeString ("Static");
337  sh->setStorageLimit(7);
338 
339  auto model = rcp(new Tempus_Test::DahlquistTestModel<double>(-1.0, false));
340  auto inArgsIC = model->getNominalValues();
341 
342  // Sequential insertion.
343  // ---------------------------------------------------------------------------
344  for (size_t i=0; i < 13; ++i) {
345  auto icSoln = rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
346  auto stateI = Tempus::createSolutionStateX<double>(icSoln);
347  stateI->setTime (i*0.9);
348  stateI->setTimeStep(0.9);
349  stateI->setIndex(i);
350  sh->addState(stateI);
351  }
352  sh->initialize();
353  TEUCHOS_TEST_FOR_EXCEPT(!sh->isInitialized());
354 
355  sh->describe(out, Teuchos::VERB_MEDIUM);
356 
357  TEST_COMPARE ( sh->getNumStates(), ==, 7);
358  TEST_FLOATING_EQUALITY( sh->getCurrentTime(), 10.8, 1.0e-14);
359  TEST_COMPARE ( sh->getCurrentIndex(), ==, 12);
360  TEST_FLOATING_EQUALITY( sh->minTime(), 5.4, 1.0e-14);
361  TEST_FLOATING_EQUALITY( sh->maxTime(), 10.8, 1.0e-14);
362 
363  TEUCHOS_ASSERT(sh->getCurrentState () != Teuchos::null);
364  TEUCHOS_ASSERT(sh->getStateTimeIndexN () != Teuchos::null);
365  TEUCHOS_ASSERT(sh->getStateTimeIndexNM1() != Teuchos::null);
366  TEUCHOS_ASSERT(sh->getStateTimeIndexNM2() != Teuchos::null);
367  TEUCHOS_ASSERT(sh->getStateTimeIndex (7)!= Teuchos::null);
368 
369  TEST_COMPARE( sh->getStateTimeIndexN ()->getIndex(), ==, 12);
370  TEST_FLOATING_EQUALITY( sh->getStateTimeIndexN ()->getTime(), 10.8, 1.0e-14);
371 
372  TEST_COMPARE( sh->getStateTimeIndexNM1()->getIndex(), ==, 11);
373  TEST_FLOATING_EQUALITY( sh->getStateTimeIndexNM1()->getTime(), 9.9, 1.0e-14);
374 
375  TEST_COMPARE( sh->getStateTimeIndexNM2()->getIndex(), ==, 10);
376  TEST_FLOATING_EQUALITY( sh->getStateTimeIndexNM2()->getTime(), 9.0, 1.0e-14);
377 
378  TEST_COMPARE( sh->getStateTimeIndex (7)->getIndex(), ==, 7);
379  TEST_FLOATING_EQUALITY( sh->getStateTimeIndex (7)->getTime(), 6.3, 1.0e-14);
380 
381  // ---------------------------------------------------------------------------
382 
383  // "Random" insertion.
384  // ---------------------------------------------------------------------------
385  sh->clear();
386  for (size_t i=0; i < 3; ++i) {
387  auto icSoln = rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
388  auto stateI = Tempus::createSolutionStateX<double>(icSoln);
389  stateI->setTime (2*i*0.9+6.3);
390  stateI->setTimeStep(0.9);
391  stateI->setIndex(2*i+7);
392  sh->addState(stateI);
393  }
394  for (size_t i=0; i < 4; ++i) {
395  auto icSoln = rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
396  auto stateI = Tempus::createSolutionStateX<double>(icSoln);
397  stateI->setTime (2*i*0.9+5.4);
398  stateI->setTimeStep(0.9);
399  stateI->setIndex(2*i+6);
400  sh->addState(stateI);
401  }
402  sh->initialize();
403  TEUCHOS_TEST_FOR_EXCEPT(!sh->isInitialized());
404 
405  sh->describe(out, Teuchos::VERB_MEDIUM);
406 
407  TEST_COMPARE ( sh->getNumStates(), ==, 7);
408  TEST_FLOATING_EQUALITY( sh->getCurrentTime(), 10.8, 1.0e-14);
409  TEST_COMPARE ( sh->getCurrentIndex(), ==, 12);
410  TEST_FLOATING_EQUALITY( sh->minTime(), 5.4, 1.0e-14);
411  TEST_FLOATING_EQUALITY( sh->maxTime(), 10.8, 1.0e-14);
412 
413  TEUCHOS_ASSERT(sh->getCurrentState () != Teuchos::null);
414  TEUCHOS_ASSERT(sh->getStateTimeIndexN () != Teuchos::null);
415  TEUCHOS_ASSERT(sh->getStateTimeIndexNM1() != Teuchos::null);
416  TEUCHOS_ASSERT(sh->getStateTimeIndexNM2() != Teuchos::null);
417  TEUCHOS_ASSERT(sh->getStateTimeIndex (7)!= Teuchos::null);
418 
419  TEST_COMPARE( sh->getStateTimeIndexN ()->getIndex(), ==, 12);
420  TEST_FLOATING_EQUALITY( sh->getStateTimeIndexN ()->getTime(), 10.8, 1.0e-14);
421 
422  TEST_COMPARE( sh->getStateTimeIndexNM1()->getIndex(), ==, 11);
423  TEST_FLOATING_EQUALITY( sh->getStateTimeIndexNM1()->getTime(), 9.9, 1.0e-14);
424 
425  TEST_COMPARE( sh->getStateTimeIndexNM2()->getIndex(), ==, 10);
426  TEST_FLOATING_EQUALITY( sh->getStateTimeIndexNM2()->getTime(), 9.0, 1.0e-14);
427 
428  TEST_COMPARE( sh->getStateTimeIndex (7)->getIndex(), ==, 7);
429  TEST_FLOATING_EQUALITY( sh->getStateTimeIndex (7)->getTime(), 6.3, 1.0e-14);
430 
431  // ---------------------------------------------------------------------------
432 
433 }
434 
435 
436 // ************************************************************
437 // ************************************************************
438 TEUCHOS_UNIT_TEST(SolutionHistory, removeState)
439 {
440  // Setup SolutionHistory for testing.
441  auto sh = rcp(new Tempus::SolutionHistory<double>());
442  TEUCHOS_TEST_FOR_EXCEPT(sh->isInitialized()); // Should NOT be initialized
443  // as no State is set.
444  sh->setStorageTypeString ("Static");
445  sh->setStorageLimit(7);
446 
447  auto model = rcp(new Tempus_Test::DahlquistTestModel<double>(-1.0, false));
448  auto inArgsIC = model->getNominalValues();
449 
450  // ---------------------------------------------------------------------------
451  for (size_t i=0; i < 13; ++i) {
452  auto icSoln = rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
453  auto stateI = Tempus::createSolutionStateX<double>(icSoln);
454  stateI->setTime (i*0.9);
455  stateI->setTimeStep(0.9);
456  stateI->setIndex(i);
457  sh->addState(stateI);
458  }
459  sh->initialize();
460  TEUCHOS_TEST_FOR_EXCEPT(!sh->isInitialized());
461 
462  sh->describe(out, Teuchos::VERB_MEDIUM);
463 
464  sh->removeState(sh->getStateTimeIndex(6));
465  sh->removeState( 7.2);
466  sh->removeState(sh->getStateTimeIndex(10));
467  sh->removeState(10.8);
468 
469  sh->describe(out, Teuchos::VERB_MEDIUM);
470 
471  TEST_COMPARE ( sh->getNumStates(), ==, 3);
472  TEST_FLOATING_EQUALITY( sh->getCurrentTime(), 9.9, 1.0e-14);
473  TEST_COMPARE ( sh->getCurrentIndex(), ==, 11);
474  TEST_FLOATING_EQUALITY( sh->minTime(), 6.3, 1.0e-14);
475  TEST_FLOATING_EQUALITY( sh->maxTime(), 9.9, 1.0e-14);
476 
477  TEUCHOS_ASSERT(sh->getCurrentState () != Teuchos::null);
478  TEUCHOS_ASSERT(sh->getStateTimeIndexN () != Teuchos::null);
479  TEUCHOS_ASSERT(sh->getStateTimeIndexNM1() == Teuchos::null);
480  TEUCHOS_ASSERT(sh->getStateTimeIndexNM2() != Teuchos::null);
481  TEUCHOS_ASSERT(sh->getStateTimeIndex (7)!= Teuchos::null);
482 
483  TEST_COMPARE( sh->getStateTimeIndexN ()->getIndex(), ==, 11);
484  TEST_FLOATING_EQUALITY( sh->getStateTimeIndexN ()->getTime(), 9.9, 1.0e-14);
485 
486  TEST_COMPARE( sh->getStateTimeIndexNM2()->getIndex(), ==, 9);
487  TEST_FLOATING_EQUALITY( sh->getStateTimeIndexNM2()->getTime(), 8.1, 1.0e-14);
488 
489  // ---------------------------------------------------------------------------
490 
491 }
492 
493 
494 // ************************************************************
495 // ************************************************************
496 TEUCHOS_UNIT_TEST(SolutionHistory, initWorkingState_Passing)
497 {
498  // Setup SolutionHistory for testing.
499  auto sh = rcp(new Tempus::SolutionHistory<double>());
500  TEUCHOS_TEST_FOR_EXCEPT(sh->isInitialized()); // Should NOT be initialized
501  // as no State is set.
502 
503  auto model = rcp(new Tempus_Test::DahlquistTestModel<double>(-1.0, false));
504  auto inArgsIC = model->getNominalValues();
505  auto icSoln = rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
506  auto state0 = Tempus::createSolutionStateX<double>(icSoln);
507  state0->setTime (0.0);
508  state0->setTimeStep(1.0);
509  state0->setIndex(0);
510  sh->addState(state0);
511  sh->initialize();
512  TEUCHOS_TEST_FOR_EXCEPT(!sh->isInitialized());
513 
514  // State before initializing workingState
515  // with sh->getWorkingState() == Teuchos::null, i.e., workingState
516  // was promoted from last time step or initial condition.
517  TEST_COMPARE ( sh->getNumStates(), ==, 1);
518  TEUCHOS_ASSERT(sh->getCurrentState() != Teuchos::null);
519  TEUCHOS_ASSERT(sh->getCurrentState() == sh->getStateTimeIndexN());
520  TEUCHOS_ASSERT(sh->getWorkingState() == Teuchos::null);
521 
522  sh->initWorkingState();
523 
524  // State after initializing workingState.
525  TEST_COMPARE ( sh->getNumStates(), ==, 2);
526  TEST_FLOATING_EQUALITY( sh->getCurrentTime(), 0.0, 1.0e-14);
527  TEST_COMPARE ( sh->getCurrentIndex(), ==, 0);
528  TEST_FLOATING_EQUALITY( sh->minTime(), 0.0, 1.0e-14);
529  TEST_FLOATING_EQUALITY( sh->maxTime(), 1.0, 1.0e-14);
530 
531  TEUCHOS_ASSERT(sh->getCurrentState() != Teuchos::null);
532  TEUCHOS_ASSERT(sh->getCurrentState() == sh->getStateTimeIndexNM1());
533  TEUCHOS_ASSERT(sh->getWorkingState() != Teuchos::null);
534  TEUCHOS_ASSERT(sh->getWorkingState() == sh->getStateTimeIndexN());
535 
536  TEST_COMPARE ( sh->getCurrentState()->getIndex(), ==, 0);
537  TEST_FLOATING_EQUALITY( sh->getCurrentState()->getTime(), 0.0, 1.0e-14);
538 
539  TEST_COMPARE ( sh->getWorkingState()->getIndex(), ==, 1);
540  TEST_FLOATING_EQUALITY( sh->getWorkingState()->getTime(), 1.0, 1.0e-14);
541 
542  // ---------------------------------------------------------------------------
543 
544 }
545 
546 
547 // ************************************************************
548 // ************************************************************
549 TEUCHOS_UNIT_TEST(SolutionHistory, initWorkingState_Failing)
550 {
551  // Setup SolutionHistory for testing.
552  auto sh = rcp(new Tempus::SolutionHistory<double>());
553  TEUCHOS_TEST_FOR_EXCEPT(sh->isInitialized()); // Should NOT be initialized
554  // as no State is set.
555 
556  auto model = rcp(new Tempus_Test::DahlquistTestModel<double>(-1.0, false));
557  auto inArgsIC = model->getNominalValues();
558  auto icSoln = rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
559  auto state0 = Tempus::createSolutionStateX<double>(icSoln);
560  state0->setTime (0.0);
561  state0->setTimeStep(1.0);
562  state0->setIndex(0);
563  sh->addState(state0);
564  sh->initialize();
565  TEUCHOS_TEST_FOR_EXCEPT(!sh->isInitialized());
566 
567  auto state1 = Tempus::createSolutionStateX<double>(icSoln);
568  state1->setTime (1.0);
569  state1->setTimeStep(1.0);
570  state1->setIndex(1);
571  sh->addWorkingState(state1);
572  sh->getWorkingState()->setSolutionStatus(Tempus::Status::FAILED);
573 
574  // State before initializing workingState
575  // with sh->getWorkingState() != Teuchos::null, i.e., workingState
576  // was NOT promoted from last time step or initial condition.
577  TEST_COMPARE ( sh->getNumStates(), ==, 2);
578  TEST_FLOATING_EQUALITY( sh->getCurrentTime(), 0.0, 1.0e-14);
579  TEST_COMPARE ( sh->getCurrentIndex(), ==, 0);
580  TEST_FLOATING_EQUALITY( sh->minTime(), 0.0, 1.0e-14);
581  TEST_FLOATING_EQUALITY( sh->maxTime(), 1.0, 1.0e-14);
582 
583  TEUCHOS_ASSERT(sh->getCurrentState() != Teuchos::null);
584  TEUCHOS_ASSERT(sh->getCurrentState() == sh->getStateTimeIndexNM1());
585  TEUCHOS_ASSERT(sh->getWorkingState() != Teuchos::null);
586  TEUCHOS_ASSERT(sh->getWorkingState() == sh->getStateTimeIndexN());
587 
588  TEST_COMPARE ( sh->getCurrentState()->getIndex(), ==, 0);
589  TEST_FLOATING_EQUALITY( sh->getCurrentState()->getTime(), 0.0, 1.0e-14);
590 
591  TEST_COMPARE ( sh->getWorkingState()->getIndex(), ==, 1);
592  TEST_FLOATING_EQUALITY( sh->getWorkingState()->getTime(), 1.0, 1.0e-14);
593 
594  sh->initWorkingState();
595 
596  // State after initializing workingState.
597  // Should be unchanged as we are trying to redo FAILing time step.
598  TEST_COMPARE ( sh->getNumStates(), ==, 2);
599  TEST_FLOATING_EQUALITY( sh->getCurrentTime(), 0.0, 1.0e-14);
600  TEST_COMPARE ( sh->getCurrentIndex(), ==, 0);
601  TEST_FLOATING_EQUALITY( sh->minTime(), 0.0, 1.0e-14);
602  TEST_FLOATING_EQUALITY( sh->maxTime(), 1.0, 1.0e-14);
603 
604  TEUCHOS_ASSERT(sh->getCurrentState() != Teuchos::null);
605  TEUCHOS_ASSERT(sh->getCurrentState() == sh->getStateTimeIndexNM1());
606  TEUCHOS_ASSERT(sh->getWorkingState() != Teuchos::null);
607  TEUCHOS_ASSERT(sh->getWorkingState() == sh->getStateTimeIndexN());
608 
609  TEST_COMPARE ( sh->getCurrentState()->getIndex(), ==, 0);
610  TEST_FLOATING_EQUALITY( sh->getCurrentState()->getTime(), 0.0, 1.0e-14);
611 
612  TEST_COMPARE ( sh->getWorkingState()->getIndex(), ==, 1);
613  TEST_FLOATING_EQUALITY( sh->getWorkingState()->getTime(), 1.0, 1.0e-14);
614 
615  // ---------------------------------------------------------------------------
616 
617 }
618 
619 
620 // ************************************************************
621 // ************************************************************
622 TEUCHOS_UNIT_TEST(SolutionHistory, promoteWorkingState_Passing)
623 {
624  // Setup SolutionHistory for testing.
625  auto sh = rcp(new Tempus::SolutionHistory<double>());
626  TEUCHOS_TEST_FOR_EXCEPT(sh->isInitialized()); // Should NOT be initialized
627  // as no State is set.
628 
629  auto model = rcp(new Tempus_Test::DahlquistTestModel<double>(-1.0, false));
630  auto inArgsIC = model->getNominalValues();
631  auto icSoln = rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
632  auto state0 = Tempus::createSolutionStateX<double>(icSoln);
633  state0->setTime (0.0);
634  state0->setTimeStep(1.0);
635  state0->setIndex(0);
636  sh->addState(state0);
637  sh->initialize();
638  TEUCHOS_TEST_FOR_EXCEPT(!sh->isInitialized());
639  sh->initWorkingState();
640  sh->getWorkingState()->setSolutionStatus(Tempus::Status::PASSED);
641 
642  // State before promoting workingState.
643  // with workingState PASSing.
644  TEST_COMPARE ( sh->getNumStates(), ==, 2);
645  TEUCHOS_ASSERT(sh->getCurrentState() != Teuchos::null);
646  TEUCHOS_ASSERT(sh->getCurrentState() == sh->getStateTimeIndexNM1());
647  TEUCHOS_ASSERT(sh->getWorkingState() != Teuchos::null);
648  TEUCHOS_ASSERT(sh->getWorkingState() == sh->getStateTimeIndexN());
649 
650  TEST_COMPARE ( sh->getCurrentState()->getIndex(), ==, 0);
651  TEST_FLOATING_EQUALITY( sh->getCurrentState()->getTime(), 0.0, 1.0e-14);
652 
653  TEST_COMPARE ( sh->getWorkingState()->getIndex(), ==, 1);
654  TEST_FLOATING_EQUALITY( sh->getWorkingState()->getTime(), 1.0, 1.0e-14);
655 
656  sh->promoteWorkingState();
657 
658  // State after promoting workingState.
659  TEST_COMPARE ( sh->getNumStates(), ==, 2);
660  TEUCHOS_ASSERT(sh->getCurrentState() != Teuchos::null);
661  TEUCHOS_ASSERT(sh->getCurrentState() == sh->getStateTimeIndexN());
662  TEUCHOS_ASSERT(sh->getWorkingState() == Teuchos::null);
663 
664  // Current state is now at t=1.0!
665  TEST_COMPARE ( sh->getCurrentState()->getIndex(), ==, 1);
666  TEST_FLOATING_EQUALITY( sh->getCurrentState()->getTime(), 1.0, 1.0e-14);
667 
668  // ---------------------------------------------------------------------------
669 
670 }
671 
672 
673 // ************************************************************
674 // ************************************************************
675 TEUCHOS_UNIT_TEST(SolutionHistory, promoteWorkingState_Failing)
676 {
677  // Setup SolutionHistory for testing.
678  auto sh = rcp(new Tempus::SolutionHistory<double>());
679  TEUCHOS_TEST_FOR_EXCEPT(sh->isInitialized()); // Should NOT be initialized
680  // as no State is set.
681 
682  auto model = rcp(new Tempus_Test::DahlquistTestModel<double>(-1.0, false));
683  auto inArgsIC = model->getNominalValues();
684  auto icSoln = rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
685  auto state0 = Tempus::createSolutionStateX<double>(icSoln);
686  state0->setTime (0.0);
687  state0->setTimeStep(1.0);
688  state0->setIndex(0);
689  sh->addState(state0);
690  sh->initialize();
691  TEUCHOS_TEST_FOR_EXCEPT(!sh->isInitialized());
692  sh->initWorkingState();
693  sh->getWorkingState()->setSolutionStatus(Tempus::Status::FAILED);
694 
695  // State before promoting workingState.
696  // with workingState FAILing.
697  TEST_COMPARE ( sh->getNumStates(), ==, 2);
698  TEUCHOS_ASSERT(sh->getCurrentState() != Teuchos::null);
699  TEUCHOS_ASSERT(sh->getCurrentState() == sh->getStateTimeIndexNM1());
700  TEUCHOS_ASSERT(sh->getWorkingState() != Teuchos::null);
701  TEUCHOS_ASSERT(sh->getWorkingState() == sh->getStateTimeIndexN());
702 
703  TEST_COMPARE ( sh->getCurrentState()->getIndex(), ==, 0);
704  TEST_FLOATING_EQUALITY( sh->getCurrentState()->getTime(), 0.0, 1.0e-14);
705 
706  TEST_COMPARE ( sh->getWorkingState()->getIndex(), ==, 1);
707  TEST_FLOATING_EQUALITY( sh->getWorkingState()->getTime(), 1.0, 1.0e-14);
708 
709  sh->promoteWorkingState();
710 
711  // State after promoting workingState.
712  // Should be unchanged as we are trying to redo FAILing time step.
713  TEST_COMPARE ( sh->getNumStates(), ==, 2);
714  TEUCHOS_ASSERT(sh->getCurrentState() != Teuchos::null);
715  TEUCHOS_ASSERT(sh->getCurrentState() == sh->getStateTimeIndexNM1());
716  TEUCHOS_ASSERT(sh->getWorkingState() != Teuchos::null);
717  TEUCHOS_ASSERT(sh->getWorkingState() == sh->getStateTimeIndexN());
718 
719  // Current state is still at t=0.0!
720  TEST_COMPARE ( sh->getCurrentState()->getIndex(), ==, 0);
721  TEST_FLOATING_EQUALITY( sh->getCurrentState()->getTime(), 0.0, 1.0e-14);
722 
723  // Working state is still at t=1.0!
724  TEST_COMPARE ( sh->getWorkingState()->getIndex(), ==, 1);
725  TEST_FLOATING_EQUALITY( sh->getWorkingState()->getTime(), 1.0, 1.0e-14);
726 
727  // ---------------------------------------------------------------------------
728 
729 }
730 
731 
732 } // namespace Tempus_Unit_Test
Keep the 2 newest states for undo.
The classic Dahlquist Test Problem.
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)
#define TEST_FLOATING_EQUALITY(v1, v2, tol)
Sine-Cosine model problem from Rythmos. This is a canonical Sine-Cosine differential equation with a...
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.