14 #include "Tempus_StepperBDF2.hpp"
20 namespace Tempus_Unit_Test {
25 using Teuchos::rcp_const_cast;
26 using Teuchos::rcp_dynamic_cast;
27 using Teuchos::sublist;
37 stepper->setModel(model);
38 stepper->initialize();
47 startUpStepper->setModel(
49 startUpStepper->initialize();
52 bool useFSAL = defaultStepper->getUseFSAL();
53 std::string ICConsistency = defaultStepper->getICConsistency();
54 bool ICConsistencyCheck = defaultStepper->getICConsistencyCheck();
55 bool zeroInitialGuess = defaultStepper->getZeroInitialGuess();
58 stepper->setAppAction(modifier);
59 stepper->initialize();
61 stepper->setSolver(solver);
62 stepper->initialize();
64 stepper->setStartUpStepper(startUpStepper);
65 stepper->initialize();
67 stepper->setUseFSAL(useFSAL);
68 stepper->initialize();
70 stepper->setICConsistency(ICConsistency);
71 stepper->initialize();
73 stepper->setICConsistencyCheck(ICConsistencyCheck);
74 stepper->initialize();
76 stepper->setZeroInitialGuess(zeroInitialGuess);
77 stepper->initialize();
81 model, solver, startUpStepper, useFSAL, ICConsistency, ICConsistencyCheck,
82 zeroInitialGuess, modifier));
102 Teuchos::getParametersFromXmlFile(
"Tempus_BDF2_SinCos.xml");
113 Tempus::createIntegratorBasic<double>(tempusPL, model);
117 integrator->getStepper()->getValidParameters();
118 bool pass = haveSameValuesSorted(*stepperPL, *defaultPL,
true);
121 out <<
"stepperPL -------------- \n"
122 << *stepperPL << std::endl;
123 out <<
"defaultPL -------------- \n"
124 << *defaultPL << std::endl;
132 Tempus::createIntegratorBasic<double>(model, std::string(
"BDF2"));
136 integrator->getStepper()->getValidParameters();
138 bool pass = haveSameValuesSorted(*stepperPL, *defaultPL,
true);
141 out <<
"stepperPL -------------- \n"
142 << *stepperPL << std::endl;
143 out <<
"defaultPL -------------- \n"
144 << *defaultPL << std::endl;
155 std::vector<std::string> options;
156 options.push_back(
"Default Parameters");
157 options.push_back(
"ICConsistency and Check");
159 for (
const auto& option : options) {
162 Teuchos::getParametersFromXmlFile(
"Tempus_BDF2_SinCos.xml");
172 stepper->setModel(model);
173 if (option ==
"ICConsistency and Check") {
174 stepper->setICConsistency(
"Consistent");
175 stepper->setICConsistencyCheck(
true);
177 stepper->initialize();
183 timeStepControl->setInitIndex(tscPL.
get<
int>(
"Initial Time Index"));
184 timeStepControl->setInitTime(tscPL.
get<
double>(
"Initial Time"));
185 timeStepControl->setFinalTime(tscPL.
get<
double>(
"Final Time"));
186 timeStepControl->setInitTimeStep(dt);
187 timeStepControl->initialize();
190 auto inArgsIC = model->getNominalValues();
194 icState->setTime(timeStepControl->getInitTime());
195 icState->setIndex(timeStepControl->getInitIndex());
196 icState->setTimeStep(0.0);
197 icState->setOrder(stepper->getOrder());
202 solutionHistory->setName(
"Forward States");
204 solutionHistory->setStorageLimit(3);
205 solutionHistory->addState(icState);
208 stepper->setInitialConditions(solutionHistory);
212 Tempus::createIntegratorBasic<double>();
213 integrator->setStepper(stepper);
214 integrator->setTimeStepControl(timeStepControl);
215 integrator->setSolutionHistory(solutionHistory);
217 integrator->initialize();
220 bool integratorStatus = integrator->advanceTime();
224 double time = integrator->getTime();
225 double timeFinal = pl->
sublist(
"Default Integrator")
227 .
get<
double>(
"Final Time");
233 model->getExactSolution(time).get_x();
237 Thyra::V_StVpStV(xdiff.
ptr(), 1.0, *x_exact, -1.0, *(x));
240 out <<
" Stepper = " << stepper->description() <<
"\n with "
241 << option << std::endl;
242 out <<
" =========================" << std::endl;
243 out <<
" Exact solution : " << get_ele(*(x_exact), 0) <<
" "
244 << get_ele(*(x_exact), 1) << std::endl;
245 out <<
" Computed solution: " << get_ele(*(x), 0) <<
" "
246 << get_ele(*(x), 1) << std::endl;
247 out <<
" Difference : " << get_ele(*(xdiff), 0) <<
" "
248 << get_ele(*(xdiff), 1) << std::endl;
249 out <<
" =========================" << std::endl;
250 TEST_FLOATING_EQUALITY(get_ele(*(x), 0), 0.839732, 1.0e-4);
251 TEST_FLOATING_EQUALITY(get_ele(*(x), 1), 0.542663, 1.0e-4);
257 class StepperBDF2ModifierTest
261 StepperBDF2ModifierTest()
262 : testBEGIN_STEP(false),
263 testBEFORE_SOLVE(false),
264 testAFTER_SOLVE(false),
266 testCurrentValue(-0.99),
267 testWorkingValue(-0.99),
274 virtual ~StepperBDF2ModifierTest() {}
285 testBEGIN_STEP =
true;
286 auto x = sh->getWorkingState()->getX();
287 testCurrentValue = get_ele(*(x), 0);
291 testBEFORE_SOLVE =
true;
292 testType =
"BDF2 - Modifier";
296 testAFTER_SOLVE =
true;
297 testDt = sh->getCurrentState()->getTimeStep() / 10.0;
298 sh->getCurrentState()->setTimeStep(testDt);
303 auto x = sh->getWorkingState()->getX();
304 testWorkingValue = get_ele(*(x), 0);
309 "Error - unknown action location.\n");
313 bool testBEFORE_SOLVE;
314 bool testAFTER_SOLVE;
316 double testCurrentValue;
317 double testWorkingValue;
319 std::string testType;
328 stepper->setModel(model);
329 auto modifier =
rcp(
new StepperBDF2ModifierTest());
330 stepper->setAppAction(modifier);
331 stepper->initialize();
334 auto inArgsIC = model->getNominalValues();
338 icState->setTime(0.0);
339 icState->setIndex(0);
340 icState->setTimeStep(1.0);
341 icState->setOrder(stepper->getOrder());
346 timeStepControl->setInitIndex(0);
347 timeStepControl->setInitTime(0.0);
348 timeStepControl->setFinalTime(2.0);
349 timeStepControl->setInitTimeStep(1.0);
350 timeStepControl->initialize();
354 solutionHistory->setName(
"Forward States");
356 solutionHistory->setStorageLimit(3);
357 solutionHistory->addState(icState);
360 stepper->setInitialConditions(solutionHistory);
361 solutionHistory->initWorkingState();
363 solutionHistory->getWorkingState()->setTimeStep(dt);
364 stepper->takeStep(solutionHistory);
365 solutionHistory->promoteWorkingState();
366 solutionHistory->initWorkingState();
367 stepper->takeStep(solutionHistory);
374 auto Dt = solutionHistory->getCurrentState()->getTimeStep();
376 auto x = solutionHistory->getCurrentState()->getX();
378 x = solutionHistory->getWorkingState()->getX();
380 TEST_COMPARE(modifier->testType, ==,
"BDF2 - Modifier");
385 class StepperBDF2ObserverTest
389 StepperBDF2ObserverTest()
390 : testBEGIN_STEP(false),
391 testBEFORE_SOLVE(false),
392 testAFTER_SOLVE(false),
394 testCurrentValue(0.99),
395 testWorkingValue(0.99),
402 virtual ~StepperBDF2ObserverTest() {}
413 testBEGIN_STEP =
true;
414 auto x = sh->getCurrentState()->getX();
415 testCurrentValue = get_ele(*(x), 0);
419 testBEFORE_SOLVE =
true;
420 testType = stepper->getStepperType();
424 testAFTER_SOLVE =
true;
425 testDt = sh->getCurrentState()->getTimeStep();
430 auto x = sh->getWorkingState()->getX();
431 testWorkingValue = get_ele(*(x), 0);
437 "Error - unknown action location.\n");
441 bool testBEFORE_SOLVE;
442 bool testAFTER_SOLVE;
444 double testCurrentValue;
445 double testWorkingValue;
447 std::string testType;
458 stepper->setModel(model);
459 auto observer =
rcp(
new StepperBDF2ModifierTest());
460 stepper->setAppAction(observer);
461 stepper->initialize();
464 auto inArgsIC = model->getNominalValues();
468 icState->setTime(0.0);
469 icState->setIndex(0);
470 icState->setTimeStep(1.0);
471 icState->setOrder(stepper->getOrder());
476 timeStepControl->setInitIndex(0);
477 timeStepControl->setInitTime(0.0);
478 timeStepControl->setFinalTime(2.0);
479 timeStepControl->setInitTimeStep(1.0);
480 timeStepControl->initialize();
484 solutionHistory->setName(
"Forward States");
486 solutionHistory->setStorageLimit(3);
487 solutionHistory->addState(icState);
490 stepper->setInitialConditions(solutionHistory);
491 solutionHistory->initWorkingState();
493 solutionHistory->getWorkingState()->setTimeStep(dt);
494 stepper->takeStep(solutionHistory);
495 solutionHistory->promoteWorkingState();
496 solutionHistory->initWorkingState();
497 stepper->takeStep(solutionHistory);
504 auto Dt = solutionHistory->getCurrentState()->getTimeStep();
507 auto x = solutionHistory->getCurrentState()->getX();
509 x = solutionHistory->getWorkingState()->getX();
511 TEST_COMPARE(observer->testType, ==,
"BDF2 - Modifier");
516 class StepperBDF2ModifierXTest
520 StepperBDF2ModifierXTest()
521 : testX_BEGIN_STEP(false),
522 testX_BEFORE_SOLVE(false),
523 testX_AFTER_SOLVE(false),
524 testX_END_STEP(false),
533 virtual ~StepperBDF2ModifierXTest() {}
544 testX_BEGIN_STEP =
true;
545 testXbegin = get_ele(*(x), 0);
549 testX_BEFORE_SOLVE =
true;
554 testX_AFTER_SOLVE =
true;
559 testX_END_STEP =
true;
560 testXend = get_ele(*(x), 0);
565 "Error - unknown action location.\n");
568 bool testX_BEGIN_STEP;
569 bool testX_BEFORE_SOLVE;
570 bool testX_AFTER_SOLVE;
585 stepper->setModel(model);
586 auto modifierX =
rcp(
new StepperBDF2ModifierXTest());
587 stepper->setAppAction(modifierX);
588 stepper->initialize();
591 auto inArgsIC = model->getNominalValues();
595 icState->setTime(0.0);
596 icState->setIndex(0);
597 icState->setTimeStep(1.0);
598 icState->setOrder(stepper->getOrder());
603 timeStepControl->setInitIndex(0);
604 timeStepControl->setInitTime(0.0);
605 timeStepControl->setFinalTime(2.0);
606 timeStepControl->setInitTimeStep(1.0);
607 timeStepControl->initialize();
611 solutionHistory->setName(
"Forward States");
613 solutionHistory->setStorageLimit(3);
614 solutionHistory->addState(icState);
617 stepper->setInitialConditions(solutionHistory);
618 solutionHistory->initWorkingState();
620 solutionHistory->getWorkingState()->setTimeStep(dt);
621 stepper->takeStep(solutionHistory);
622 solutionHistory->promoteWorkingState();
623 solutionHistory->initWorkingState();
624 stepper->takeStep(solutionHistory);
632 auto x = solutionHistory->getCurrentState()->getX();
634 auto Dt = solutionHistory->getWorkingState()->getTimeStep();
635 x = solutionHistory->getWorkingState()->getX();
638 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)