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