13 #include "Teuchos_DefaultComm.hpp"
15 #include "Thyra_VectorStdOps.hpp"
17 #include "Tempus_IntegratorBasic.hpp"
18 #include "Tempus_StepperFactory.hpp"
19 #include "Tempus_StepperExplicitRK.hpp"
21 #include "../TestModels/SinCosModel.hpp"
22 #include "../TestModels/VanDerPolModel.hpp"
23 #include "../TestUtils/Tempus_ConvergenceTestUtils.hpp"
28 namespace Tempus_Test {
30 using Teuchos::getParametersFromXmlFile;
34 using Teuchos::rcp_const_cast;
35 using Teuchos::sublist;
45 std::vector<std::string> RKMethods;
46 RKMethods.push_back(
"General ERK");
47 RKMethods.push_back(
"RK Forward Euler");
48 RKMethods.push_back(
"RK Explicit 4 Stage");
49 RKMethods.push_back(
"RK Explicit 3/8 Rule");
50 RKMethods.push_back(
"RK Explicit 4 Stage 3rd order by Runge");
51 RKMethods.push_back(
"RK Explicit 5 Stage 3rd order by Kinnmark and Gray");
52 RKMethods.push_back(
"RK Explicit 3 Stage 3rd order");
53 RKMethods.push_back(
"RK Explicit 3 Stage 3rd order TVD");
54 RKMethods.push_back(
"RK Explicit 3 Stage 3rd order by Heun");
55 RKMethods.push_back(
"RK Explicit Midpoint");
56 RKMethods.push_back(
"RK Explicit Trapezoidal");
57 RKMethods.push_back(
"Heuns Method");
58 RKMethods.push_back(
"Bogacki-Shampine 3(2) Pair");
59 RKMethods.push_back(
"Merson 4(5) Pair");
61 for (std::vector<std::string>::size_type m = 0; m != RKMethods.size(); m++) {
62 std::string RKMethod = RKMethods[m];
63 std::replace(RKMethod.begin(), RKMethod.end(),
' ',
'_');
64 std::replace(RKMethod.begin(), RKMethod.end(),
'/',
'.');
68 getParametersFromXmlFile(
"Tempus_ExplicitRK_SinCos.xml");
76 if (RKMethods[m] ==
"General ERK") {
77 tempusPL->
sublist(
"Demo Integrator")
78 .
set(
"Stepper Name",
"Demo Stepper 2");
81 tempusPL->
sublist(
"Demo Stepper").
set(
"Stepper Type", RKMethods[m]);
87 Tempus::createIntegratorBasic<double>(tempusPL, model,
false);
92 TEST_ASSERT(!integrator->getNonConstTimeStepControl()->isInitialized());
94 integrator->initialize();
96 TEST_ASSERT(integrator->getNonConstTimeStepControl()->isInitialized());
100 if (RKMethods[m] ==
"General ERK")
101 stepperPL = sublist(tempusPL,
"Demo Stepper 2",
true);
104 integrator->getStepper()->getValidParameters());
106 bool pass = haveSameValuesSorted(*stepperPL, *defaultPL,
true);
109 out <<
"stepperPL -------------- \n"
110 << *stepperPL << std::endl;
111 out <<
"defaultPL -------------- \n"
112 << *defaultPL << std::endl;
118 if (RKMethods[m] ==
"General ERK") {
119 tempusPL->
sublist(
"Demo Stepper 2")
120 .
set(
"Initial Condition Consistency",
"None");
122 if (RKMethods[m] ==
"RK Forward Euler" ||
123 RKMethods[m] ==
"Bogacki-Shampine 3(2) Pair") {
124 tempusPL->
sublist(
"Demo Stepper")
125 .
set(
"Initial Condition Consistency",
"Consistent");
126 tempusPL->
sublist(
"Demo Stepper").
set(
"Use FSAL",
true);
129 tempusPL->
sublist(
"Demo Stepper")
130 .
set(
"Initial Condition Consistency",
"None");
136 Tempus::createIntegratorBasic<double>(model, RKMethods[m]);
139 if (RKMethods[m] ==
"General ERK")
140 stepperPL = sublist(tempusPL,
"Demo Stepper 2",
true);
143 integrator->getStepper()->getValidParameters());
145 bool pass = haveSameValuesSorted(*stepperPL, *defaultPL,
true);
148 out <<
"stepperPL -------------- \n"
149 << *stepperPL << std::endl;
150 out <<
"defaultPL -------------- \n"
151 << *defaultPL << std::endl;
163 std::vector<std::string> options;
164 options.push_back(
"Default Parameters");
165 options.push_back(
"ICConsistency and Check");
167 for (
const auto& option : options) {
170 getParametersFromXmlFile(
"Tempus_ExplicitRK_SinCos.xml");
182 sf->createStepper(
"RK Explicit 4 Stage");
183 stepper->setModel(model);
184 if (option ==
"ICConsistency and Check") {
185 stepper->setICConsistency(
"Consistent");
186 stepper->setICConsistencyCheck(
true);
188 stepper->initialize();
194 timeStepControl->setInitIndex(tscPL.
get<
int>(
"Initial Time Index"));
195 timeStepControl->setInitTime(tscPL.
get<
double>(
"Initial Time"));
196 timeStepControl->setFinalTime(tscPL.
get<
double>(
"Final Time"));
197 timeStepControl->setInitTimeStep(dt);
198 timeStepControl->initialize();
201 auto inArgsIC = model->getNominalValues();
205 icState->setTime(timeStepControl->getInitTime());
206 icState->setIndex(timeStepControl->getInitIndex());
207 icState->setTimeStep(0.0);
208 icState->setOrder(stepper->getOrder());
213 solutionHistory->setName(
"Forward States");
215 solutionHistory->setStorageLimit(2);
216 solutionHistory->addState(icState);
220 Tempus::createIntegratorBasic<double>();
221 integrator->setStepper(stepper);
222 integrator->setTimeStepControl(timeStepControl);
223 integrator->setSolutionHistory(solutionHistory);
225 integrator->initialize();
228 bool integratorStatus = integrator->advanceTime();
232 double time = integrator->getTime();
233 double timeFinal = pl->
sublist(
"Demo Integrator")
235 .
get<
double>(
"Final Time");
241 model->getExactSolution(time).get_x();
245 Thyra::V_StVpStV(xdiff.
ptr(), 1.0, *x_exact, -1.0, *(x));
248 out <<
" Stepper = " << stepper->description() <<
"\n with "
249 << option << std::endl;
250 out <<
" =========================" << std::endl;
251 out <<
" Exact solution : " << get_ele(*(x_exact), 0) <<
" "
252 << get_ele(*(x_exact), 1) << std::endl;
253 out <<
" Computed solution: " << get_ele(*(x), 0) <<
" "
254 << get_ele(*(x), 1) << std::endl;
255 out <<
" Difference : " << get_ele(*(xdiff), 0) <<
" "
256 << get_ele(*(xdiff), 1) << std::endl;
257 out <<
" =========================" << std::endl;
258 TEST_FLOATING_EQUALITY(get_ele(*(x), 0), 0.841470, 1.0e-4);
259 TEST_FLOATING_EQUALITY(get_ele(*(x), 1), 0.540303, 1.0e-4);
268 std::vector<std::string> RKMethods;
269 RKMethods.push_back(
"RK Forward Euler");
270 RKMethods.push_back(
"Bogacki-Shampine 3(2) Pair");
272 for (std::vector<std::string>::size_type m = 0; m != RKMethods.size(); m++) {
278 auto stepper = sf->createStepper(RKMethods[m]);
279 stepper->setModel(model);
280 stepper->setUseFSAL(
false);
281 stepper->initialize();
285 timeStepControl->setInitTime(0.0);
286 timeStepControl->setFinalTime(1.0);
287 timeStepControl->setInitTimeStep(dt);
288 timeStepControl->initialize();
291 auto inArgsIC = model->getNominalValues();
295 icState->setTime(timeStepControl->getInitTime());
296 icState->setIndex(timeStepControl->getInitIndex());
297 icState->setTimeStep(0.0);
298 icState->setOrder(stepper->getOrder());
303 solutionHistory->setName(
"Forward States");
305 solutionHistory->setStorageLimit(2);
306 solutionHistory->addState(icState);
309 auto integrator = Tempus::createIntegratorBasic<double>();
310 integrator->setStepper(stepper);
311 integrator->setTimeStepControl(timeStepControl);
312 integrator->setSolutionHistory(solutionHistory);
313 integrator->initialize();
316 bool integratorStatus = integrator->advanceTime();
320 double time = integrator->getTime();
324 auto x = integrator->getX();
325 auto x_exact = model->getExactSolution(time).get_x();
329 Thyra::V_StVpStV(xdiff.
ptr(), 1.0, *x_exact, -1.0, *(x));
332 out <<
" Stepper = " << stepper->description() <<
"\n with "
333 <<
"useFSAL=false" << std::endl;
334 out <<
" =========================" << std::endl;
335 out <<
" Exact solution : " << get_ele(*(x_exact), 0) <<
" "
336 << get_ele(*(x_exact), 1) << std::endl;
337 out <<
" Computed solution: " << get_ele(*(x), 0) <<
" "
338 << get_ele(*(x), 1) << std::endl;
339 out <<
" Difference : " << get_ele(*(xdiff), 0) <<
" "
340 << get_ele(*(xdiff), 1) << std::endl;
341 out <<
" =========================" << std::endl;
342 if (RKMethods[m] ==
"RK Forward Euler") {
346 else if (RKMethods[m] ==
"Bogacki-Shampine 3(2) Pair") {
357 std::vector<std::string> RKMethods;
358 RKMethods.push_back(
"General ERK");
359 RKMethods.push_back(
"RK Forward Euler");
360 RKMethods.push_back(
"RK Explicit 4 Stage");
361 RKMethods.push_back(
"RK Explicit 3/8 Rule");
362 RKMethods.push_back(
"RK Explicit 4 Stage 3rd order by Runge");
363 RKMethods.push_back(
"RK Explicit 5 Stage 3rd order by Kinnmark and Gray");
364 RKMethods.push_back(
"RK Explicit 3 Stage 3rd order");
365 RKMethods.push_back(
"RK Explicit 3 Stage 3rd order TVD");
366 RKMethods.push_back(
"RK Explicit 3 Stage 3rd order by Heun");
367 RKMethods.push_back(
"RK Explicit Midpoint");
368 RKMethods.push_back(
"RK Explicit Trapezoidal");
369 RKMethods.push_back(
"Heuns Method");
370 RKMethods.push_back(
"Bogacki-Shampine 3(2) Pair");
371 RKMethods.push_back(
"Merson 4(5) Pair");
372 RKMethods.push_back(
"General ERK Embedded");
373 RKMethods.push_back(
"SSPERK22");
374 RKMethods.push_back(
"SSPERK33");
375 RKMethods.push_back(
"SSPERK54");
376 RKMethods.push_back(
"RK2");
378 std::vector<double> RKMethodErrors;
379 RKMethodErrors.push_back(8.33251e-07);
380 RKMethodErrors.push_back(0.051123);
381 RKMethodErrors.push_back(8.33251e-07);
382 RKMethodErrors.push_back(8.33251e-07);
383 RKMethodErrors.push_back(4.16897e-05);
384 RKMethodErrors.push_back(8.32108e-06);
385 RKMethodErrors.push_back(4.16603e-05);
386 RKMethodErrors.push_back(4.16603e-05);
387 RKMethodErrors.push_back(4.16603e-05);
388 RKMethodErrors.push_back(0.00166645);
389 RKMethodErrors.push_back(0.00166645);
390 RKMethodErrors.push_back(0.00166645);
391 RKMethodErrors.push_back(4.16603e-05);
392 RKMethodErrors.push_back(1.39383e-07);
393 RKMethodErrors.push_back(4.16603e-05);
394 RKMethodErrors.push_back(0.00166645);
395 RKMethodErrors.push_back(4.16603e-05);
396 RKMethodErrors.push_back(3.85613e-07);
397 RKMethodErrors.push_back(0.00166644);
399 TEST_ASSERT(RKMethods.size() == RKMethodErrors.size());
401 for (std::vector<std::string>::size_type m = 0; m != RKMethods.size(); m++) {
402 std::string RKMethod = RKMethods[m];
403 std::replace(RKMethod.begin(), RKMethod.end(),
' ',
'_');
404 std::replace(RKMethod.begin(), RKMethod.end(),
'/',
'.');
407 std::vector<RCP<Thyra::VectorBase<double>>> solutions;
408 std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
409 std::vector<double> StepSize;
410 std::vector<double> xErrorNorm;
411 std::vector<double> xDotErrorNorm;
413 const int nTimeStepSizes = 7;
416 for (
int n = 0; n < nTimeStepSizes; n++) {
419 getParametersFromXmlFile(
"Tempus_ExplicitRK_SinCos.xml");
428 if (RKMethods[m] ==
"General ERK") {
429 pl->
sublist(
"Demo Integrator").
set(
"Stepper Name",
"Demo Stepper 2");
431 else if (RKMethods[m] ==
"General ERK Embedded") {
433 .
set(
"Stepper Name",
"General ERK Embedded Stepper");
436 pl->
sublist(
"Demo Stepper").
set(
"Stepper Type", RKMethods[m]);
444 .
set(
"Initial Time Step", dt);
445 integrator = Tempus::createIntegratorBasic<double>(pl, model);
453 model->getNominalValues().get_x()->clone_v();
454 integrator->initializeSolutionHistory(0.0, x0);
457 bool integratorStatus = integrator->advanceTime();
461 time = integrator->getTime();
462 double timeFinal = pl->sublist(
"Demo Integrator")
463 .sublist(
"Time Step Control")
464 .get<
double>(
"Final Time");
470 model->getExactSolution(time).get_x();
475 integrator->getSolutionHistory();
476 writeSolution(
"Tempus_" + RKMethod +
"_SinCos.dat", solutionHistory);
479 for (
int i = 0; i < solutionHistory->getNumStates(); i++) {
480 double time_i = (*solutionHistory)[i]->getTime();
483 model->getExactSolution(time_i).get_x()),
485 model->getExactSolution(time_i).get_x_dot()));
486 state->setTime((*solutionHistory)[i]->getTime());
487 solnHistExact->addState(state);
489 writeSolution(
"Tempus_" + RKMethod +
"_SinCos-Ref.dat", solnHistExact);
493 StepSize.push_back(dt);
494 auto solution = Thyra::createMember(model->get_x_space());
495 Thyra::copy(*(integrator->getX()), solution.ptr());
496 solutions.push_back(solution);
497 auto solutionDot = Thyra::createMember(model->get_x_space());
498 Thyra::copy(*(integrator->getXDot()), solutionDot.ptr());
499 solutionsDot.push_back(solutionDot);
500 if (n == nTimeStepSizes - 1) {
501 StepSize.push_back(0.0);
502 auto solutionExact = Thyra::createMember(model->get_x_space());
503 Thyra::copy(*(model->getExactSolution(time).get_x()),
504 solutionExact.ptr());
505 solutions.push_back(solutionExact);
506 auto solutionDotExact = Thyra::createMember(model->get_x_space());
507 Thyra::copy(*(model->getExactSolution(time).get_x_dot()),
508 solutionDotExact.ptr());
509 solutionsDot.push_back(solutionDotExact);
515 double xDotSlope = 0.0;
517 double order = stepper->getOrder();
519 StepSize, solutions, xErrorNorm, xSlope, solutionsDot,
520 xDotErrorNorm, xDotSlope, out);
522 double order_tol = 0.01;
523 if (RKMethods[m] ==
"Merson 4(5) Pair") order_tol = 0.04;
524 if (RKMethods[m] ==
"SSPERK54") order_tol = 0.06;
539 std::vector<std::string> IntegratorList;
540 IntegratorList.push_back(
"Embedded_Integrator_PID");
541 IntegratorList.push_back(
"Demo_Integrator");
542 IntegratorList.push_back(
"Embedded_Integrator");
543 IntegratorList.push_back(
"General_Embedded_Integrator");
544 IntegratorList.push_back(
"Embedded_Integrator_PID_General");
548 const int refIstep = 36;
550 for (
auto integratorChoice : IntegratorList) {
551 out <<
"Using Integrator: " << integratorChoice <<
" !!!" << std::endl;
555 getParametersFromXmlFile(
"Tempus_ExplicitRK_VanDerPol.xml");
563 pl->
set(
"Integrator Name", integratorChoice);
567 Tempus::createIntegratorBasic<double>(pl, model);
569 const std::string RKMethod =
570 pl->sublist(integratorChoice).
get<std::string>(
"Stepper Name");
573 bool integratorStatus = integrator->advanceTime();
577 double time = integrator->getTime();
578 double timeFinal = pl->sublist(integratorChoice)
579 .sublist(
"Time Step Control")
580 .
get<
double>(
"Final Time");
586 Thyra::set_ele(0, -1.5484458614405929, xref.
ptr());
587 Thyra::set_ele(1, 1.0181127316101317, xref.
ptr());
591 Thyra::V_StVpStV(xdiff.ptr(), 1.0, *xref, -1.0, *(x));
592 const double L2norm = Thyra::norm_2(*xdiff);
595 if ((integratorChoice ==
"Embedded_Integrator_PID") ||
596 (integratorChoice ==
"Embedded_Integrator_PID_General")) {
597 const double absTol = pl->sublist(integratorChoice)
598 .sublist(
"Time Step Control")
599 .get<
double>(
"Maximum Absolute Error");
600 const double relTol = pl->sublist(integratorChoice)
601 .sublist(
"Time Step Control")
602 .get<
double>(
"Maximum Relative Error");
605 TEST_COMPARE(std::log10(L2norm), <, std::log10(absTol));
606 TEST_COMPARE(std::log10(L2norm), <, std::log10(relTol));
612 integrator->getSolutionHistory()->getCurrentState()->getIndex();
613 const int nFail = integrator->getSolutionHistory()
616 ->getNRunningFailures();
620 out <<
"Tolerance = " << absTol <<
" L2norm = " << L2norm
621 <<
" iStep = " << iStep <<
" nFail = " << nFail << std::endl;
625 std::ofstream ftmp(
"Tempus_" + integratorChoice + RKMethod +
628 integrator->getSolutionHistory();
629 int nStates = solutionHistory->getNumStates();
631 for (
int i = 0; i < nStates; i++) {
633 double time_i = solutionState->getTime();
636 ftmp << time_i <<
" " << Thyra::get_ele(*(x_plot), 0) <<
" "
637 << Thyra::get_ele(*(x_plot), 1) <<
" " << std::endl;
653 getParametersFromXmlFile(
"Tempus_ExplicitRK_SinCos.xml");
665 sf->createStepper(
"RK Explicit 4 Stage");
666 stepper->setModel(model);
667 stepper->initialize();
673 timeStepControl->setInitIndex(tscPL.
get<
int>(
"Initial Time Index"));
674 timeStepControl->setInitTime(tscPL.
get<
double>(
"Initial Time"));
675 timeStepControl->setFinalTime(tscPL.
get<
double>(
"Final Time"));
676 timeStepControl->setInitTimeStep(dt);
677 timeStepControl->initialize();
680 auto inArgsIC = model->getNominalValues();
683 icState->setTime(timeStepControl->getInitTime());
684 icState->setIndex(timeStepControl->getInitIndex());
685 icState->setTimeStep(0.0);
686 icState->setOrder(stepper->getOrder());
691 solutionHistory->setName(
"Forward States");
693 solutionHistory->setStorageLimit(2);
694 solutionHistory->addState(icState);
698 Tempus::createIntegratorBasic<double>();
699 integrator->setStepper(stepper);
700 integrator->setTimeStepControl(timeStepControl);
701 integrator->setSolutionHistory(solutionHistory);
703 integrator->initialize();
711 const std::vector<int> ref_stageNumber = {1, 4, 8, 10, 11, -1, 5};
712 for (
auto stage_number : ref_stageNumber) {
714 erk_stepper->setStageNumber(stage_number);
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.
Explicit Runge-Kutta time stepper.
T & get(const std::string &name, T def_value)
#define TEST_COMPARE(v1, comp, v2)
void writeOrderError(const std::string filename, Teuchos::RCP< Tempus::Stepper< Scalar >> stepper, std::vector< Scalar > &StepSize, std::vector< Teuchos::RCP< Thyra::VectorBase< Scalar >>> &solutions, std::vector< Scalar > &xErrorNorm, Scalar &xSlope, std::vector< Teuchos::RCP< Thyra::VectorBase< Scalar >>> &solutionsDot, std::vector< Scalar > &xDotErrorNorm, Scalar &xDotSlope, std::vector< Teuchos::RCP< Thyra::VectorBase< Scalar >>> &solutionsDotDot, std::vector< Scalar > &xDotDotErrorNorm, Scalar &xDotDotSlope, Teuchos::FancyOStream &out)
#define TEST_FLOATING_EQUALITY(v1, v2, tol)
Sine-Cosine model problem from Rythmos. This is a canonical Sine-Cosine differential equation with a...
void writeSolution(const std::string filename, Teuchos::RCP< const Tempus::SolutionHistory< Scalar >> solutionHistory)
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
TEUCHOS_UNIT_TEST(BackwardEuler, SinCos_ASA)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
static void summarize(Ptr< const Comm< int > > comm, std::ostream &out=std::cout, const bool alwaysWriteLocal=false, const bool writeGlobalStats=true, const bool writeZeroTimers=true, const ECounterSetOp setOp=Intersection, const std::string &filter="", const bool ignoreZeroTimers=false)
TimeStepControl manages the time step size. There several mechanisms that effect the time step size a...
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
Keep a fix number of states.
van der Pol model problem for nonlinear electrical circuit.
ParameterList & sublist(const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
#define TEST_EQUALITY(v1, v2)
Solution state for integrators and steppers.