13 #include "Tempus_StepperBDF2.hpp"
19 namespace Tempus_Unit_Test {
24 using Teuchos::rcp_const_cast;
25 using Teuchos::rcp_dynamic_cast;
26 using Teuchos::sublist;
36 stepper->setModel(model);
37 stepper->initialize();
46 startUpStepper->setModel(
48 startUpStepper->initialize();
51 bool useFSAL = defaultStepper->getUseFSAL();
52 std::string ICConsistency = defaultStepper->getICConsistency();
53 bool ICConsistencyCheck = defaultStepper->getICConsistencyCheck();
54 bool zeroInitialGuess = defaultStepper->getZeroInitialGuess();
57 stepper->setAppAction(modifier);
58 stepper->initialize();
60 stepper->setSolver(solver);
61 stepper->initialize();
63 stepper->setStartUpStepper(startUpStepper);
64 stepper->initialize();
66 stepper->setUseFSAL(useFSAL);
67 stepper->initialize();
69 stepper->setICConsistency(ICConsistency);
70 stepper->initialize();
72 stepper->setICConsistencyCheck(ICConsistencyCheck);
73 stepper->initialize();
75 stepper->setZeroInitialGuess(zeroInitialGuess);
76 stepper->initialize();
80 model, solver, startUpStepper, useFSAL, ICConsistency, ICConsistencyCheck,
81 zeroInitialGuess, modifier));
101 Teuchos::getParametersFromXmlFile(
"Tempus_BDF2_SinCos.xml");
112 Tempus::createIntegratorBasic<double>(tempusPL, model);
116 integrator->getStepper()->getValidParameters();
117 bool pass = haveSameValuesSorted(*stepperPL, *defaultPL,
true);
120 out <<
"stepperPL -------------- \n"
121 << *stepperPL << std::endl;
122 out <<
"defaultPL -------------- \n"
123 << *defaultPL << std::endl;
131 Tempus::createIntegratorBasic<double>(model, std::string(
"BDF2"));
135 integrator->getStepper()->getValidParameters();
137 bool pass = haveSameValuesSorted(*stepperPL, *defaultPL,
true);
140 out <<
"stepperPL -------------- \n"
141 << *stepperPL << std::endl;
142 out <<
"defaultPL -------------- \n"
143 << *defaultPL << std::endl;
154 std::vector<std::string> options;
155 options.push_back(
"Default Parameters");
156 options.push_back(
"ICConsistency and Check");
158 for (
const auto& option : options) {
161 Teuchos::getParametersFromXmlFile(
"Tempus_BDF2_SinCos.xml");
171 stepper->setModel(model);
172 if (option ==
"ICConsistency and Check") {
173 stepper->setICConsistency(
"Consistent");
174 stepper->setICConsistencyCheck(
true);
176 stepper->initialize();
182 timeStepControl->setInitIndex(tscPL.
get<
int>(
"Initial Time Index"));
183 timeStepControl->setInitTime(tscPL.
get<
double>(
"Initial Time"));
184 timeStepControl->setFinalTime(tscPL.
get<
double>(
"Final Time"));
185 timeStepControl->setInitTimeStep(dt);
186 timeStepControl->initialize();
189 auto inArgsIC = model->getNominalValues();
193 icState->setTime(timeStepControl->getInitTime());
194 icState->setIndex(timeStepControl->getInitIndex());
195 icState->setTimeStep(0.0);
196 icState->setOrder(stepper->getOrder());
201 solutionHistory->setName(
"Forward States");
203 solutionHistory->setStorageLimit(3);
204 solutionHistory->addState(icState);
207 stepper->setInitialConditions(solutionHistory);
211 Tempus::createIntegratorBasic<double>();
212 integrator->setStepper(stepper);
213 integrator->setTimeStepControl(timeStepControl);
214 integrator->setSolutionHistory(solutionHistory);
216 integrator->initialize();
219 bool integratorStatus = integrator->advanceTime();
223 double time = integrator->getTime();
224 double timeFinal = pl->
sublist(
"Default Integrator")
226 .
get<
double>(
"Final Time");
232 model->getExactSolution(time).get_x();
236 Thyra::V_StVpStV(xdiff.
ptr(), 1.0, *x_exact, -1.0, *(x));
239 out <<
" Stepper = " << stepper->description() <<
"\n with "
240 << option << std::endl;
241 out <<
" =========================" << std::endl;
242 out <<
" Exact solution : " << get_ele(*(x_exact), 0) <<
" "
243 << get_ele(*(x_exact), 1) << std::endl;
244 out <<
" Computed solution: " << get_ele(*(x), 0) <<
" "
245 << get_ele(*(x), 1) << std::endl;
246 out <<
" Difference : " << get_ele(*(xdiff), 0) <<
" "
247 << get_ele(*(xdiff), 1) << std::endl;
248 out <<
" =========================" << std::endl;
249 TEST_FLOATING_EQUALITY(get_ele(*(x), 0), 0.839732, 1.0e-4);
250 TEST_FLOATING_EQUALITY(get_ele(*(x), 1), 0.542663, 1.0e-4);
256 class StepperBDF2ModifierTest
260 StepperBDF2ModifierTest()
261 : testBEGIN_STEP(false),
262 testBEFORE_SOLVE(false),
263 testAFTER_SOLVE(false),
265 testCurrentValue(-0.99),
266 testWorkingValue(-0.99),
273 virtual ~StepperBDF2ModifierTest() {}
284 testBEGIN_STEP =
true;
285 auto x = sh->getWorkingState()->getX();
286 testCurrentValue = get_ele(*(x), 0);
290 testBEFORE_SOLVE =
true;
291 testType =
"BDF2 - Modifier";
295 testAFTER_SOLVE =
true;
296 testDt = sh->getCurrentState()->getTimeStep() / 10.0;
297 sh->getCurrentState()->setTimeStep(testDt);
302 auto x = sh->getWorkingState()->getX();
303 testWorkingValue = get_ele(*(x), 0);
308 "Error - unknown action location.\n");
312 bool testBEFORE_SOLVE;
313 bool testAFTER_SOLVE;
315 double testCurrentValue;
316 double testWorkingValue;
318 std::string testType;
327 stepper->setModel(model);
328 auto modifier =
rcp(
new StepperBDF2ModifierTest());
329 stepper->setAppAction(modifier);
330 stepper->initialize();
333 auto inArgsIC = model->getNominalValues();
337 icState->setTime(0.0);
338 icState->setIndex(0);
339 icState->setTimeStep(1.0);
340 icState->setOrder(stepper->getOrder());
345 timeStepControl->setInitIndex(0);
346 timeStepControl->setInitTime(0.0);
347 timeStepControl->setFinalTime(2.0);
348 timeStepControl->setInitTimeStep(1.0);
349 timeStepControl->initialize();
353 solutionHistory->setName(
"Forward States");
355 solutionHistory->setStorageLimit(3);
356 solutionHistory->addState(icState);
359 stepper->setInitialConditions(solutionHistory);
360 solutionHistory->initWorkingState();
362 solutionHistory->getWorkingState()->setTimeStep(dt);
363 stepper->takeStep(solutionHistory);
364 solutionHistory->promoteWorkingState();
365 solutionHistory->initWorkingState();
366 stepper->takeStep(solutionHistory);
373 auto Dt = solutionHistory->getCurrentState()->getTimeStep();
375 auto x = solutionHistory->getCurrentState()->getX();
377 x = solutionHistory->getWorkingState()->getX();
379 TEST_COMPARE(modifier->testType, ==,
"BDF2 - Modifier");
384 class StepperBDF2ObserverTest
388 StepperBDF2ObserverTest()
389 : testBEGIN_STEP(false),
390 testBEFORE_SOLVE(false),
391 testAFTER_SOLVE(false),
393 testCurrentValue(0.99),
394 testWorkingValue(0.99),
401 virtual ~StepperBDF2ObserverTest() {}
412 testBEGIN_STEP =
true;
413 auto x = sh->getCurrentState()->getX();
414 testCurrentValue = get_ele(*(x), 0);
418 testBEFORE_SOLVE =
true;
419 testType = stepper->getStepperType();
423 testAFTER_SOLVE =
true;
424 testDt = sh->getCurrentState()->getTimeStep();
429 auto x = sh->getWorkingState()->getX();
430 testWorkingValue = get_ele(*(x), 0);
436 "Error - unknown action location.\n");
440 bool testBEFORE_SOLVE;
441 bool testAFTER_SOLVE;
443 double testCurrentValue;
444 double testWorkingValue;
446 std::string testType;
457 stepper->setModel(model);
458 auto observer =
rcp(
new StepperBDF2ModifierTest());
459 stepper->setAppAction(observer);
460 stepper->initialize();
463 auto inArgsIC = model->getNominalValues();
467 icState->setTime(0.0);
468 icState->setIndex(0);
469 icState->setTimeStep(1.0);
470 icState->setOrder(stepper->getOrder());
475 timeStepControl->setInitIndex(0);
476 timeStepControl->setInitTime(0.0);
477 timeStepControl->setFinalTime(2.0);
478 timeStepControl->setInitTimeStep(1.0);
479 timeStepControl->initialize();
483 solutionHistory->setName(
"Forward States");
485 solutionHistory->setStorageLimit(3);
486 solutionHistory->addState(icState);
489 stepper->setInitialConditions(solutionHistory);
490 solutionHistory->initWorkingState();
492 solutionHistory->getWorkingState()->setTimeStep(dt);
493 stepper->takeStep(solutionHistory);
494 solutionHistory->promoteWorkingState();
495 solutionHistory->initWorkingState();
496 stepper->takeStep(solutionHistory);
503 auto Dt = solutionHistory->getCurrentState()->getTimeStep();
506 auto x = solutionHistory->getCurrentState()->getX();
508 x = solutionHistory->getWorkingState()->getX();
510 TEST_COMPARE(observer->testType, ==,
"BDF2 - Modifier");
515 class StepperBDF2ModifierXTest
519 StepperBDF2ModifierXTest()
520 : testX_BEGIN_STEP(false),
521 testX_BEFORE_SOLVE(false),
522 testX_AFTER_SOLVE(false),
523 testX_END_STEP(false),
532 virtual ~StepperBDF2ModifierXTest() {}
543 testX_BEGIN_STEP =
true;
544 testXbegin = get_ele(*(x), 0);
548 testX_BEFORE_SOLVE =
true;
553 testX_AFTER_SOLVE =
true;
558 testX_END_STEP =
true;
559 testXend = get_ele(*(x), 0);
564 "Error - unknown action location.\n");
567 bool testX_BEGIN_STEP;
568 bool testX_BEFORE_SOLVE;
569 bool testX_AFTER_SOLVE;
584 stepper->setModel(model);
585 auto modifierX =
rcp(
new StepperBDF2ModifierXTest());
586 stepper->setAppAction(modifierX);
587 stepper->initialize();
590 auto inArgsIC = model->getNominalValues();
594 icState->setTime(0.0);
595 icState->setIndex(0);
596 icState->setTimeStep(1.0);
597 icState->setOrder(stepper->getOrder());
602 timeStepControl->setInitIndex(0);
603 timeStepControl->setInitTime(0.0);
604 timeStepControl->setFinalTime(2.0);
605 timeStepControl->setInitTimeStep(1.0);
606 timeStepControl->initialize();
610 solutionHistory->setName(
"Forward States");
612 solutionHistory->setStorageLimit(3);
613 solutionHistory->addState(icState);
616 stepper->setInitialConditions(solutionHistory);
617 solutionHistory->initWorkingState();
619 solutionHistory->getWorkingState()->setTimeStep(dt);
620 stepper->takeStep(solutionHistory);
621 solutionHistory->promoteWorkingState();
622 solutionHistory->initWorkingState();
623 stepper->takeStep(solutionHistory);
631 auto x = solutionHistory->getCurrentState()->getX();
633 auto Dt = solutionHistory->getWorkingState()->getTimeStep();
634 x = solutionHistory->getWorkingState()->getX();
637 auto time = solutionHistory->getWorkingState()->getTime();
BDF2 (Backward-Difference-Formula-2) time stepper.
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.
ACTION_LOCATION
Indicates the location of application action (see algorithm).
Modify after the implicit solve.
void testFactoryConstruction(std::string stepperType, const Teuchos::RCP< const Thyra::ModelEvaluator< double > > &model)
Unit test utility for Stepper construction through StepperFactory.
Modify at the beginning of the step.
T & get(const std::string &name, T def_value)
Base ModifierX for StepperBDF2.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
#define TEST_COMPARE(v1, comp, v2)
Modify before the implicit solve.
#define TEST_FLOATING_EQUALITY(v1, v2, tol)
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.
Modify at the end of the step.
Default modifier for StepperBDF2.
Base observer for StepperBDF2.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
virtual void modify(Teuchos::RCP< Thyra::VectorBase< double > >, const double, const double, const MODIFIER_TYPE modType)=0
Modify solution based on the MODIFIER_TYPE.
TEUCHOS_UNIT_TEST(BackwardEuler, Default_Construction)
TimeStepControl manages the time step size. There several mechanisms that effect the time step size a...
At the beginning of the step.
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
Keep a fix number of states.
Base modifier for StepperBDF2.
ParameterList & sublist(const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
#define TEUCHOS_ASSERT(assertion_test)
MODIFIER_TYPE
Indicates the location of application action (see algorithm).
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)