12 #include "Teuchos_DefaultComm.hpp"
14 #include "Tempus_config.hpp"
15 #include "Tempus_IntegratorBasic.hpp"
16 #include "Tempus_StepperBDF2.hpp"
18 #include "../TestModels/SinCosModel.hpp"
19 #include "../TestModels/CDR_Model.hpp"
20 #include "../TestModels/VanDerPolModel.hpp"
21 #include "../TestUtils/Tempus_ConvergenceTestUtils.hpp"
23 #include "Stratimikos_DefaultLinearSolverBuilder.hpp"
24 #include "Thyra_LinearOpWithSolveFactoryHelpers.hpp"
26 #ifdef Tempus_ENABLE_MPI
27 #include "Epetra_MpiComm.h"
29 #include "Epetra_SerialComm.h"
37 namespace Tempus_Test {
41 using Teuchos::rcp_const_cast;
43 using Teuchos::sublist;
44 using Teuchos::getParametersFromXmlFile;
57 getParametersFromXmlFile(
"Tempus_BDF2_SinCos.xml");
68 Tempus::createIntegratorBasic<double>(tempusPL, model);
72 integrator->getStepper()->getValidParameters();
73 bool pass = haveSameValuesSorted(*stepperPL, *defaultPL,
true);
75 std::cout << std::endl;
76 std::cout <<
"stepperPL -------------- \n" << *stepperPL << std::endl;
77 std::cout <<
"defaultPL -------------- \n" << *defaultPL << std::endl;
85 Tempus::createIntegratorBasic<double>(model,
"BDF2");
89 integrator->getStepper()->getValidParameters();
91 bool pass = haveSameValuesSorted(*stepperPL, *defaultPL,
true);
93 std::cout << std::endl;
94 std::cout <<
"stepperPL -------------- \n" << *stepperPL << std::endl;
95 std::cout <<
"defaultPL -------------- \n" << *defaultPL << std::endl;
107 std::vector<std::string> options;
108 options.push_back(
"Default Parameters");
109 options.push_back(
"ICConsistency and Check");
111 for(
const auto& option: options) {
115 getParametersFromXmlFile(
"Tempus_BDF2_SinCos.xml");
125 stepper->setModel(model);
126 if ( option ==
"ICConsistency and Check") {
127 stepper->setICConsistency(
"Consistent");
128 stepper->setICConsistencyCheck(
true);
130 stepper->initialize();
136 timeStepControl->setInitIndex(tscPL.
get<
int> (
"Initial Time Index"));
137 timeStepControl->setInitTime (tscPL.
get<
double>(
"Initial Time"));
138 timeStepControl->setFinalTime(tscPL.
get<
double>(
"Final Time"));
139 timeStepControl->setInitTimeStep(dt);
140 timeStepControl->initialize();
143 auto inArgsIC = model->getNominalValues();
146 icState->setTime (timeStepControl->getInitTime());
147 icState->setIndex (timeStepControl->getInitIndex());
148 icState->setTimeStep(0.0);
149 icState->setOrder (stepper->getOrder());
154 solutionHistory->setName(
"Forward States");
156 solutionHistory->setStorageLimit(3);
157 solutionHistory->addState(icState);
160 stepper->setInitialConditions(solutionHistory);
164 Tempus::createIntegratorBasic<double>();
165 integrator->setStepper(stepper);
166 integrator->setTimeStepControl(timeStepControl);
167 integrator->setSolutionHistory(solutionHistory);
169 integrator->initialize();
173 bool integratorStatus = integrator->advanceTime();
178 double time = integrator->getTime();
179 double timeFinal =pl->
sublist(
"Default Integrator")
180 .
sublist(
"Time Step Control").
get<
double>(
"Final Time");
186 model->getExactSolution(time).get_x();
190 Thyra::V_StVpStV(xdiff.
ptr(), 1.0, *x_exact, -1.0, *(x));
193 std::cout <<
" Stepper = " << stepper->description()
194 <<
"\n with " << option << std::endl;
195 std::cout <<
" =========================" << std::endl;
196 std::cout <<
" Exact solution : " << get_ele(*(x_exact), 0) <<
" "
197 << get_ele(*(x_exact), 1) << std::endl;
198 std::cout <<
" Computed solution: " << get_ele(*(x ), 0) <<
" "
199 << get_ele(*(x ), 1) << std::endl;
200 std::cout <<
" Difference : " << get_ele(*(xdiff ), 0) <<
" "
201 << get_ele(*(xdiff ), 1) << std::endl;
202 std::cout <<
" =========================" << std::endl;
203 TEST_FLOATING_EQUALITY(get_ele(*(x), 0), 0.839732, 1.0e-4 );
204 TEST_FLOATING_EQUALITY(get_ele(*(x), 1), 0.542663, 1.0e-4 );
214 std::vector<RCP<Thyra::VectorBase<double>>> solutions;
215 std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
216 std::vector<double> StepSize;
221 double dt = pList->
sublist(
"Tempus")
223 .
sublist(
"Time Step Control").
get<
double>(
"Initial Time Step");
228 const int nTimeStepSizes = scm_pl->
get<
int>(
"Number of Time Step Sizes", 7);
229 std::string output_file_string =
230 scm_pl->
get<std::string>(
"Output File Name",
"Tempus_BDF2_SinCos");
231 std::string output_file_name = output_file_string +
".dat";
232 std::string ref_out_file_name = output_file_string +
"-Ref.dat";
233 std::string err_out_file_name = output_file_string +
"-Error.dat";
235 for (
int n=0; n<nTimeStepSizes; n++) {
243 getParametersFromXmlFile(
"Tempus_BDF2_SinCos.xml");
245 pl->
sublist(
"Default Integrator")
246 .
sublist(
"Time Step Control").
set(
"Initial Time Step", dt);
247 integrator = Tempus::createIntegratorBasic<double>(pl, model);
254 model->getNominalValues().get_x()->clone_v();
255 integrator->initializeSolutionHistory(0.0, x0);
258 bool integratorStatus = integrator->advanceTime();
262 time = integrator->getTime();
263 double timeFinal = pl->sublist(
"Default Integrator")
264 .sublist(
"Time Step Control").
get<
double>(
"Final Time");
270 integrator->getSolutionHistory();
274 for (
int i=0; i<solutionHistory->getNumStates(); i++) {
275 double time_i = (*solutionHistory)[i]->getTime();
278 model->getExactSolution(time_i).get_x()),
280 model->getExactSolution(time_i).get_x_dot()));
281 state->setTime((*solutionHistory)[i]->getTime());
282 solnHistExact->addState(state);
288 StepSize.push_back(dt);
289 auto solution = Thyra::createMember(model->get_x_space());
290 Thyra::copy(*(integrator->getX()),solution.ptr());
291 solutions.push_back(solution);
292 auto solutionDot = Thyra::createMember(model->get_x_space());
293 Thyra::copy(*(integrator->getXDot()),solutionDot.ptr());
294 solutionsDot.push_back(solutionDot);
295 if (n == nTimeStepSizes-1) {
296 StepSize.push_back(0.0);
297 auto solutionExact = Thyra::createMember(model->get_x_space());
298 Thyra::copy(*(model->getExactSolution(time).get_x()),solutionExact.ptr());
299 solutions.push_back(solutionExact);
300 auto solutionDotExact = Thyra::createMember(model->get_x_space());
301 Thyra::copy(*(model->getExactSolution(time).get_x_dot()),
302 solutionDotExact.ptr());
303 solutionsDot.push_back(solutionDotExact);
308 if (nTimeStepSizes > 1) {
310 double xDotSlope = 0.0;
311 std::vector<double> xErrorNorm;
312 std::vector<double> xDotErrorNorm;
314 double order = stepper->getOrder();
317 solutions, xErrorNorm, xSlope,
318 solutionsDot, xDotErrorNorm, xDotSlope);
335 std::vector<RCP<Thyra::VectorBase<double>>> solutions;
336 std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
337 std::vector<double> StepSize;
341 getParametersFromXmlFile(
"Tempus_BDF2_SinCos_AdaptDt.xml");
344 double dt = pList->
sublist(
"Tempus")
346 .
sublist(
"Time Step Control").
get<
double>(
"Initial Time Step");
351 const int nTimeStepSizes = scm_pl->
get<
int>(
"Number of Time Step Sizes", 7);
352 std::string output_file_string =
353 scm_pl->
get<std::string>(
"Output File Name",
"Tempus_BDF2_SinCos");
354 std::string output_file_name = output_file_string +
".dat";
355 std::string err_out_file_name = output_file_string +
"-Error.dat";
357 for (
int n=0; n<nTimeStepSizes; n++) {
364 getParametersFromXmlFile(
"Tempus_BDF2_SinCos_AdaptDt.xml");
368 pl->
sublist(
"Default Integrator")
369 .
sublist(
"Time Step Control").
set(
"Initial Time Step", dt/4.0);
372 pl->
sublist(
"Default Integrator")
373 .
sublist(
"Time Step Control").
set(
"Maximum Time Step", dt);
375 pl->
sublist(
"Default Integrator")
376 .
sublist(
"Time Step Control").
set(
"Minimum Time Step", dt/4.0);
378 pl->
sublist(
"Default Integrator")
380 .
sublist(
"Time Step Control Strategy")
381 .
set(
"Minimum Value Monitoring Function", dt*0.99);
382 integrator = Tempus::createIntegratorBasic<double>(pl, model);
389 model->getNominalValues().get_x()->clone_v();
390 integrator->initializeSolutionHistory(0.0, x0);
393 bool integratorStatus = integrator->advanceTime();
397 time = integrator->getTime();
398 double timeFinal =pl->sublist(
"Default Integrator")
399 .sublist(
"Time Step Control").
get<
double>(
"Final Time");
405 model->getExactSolution(time).get_x();
409 std::ofstream ftmp(output_file_name);
411 FILE *gold_file = fopen(
"Tempus_BDF2_SinCos_AdaptDt_gold.dat",
"r");
413 integrator->getSolutionHistory();
415 for (
int i=0; i<solutionHistory->getNumStates(); i++) {
416 char time_gold_char[100];
417 fgets(time_gold_char, 100, gold_file);
419 sscanf(time_gold_char,
"%lf", &time_gold);
421 double time_i = solutionState->getTime();
423 TEST_FLOATING_EQUALITY( time_i, time_gold, 1.0e-5 );
425 x_exact_plot = model->getExactSolution(time_i).get_x();
426 ftmp << time_i <<
" "
427 << get_ele(*(x_plot), 0) <<
" "
428 << get_ele(*(x_plot), 1) <<
" "
429 << get_ele(*(x_exact_plot), 0) <<
" "
430 << get_ele(*(x_exact_plot), 1) << std::endl;
436 StepSize.push_back(dt);
437 auto solution = Thyra::createMember(model->get_x_space());
438 Thyra::copy(*(integrator->getX()),solution.ptr());
439 solutions.push_back(solution);
440 auto solutionDot = Thyra::createMember(model->get_x_space());
441 Thyra::copy(*(integrator->getXDot()),solutionDot.ptr());
442 solutionsDot.push_back(solutionDot);
443 if (n == nTimeStepSizes-1) {
444 StepSize.push_back(0.0);
445 auto solutionExact = Thyra::createMember(model->get_x_space());
446 Thyra::copy(*(model->getExactSolution(time).get_x()),solutionExact.ptr());
447 solutions.push_back(solutionExact);
448 auto solutionDotExact = Thyra::createMember(model->get_x_space());
449 Thyra::copy(*(model->getExactSolution(time).get_x_dot()),
450 solutionDotExact.ptr());
451 solutionsDot.push_back(solutionDotExact);
456 if (nTimeStepSizes > 1) {
458 double xDotSlope = 0.0;
459 std::vector<double> xErrorNorm;
460 std::vector<double> xDotErrorNorm;
465 solutions, xErrorNorm, xSlope,
466 solutionsDot, xDotErrorNorm, xDotSlope);
484 #ifdef Tempus_ENABLE_MPI
485 comm =
rcp(
new Epetra_MpiComm(MPI_COMM_WORLD));
487 comm =
rcp(
new Epetra_SerialComm);
491 std::vector<RCP<Thyra::VectorBase<double>>> solutions;
492 std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
493 std::vector<double> StepSize;
497 getParametersFromXmlFile(
"Tempus_BDF2_CDR.xml");
501 double dt = pl->
sublist(
"Demo Integrator")
502 .
sublist(
"Time Step Control").
get<
double>(
"Initial Time Step");
506 const int nTimeStepSizes = model_pl->
get<
int>(
"Number of Time Step Sizes", 5);
508 for (
int n=0; n<nTimeStepSizes; n++) {
511 const int num_elements = model_pl->
get<
int>(
"num elements");
512 const double left_end = model_pl->
get<
double>(
"left end");
513 const double right_end = model_pl->
get<
double>(
"right end");
514 const double a_convection = model_pl->
get<
double>(
"a (convection)");
515 const double k_source = model_pl->
get<
double>(
"k (source)");
525 ::Stratimikos::DefaultLinearSolverBuilder builder;
528 p->set(
"Linear Solver Type",
"Belos");
529 p->set(
"Preconditioner Type",
"None");
530 builder.setParameterList(p);
533 lowsFactory = builder.createLinearSolveStrategy(
"");
535 model->set_W_factory(lowsFactory);
542 .
sublist(
"Time Step Control").
set(
"Initial Time Step", dt);
543 integrator = Tempus::createIntegratorBasic<double>(pl, model);
546 bool integratorStatus = integrator->advanceTime();
550 double time = integrator->getTime();
551 double timeFinal =pl->sublist(
"Demo Integrator")
552 .sublist(
"Time Step Control").
get<
double>(
"Final Time");
553 double tol = 100.0 * std::numeric_limits<double>::epsilon();
557 StepSize.push_back(dt);
558 auto solution = Thyra::createMember(model->get_x_space());
559 Thyra::copy(*(integrator->getX()),solution.ptr());
560 solutions.push_back(solution);
561 auto solutionDot = Thyra::createMember(model->get_x_space());
562 Thyra::copy(*(integrator->getXDot()),solutionDot.ptr());
563 solutionsDot.push_back(solutionDot);
567 if ((n == nTimeStepSizes-1) && (comm->NumProc() == 1)) {
568 std::ofstream ftmp(
"Tempus_BDF2_CDR.dat");
569 ftmp <<
"TITLE=\"BDF2 Solution to CDR\"\n"
570 <<
"VARIABLES=\"z\",\"T\"\n";
571 const double dx = std::fabs(left_end-right_end) /
572 static_cast<double>(num_elements);
574 integrator->getSolutionHistory();
575 int nStates = solutionHistory->getNumStates();
576 for (
int i=0; i<nStates; i++) {
579 double ttime = solutionState->getTime();
580 ftmp <<
"ZONE T=\"Time="<<ttime<<
"\", I="
581 <<num_elements+1<<
", F=BLOCK\n";
582 for (
int j = 0; j < num_elements+1; j++) {
583 const double x_coord = left_end +
static_cast<double>(j) * dx;
584 ftmp << x_coord <<
" ";
587 for (
int j=0; j<num_elements+1; j++) ftmp << get_ele(*x, j) <<
" ";
595 if (nTimeStepSizes > 2) {
597 double xDotSlope = 0.0;
598 std::vector<double> xErrorNorm;
599 std::vector<double> xDotErrorNorm;
601 double order = stepper->getOrder();
604 solutions, xErrorNorm, xSlope,
605 solutionsDot, xDotErrorNorm, xDotSlope);
617 if (comm->NumProc() == 1) {
619 getParametersFromXmlFile(
"Tempus_BDF2_CDR.xml");
621 const int num_elements = model_pl_CDR->
get<
int>(
"num elements");
622 const double left_end = model_pl_CDR->
get<
double>(
"left end");
623 const double right_end = model_pl_CDR->
get<
double>(
"right end");
627 std::ofstream ftmp(
"Tempus_BDF2_CDR-Solution.dat");
628 for (
int n = 0; n < num_elements+1; n++) {
629 const double dx = std::fabs(left_end-right_end) /
630 static_cast<double>(num_elements);
631 const double x_coord = left_end +
static_cast<double>(n) * dx;
632 ftmp << x_coord <<
" " << Thyra::get_ele(x,n) << std::endl;
645 std::vector<RCP<Thyra::VectorBase<double>>> solutions;
646 std::vector<double> StepSize;
647 std::vector<double> ErrorNorm;
651 getParametersFromXmlFile(
"Tempus_BDF2_VanDerPol.xml");
655 double dt = pl->
sublist(
"Demo Integrator")
656 .
sublist(
"Time Step Control").
get<
double>(
"Initial Time Step");
660 const int nTimeStepSizes = vdpm_pl->
get<
int>(
"Number of Time Step Sizes", 3);
664 for (
int n=0; n<nTimeStepSizes; n++) {
671 if (n == nTimeStepSizes-1) dt /= 10.0;
675 .
sublist(
"Time Step Control").
set(
"Initial Time Step", dt);
677 Tempus::createIntegratorBasic<double>(pl, model);
678 order = integrator->getStepper()->getOrder();
681 bool integratorStatus = integrator->advanceTime();
685 double time = integrator->getTime();
686 double timeFinal =pl->sublist(
"Demo Integrator")
687 .sublist(
"Time Step Control").
get<
double>(
"Final Time");
688 double tol = 100.0 * std::numeric_limits<double>::epsilon();
692 auto solution = Thyra::createMember(model->get_x_space());
693 Thyra::copy(*(integrator->getX()),solution.ptr());
694 solutions.push_back(solution);
695 StepSize.push_back(dt);
699 if ((n == 0) || (n == nTimeStepSizes-1)) {
700 std::string fname =
"Tempus_BDF2_VanDerPol-Ref.dat";
701 if (n == 0) fname =
"Tempus_BDF2_VanDerPol.dat";
702 std::ofstream ftmp(fname);
704 integrator->getSolutionHistory();
705 int nStates = solutionHistory->getNumStates();
706 for (
int i=0; i<nStates; i++) {
709 double ttime = solutionState->getTime();
710 ftmp << ttime <<
" " << get_ele(*x, 0) <<
" " << get_ele(*x, 1)
719 auto ref_solution = solutions[solutions.size()-1];
720 std::vector<double> StepSizeCheck;
721 for (std::size_t i=0; i < (solutions.size()-1); ++i) {
722 auto tmp = solutions[i];
723 Thyra::Vp_StV(tmp.ptr(), -1.0, *ref_solution);
724 const double L2norm = Thyra::norm_2(*tmp);
725 StepSizeCheck.push_back(StepSize[i]);
726 ErrorNorm.push_back(L2norm);
729 if (nTimeStepSizes > 2) {
731 double slope = computeLinearRegressionLogLog<double>(StepSizeCheck,ErrorNorm);
732 std::cout <<
" Stepper = BDF2" << std::endl;
733 std::cout <<
" =========================" << std::endl;
734 std::cout <<
" Expected order: " << order << std::endl;
735 std::cout <<
" Observed order: " << slope << std::endl;
736 std::cout <<
" =========================" << std::endl;
738 out <<
"\n\n ** Slope on BDF2 Method = " << slope
739 <<
"\n" << std::endl;
744 std::ofstream ftmp(
"Tempus_BDF2_VanDerPol-Error.dat");
745 double error0 = 0.8*ErrorNorm[0];
746 for (std::size_t n = 0; n < StepSizeCheck.size(); n++) {
747 ftmp << StepSizeCheck[n] <<
" " << ErrorNorm[n] <<
" "
748 << error0*(pow(StepSize[n]/StepSize[0],order)) << std::endl;
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.
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="")
Solution state for integrators and steppers. SolutionState contains the metadata for solutions and th...
1D CGFEM model for convection/diffusion/reaction