12 #include "Teuchos_DefaultComm.hpp"
14 #include "Thyra_VectorStdOps.hpp"
16 #include "Tempus_IntegratorBasic.hpp"
17 #include "Tempus_StepperFactory.hpp"
18 #include "Tempus_StepperExplicitRK.hpp"
20 #include "../TestModels/SinCosModel.hpp"
21 #include "../TestModels/VanDerPolModel.hpp"
22 #include "../TestUtils/Tempus_ConvergenceTestUtils.hpp"
27 namespace Tempus_Test {
31 using Teuchos::rcp_const_cast;
33 using Teuchos::sublist;
34 using Teuchos::getParametersFromXmlFile;
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++) {
63 std::string RKMethod = RKMethods[m];
64 std::replace(RKMethod.begin(), RKMethod.end(),
' ',
'_');
65 std::replace(RKMethod.begin(), RKMethod.end(),
'/',
'.');
69 getParametersFromXmlFile(
"Tempus_ExplicitRK_SinCos.xml");
77 if (RKMethods[m] ==
"General ERK") {
78 tempusPL->
sublist(
"Demo Integrator").
set(
"Stepper Name",
"Demo Stepper 2");
80 tempusPL->
sublist(
"Demo Stepper").
set(
"Stepper Type", RKMethods[m]);
86 Tempus::createIntegratorBasic<double>(tempusPL, model);
89 if (RKMethods[m] ==
"General ERK")
90 stepperPL = sublist(tempusPL,
"Demo Stepper 2",
true);
93 integrator->getStepper()->getValidParameters());
95 bool pass = haveSameValuesSorted(*stepperPL, *defaultPL,
true);
97 std::cout << std::endl;
98 std::cout <<
"stepperPL -------------- \n" << *stepperPL << std::endl;
99 std::cout <<
"defaultPL -------------- \n" << *defaultPL << std::endl;
105 if (RKMethods[m] ==
"General ERK") {
106 tempusPL->
sublist(
"Demo Stepper 2")
107 .
set(
"Initial Condition Consistency",
"None");
109 if (RKMethods[m] ==
"RK Forward Euler" ||
110 RKMethods[m] ==
"Bogacki-Shampine 3(2) Pair") {
111 tempusPL->
sublist(
"Demo Stepper")
112 .
set(
"Initial Condition Consistency",
"Consistent");
113 tempusPL->
sublist(
"Demo Stepper")
114 .
set(
"Use FSAL",
true);
116 tempusPL->
sublist(
"Demo Stepper")
117 .
set(
"Initial Condition Consistency",
"None");
123 Tempus::createIntegratorBasic<double>(model, RKMethods[m]);
126 if (RKMethods[m] ==
"General ERK")
127 stepperPL = sublist(tempusPL,
"Demo Stepper 2",
true);
130 integrator->getStepper()->getValidParameters());
132 bool pass = haveSameValuesSorted(*stepperPL, *defaultPL,
true);
134 std::cout << std::endl;
135 std::cout <<
"stepperPL -------------- \n" << *stepperPL << std::endl;
136 std::cout <<
"defaultPL -------------- \n" << *defaultPL << std::endl;
149 std::vector<std::string> options;
150 options.push_back(
"Default Parameters");
151 options.push_back(
"ICConsistency and Check");
153 for(
const auto& option: options) {
157 getParametersFromXmlFile(
"Tempus_ExplicitRK_SinCos.xml");
169 sf->createStepper(
"RK Explicit 4 Stage");
170 stepper->setModel(model);
171 if ( option ==
"ICConsistency and Check") {
172 stepper->setICConsistency(
"Consistent");
173 stepper->setICConsistencyCheck(
true);
175 stepper->initialize();
181 timeStepControl->setInitIndex(tscPL.
get<
int> (
"Initial Time Index"));
182 timeStepControl->setInitTime (tscPL.
get<
double>(
"Initial Time"));
183 timeStepControl->setFinalTime(tscPL.
get<
double>(
"Final Time"));
184 timeStepControl->setInitTimeStep(dt);
185 timeStepControl->initialize();
188 auto inArgsIC = model->getNominalValues();
191 icState->setTime (timeStepControl->getInitTime());
192 icState->setIndex (timeStepControl->getInitIndex());
193 icState->setTimeStep(0.0);
194 icState->setOrder (stepper->getOrder());
199 solutionHistory->setName(
"Forward States");
201 solutionHistory->setStorageLimit(2);
202 solutionHistory->addState(icState);
206 Tempus::createIntegratorBasic<double>();
207 integrator->setStepper(stepper);
208 integrator->setTimeStepControl(timeStepControl);
209 integrator->setSolutionHistory(solutionHistory);
211 integrator->initialize();
215 bool integratorStatus = integrator->advanceTime();
220 double time = integrator->getTime();
221 double timeFinal =pl->
sublist(
"Demo Integrator")
222 .
sublist(
"Time Step Control").
get<
double>(
"Final Time");
228 model->getExactSolution(time).get_x();
232 Thyra::V_StVpStV(xdiff.
ptr(), 1.0, *x_exact, -1.0, *(x));
235 std::cout <<
" Stepper = " << stepper->description()
236 <<
"\n with " << option << std::endl;
237 std::cout <<
" =========================" << std::endl;
238 std::cout <<
" Exact solution : " << get_ele(*(x_exact), 0) <<
" "
239 << get_ele(*(x_exact), 1) << std::endl;
240 std::cout <<
" Computed solution: " << get_ele(*(x ), 0) <<
" "
241 << get_ele(*(x ), 1) << std::endl;
242 std::cout <<
" Difference : " << get_ele(*(xdiff ), 0) <<
" "
243 << get_ele(*(xdiff ), 1) << std::endl;
244 std::cout <<
" =========================" << std::endl;
245 TEST_FLOATING_EQUALITY(get_ele(*(x), 0), 0.841470, 1.0e-4 );
246 TEST_FLOATING_EQUALITY(get_ele(*(x), 1), 0.540303, 1.0e-4 );
256 std::vector<std::string> RKMethods;
257 RKMethods.push_back(
"RK Forward Euler");
258 RKMethods.push_back(
"Bogacki-Shampine 3(2) Pair");
260 for(std::vector<std::string>::size_type m = 0; m != RKMethods.size(); m++) {
267 auto stepper = sf->createStepper(RKMethods[m]);
268 stepper->setModel(model);
269 stepper->setUseFSAL(
false);
270 stepper->initialize();
274 timeStepControl->setInitTime (0.0);
275 timeStepControl->setFinalTime(1.0);
276 timeStepControl->setInitTimeStep(dt);
277 timeStepControl->initialize();
280 auto inArgsIC = model->getNominalValues();
283 icState->setTime (timeStepControl->getInitTime());
284 icState->setIndex (timeStepControl->getInitIndex());
285 icState->setTimeStep(0.0);
286 icState->setOrder (stepper->getOrder());
291 solutionHistory->setName(
"Forward States");
293 solutionHistory->setStorageLimit(2);
294 solutionHistory->addState(icState);
297 auto integrator = Tempus::createIntegratorBasic<double>();
298 integrator->setStepper(stepper);
299 integrator->setTimeStepControl(timeStepControl);
300 integrator->setSolutionHistory(solutionHistory);
301 integrator->initialize();
305 bool integratorStatus = integrator->advanceTime();
310 double time = integrator->getTime();
314 auto x = integrator->getX();
315 auto x_exact = model->getExactSolution(time).get_x();
319 Thyra::V_StVpStV(xdiff.
ptr(), 1.0, *x_exact, -1.0, *(x));
322 std::cout <<
" Stepper = " << stepper->description()
323 <<
"\n with " <<
"useFSAL=false" << std::endl;
324 std::cout <<
" =========================" << std::endl;
325 std::cout <<
" Exact solution : " << get_ele(*(x_exact), 0) <<
" "
326 << get_ele(*(x_exact), 1) << std::endl;
327 std::cout <<
" Computed solution: " << get_ele(*(x ), 0) <<
" "
328 << get_ele(*(x ), 1) << std::endl;
329 std::cout <<
" Difference : " << get_ele(*(xdiff ), 0) <<
" "
330 << get_ele(*(xdiff ), 1) << std::endl;
331 std::cout <<
" =========================" << std::endl;
332 if (RKMethods[m] ==
"RK Forward Euler") {
335 }
else if (RKMethods[m] ==
"Bogacki-Shampine 3(2) Pair") {
347 std::vector<std::string> RKMethods;
348 RKMethods.push_back(
"General ERK");
349 RKMethods.push_back(
"RK Forward Euler");
350 RKMethods.push_back(
"RK Explicit 4 Stage");
351 RKMethods.push_back(
"RK Explicit 3/8 Rule");
352 RKMethods.push_back(
"RK Explicit 4 Stage 3rd order by Runge");
353 RKMethods.push_back(
"RK Explicit 5 Stage 3rd order by Kinnmark and Gray");
354 RKMethods.push_back(
"RK Explicit 3 Stage 3rd order");
355 RKMethods.push_back(
"RK Explicit 3 Stage 3rd order TVD");
356 RKMethods.push_back(
"RK Explicit 3 Stage 3rd order by Heun");
357 RKMethods.push_back(
"RK Explicit Midpoint");
358 RKMethods.push_back(
"RK Explicit Trapezoidal");
359 RKMethods.push_back(
"Heuns Method");
360 RKMethods.push_back(
"Bogacki-Shampine 3(2) Pair");
361 RKMethods.push_back(
"Merson 4(5) Pair");
362 RKMethods.push_back(
"General ERK Embedded");
363 RKMethods.push_back(
"SSPERK22");
364 RKMethods.push_back(
"SSPERK33");
365 RKMethods.push_back(
"SSPERK54");
366 RKMethods.push_back(
"RK2");
368 std::vector<double> RKMethodErrors;
369 RKMethodErrors.push_back(8.33251e-07);
370 RKMethodErrors.push_back(0.051123);
371 RKMethodErrors.push_back(8.33251e-07);
372 RKMethodErrors.push_back(8.33251e-07);
373 RKMethodErrors.push_back(4.16897e-05);
374 RKMethodErrors.push_back(8.32108e-06);
375 RKMethodErrors.push_back(4.16603e-05);
376 RKMethodErrors.push_back(4.16603e-05);
377 RKMethodErrors.push_back(4.16603e-05);
378 RKMethodErrors.push_back(0.00166645);
379 RKMethodErrors.push_back(0.00166645);
380 RKMethodErrors.push_back(0.00166645);
381 RKMethodErrors.push_back(4.16603e-05);
382 RKMethodErrors.push_back(1.39383e-07);
383 RKMethodErrors.push_back(4.16603e-05);
384 RKMethodErrors.push_back(0.00166645);
385 RKMethodErrors.push_back(4.16603e-05);
386 RKMethodErrors.push_back(3.85613e-07);
387 RKMethodErrors.push_back(0.00166644);
390 TEST_ASSERT(RKMethods.size() == RKMethodErrors.size() );
392 for(std::vector<std::string>::size_type m = 0; m != RKMethods.size(); m++) {
394 std::string RKMethod = RKMethods[m];
395 std::replace(RKMethod.begin(), RKMethod.end(),
' ',
'_');
396 std::replace(RKMethod.begin(), RKMethod.end(),
'/',
'.');
399 std::vector<RCP<Thyra::VectorBase<double>>> solutions;
400 std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
401 std::vector<double> StepSize;
402 std::vector<double> xErrorNorm;
403 std::vector<double> xDotErrorNorm;
405 const int nTimeStepSizes = 7;
408 for (
int n=0; n<nTimeStepSizes; n++) {
412 getParametersFromXmlFile(
"Tempus_ExplicitRK_SinCos.xml");
421 if (RKMethods[m] ==
"General ERK") {
422 pl->
sublist(
"Demo Integrator").
set(
"Stepper Name",
"Demo Stepper 2");
423 }
else if (RKMethods[m] ==
"General ERK Embedded"){
424 pl->
sublist(
"Demo Integrator").
set(
"Stepper Name",
"General ERK Embedded Stepper");
426 pl->
sublist(
"Demo Stepper").
set(
"Stepper Type", RKMethods[m]);
434 .
sublist(
"Time Step Control").
set(
"Initial Time Step", dt);
435 integrator = Tempus::createIntegratorBasic<double>(pl, model);
442 model->getNominalValues().get_x()->clone_v();
443 integrator->initializeSolutionHistory(0.0, x0);
446 bool integratorStatus = integrator->advanceTime();
450 time = integrator->getTime();
451 double timeFinal = pl->sublist(
"Demo Integrator")
452 .sublist(
"Time Step Control").get<
double>(
"Final Time");
458 model->getExactSolution(time).get_x();
463 integrator->getSolutionHistory();
464 writeSolution(
"Tempus_"+RKMethod+
"_SinCos.dat", solutionHistory);
467 for (
int i=0; i<solutionHistory->getNumStates(); i++) {
468 double time_i = (*solutionHistory)[i]->getTime();
471 model->getExactSolution(time_i).get_x()),
473 model->getExactSolution(time_i).get_x_dot()));
474 state->setTime((*solutionHistory)[i]->getTime());
475 solnHistExact->addState(state);
477 writeSolution(
"Tempus_"+RKMethod+
"_SinCos-Ref.dat", solnHistExact);
481 StepSize.push_back(dt);
482 auto solution = Thyra::createMember(model->get_x_space());
483 Thyra::copy(*(integrator->getX()),solution.ptr());
484 solutions.push_back(solution);
485 auto solutionDot = Thyra::createMember(model->get_x_space());
486 Thyra::copy(*(integrator->getXDot()),solutionDot.ptr());
487 solutionsDot.push_back(solutionDot);
488 if (n == nTimeStepSizes-1) {
489 StepSize.push_back(0.0);
490 auto solutionExact = Thyra::createMember(model->get_x_space());
491 Thyra::copy(*(model->getExactSolution(time).get_x()),solutionExact.ptr());
492 solutions.push_back(solutionExact);
493 auto solutionDotExact = Thyra::createMember(model->get_x_space());
494 Thyra::copy(*(model->getExactSolution(time).get_x_dot()),
495 solutionDotExact.ptr());
496 solutionsDot.push_back(solutionDotExact);
502 double xDotSlope = 0.0;
504 double order = stepper->getOrder();
507 solutions, xErrorNorm, xSlope,
508 solutionsDot, xDotErrorNorm, xDotSlope);
510 double order_tol = 0.01;
511 if (RKMethods[m] ==
"Merson 4(5) Pair") order_tol = 0.04;
512 if (RKMethods[m] ==
"SSPERK54") order_tol = 0.06;
530 std::vector<std::string> IntegratorList;
531 IntegratorList.push_back(
"Embedded_Integrator_PID");
532 IntegratorList.push_back(
"Demo_Integrator");
533 IntegratorList.push_back(
"Embedded_Integrator");
534 IntegratorList.push_back(
"General_Embedded_Integrator");
535 IntegratorList.push_back(
"Embedded_Integrator_PID_General");
539 const int refIstep = 36;
541 for(
auto integratorChoice : IntegratorList){
543 std::cout <<
"Using Integrator: " << integratorChoice <<
" !!!" << std::endl;
547 getParametersFromXmlFile(
"Tempus_ExplicitRK_VanDerPol.xml");
557 pl->
set(
"Integrator Name", integratorChoice);
561 Tempus::createIntegratorBasic<double>(pl, model);
563 const std::string RKMethod =
564 pl->sublist(integratorChoice).
get<std::string>(
"Stepper Name");
567 bool integratorStatus = integrator->advanceTime();
571 double time = integrator->getTime();
572 double timeFinal = pl->sublist(integratorChoice)
573 .sublist(
"Time Step Control").
get<
double>(
"Final Time");
580 Thyra::set_ele(0, -1.5484458614405929, xref.
ptr());
581 Thyra::set_ele(1, 1.0181127316101317, xref.
ptr());
585 Thyra::V_StVpStV(xdiff.ptr(), 1.0, *xref, -1.0, *(x));
586 const double L2norm = Thyra::norm_2(*xdiff);
589 if ((integratorChoice ==
"Embedded_Integrator_PID") ||
590 (integratorChoice ==
"Embedded_Integrator_PID_General")) {
592 const double absTol = pl->sublist(integratorChoice).
593 sublist(
"Time Step Control").get<
double>(
"Maximum Absolute Error");
594 const double relTol = pl->sublist(integratorChoice).
595 sublist(
"Time Step Control").get<
double>(
"Maximum Relative Error");
598 TEST_COMPARE(std::log10(L2norm), <, std::log10(absTol));
599 TEST_COMPARE(std::log10(L2norm), <, std::log10(relTol));
604 const int iStep = integrator->getSolutionHistory()->
605 getCurrentState()->getIndex();
606 const int nFail = integrator->getSolutionHistory()->
607 getCurrentState()->getMetaData()->getNRunningFailures();
611 std::cout <<
"Tolerance = " << absTol
612 <<
" L2norm = " << L2norm
613 <<
" iStep = " << iStep
614 <<
" nFail = " << nFail << std::endl;
618 std::ofstream ftmp(
"Tempus_"+integratorChoice+RKMethod+
"_VDP_Example.dat");
620 integrator->getSolutionHistory();
621 int nStates = solutionHistory->getNumStates();
623 for (
int i=0; i<nStates; i++) {
625 double time_i = solutionState->getTime();
628 ftmp << time_i <<
" "
629 << Thyra::get_ele(*(x_plot), 0) <<
" "
630 << Thyra::get_ele(*(x_plot), 1) <<
" " << std::endl;
647 getParametersFromXmlFile(
"Tempus_ExplicitRK_SinCos.xml");
659 sf->createStepper(
"RK Explicit 4 Stage");
660 stepper->setModel(model);
661 stepper->initialize();
667 timeStepControl->setInitIndex(tscPL.
get<
int> (
"Initial Time Index"));
668 timeStepControl->setInitTime (tscPL.
get<
double>(
"Initial Time"));
669 timeStepControl->setFinalTime(tscPL.
get<
double>(
"Final Time"));
670 timeStepControl->setInitTimeStep(dt);
671 timeStepControl->initialize();
674 auto inArgsIC = model->getNominalValues();
677 icState->setTime (timeStepControl->getInitTime());
678 icState->setIndex (timeStepControl->getInitIndex());
679 icState->setTimeStep(0.0);
680 icState->setOrder (stepper->getOrder());
685 solutionHistory->setName(
"Forward States");
687 solutionHistory->setStorageLimit(2);
688 solutionHistory->addState(icState);
692 Tempus::createIntegratorBasic<double>();
693 integrator->setStepper(stepper);
694 integrator->setTimeStepControl(timeStepControl);
695 integrator->setSolutionHistory(solutionHistory);
697 integrator->initialize();
703 const std::vector<int> ref_stageNumber = { 1, 4, 8, 10, 11, -1, 5};
704 for(
auto stage_number : ref_stageNumber) {
706 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)
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
#define TEST_COMPARE(v1, comp, v2)
#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)
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_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. SolutionState contains the metadata for solutions and th...