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