12 #include "Tempus_StepperForwardEuler.hpp"
13 #include "Tempus_StepperBackwardEuler.hpp"
22 namespace Tempus_Unit_Test {
27 using Teuchos::rcp_const_cast;
28 using Teuchos::rcp_dynamic_cast;
29 using Teuchos::sublist;
39 stepper->setModel(model);
40 stepper->initialize();
54 predictorStepper->setModel(
56 predictorStepper->initialize();
59 bool useFSAL = defaultStepper->getUseFSAL();
60 std::string ICConsistency = defaultStepper->getICConsistency();
61 bool ICConsistencyCheck = defaultStepper->getICConsistencyCheck();
62 bool zeroInitialGuess = defaultStepper->getZeroInitialGuess();
65 stepper->setAppAction(modifier);
66 stepper->initialize();
68 stepper->setAppAction(modifierX);
69 stepper->initialize();
71 stepper->setAppAction(observer);
72 stepper->initialize();
74 stepper->setSolver(solver);
75 stepper->initialize();
77 stepper->setPredictor(predictorStepper);
78 stepper->initialize();
80 stepper->setUseFSAL(useFSAL);
81 stepper->initialize();
83 stepper->setICConsistency(ICConsistency);
84 stepper->initialize();
86 stepper->setICConsistencyCheck(ICConsistencyCheck);
87 stepper->initialize();
89 stepper->setZeroInitialGuess(zeroInitialGuess);
90 stepper->initialize();
95 model, solver, predictorStepper, useFSAL, ICConsistency,
96 ICConsistencyCheck, zeroInitialGuess, modifier));
113 class StepperBackwardEulerModifierTest
117 StepperBackwardEulerModifierTest()
118 : testBEGIN_STEP(false),
119 testBEFORE_SOLVE(false),
120 testAFTER_SOLVE(false),
122 testCurrentValue(-0.99),
123 testWorkingValue(-0.99),
130 virtual ~StepperBackwardEulerModifierTest() {}
137 double>::ACTION_LOCATION actLoc)
141 testBEGIN_STEP =
true;
142 auto x = sh->getCurrentState()->getX();
143 testCurrentValue = get_ele(*(x), 0);
147 testBEFORE_SOLVE =
true;
148 testDt = sh->getWorkingState()->getTimeStep() / 10.0;
149 sh->getWorkingState()->setTimeStep(testDt);
153 testAFTER_SOLVE =
true;
154 testName =
"Backward Euler - Modifier";
155 stepper->setStepperName(testName);
160 auto x = sh->getWorkingState()->getX();
161 testWorkingValue = get_ele(*(x), 0);
166 "Error - unknown action location.\n");
171 bool testBEFORE_SOLVE;
172 bool testAFTER_SOLVE;
174 double testCurrentValue;
175 double testWorkingValue;
177 std::string testName;
187 stepper->setModel(model);
188 auto modifier =
rcp(
new StepperBackwardEulerModifierTest());
189 stepper->setAppAction(modifier);
190 stepper->initialize();
196 stepper->setInitialConditions(solutionHistory);
197 solutionHistory->initWorkingState();
199 solutionHistory->getWorkingState()->setTimeStep(dt);
200 stepper->takeStep(solutionHistory);
209 auto x = solutionHistory->getCurrentState()->getX();
211 x = solutionHistory->getWorkingState()->getX();
213 auto Dt = solutionHistory->getWorkingState()->getTimeStep();
216 TEST_COMPARE(modifier->testName, ==,
"Backward Euler - Modifier");
221 class StepperBackwardEulerObserverTest
225 StepperBackwardEulerObserverTest()
226 : testBEGIN_STEP(false),
227 testBEFORE_SOLVE(false),
228 testAFTER_SOLVE(false),
230 testCurrentValue(-0.99),
231 testWorkingValue(-0.99),
238 virtual ~StepperBackwardEulerObserverTest() {}
241 virtual void observe(
245 double>::ACTION_LOCATION actLoc)
249 testBEGIN_STEP =
true;
250 auto x = sh->getCurrentState()->getX();
251 testCurrentValue = get_ele(*(x), 0);
255 testBEFORE_SOLVE =
true;
256 testDt = sh->getWorkingState()->getTimeStep();
260 testAFTER_SOLVE =
true;
261 testName = stepper->getStepperType();
266 auto x = sh->getWorkingState()->getX();
267 testWorkingValue = get_ele(*(x), 0);
272 "Error - unknown action location.\n");
277 bool testBEFORE_SOLVE;
278 bool testAFTER_SOLVE;
280 double testCurrentValue;
281 double testWorkingValue;
283 std::string testName;
293 stepper->setModel(model);
294 auto observer =
rcp(
new StepperBackwardEulerObserverTest());
295 stepper->setAppAction(observer);
296 stepper->initialize();
302 stepper->setInitialConditions(solutionHistory);
303 solutionHistory->initWorkingState();
305 solutionHistory->getWorkingState()->setTimeStep(dt);
306 stepper->takeStep(solutionHistory);
315 auto x = solutionHistory->getCurrentState()->getX();
317 x = solutionHistory->getWorkingState()->getX();
326 class StepperBackwardEulerModifierXTest
330 StepperBackwardEulerModifierXTest()
331 : testX_BEGIN_STEP(false),
332 testX_BEFORE_SOLVE(false),
333 testX_AFTER_SOLVE(false),
334 testXDOT_END_STEP(false),
343 virtual ~StepperBackwardEulerModifierXTest() {}
347 const double time,
const double dt,
349 double>::MODIFIER_TYPE modType)
353 testX_BEGIN_STEP =
true;
354 testX = get_ele(*(x), 0);
358 testX_BEFORE_SOLVE =
true;
363 testX_AFTER_SOLVE =
true;
368 testXDOT_END_STEP =
true;
369 testXDot = get_ele(*(x), 0);
374 "Error - unknown action location.\n");
378 bool testX_BEGIN_STEP;
379 bool testX_BEFORE_SOLVE;
380 bool testX_AFTER_SOLVE;
381 bool testXDOT_END_STEP;
395 stepper->setModel(model);
396 auto modifierX =
rcp(
new StepperBackwardEulerModifierXTest());
397 stepper->setAppAction(modifierX);
398 stepper->initialize();
404 stepper->setInitialConditions(solutionHistory);
405 solutionHistory->initWorkingState();
407 solutionHistory->getWorkingState()->setTimeStep(dt);
408 stepper->takeStep(solutionHistory);
417 auto x = solutionHistory->getCurrentState()->getX();
420 auto xDot = solutionHistory->getWorkingState()->getXDot();
421 if (xDot == Teuchos::null) xDot = stepper->getStepperXDot();
424 auto Dt = solutionHistory->getWorkingState()->getTimeStep();
427 auto time = solutionHistory->getWorkingState()->getTime();
437 Teuchos::getParametersFromXmlFile(
"Tempus_BackwardEuler_SinCos.xml");
448 Tempus::createIntegratorBasic<double>(tempusPL, model);
452 integrator->getStepper()->getValidParameters();
454 bool pass = haveSameValuesSorted(*stepperPL, *defaultPL,
true);
457 out <<
"stepperPL -------------- \n"
458 << *stepperPL << std::endl;
459 out <<
"defaultPL -------------- \n"
460 << *defaultPL << std::endl;
468 Tempus::createIntegratorBasic<double>(model,
469 std::string(
"Backward Euler"));
473 stepperPL->set(
"Predictor Stepper Type",
"None");
475 integrator->getStepper()->getValidParameters();
477 bool pass = haveSameValuesSorted(*stepperPL, *defaultPL,
true);
480 out <<
"stepperPL -------------- \n"
481 << *stepperPL << std::endl;
482 out <<
"defaultPL -------------- \n"
483 << *defaultPL << std::endl;
494 std::vector<std::string> options;
495 options.push_back(
"Default Parameters");
496 options.push_back(
"ICConsistency and Check");
498 for (
const auto& option : options) {
501 Teuchos::getParametersFromXmlFile(
"Tempus_BackwardEuler_SinCos.xml");
511 stepper->setModel(model);
512 if (option ==
"ICConsistency and Check") {
513 stepper->setICConsistency(
"Consistent");
514 stepper->setICConsistencyCheck(
true);
516 stepper->initialize();
522 timeStepControl->setInitIndex(tscPL.
get<
int>(
"Initial Time Index"));
523 timeStepControl->setInitTime(tscPL.
get<
double>(
"Initial Time"));
524 timeStepControl->setFinalTime(tscPL.
get<
double>(
"Final Time"));
525 timeStepControl->setInitTimeStep(dt);
526 timeStepControl->initialize();
529 auto inArgsIC = model->getNominalValues();
533 icState->setTime(timeStepControl->getInitTime());
534 icState->setIndex(timeStepControl->getInitIndex());
535 icState->setTimeStep(0.0);
536 icState->setOrder(stepper->getOrder());
541 solutionHistory->setName(
"Forward States");
543 solutionHistory->setStorageLimit(2);
544 solutionHistory->addState(icState);
547 stepper->setInitialConditions(solutionHistory);
551 Tempus::createIntegratorBasic<double>();
552 integrator->setStepper(stepper);
553 integrator->setTimeStepControl(timeStepControl);
554 integrator->setSolutionHistory(solutionHistory);
556 integrator->initialize();
559 bool integratorStatus = integrator->advanceTime();
563 double time = integrator->getTime();
564 double timeFinal = pl->
sublist(
"Default Integrator")
566 .
get<
double>(
"Final Time");
572 model->getExactSolution(time).get_x();
576 Thyra::V_StVpStV(xdiff.
ptr(), 1.0, *x_exact, -1.0, *(x));
579 out <<
" Stepper = " << stepper->description() <<
" with " << option
581 out <<
" =========================" << std::endl;
582 out <<
" Exact solution : " << get_ele(*(x_exact), 0) <<
" "
583 << get_ele(*(x_exact), 1) << std::endl;
584 out <<
" Computed solution: " << get_ele(*(x), 0) <<
" "
585 << get_ele(*(x), 1) << std::endl;
586 out <<
" Difference : " << get_ele(*(xdiff), 0) <<
" "
587 << get_ele(*(xdiff), 1) << std::endl;
588 out <<
" =========================" << std::endl;
589 TEST_FLOATING_EQUALITY(get_ele(*(x), 0), 0.798923, 1.0e-4);
590 TEST_FLOATING_EQUALITY(get_ele(*(x), 1), 0.516729, 1.0e-4);
Teuchos::RCP< SolutionState< Scalar > > createSolutionStateX(const Teuchos::RCP< Thyra::VectorBase< Scalar > > &x, const Teuchos::RCP< Thyra::VectorBase< Scalar > > &xdot=Teuchos::null, const Teuchos::RCP< Thyra::VectorBase< Scalar > > &xdotdot=Teuchos::null)
Nonmember constructor from non-const solution vectors, x.
void testFactoryConstruction(std::string stepperType, const Teuchos::RCP< const Thyra::ModelEvaluator< double > > &model)
Unit test utility for Stepper construction through StepperFactory.
Default observer for StepperBackwardEuler.
T & get(const std::string &name, T def_value)
Forward Euler time stepper.
Modify before the implicit solve.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
#define TEST_COMPARE(v1, comp, v2)
After the implicit solve.
At the beginning of the step.
#define TEST_FLOATING_EQUALITY(v1, v2, tol)
Default modifier for StepperBackwardEuler.
Modify after the implicit solve.
Sine-Cosine model problem from Rythmos. This is a canonical Sine-Cosine differential equation with a...
Teuchos::RCP< Teuchos::ParameterList > defaultSolverParameters()
Returns the default solver ParameterList for implicit Steppers.
virtual void modify(Teuchos::RCP< Thyra::VectorBase< double > >, const double, const double, const MODIFIER_TYPE modType)=0
Modify solution based on the MODIFIER_TYPE.
Application Action for StepperBackwardEuler.
Base ModifierX for StepperBackwardEuler.
Teuchos::RCP< SolutionHistory< Scalar > > createSolutionHistoryME(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &model)
Nonmember contructor from a Thyra ModelEvaluator.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Modify at the beginning of the step.
Base observer for StepperBackwardEuler.
TEUCHOS_UNIT_TEST(BackwardEuler, Default_Construction)
TimeStepControl manages the time step size. There several mechanisms that effect the time step size a...
Backward Euler time stepper.
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
Keep a fix number of states.
Modify at the end of the step.
Before the implicit solve.
Base modifier for StepperBackwardEuler.
ParameterList & sublist(const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
#define TEUCHOS_ASSERT(assertion_test)
Default ModifierX for StepperBackwardEuler.
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)