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