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: 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_StepperLeapfrog.hpp"
16 
17 #include "../TestModels/HarmonicOscillatorModel.hpp"
18 
19 namespace Tempus_Unit_Test {
20 
22 using Teuchos::RCP;
23 using Teuchos::rcp;
24 using Teuchos::rcp_const_cast;
25 using Teuchos::rcp_dynamic_cast;
26 using Teuchos::sublist;
27 
28 // ************************************************************
29 // ************************************************************
30 TEUCHOS_UNIT_TEST(Leapfrog, Default_Construction)
31 {
33 
34  // Default construction.
35  auto stepper = rcp(new Tempus::StepperLeapfrog<double>());
36  stepper->setModel(model);
37  stepper->initialize();
38  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
39 
40  // Default values for construction.
42  stepper->setAppAction(modifier);
43  bool useFSAL = stepper->getUseFSAL();
44  std::string ICConsistency = stepper->getICConsistency();
45  bool ICConsistencyCheck = stepper->getICConsistencyCheck();
46 
47  // Test the set functions.
48  stepper->setAppAction(modifier);
49  stepper->initialize();
50  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
51  stepper->setUseFSAL(useFSAL);
52  stepper->initialize();
53  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
54  stepper->setICConsistency(ICConsistency);
55  stepper->initialize();
56  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
57  stepper->setICConsistencyCheck(ICConsistencyCheck);
58  stepper->initialize();
59  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
60 
61  // Full argument list construction.
63  model, useFSAL, ICConsistency, ICConsistencyCheck, modifier));
64  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
65 
66  // Test stepper properties.
67  TEUCHOS_ASSERT(stepper->getOrder() == 2);
68 }
69 
70 // ************************************************************
71 // ************************************************************
72 TEUCHOS_UNIT_TEST(Leapfrog, StepperFactory_Construction)
73 {
75  testFactoryConstruction("Leapfrog", model);
76 }
77 
78 // ************************************************************
79 // ************************************************************
80 class StepperLeapfrogModifierTest
81  : virtual public Tempus::StepperLeapfrogModifierBase<double> {
82  public:
84  StepperLeapfrogModifierTest()
85  : testBEGIN_STEP(false),
86  testBEFORE_X_UPDATE(false),
87  testBEFORE_EXPLICIT_EVAL(false),
88  testBEFORE_XDOT_UPDATE(false),
89  testEND_STEP(false),
90  testCurrentValue(-0.99),
91  testWorkingValue(-0.99),
92  testDt(-1.5),
93  testName("")
94  {
95  }
96 
98  virtual ~StepperLeapfrogModifierTest() {}
99 
101  virtual void modify(
105  actLoc)
106  {
107  switch (actLoc) {
109  testBEGIN_STEP = true;
110  auto x = sh->getCurrentState()->getX();
111  testCurrentValue = get_ele(*(x), 0);
112  break;
113  }
115  testBEFORE_EXPLICIT_EVAL = true;
116  testDt = sh->getWorkingState()->getTimeStep() / 10.0;
117  sh->getWorkingState()->setTimeStep(testDt);
118  break;
119  }
121  testBEFORE_X_UPDATE = true;
122  testName = "Leapfrog - Modifier";
123  stepper->setStepperName(testName);
124  break;
125  }
127  testBEFORE_XDOT_UPDATE = true;
128  auto x = sh->getWorkingState()->getX();
129  testWorkingValue = get_ele(*(x), 0);
130  break;
131  }
133  testEND_STEP = true;
134  break;
135  }
136  default:
137  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
138  "Error - unknown action location.\n");
139  }
140  }
141  bool testBEGIN_STEP;
142  bool testBEFORE_X_UPDATE;
143  bool testBEFORE_EXPLICIT_EVAL;
144  bool testBEFORE_XDOT_UPDATE;
145  bool testEND_STEP;
146  double testCurrentValue;
147  double testWorkingValue;
148  double testDt;
149  std::string testName;
150 };
151 
152 TEUCHOS_UNIT_TEST(Leapfrog, AppAction_Modifier)
153 {
155 
156  // Setup Stepper for field solve ----------------------------
157  auto stepper = rcp(new Tempus::StepperLeapfrog<double>());
158  stepper->setModel(model);
159  auto modifier = rcp(new StepperLeapfrogModifierTest());
160  stepper->setAppAction(modifier);
161  stepper->initialize();
162 
163  // Setup TimeStepControl ------------------------------------
164  auto timeStepControl = rcp(new Tempus::TimeStepControl<double>());
165  timeStepControl->setInitTimeStep(15.0);
166  timeStepControl->initialize();
167 
168  // Setup initial condition SolutionState --------------------
169  auto inArgsIC = model->getNominalValues();
170  auto icX = rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
171  auto icXDot =
172  rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x_dot());
173  auto icXDotDot =
174  rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x_dot_dot());
175  auto icState = Tempus::createSolutionStateX<double>(icX, icXDot, icXDotDot);
176  icState->setTime(timeStepControl->getInitTime());
177  icState->setIndex(timeStepControl->getInitIndex());
178  icState->setTimeStep(15.0);
179  icState->setOrder(stepper->getOrder());
180  icState->setSolutionStatus(Tempus::Status::PASSED); // ICs are passing.
181 
182  // Setup SolutionHistory ------------------------------------
183  auto solutionHistory = rcp(new Tempus::SolutionHistory<double>());
184  solutionHistory->setName("Forward States");
185  solutionHistory->setStorageType(Tempus::STORAGE_TYPE_STATIC);
186  solutionHistory->setStorageLimit(2);
187  solutionHistory->addState(icState);
188 
189  // Take one time step.
190  stepper->setInitialConditions(solutionHistory);
191  solutionHistory->initWorkingState();
192  solutionHistory->getWorkingState()->setTimeStep(15.0);
193  stepper->takeStep(solutionHistory);
194 
195  TEST_COMPARE(modifier->testBEGIN_STEP, ==, true);
196  TEST_COMPARE(modifier->testBEFORE_EXPLICIT_EVAL, ==, true);
197  TEST_COMPARE(modifier->testBEFORE_X_UPDATE, ==, true);
198  TEST_COMPARE(modifier->testBEFORE_XDOT_UPDATE, ==, true);
199  TEST_COMPARE(modifier->testEND_STEP, ==, true);
200 
201  auto x = solutionHistory->getCurrentState()->getX();
202  TEST_FLOATING_EQUALITY(modifier->testCurrentValue, get_ele(*(x), 0), 1.0e-15);
203  x = solutionHistory->getWorkingState()->getX();
204  TEST_FLOATING_EQUALITY(modifier->testWorkingValue, get_ele(*(x), 0), 1.0e-15);
205  auto Dt = solutionHistory->getWorkingState()->getTimeStep();
206  TEST_FLOATING_EQUALITY(modifier->testDt, Dt, 1.0e-15);
207 
208  TEST_COMPARE(modifier->testName, ==, "Leapfrog - Modifier");
209 }
210 // ************************************************************
211 // ************************************************************
212 class StepperLeapfrogModifierXTest
213  : virtual public Tempus::StepperLeapfrogModifierXBase<double> {
214  public:
216  StepperLeapfrogModifierXTest()
217  : testX_BEGIN_STEP(false),
218  testX_BEFORE_EXPLICIT_EVAL(false),
219  testX_BEFORE_X_UPDATE(false),
220  testX_BEFORE_XDOT_UPDATE(false),
221  testX_END_STEP(false),
222  testX(0.0),
223  testDt(-1.25),
224  testTime(-1.25),
225  testName("")
226  {
227  }
228 
230  virtual ~StepperLeapfrogModifierXTest() {}
231 
233  virtual void modify(
234  Teuchos::RCP<Thyra::VectorBase<double> > x, const double time,
235  const double dt,
237  modType)
238  {
239  switch (modType) {
241  testX_BEGIN_STEP = true;
242  testX = get_ele(*(x), 0);
243  break;
244  }
246  testX_BEFORE_EXPLICIT_EVAL = true;
247  testDt = dt;
248  break;
249  }
251  testX_BEFORE_X_UPDATE = true;
252  testTime = time;
253  break;
254  }
256  testX_BEFORE_XDOT_UPDATE = true;
257  testName = "Leapfrog - ModifierX";
258  break;
259  }
261  testX_END_STEP = true;
262  break;
263  }
264  default:
265  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
266  "Error - unknown action location.\n");
267  }
268  }
269  bool testX_BEGIN_STEP;
270  bool testX_BEFORE_EXPLICIT_EVAL;
271  bool testX_BEFORE_X_UPDATE;
272  bool testX_BEFORE_XDOT_UPDATE;
273  bool testX_END_STEP;
274  double testX;
275  double testDt;
276  double testTime;
277  std::string testName;
278 };
279 
280 TEUCHOS_UNIT_TEST(LeapFrog, AppAction_ModifierX)
281 {
283 
284  // Setup Stepper for field solve ----------------------------
285  auto stepper = rcp(new Tempus::StepperLeapfrog<double>());
286  stepper->setModel(model);
287  auto modifierX = rcp(new StepperLeapfrogModifierXTest());
288  stepper->setAppAction(modifierX);
289  stepper->initialize();
290 
291  // Setup TimeStepControl ------------------------------------
292  auto timeStepControl = rcp(new Tempus::TimeStepControl<double>());
293  timeStepControl->setInitTimeStep(15.0);
294  timeStepControl->initialize();
295 
296  // Setup initial condition SolutionState --------------------
297  auto inArgsIC = model->getNominalValues();
298  auto icX = rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
299  auto icXDot =
300  rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x_dot());
301  auto icXDotDot =
302  rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x_dot_dot());
303  auto icState = Tempus::createSolutionStateX<double>(icX, icXDot, icXDotDot);
304  icState->setTime(timeStepControl->getInitTime());
305  icState->setIndex(timeStepControl->getInitIndex());
306  icState->setTimeStep(15.0);
307  icState->setOrder(stepper->getOrder());
308  icState->setSolutionStatus(Tempus::Status::PASSED); // ICs are passing.
309 
310  // Setup SolutionHistory ------------------------------------
311  auto solutionHistory = rcp(new Tempus::SolutionHistory<double>());
312  solutionHistory->setName("Forward States");
313  solutionHistory->setStorageType(Tempus::STORAGE_TYPE_STATIC);
314  solutionHistory->setStorageLimit(2);
315  solutionHistory->addState(icState);
316 
317  // Take one time step.
318  stepper->setInitialConditions(solutionHistory);
319  solutionHistory->initWorkingState();
320  solutionHistory->getWorkingState()->setTimeStep(15.0);
321  stepper->takeStep(solutionHistory);
322 
323  TEST_COMPARE(modifierX->testX_BEGIN_STEP, ==, true);
324  TEST_COMPARE(modifierX->testX_BEFORE_EXPLICIT_EVAL, ==, true);
325  TEST_COMPARE(modifierX->testX_BEFORE_XDOT_UPDATE, ==, true);
326  TEST_COMPARE(modifierX->testX_BEFORE_X_UPDATE, ==, true);
327  TEST_COMPARE(modifierX->testX_END_STEP, ==, true);
328 
329  auto x = solutionHistory->getCurrentState()->getX();
330  TEST_FLOATING_EQUALITY(modifierX->testX, get_ele(*(x), 0), 1.0e-15);
331  auto Dt = solutionHistory->getWorkingState()->getTimeStep();
332  TEST_FLOATING_EQUALITY(modifierX->testDt, Dt, 1.0e-15);
333  auto time = solutionHistory->getWorkingState()->getTime();
334  TEST_FLOATING_EQUALITY(modifierX->testTime, time, 1.0e-15);
335  TEST_COMPARE(modifierX->testName, ==, "Leapfrog - ModifierX");
336 }
337 
338 // ************************************************************
339 // ************************************************************
340 class StepperLeapfrogObserverTest
341  : virtual public Tempus::StepperLeapfrogObserverBase<double> {
342  public:
344  StepperLeapfrogObserverTest()
345  : testBEGIN_STEP(false),
346  testBEFORE_EXPLICIT_EVAL(false),
347  testBEFORE_X_UPDATE(false),
348  testBEFORE_XDOT_UPDATE(false),
349  testEND_STEP(false),
350  testCurrentValue(-0.99),
351  testWorkingValue(-0.99),
352  testDt(-1.5),
353  testName("")
354  {
355  }
357  virtual ~StepperLeapfrogObserverTest() {}
358 
360  virtual void observe(
364  actLoc)
365  {
366  switch (actLoc) {
368  testBEGIN_STEP = true;
369  auto x = sh->getCurrentState()->getX();
370  testCurrentValue = get_ele(*(x), 0);
371  break;
372  }
374  testBEFORE_EXPLICIT_EVAL = true;
375  testDt = sh->getWorkingState()->getTimeStep();
376  break;
377  }
379  testBEFORE_X_UPDATE = true;
380  testName = stepper->getStepperType();
381  break;
382  }
384  testBEFORE_XDOT_UPDATE = true;
385  auto x = sh->getWorkingState()->getX();
386  testWorkingValue = get_ele(*(x), 0);
387  break;
388  }
390  testEND_STEP = true;
391  break;
392  }
393  default:
394  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
395  "Error - unknown action location.\n");
396  }
397  }
398  bool testBEGIN_STEP;
399  bool testBEFORE_EXPLICIT_EVAL;
400  bool testBEFORE_X_UPDATE;
401  bool testBEFORE_XDOT_UPDATE;
402  bool testEND_STEP;
403  double testCurrentValue;
404  double testWorkingValue;
405  double testDt;
406  std::string testName;
407 };
408 
409 TEUCHOS_UNIT_TEST(Leapfrog, AppAction_Observer)
410 {
412 
413  // Setup Stepper for field solve ----------------------------
414  auto stepper = rcp(new Tempus::StepperLeapfrog<double>());
415  stepper->setModel(model);
416  auto observer = rcp(new StepperLeapfrogObserverTest());
417  stepper->setAppAction(observer);
418  stepper->initialize();
419 
420  // Setup TimeStepControl ------------------------------------
421  auto timeStepControl = rcp(new Tempus::TimeStepControl<double>());
422  double dt = 0.173;
423  timeStepControl->setInitTimeStep(dt);
424  timeStepControl->initialize();
425 
426  // Setup initial condition SolutionState --------------------
427  auto inArgsIC = model->getNominalValues();
428  auto icX = rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
429  auto icXDot =
430  rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x_dot());
431  auto icXDotDot =
432  rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x_dot_dot());
433  auto icState = Tempus::createSolutionStateX<double>(icX, icXDot, icXDotDot);
434  icState->setTime(timeStepControl->getInitTime());
435  icState->setIndex(timeStepControl->getInitIndex());
436  icState->setTimeStep(dt);
437  icState->setOrder(stepper->getOrder());
438  icState->setSolutionStatus(Tempus::Status::PASSED); // ICs are passing.
439 
440  // Setup SolutionHistory ------------------------------------
441  auto solutionHistory = rcp(new Tempus::SolutionHistory<double>());
442  solutionHistory->setName("Forward States");
443  solutionHistory->setStorageType(Tempus::STORAGE_TYPE_STATIC);
444  solutionHistory->setStorageLimit(2);
445  solutionHistory->addState(icState);
446 
447  // Take one time step.
448  stepper->setInitialConditions(solutionHistory);
449  solutionHistory->initWorkingState();
450  solutionHistory->getWorkingState()->setTimeStep(dt);
451  stepper->takeStep(solutionHistory);
452 
453  // Testing that values can be observed through the observer.
454  TEST_COMPARE(observer->testBEGIN_STEP, ==, true);
455  TEST_COMPARE(observer->testBEFORE_EXPLICIT_EVAL, ==, true);
456  TEST_COMPARE(observer->testBEFORE_X_UPDATE, ==, true);
457  TEST_COMPARE(observer->testBEFORE_XDOT_UPDATE, ==, true);
458  TEST_COMPARE(observer->testEND_STEP, ==, true);
459 
460  auto x = solutionHistory->getCurrentState()->getX();
461  TEST_FLOATING_EQUALITY(observer->testCurrentValue, get_ele(*(x), 0), 1.0e-15);
462  x = solutionHistory->getWorkingState()->getX();
463  TEST_FLOATING_EQUALITY(observer->testWorkingValue, get_ele(*(x), 0), 1.0e-15);
464  TEST_FLOATING_EQUALITY(observer->testDt, dt, 1.0e-15);
465 
466  TEST_COMPARE(observer->testName, ==, "Leapfrog");
467 }
468 
469 } // 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)