14#include "Tempus_StepperBDF2.hpp"
22using Teuchos::ParameterList;
25using Teuchos::rcp_const_cast;
26using Teuchos::rcp_dynamic_cast;
27using Teuchos::sublist;
37 stepper->setModel(model);
38 stepper->initialize();
39 TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
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();
60 TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
61 stepper->setSolver(solver);
62 stepper->initialize();
63 TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
64 stepper->setStartUpStepper(startUpStepper);
65 stepper->initialize();
66 TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
67 stepper->setUseFSAL(useFSAL);
68 stepper->initialize();
69 TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
70 stepper->setICConsistency(ICConsistency);
71 stepper->initialize();
72 TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
73 stepper->setICConsistencyCheck(ICConsistencyCheck);
74 stepper->initialize();
75 TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
76 stepper->setZeroInitialGuess(zeroInitialGuess);
77 stepper->initialize();
78 TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
81 model, solver, startUpStepper, useFSAL, ICConsistency, ICConsistencyCheck,
82 zeroInitialGuess, modifier));
83 TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
85 TEUCHOS_ASSERT(stepper->getOrder() == 2);
101 RCP<ParameterList> pList =
102 Teuchos::getParametersFromXmlFile(
"Tempus_BDF2_SinCos.xml");
105 RCP<ParameterList> scm_pl = sublist(pList,
"SinCosModel",
true);
108 RCP<ParameterList> tempusPL = sublist(pList,
"Tempus",
true);
112 RCP<Tempus::IntegratorBasic<double> > integrator =
113 Tempus::createIntegratorBasic<double>(tempusPL, model);
115 RCP<ParameterList> stepperPL = sublist(tempusPL,
"Default Stepper",
true);
116 RCP<const ParameterList> defaultPL =
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;
131 RCP<Tempus::IntegratorBasic<double> > integrator =
132 Tempus::createIntegratorBasic<double>(model, std::string(
"BDF2"));
134 RCP<ParameterList> stepperPL = sublist(tempusPL,
"Default Stepper",
true);
135 RCP<const ParameterList> defaultPL =
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) {
161 RCP<ParameterList> pList =
162 Teuchos::getParametersFromXmlFile(
"Tempus_BDF2_SinCos.xml");
163 RCP<ParameterList> pl = sublist(pList,
"Tempus",
true);
166 RCP<ParameterList> scm_pl = sublist(pList,
"SinCosModel",
true);
172 stepper->setModel(model);
173 if (option ==
"ICConsistency and Check") {
174 stepper->setICConsistency(
"Consistent");
175 stepper->setICConsistencyCheck(
true);
177 stepper->initialize();
181 ParameterList tscPL =
182 pl->sublist(
"Default Integrator").sublist(
"Time Step Control");
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();
192 rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
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);
211 RCP<Tempus::IntegratorBasic<double> > integrator =
212 Tempus::createIntegratorBasic<double>();
213 integrator->setStepper(stepper);
214 integrator->setTimeStepControl(timeStepControl);
215 integrator->setSolutionHistory(solutionHistory);
217 integrator->initialize();
220 bool integratorStatus = integrator->advanceTime();
221 TEST_ASSERT(integratorStatus)
224 double time = integrator->getTime();
225 double timeFinal = pl->sublist(
"Default Integrator")
226 .sublist(
"Time Step Control")
227 .get<
double>(
"Final Time");
228 TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
231 RCP<Thyra::VectorBase<double> > x = integrator->getX();
232 RCP<const Thyra::VectorBase<double> > x_exact =
233 model->getExactSolution(time).get_x();
236 RCP<Thyra::VectorBase<double> > xdiff = x->clone_v();
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);
257class 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() {}
284 case StepperBDF2AppAction<double>::BEGIN_STEP: {
285 testBEGIN_STEP =
true;
286 auto x = sh->getWorkingState()->getX();
287 testCurrentValue = get_ele(*(x), 0);
290 case StepperBDF2AppAction<double>::BEFORE_SOLVE: {
291 testBEFORE_SOLVE =
true;
292 testType =
"BDF2 - Modifier";
295 case StepperBDF2AppAction<double>::AFTER_SOLVE: {
296 testAFTER_SOLVE =
true;
297 testDt = sh->getCurrentState()->getTimeStep() / 10.0;
298 sh->getCurrentState()->setTimeStep(testDt);
301 case StepperBDF2AppAction<double>::END_STEP: {
303 auto x = sh->getWorkingState()->getX();
304 testWorkingValue = get_ele(*(x), 0);
308 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
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();
336 rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
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);
369 TEST_COMPARE(modifier->testBEGIN_STEP, ==,
true);
370 TEST_COMPARE(modifier->testBEFORE_SOLVE, ==,
true);
371 TEST_COMPARE(modifier->testAFTER_SOLVE, ==,
true);
372 TEST_COMPARE(modifier->testEND_STEP, ==,
true);
374 auto Dt = solutionHistory->getCurrentState()->getTimeStep();
375 TEST_FLOATING_EQUALITY(modifier->testDt, Dt, 1.0e-15);
376 auto x = solutionHistory->getCurrentState()->getX();
377 TEST_FLOATING_EQUALITY(modifier->testCurrentValue, get_ele(*(x), 0), 1.0e-15);
378 x = solutionHistory->getWorkingState()->getX();
379 TEST_FLOATING_EQUALITY(modifier->testWorkingValue, get_ele(*(x), 0), 1.0e-15);
380 TEST_COMPARE(modifier->testType, ==,
"BDF2 - Modifier");
385class 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() {}
412 case StepperBDF2AppAction<double>::BEGIN_STEP: {
413 testBEGIN_STEP =
true;
414 auto x = sh->getCurrentState()->getX();
415 testCurrentValue = get_ele(*(x), 0);
418 case StepperBDF2AppAction<double>::BEFORE_SOLVE: {
419 testBEFORE_SOLVE =
true;
420 testType = stepper->getStepperType();
423 case StepperBDF2AppAction<double>::AFTER_SOLVE: {
424 testAFTER_SOLVE =
true;
425 testDt = sh->getCurrentState()->getTimeStep();
428 case StepperBDF2AppAction<double>::END_STEP: {
430 auto x = sh->getWorkingState()->getX();
431 testWorkingValue = get_ele(*(x), 0);
436 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
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();
466 rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
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);
499 TEST_COMPARE(observer->testBEGIN_STEP, ==,
true);
500 TEST_COMPARE(observer->testBEFORE_SOLVE, ==,
true);
501 TEST_COMPARE(observer->testAFTER_SOLVE, ==,
true);
502 TEST_COMPARE(observer->testEND_STEP, ==,
true);
504 auto Dt = solutionHistory->getCurrentState()->getTimeStep();
505 TEST_FLOATING_EQUALITY(observer->testDt, Dt, 1.0e-15);
507 auto x = solutionHistory->getCurrentState()->getX();
508 TEST_FLOATING_EQUALITY(observer->testCurrentValue, get_ele(*(x), 0), 1.0e-15);
509 x = solutionHistory->getWorkingState()->getX();
510 TEST_FLOATING_EQUALITY(observer->testWorkingValue, get_ele(*(x), 0), 1.0e-15);
511 TEST_COMPARE(observer->testType, ==,
"BDF2 - Modifier");
516class 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() {}
543 case StepperBDF2ModifierXBase<double>::X_BEGIN_STEP: {
544 testX_BEGIN_STEP =
true;
545 testXbegin = get_ele(*(x), 0);
548 case StepperBDF2ModifierXBase<double>::X_BEFORE_SOLVE: {
549 testX_BEFORE_SOLVE =
true;
553 case StepperBDF2ModifierXBase<double>::X_AFTER_SOLVE: {
554 testX_AFTER_SOLVE =
true;
558 case StepperBDF2ModifierXBase<double>::X_END_STEP: {
559 testX_END_STEP =
true;
560 testXend = get_ele(*(x), 0);
564 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
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();
593 rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
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);
626 TEST_COMPARE(modifierX->testX_BEGIN_STEP, ==,
true);
627 TEST_COMPARE(modifierX->testX_BEFORE_SOLVE, ==,
true);
628 TEST_COMPARE(modifierX->testX_AFTER_SOLVE, ==,
true);
629 TEST_COMPARE(modifierX->testX_END_STEP, ==,
true);
632 auto x = solutionHistory->getCurrentState()->getX();
633 TEST_FLOATING_EQUALITY(modifierX->testXbegin, get_ele(*(x), 0), 1.0e-15);
634 auto Dt = solutionHistory->getWorkingState()->getTimeStep();
635 x = solutionHistory->getWorkingState()->getX();
636 TEST_FLOATING_EQUALITY(modifierX->testXend, get_ele(*(x), 0), 1.0e-15);
637 TEST_FLOATING_EQUALITY(modifierX->testDt, Dt, 1.0e-15);
638 auto time = solutionHistory->getWorkingState()->getTime();
639 TEST_FLOATING_EQUALITY(modifierX->testTime, time, 1.0e-15);
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
ACTION_LOCATION
Indicates the location of application action (see algorithm).
Base modifier for StepperBDF2.
Default modifier for StepperBDF2.
Base ModifierX for StepperBDF2.
MODIFIER_TYPE
Indicates the location of application action (see algorithm).
virtual void modify(Teuchos::RCP< Thyra::VectorBase< double > >, const double, const double, const MODIFIER_TYPE modType)=0
Modify solution based on the MODIFIER_TYPE.
Base observer for StepperBDF2.
BDF2 (Backward-Difference-Formula-2) time stepper.
TimeStepControl manages the time step size. There several mechanisms that effect the time step size a...
Sine-Cosine model problem from Rythmos. This is a canonical Sine-Cosine differential equation.
TEUCHOS_UNIT_TEST(BackwardEuler, Default_Construction)
void testFactoryConstruction(std::string stepperType, const Teuchos::RCP< const Thyra::ModelEvaluator< double > > &model)
Unit test utility for Stepper construction through StepperFactory.
@ STORAGE_TYPE_STATIC
Keep a fix number of states.
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.
Teuchos::RCP< Teuchos::ParameterList > defaultSolverParameters()
Returns the default solver ParameterList for implicit Steppers.