9 #include "Teuchos_UnitTestHarness.hpp" 10 #include "Teuchos_XMLParameterListHelpers.hpp" 11 #include "Teuchos_TimeMonitor.hpp" 12 #include "Teuchos_DefaultComm.hpp" 14 #include "Thyra_VectorStdOps.hpp" 18 #include "Tempus_StepperLeapfrog.hpp" 24 #include "../TestModels/SinCosModel.hpp" 25 #include "../TestModels/VanDerPolModel.hpp" 26 #include "../TestModels/HarmonicOscillatorModel.hpp" 27 #include "../TestUtils/Tempus_ConvergenceTestUtils.hpp" 36 using Teuchos::rcp_const_cast;
37 using Teuchos::rcp_dynamic_cast;
38 using Teuchos::ParameterList;
39 using Teuchos::sublist;
40 using Teuchos::getParametersFromXmlFile;
52 stepper->setModel(model);
53 stepper->initialize();
54 TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
59 stepper->setAppAction(modifier);
60 bool useFSAL = stepper->getUseFSAL();
61 std::string ICConsistency = stepper->getICConsistency();
62 bool ICConsistencyCheck = stepper->getICConsistencyCheck();
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());
73 model, useFSAL, ICConsistency, ICConsistencyCheck,modifier));
74 TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
77 TEUCHOS_ASSERT(stepper->getOrder() == 2);
91 class StepperLeapfrogModifierTest
97 StepperLeapfrogModifierTest()
98 : testBEGIN_STEP(false), testBEFORE_X_UPDATE(false),
99 testBEFORE_EXPLICIT_EVAL(false), testBEFORE_XDOT_UPDATE(false),
101 testCurrentValue(-0.99), testWorkingValue(-0.99),
102 testDt(-1.5), testName(
"")
106 virtual ~StepperLeapfrogModifierTest(){}
117 testBEGIN_STEP =
true;
118 auto x = sh->getCurrentState()->getX();
119 testCurrentValue = get_ele(*(x), 0);
124 testBEFORE_EXPLICIT_EVAL =
true;
125 testDt = sh->getWorkingState()->getTimeStep()/10.0;
126 sh->getWorkingState()->setTimeStep(testDt);
131 testBEFORE_X_UPDATE =
true;
132 testName =
"Leapfrog - Modifier";
133 stepper->setStepperName(testName);
138 testBEFORE_XDOT_UPDATE =
true;
139 auto x = sh->getWorkingState()->getX();
140 testWorkingValue = get_ele(*(x), 0);
149 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
150 "Error - unknown action location.\n");
154 bool testBEFORE_X_UPDATE;
155 bool testBEFORE_EXPLICIT_EVAL;
156 bool testBEFORE_XDOT_UPDATE;
158 double testCurrentValue;
159 double testWorkingValue;
161 std::string testName;
170 stepper->setModel(model);
171 auto modifier = rcp(
new StepperLeapfrogModifierTest());
172 stepper->setAppAction(modifier);
173 stepper->initialize();
177 timeStepControl->setInitTimeStep(15.0);
178 timeStepControl->initialize();
181 auto inArgsIC = model->getNominalValues();
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());
194 solutionHistory->setName(
"Forward States");
196 solutionHistory->setStorageLimit(2);
197 solutionHistory->addState(icState);
200 stepper->setInitialConditions(solutionHistory);
201 solutionHistory->initWorkingState();
202 solutionHistory->getWorkingState()->setTimeStep(15.0);
203 stepper->takeStep(solutionHistory);
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);
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);
218 TEST_COMPARE(modifier->testName, ==,
"Leapfrog - Modifier");
222 class StepperLeapfrogModifierXTest
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(
"")
236 virtual ~StepperLeapfrogModifierXTest(){}
241 const double time,
const double dt,
247 testX_BEGIN_STEP =
true;
248 testX = get_ele(*(x), 0);
253 testX_BEFORE_EXPLICIT_EVAL =
true;
259 testX_BEFORE_X_UPDATE =
true;
265 testX_BEFORE_XDOT_UPDATE =
true;
266 testName =
"Leapfrog - ModifierX";
271 testX_END_STEP =
true;
275 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
276 "Error - unknown action location.\n");
279 bool testX_BEGIN_STEP;
280 bool testX_BEFORE_EXPLICIT_EVAL;
281 bool testX_BEFORE_X_UPDATE;
282 bool testX_BEFORE_XDOT_UPDATE;
287 std::string testName;
296 stepper->setModel(model);
297 auto modifierX = rcp(
new StepperLeapfrogModifierXTest());
298 stepper->setAppAction(modifierX);
299 stepper->initialize();
303 timeStepControl->setInitTimeStep(15.0);
304 timeStepControl->initialize();
307 auto inArgsIC = model->getNominalValues();
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());
320 solutionHistory->setName(
"Forward States");
322 solutionHistory->setStorageLimit(2);
323 solutionHistory->addState(icState);
326 stepper->setInitialConditions(solutionHistory);
327 solutionHistory->initWorkingState();
328 solutionHistory->getWorkingState()->setTimeStep(15.0);
329 stepper->takeStep(solutionHistory);
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);
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");
349 class StepperLeapfrogObserverTest
354 StepperLeapfrogObserverTest()
355 : testBEGIN_STEP(false), testBEFORE_EXPLICIT_EVAL(false),
356 testBEFORE_X_UPDATE(false), testBEFORE_XDOT_UPDATE(false),
358 testCurrentValue(-0.99), testWorkingValue(-0.99),
359 testDt(-1.5), testName(
"")
362 virtual ~StepperLeapfrogObserverTest(){}
365 virtual void observe(
373 testBEGIN_STEP =
true;
374 auto x = sh->getCurrentState()->getX();
375 testCurrentValue = get_ele(*(x), 0);
380 testBEFORE_EXPLICIT_EVAL =
true;
381 testDt = sh->getWorkingState()->getTimeStep();
386 testBEFORE_X_UPDATE =
true;
387 testName = stepper->getStepperType();
392 testBEFORE_XDOT_UPDATE =
true;
393 auto x = sh->getWorkingState()->getX();
394 testWorkingValue = get_ele(*(x), 0);
403 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
404 "Error - unknown action location.\n");
408 bool testBEFORE_EXPLICIT_EVAL;
409 bool testBEFORE_X_UPDATE;
410 bool testBEFORE_XDOT_UPDATE;
412 double testCurrentValue;
413 double testWorkingValue;
415 std::string testName;
424 stepper->setModel(model);
425 auto observer = rcp(
new StepperLeapfrogObserverTest());
426 stepper->setAppAction(observer);
427 stepper->initialize();
432 timeStepControl->setInitTimeStep(dt);
433 timeStepControl->initialize();
436 auto inArgsIC = model->getNominalValues();
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());
449 solutionHistory->setName(
"Forward States");
451 solutionHistory->setStorageLimit(2);
452 solutionHistory->addState(icState);
455 stepper->setInitialConditions(solutionHistory);
456 solutionHistory->initWorkingState();
457 solutionHistory->getWorkingState()->setTimeStep(dt);
458 stepper->takeStep(solutionHistory);
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);
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);
473 TEST_COMPARE(observer->testName, ==,
"Leapfrog");
void testFactoryConstruction(std::string stepperType, const Teuchos::RCP< const Thyra::ModelEvaluator< double > > &model)
Unit test utility for Stepper construction through StepperFactory.
Before the explicit ME evaluation.
Default modifier for StepperLeapfrog.
Modify Before updating xDot.
Modify at the beginning of the step.
Base ModifierX for StepperLeapfrog.
Base observer for StepperLeapfrog.
ACTION_LOCATION
Indicates the location of application action (see algorithm).
Base modifier for StepperLeapfrog.
Consider the ODE: where is a constant, is a constant damping parameter, is a constant forcing par...
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...
Modify before updating x.
Modify at the end of the step.
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
Keep a fix number of states.
Modify before the explicit ME evaluation.
virtual void modify(Teuchos::RCP< Thyra::VectorBase< double > >, const double, const double, const MODIFIER_TYPE modType)=0
Modify solution based on the MODIFIER_TYPE.
At the beginning of the step.