13 #include "Tempus_config.hpp"
14 #include "Tempus_IntegratorBasic.hpp"
15 #include "Tempus_StepperHHTAlpha.hpp"
17 #include "../TestModels/HarmonicOscillatorModel.hpp"
18 #include "../TestUtils/Tempus_ConvergenceTestUtils.hpp"
25 namespace Tempus_Test {
27 using Teuchos::getParametersFromXmlFile;
31 using Teuchos::rcp_const_cast;
32 using Teuchos::sublist;
43 double tolerance = 1.0e-14;
46 getParametersFromXmlFile(
"Tempus_HHTAlpha_BallParabolic.xml");
56 Tempus::createIntegratorBasic<double>(pl, model);
59 bool integratorStatus = integrator->advanceTime();
63 double time = integrator->getTime();
64 double timeFinal = pl->sublist(
"Default Integrator")
65 .sublist(
"Time Step Control")
66 .
get<
double>(
"Final Time");
72 model->getExactSolution(time).get_x();
75 std::ofstream ftmp(
"Tempus_HHTAlpha_BallParabolic.dat");
78 integrator->getSolutionHistory();
82 for (
int i = 0; i < solutionHistory->getNumStates(); i++) {
84 double time_i = solutionState->getTime();
86 x_exact_plot = model->getExactSolution(time_i).get_x();
87 ftmp << time_i <<
" " << get_ele(*(x_plot), 0) <<
" "
88 << get_ele(*(x_exact_plot), 0) << std::endl;
89 if (abs(get_ele(*(x_plot), 0) - get_ele(*(x_exact_plot), 0)) > err)
90 err = abs(get_ele(*(x_plot), 0) - get_ele(*(x_exact_plot), 0));
93 out <<
"Max error = " << err <<
"\n \n";
94 if (err > tolerance) passed =
false;
97 !passed, std::logic_error,
98 "\n Test failed! Max error = " << err <<
" > tolerance = " << tolerance
110 getParametersFromXmlFile(
"Tempus_HHTAlpha_SinCos_SecondOrder.xml");
119 stepper->setModel(model);
120 stepper->initialize();
126 timeStepControl->setInitIndex(tscPL.
get<
int>(
"Initial Time Index"));
127 timeStepControl->setInitTime(tscPL.
get<
double>(
"Initial Time"));
128 timeStepControl->setFinalTime(tscPL.
get<
double>(
"Final Time"));
129 timeStepControl->setInitTimeStep(dt);
130 timeStepControl->initialize();
133 auto inArgsIC = model->getNominalValues();
139 icState->setTime(timeStepControl->getInitTime());
140 icState->setIndex(timeStepControl->getInitIndex());
141 icState->setTimeStep(0.0);
142 icState->setOrder(stepper->getOrder());
147 solutionHistory->setName(
"Forward States");
149 solutionHistory->setStorageLimit(2);
150 solutionHistory->addState(icState);
154 Tempus::createIntegratorBasic<double>();
155 integrator->setStepper(stepper);
156 integrator->setTimeStepControl(timeStepControl);
157 integrator->setSolutionHistory(solutionHistory);
159 integrator->initialize();
162 bool integratorStatus = integrator->advanceTime();
166 double time = integrator->getTime();
167 double timeFinal = pl->
sublist(
"Default Integrator")
169 .
get<
double>(
"Final Time");
175 model->getExactSolution(time).get_x();
179 Thyra::V_StVpStV(xdiff.
ptr(), 1.0, *x_exact, -1.0, *(x));
182 out <<
" Stepper = " << stepper->description() << std::endl;
183 out <<
" =========================" << std::endl;
184 out <<
" Exact solution : " << get_ele(*(x_exact), 0) << std::endl;
185 out <<
" Computed solution: " << get_ele(*(x), 0) << std::endl;
186 out <<
" Difference : " << get_ele(*(xdiff), 0) << std::endl;
187 out <<
" =========================" << std::endl;
188 TEST_FLOATING_EQUALITY(get_ele(*(x), 0), 0.144918, 1.0e-4);
195 std::vector<RCP<Thyra::VectorBase<double>>> solutions;
196 std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
197 std::vector<double> StepSize;
198 std::vector<double> xErrorNorm;
199 std::vector<double> xDotErrorNorm;
200 const int nTimeStepSizes = 7;
205 getParametersFromXmlFile(
"Tempus_HHTAlpha_SinCos_SecondOrder.xml");
212 double k = hom_pl->
get<
double>(
"x coeff k");
213 double m = hom_pl->
get<
double>(
"Mass coeff m");
221 double dt = pl->sublist(
"Default Integrator")
222 .sublist(
"Time Step Control")
223 .get<
double>(
"Initial Time Step");
226 for (
int n = 0; n < nTimeStepSizes; n++) {
229 out <<
"\n \n time step #" << n <<
" (out of " << nTimeStepSizes - 1
230 <<
"), dt = " << dt <<
"\n";
231 pl->sublist(
"Default Integrator")
232 .sublist(
"Time Step Control")
233 .set(
"Initial Time Step", dt);
234 integrator = Tempus::createIntegratorBasic<double>(pl, model);
237 bool integratorStatus = integrator->advanceTime();
241 time = integrator->getTime();
242 double timeFinal = pl->sublist(
"Default Integrator")
243 .sublist(
"Time Step Control")
244 .
get<
double>(
"Final Time");
250 model->getExactSolution(time).get_x();
253 if (n == nTimeStepSizes - 1) {
255 integrator->getSolutionHistory();
256 writeSolution(
"Tempus_HHTAlpha_SinCos_SecondOrder.dat", solutionHistory);
259 for (
int i = 0; i < solutionHistory->getNumStates(); i++) {
260 double time_i = (*solutionHistory)[i]->getTime();
263 model->getExactSolution(time_i).get_x()),
265 model->getExactSolution(time_i).get_x_dot()));
266 state->setTime((*solutionHistory)[i]->getTime());
267 solnHistExact->addState(state);
274 std::ofstream ftmp(
"Tempus_HHTAlpha_SinCos_SecondOrder-Energy.dat");
277 for (
int i = 0; i < solutionHistory->getNumStates(); i++) {
279 (*solutionHistory)[i];
280 double time_i = solutionState->getTime();
283 solutionState->getXDot();
284 x_exact_plot = model->getExactSolution(time_i).get_x();
286 double ke = Thyra::dot(*x_dot_plot, *x_dot_plot);
289 double pe = Thyra::dot(*x_plot, *x_plot);
294 ftmp << time_i <<
" " << get_ele(*(x_plot), 0) <<
" "
295 << get_ele(*(x_exact_plot), 0) <<
" "
296 << get_ele(*(x_dot_plot), 0) <<
" " << ke <<
" " << pe
297 <<
" " << te << std::endl;
304 StepSize.push_back(dt);
305 auto solution = Thyra::createMember(model->get_x_space());
306 Thyra::copy(*(integrator->getX()), solution.ptr());
307 solutions.push_back(solution);
308 auto solutionDot = Thyra::createMember(model->get_x_space());
309 Thyra::copy(*(integrator->getXDot()), solutionDot.ptr());
310 solutionsDot.push_back(solutionDot);
311 if (n == nTimeStepSizes - 1) {
312 StepSize.push_back(0.0);
313 auto solutionExact = Thyra::createMember(model->get_x_space());
314 Thyra::copy(*(model->getExactSolution(time).get_x()),
315 solutionExact.ptr());
316 solutions.push_back(solutionExact);
317 auto solutionDotExact = Thyra::createMember(model->get_x_space());
318 Thyra::copy(*(model->getExactSolution(time).get_x_dot()),
319 solutionDotExact.ptr());
320 solutionsDot.push_back(solutionDotExact);
326 double xDotSlope = 0.0;
328 double order = stepper->getOrder();
329 writeOrderError(
"Tempus_HHTAlpha_SinCos_SecondOrder-Error.dat", stepper,
330 StepSize, solutions, xErrorNorm, xSlope, solutionsDot,
331 xDotErrorNorm, xDotSlope, out);
344 std::vector<RCP<Thyra::VectorBase<double>>> solutions;
345 std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
346 std::vector<double> StepSize;
347 std::vector<double> xErrorNorm;
348 std::vector<double> xDotErrorNorm;
349 const int nTimeStepSizes = 7;
354 getParametersFromXmlFile(
"Tempus_HHTAlpha_SinCos_FirstOrder.xml");
361 double k = hom_pl->
get<
double>(
"x coeff k");
362 double m = hom_pl->
get<
double>(
"Mass coeff m");
370 double dt = pl->sublist(
"Default Integrator")
371 .sublist(
"Time Step Control")
372 .get<
double>(
"Initial Time Step");
375 for (
int n = 0; n < nTimeStepSizes; n++) {
378 out <<
"\n \n time step #" << n <<
" (out of " << nTimeStepSizes - 1
379 <<
"), dt = " << dt <<
"\n";
380 pl->sublist(
"Default Integrator")
381 .sublist(
"Time Step Control")
382 .set(
"Initial Time Step", dt);
383 integrator = Tempus::createIntegratorBasic<double>(pl, model);
386 bool integratorStatus = integrator->advanceTime();
390 time = integrator->getTime();
391 double timeFinal = pl->sublist(
"Default Integrator")
392 .sublist(
"Time Step Control")
393 .
get<
double>(
"Final Time");
399 model->getExactSolution(time).get_x();
402 if (n == nTimeStepSizes - 1) {
404 integrator->getSolutionHistory();
405 writeSolution(
"Tempus_HHTAlpha_SinCos_FirstOrder.dat", solutionHistory);
408 for (
int i = 0; i < solutionHistory->getNumStates(); i++) {
409 double time_i = (*solutionHistory)[i]->getTime();
412 model->getExactSolution(time_i).get_x()),
414 model->getExactSolution(time_i).get_x_dot()));
415 state->setTime((*solutionHistory)[i]->getTime());
416 solnHistExact->addState(state);
418 writeSolution(
"Tempus_HHTAlpha_SinCos_FirstOrder-Ref.dat", solnHistExact);
422 std::ofstream ftmp(
"Tempus_HHTAlpha_SinCos_FirstOrder-Energy.dat");
425 for (
int i = 0; i < solutionHistory->getNumStates(); i++) {
427 (*solutionHistory)[i];
428 double time_i = solutionState->getTime();
431 solutionState->getXDot();
432 x_exact_plot = model->getExactSolution(time_i).get_x();
434 double ke = Thyra::dot(*x_dot_plot, *x_dot_plot);
437 double pe = Thyra::dot(*x_plot, *x_plot);
442 ftmp << time_i <<
" " << get_ele(*(x_plot), 0) <<
" "
443 << get_ele(*(x_exact_plot), 0) <<
" "
444 << get_ele(*(x_dot_plot), 0) <<
" " << ke <<
" " << pe
445 <<
" " << te << std::endl;
452 StepSize.push_back(dt);
453 auto solution = Thyra::createMember(model->get_x_space());
454 Thyra::copy(*(integrator->getX()), solution.ptr());
455 solutions.push_back(solution);
456 auto solutionDot = Thyra::createMember(model->get_x_space());
457 Thyra::copy(*(integrator->getXDot()), solutionDot.ptr());
458 solutionsDot.push_back(solutionDot);
459 if (n == nTimeStepSizes - 1) {
460 StepSize.push_back(0.0);
461 auto solutionExact = Thyra::createMember(model->get_x_space());
462 Thyra::copy(*(model->getExactSolution(time).get_x()),
463 solutionExact.ptr());
464 solutions.push_back(solutionExact);
465 auto solutionDotExact = Thyra::createMember(model->get_x_space());
466 Thyra::copy(*(model->getExactSolution(time).get_x_dot()),
467 solutionDotExact.ptr());
468 solutionsDot.push_back(solutionDotExact);
474 double xDotSlope = 0.0;
477 writeOrderError(
"Tempus_HHTAlpha_SinCos_FirstOrder-Error.dat", stepper,
478 StepSize, solutions, xErrorNorm, xSlope, solutionsDot,
479 xDotErrorNorm, xDotSlope, out);
492 std::vector<RCP<Thyra::VectorBase<double>>> solutions;
493 std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
494 std::vector<double> StepSize;
495 std::vector<double> xErrorNorm;
496 std::vector<double> xDotErrorNorm;
497 const int nTimeStepSizes = 7;
502 getParametersFromXmlFile(
"Tempus_HHTAlpha_SinCos_ExplicitCD.xml");
509 double k = hom_pl->
get<
double>(
"x coeff k");
510 double m = hom_pl->
get<
double>(
"Mass coeff m");
518 double dt = pl->sublist(
"Default Integrator")
519 .sublist(
"Time Step Control")
520 .get<
double>(
"Initial Time Step");
523 for (
int n = 0; n < nTimeStepSizes; n++) {
526 out <<
"\n \n time step #" << n <<
" (out of " << nTimeStepSizes - 1
527 <<
"), dt = " << dt <<
"\n";
528 pl->sublist(
"Default Integrator")
529 .sublist(
"Time Step Control")
530 .set(
"Initial Time Step", dt);
531 integrator = Tempus::createIntegratorBasic<double>(pl, model);
534 bool integratorStatus = integrator->advanceTime();
538 time = integrator->getTime();
539 double timeFinal = pl->sublist(
"Default Integrator")
540 .sublist(
"Time Step Control")
541 .
get<
double>(
"Final Time");
547 model->getExactSolution(time).get_x();
550 if (n == nTimeStepSizes - 1) {
552 integrator->getSolutionHistory();
553 writeSolution(
"Tempus_HHTAlpha_SinCos_ExplicitCD.dat", solutionHistory);
556 for (
int i = 0; i < solutionHistory->getNumStates(); i++) {
557 double time_i = (*solutionHistory)[i]->getTime();
560 model->getExactSolution(time_i).get_x()),
562 model->getExactSolution(time_i).get_x_dot()));
563 state->setTime((*solutionHistory)[i]->getTime());
564 solnHistExact->addState(state);
566 writeSolution(
"Tempus_HHTAlpha_SinCos_ExplicitCD-Ref.dat", solnHistExact);
570 std::ofstream ftmp(
"Tempus_HHTAlpha_SinCos_ExplicitCD-Energy.dat");
573 for (
int i = 0; i < solutionHistory->getNumStates(); i++) {
575 (*solutionHistory)[i];
576 double time_i = solutionState->getTime();
579 solutionState->getXDot();
580 x_exact_plot = model->getExactSolution(time_i).get_x();
582 double ke = Thyra::dot(*x_dot_plot, *x_dot_plot);
585 double pe = Thyra::dot(*x_plot, *x_plot);
590 ftmp << time_i <<
" " << get_ele(*(x_plot), 0) <<
" "
591 << get_ele(*(x_exact_plot), 0) <<
" "
592 << get_ele(*(x_dot_plot), 0) <<
" " << ke <<
" " << pe
593 <<
" " << te << std::endl;
600 StepSize.push_back(dt);
601 auto solution = Thyra::createMember(model->get_x_space());
602 Thyra::copy(*(integrator->getX()), solution.ptr());
603 solutions.push_back(solution);
604 auto solutionDot = Thyra::createMember(model->get_x_space());
605 Thyra::copy(*(integrator->getXDot()), solutionDot.ptr());
606 solutionsDot.push_back(solutionDot);
607 if (n == nTimeStepSizes - 1) {
608 StepSize.push_back(0.0);
609 auto solutionExact = Thyra::createMember(model->get_x_space());
610 Thyra::copy(*(model->getExactSolution(time).get_x()),
611 solutionExact.ptr());
612 solutions.push_back(solutionExact);
613 auto solutionDotExact = Thyra::createMember(model->get_x_space());
614 Thyra::copy(*(model->getExactSolution(time).get_x_dot()),
615 solutionDotExact.ptr());
616 solutionsDot.push_back(solutionDotExact);
622 double xDotSlope = 0.0;
624 double order = stepper->getOrder();
625 writeOrderError(
"Tempus_HHTAlpha_SinCos_ExplicitCD-Error.dat", stepper,
626 StepSize, solutions, xErrorNorm, xSlope, solutionsDot,
627 xDotErrorNorm, xDotSlope, out);
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)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
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)
void writeSolution(const std::string filename, Teuchos::RCP< const Tempus::SolutionHistory< Scalar >> solutionHistory)
Consider the ODE: where is a constant, is a constant damping parameter, is a constant forcing par...
TEUCHOS_UNIT_TEST(BackwardEuler, SinCos_ASA)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
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.
ParameterList & sublist(const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
Solution state for integrators and steppers.