Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_UnitTest_Leapfrog.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_StepperLeapfrog.hpp"
17 
18 #include "../TestModels/HarmonicOscillatorModel.hpp"
19 
20 namespace Tempus_Unit_Test {
21 
23 using Teuchos::RCP;
24 using Teuchos::rcp;
25 using Teuchos::rcp_const_cast;
26 using Teuchos::rcp_dynamic_cast;
27 using Teuchos::sublist;
28 
29 // ************************************************************
30 // ************************************************************
31 TEUCHOS_UNIT_TEST(Leapfrog, Default_Construction)
32 {
34 
35  // Default construction.
36  auto stepper = rcp(new Tempus::StepperLeapfrog<double>());
37  stepper->setModel(model);
38  stepper->initialize();
39  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
40 
41  // Default values for construction.
43  stepper->setAppAction(modifier);
44  bool useFSAL = stepper->getUseFSAL();
45  std::string ICConsistency = stepper->getICConsistency();
46  bool ICConsistencyCheck = stepper->getICConsistencyCheck();
47 
48  // Test the set functions.
49  stepper->setAppAction(modifier);
50  stepper->initialize();
51  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
52  stepper->setUseFSAL(useFSAL);
53  stepper->initialize();
54  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
55  stepper->setICConsistency(ICConsistency);
56  stepper->initialize();
57  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
58  stepper->setICConsistencyCheck(ICConsistencyCheck);
59  stepper->initialize();
60  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
61 
62  // Full argument list construction.
64  model, useFSAL, ICConsistency, ICConsistencyCheck, modifier));
65  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
66 
67  // Test stepper properties.
68  TEUCHOS_ASSERT(stepper->getOrder() == 2);
69 }
70 
71 // ************************************************************
72 // ************************************************************
73 TEUCHOS_UNIT_TEST(Leapfrog, StepperFactory_Construction)
74 {
76  testFactoryConstruction("Leapfrog", model);
77 }
78 
79 // ************************************************************
80 // ************************************************************
81 class StepperLeapfrogModifierTest
82  : virtual public Tempus::StepperLeapfrogModifierBase<double> {
83  public:
85  StepperLeapfrogModifierTest()
86  : testBEGIN_STEP(false),
87  testBEFORE_X_UPDATE(false),
88  testBEFORE_EXPLICIT_EVAL(false),
89  testBEFORE_XDOT_UPDATE(false),
90  testEND_STEP(false),
91  testCurrentValue(-0.99),
92  testWorkingValue(-0.99),
93  testDt(-1.5),
94  testName("")
95  {
96  }
97 
99  virtual ~StepperLeapfrogModifierTest() {}
100 
102  virtual void modify(
106  actLoc)
107  {
108  switch (actLoc) {
110  testBEGIN_STEP = true;
111  auto x = sh->getCurrentState()->getX();
112  testCurrentValue = get_ele(*(x), 0);
113  break;
114  }
116  testBEFORE_EXPLICIT_EVAL = true;
117  testDt = sh->getWorkingState()->getTimeStep() / 10.0;
118  sh->getWorkingState()->setTimeStep(testDt);
119  break;
120  }
122  testBEFORE_X_UPDATE = true;
123  testName = "Leapfrog - Modifier";
124  stepper->setStepperName(testName);
125  break;
126  }
128  testBEFORE_XDOT_UPDATE = true;
129  auto x = sh->getWorkingState()->getX();
130  testWorkingValue = get_ele(*(x), 0);
131  break;
132  }
134  testEND_STEP = true;
135  break;
136  }
137  default:
138  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
139  "Error - unknown action location.\n");
140  }
141  }
142  bool testBEGIN_STEP;
143  bool testBEFORE_X_UPDATE;
144  bool testBEFORE_EXPLICIT_EVAL;
145  bool testBEFORE_XDOT_UPDATE;
146  bool testEND_STEP;
147  double testCurrentValue;
148  double testWorkingValue;
149  double testDt;
150  std::string testName;
151 };
152 
153 TEUCHOS_UNIT_TEST(Leapfrog, AppAction_Modifier)
154 {
156 
157  // Setup Stepper for field solve ----------------------------
158  auto stepper = rcp(new Tempus::StepperLeapfrog<double>());
159  stepper->setModel(model);
160  auto modifier = rcp(new StepperLeapfrogModifierTest());
161  stepper->setAppAction(modifier);
162  stepper->initialize();
163 
164  // Setup TimeStepControl ------------------------------------
165  auto timeStepControl = rcp(new Tempus::TimeStepControl<double>());
166  timeStepControl->setInitTimeStep(15.0);
167  timeStepControl->initialize();
168 
169  // Setup initial condition SolutionState --------------------
170  auto inArgsIC = model->getNominalValues();
171  auto icX = rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
172  auto icXDot =
173  rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x_dot());
174  auto icXDotDot =
175  rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x_dot_dot());
176  auto icState = Tempus::createSolutionStateX<double>(icX, icXDot, icXDotDot);
177  icState->setTime(timeStepControl->getInitTime());
178  icState->setIndex(timeStepControl->getInitIndex());
179  icState->setTimeStep(15.0);
180  icState->setOrder(stepper->getOrder());
181  icState->setSolutionStatus(Tempus::Status::PASSED); // ICs are passing.
182 
183  // Setup SolutionHistory ------------------------------------
184  auto solutionHistory = rcp(new Tempus::SolutionHistory<double>());
185  solutionHistory->setName("Forward States");
186  solutionHistory->setStorageType(Tempus::STORAGE_TYPE_STATIC);
187  solutionHistory->setStorageLimit(2);
188  solutionHistory->addState(icState);
189 
190  // Take one time step.
191  stepper->setInitialConditions(solutionHistory);
192  solutionHistory->initWorkingState();
193  solutionHistory->getWorkingState()->setTimeStep(15.0);
194  stepper->takeStep(solutionHistory);
195 
196  TEST_COMPARE(modifier->testBEGIN_STEP, ==, true);
197  TEST_COMPARE(modifier->testBEFORE_EXPLICIT_EVAL, ==, true);
198  TEST_COMPARE(modifier->testBEFORE_X_UPDATE, ==, true);
199  TEST_COMPARE(modifier->testBEFORE_XDOT_UPDATE, ==, true);
200  TEST_COMPARE(modifier->testEND_STEP, ==, true);
201 
202  auto x = solutionHistory->getCurrentState()->getX();
203  TEST_FLOATING_EQUALITY(modifier->testCurrentValue, get_ele(*(x), 0), 1.0e-15);
204  x = solutionHistory->getWorkingState()->getX();
205  TEST_FLOATING_EQUALITY(modifier->testWorkingValue, get_ele(*(x), 0), 1.0e-15);
206  auto Dt = solutionHistory->getWorkingState()->getTimeStep();
207  TEST_FLOATING_EQUALITY(modifier->testDt, Dt, 1.0e-15);
208 
209  TEST_COMPARE(modifier->testName, ==, "Leapfrog - Modifier");
210 }
211 // ************************************************************
212 // ************************************************************
213 class StepperLeapfrogModifierXTest
214  : virtual public Tempus::StepperLeapfrogModifierXBase<double> {
215  public:
217  StepperLeapfrogModifierXTest()
218  : testX_BEGIN_STEP(false),
219  testX_BEFORE_EXPLICIT_EVAL(false),
220  testX_BEFORE_X_UPDATE(false),
221  testX_BEFORE_XDOT_UPDATE(false),
222  testX_END_STEP(false),
223  testX(0.0),
224  testDt(-1.25),
225  testTime(-1.25),
226  testName("")
227  {
228  }
229 
231  virtual ~StepperLeapfrogModifierXTest() {}
232 
234  virtual void modify(
235  Teuchos::RCP<Thyra::VectorBase<double> > x, const double time,
236  const double dt,
238  modType)
239  {
240  switch (modType) {
242  testX_BEGIN_STEP = true;
243  testX = get_ele(*(x), 0);
244  break;
245  }
247  testX_BEFORE_EXPLICIT_EVAL = true;
248  testDt = dt;
249  break;
250  }
252  testX_BEFORE_X_UPDATE = true;
253  testTime = time;
254  break;
255  }
257  testX_BEFORE_XDOT_UPDATE = true;
258  testName = "Leapfrog - ModifierX";
259  break;
260  }
262  testX_END_STEP = true;
263  break;
264  }
265  default:
266  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
267  "Error - unknown action location.\n");
268  }
269  }
270  bool testX_BEGIN_STEP;
271  bool testX_BEFORE_EXPLICIT_EVAL;
272  bool testX_BEFORE_X_UPDATE;
273  bool testX_BEFORE_XDOT_UPDATE;
274  bool testX_END_STEP;
275  double testX;
276  double testDt;
277  double testTime;
278  std::string testName;
279 };
280 
281 TEUCHOS_UNIT_TEST(LeapFrog, AppAction_ModifierX)
282 {
284 
285  // Setup Stepper for field solve ----------------------------
286  auto stepper = rcp(new Tempus::StepperLeapfrog<double>());
287  stepper->setModel(model);
288  auto modifierX = rcp(new StepperLeapfrogModifierXTest());
289  stepper->setAppAction(modifierX);
290  stepper->initialize();
291 
292  // Setup TimeStepControl ------------------------------------
293  auto timeStepControl = rcp(new Tempus::TimeStepControl<double>());
294  timeStepControl->setInitTimeStep(15.0);
295  timeStepControl->initialize();
296 
297  // Setup initial condition SolutionState --------------------
298  auto inArgsIC = model->getNominalValues();
299  auto icX = rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
300  auto icXDot =
301  rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x_dot());
302  auto icXDotDot =
303  rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x_dot_dot());
304  auto icState = Tempus::createSolutionStateX<double>(icX, icXDot, icXDotDot);
305  icState->setTime(timeStepControl->getInitTime());
306  icState->setIndex(timeStepControl->getInitIndex());
307  icState->setTimeStep(15.0);
308  icState->setOrder(stepper->getOrder());
309  icState->setSolutionStatus(Tempus::Status::PASSED); // ICs are passing.
310 
311  // Setup SolutionHistory ------------------------------------
312  auto solutionHistory = rcp(new Tempus::SolutionHistory<double>());
313  solutionHistory->setName("Forward States");
314  solutionHistory->setStorageType(Tempus::STORAGE_TYPE_STATIC);
315  solutionHistory->setStorageLimit(2);
316  solutionHistory->addState(icState);
317 
318  // Take one time step.
319  stepper->setInitialConditions(solutionHistory);
320  solutionHistory->initWorkingState();
321  solutionHistory->getWorkingState()->setTimeStep(15.0);
322  stepper->takeStep(solutionHistory);
323 
324  TEST_COMPARE(modifierX->testX_BEGIN_STEP, ==, true);
325  TEST_COMPARE(modifierX->testX_BEFORE_EXPLICIT_EVAL, ==, true);
326  TEST_COMPARE(modifierX->testX_BEFORE_XDOT_UPDATE, ==, true);
327  TEST_COMPARE(modifierX->testX_BEFORE_X_UPDATE, ==, true);
328  TEST_COMPARE(modifierX->testX_END_STEP, ==, true);
329 
330  auto x = solutionHistory->getCurrentState()->getX();
331  TEST_FLOATING_EQUALITY(modifierX->testX, get_ele(*(x), 0), 1.0e-15);
332  auto Dt = solutionHistory->getWorkingState()->getTimeStep();
333  TEST_FLOATING_EQUALITY(modifierX->testDt, Dt, 1.0e-15);
334  auto time = solutionHistory->getWorkingState()->getTime();
335  TEST_FLOATING_EQUALITY(modifierX->testTime, time, 1.0e-15);
336  TEST_COMPARE(modifierX->testName, ==, "Leapfrog - ModifierX");
337 }
338 
339 // ************************************************************
340 // ************************************************************
341 class StepperLeapfrogObserverTest
342  : virtual public Tempus::StepperLeapfrogObserverBase<double> {
343  public:
345  StepperLeapfrogObserverTest()
346  : testBEGIN_STEP(false),
347  testBEFORE_EXPLICIT_EVAL(false),
348  testBEFORE_X_UPDATE(false),
349  testBEFORE_XDOT_UPDATE(false),
350  testEND_STEP(false),
351  testCurrentValue(-0.99),
352  testWorkingValue(-0.99),
353  testDt(-1.5),
354  testName("")
355  {
356  }
358  virtual ~StepperLeapfrogObserverTest() {}
359 
361  virtual void observe(
365  actLoc)
366  {
367  switch (actLoc) {
369  testBEGIN_STEP = true;
370  auto x = sh->getCurrentState()->getX();
371  testCurrentValue = get_ele(*(x), 0);
372  break;
373  }
375  testBEFORE_EXPLICIT_EVAL = true;
376  testDt = sh->getWorkingState()->getTimeStep();
377  break;
378  }
380  testBEFORE_X_UPDATE = true;
381  testName = stepper->getStepperType();
382  break;
383  }
385  testBEFORE_XDOT_UPDATE = true;
386  auto x = sh->getWorkingState()->getX();
387  testWorkingValue = get_ele(*(x), 0);
388  break;
389  }
391  testEND_STEP = true;
392  break;
393  }
394  default:
395  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
396  "Error - unknown action location.\n");
397  }
398  }
399  bool testBEGIN_STEP;
400  bool testBEFORE_EXPLICIT_EVAL;
401  bool testBEFORE_X_UPDATE;
402  bool testBEFORE_XDOT_UPDATE;
403  bool testEND_STEP;
404  double testCurrentValue;
405  double testWorkingValue;
406  double testDt;
407  std::string testName;
408 };
409 
410 TEUCHOS_UNIT_TEST(Leapfrog, AppAction_Observer)
411 {
413 
414  // Setup Stepper for field solve ----------------------------
415  auto stepper = rcp(new Tempus::StepperLeapfrog<double>());
416  stepper->setModel(model);
417  auto observer = rcp(new StepperLeapfrogObserverTest());
418  stepper->setAppAction(observer);
419  stepper->initialize();
420 
421  // Setup TimeStepControl ------------------------------------
422  auto timeStepControl = rcp(new Tempus::TimeStepControl<double>());
423  double dt = 0.173;
424  timeStepControl->setInitTimeStep(dt);
425  timeStepControl->initialize();
426 
427  // Setup initial condition SolutionState --------------------
428  auto inArgsIC = model->getNominalValues();
429  auto icX = rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
430  auto icXDot =
431  rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x_dot());
432  auto icXDotDot =
433  rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x_dot_dot());
434  auto icState = Tempus::createSolutionStateX<double>(icX, icXDot, icXDotDot);
435  icState->setTime(timeStepControl->getInitTime());
436  icState->setIndex(timeStepControl->getInitIndex());
437  icState->setTimeStep(dt);
438  icState->setOrder(stepper->getOrder());
439  icState->setSolutionStatus(Tempus::Status::PASSED); // ICs are passing.
440 
441  // Setup SolutionHistory ------------------------------------
442  auto solutionHistory = rcp(new Tempus::SolutionHistory<double>());
443  solutionHistory->setName("Forward States");
444  solutionHistory->setStorageType(Tempus::STORAGE_TYPE_STATIC);
445  solutionHistory->setStorageLimit(2);
446  solutionHistory->addState(icState);
447 
448  // Take one time step.
449  stepper->setInitialConditions(solutionHistory);
450  solutionHistory->initWorkingState();
451  solutionHistory->getWorkingState()->setTimeStep(dt);
452  stepper->takeStep(solutionHistory);
453 
454  // Testing that values can be observed through the observer.
455  TEST_COMPARE(observer->testBEGIN_STEP, ==, true);
456  TEST_COMPARE(observer->testBEFORE_EXPLICIT_EVAL, ==, true);
457  TEST_COMPARE(observer->testBEFORE_X_UPDATE, ==, true);
458  TEST_COMPARE(observer->testBEFORE_XDOT_UPDATE, ==, true);
459  TEST_COMPARE(observer->testEND_STEP, ==, true);
460 
461  auto x = solutionHistory->getCurrentState()->getX();
462  TEST_FLOATING_EQUALITY(observer->testCurrentValue, get_ele(*(x), 0), 1.0e-15);
463  x = solutionHistory->getWorkingState()->getX();
464  TEST_FLOATING_EQUALITY(observer->testWorkingValue, get_ele(*(x), 0), 1.0e-15);
465  TEST_FLOATING_EQUALITY(observer->testDt, dt, 1.0e-15);
466 
467  TEST_COMPARE(observer->testName, ==, "Leapfrog");
468 }
469 
470 } // namespace Tempus_Unit_Test
void testFactoryConstruction(std::string stepperType, const Teuchos::RCP< const Thyra::ModelEvaluator< double > > &model)
Unit test utility for Stepper construction through StepperFactory.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
#define TEST_COMPARE(v1, comp, v2)
#define TEST_FLOATING_EQUALITY(v1, v2, tol)
ACTION_LOCATION
Indicates the location of application action (see algorithm).
Consider the ODE: where is a constant, is a constant damping parameter, is a constant forcing par...
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
MODIFIER_TYPE
Indicates the location of application action (see algorithm).
TEUCHOS_UNIT_TEST(BackwardEuler, Default_Construction)
TimeStepControl manages the time step size. There several mechanisms that effect the time step size a...
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
Keep a fix number of states.
virtual void modify(Teuchos::RCP< Thyra::VectorBase< double > >, const double, const double, const MODIFIER_TYPE modType)=0
Modify solution based on the MODIFIER_TYPE.
#define TEUCHOS_ASSERT(assertion_test)
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)