14 #include "Tempus_config.hpp"
15 #include "Tempus_IntegratorBasic.hpp"
16 #include "Tempus_StepperHHTAlpha.hpp"
18 #include "../TestModels/HarmonicOscillatorModel.hpp"
19 #include "../TestUtils/Tempus_ConvergenceTestUtils.hpp"
26 namespace Tempus_Test {
28 using Teuchos::getParametersFromXmlFile;
32 using Teuchos::rcp_const_cast;
33 using Teuchos::sublist;
44 double tolerance = 1.0e-14;
47 getParametersFromXmlFile(
"Tempus_HHTAlpha_BallParabolic.xml");
57 Tempus::createIntegratorBasic<double>(pl, model);
60 bool integratorStatus = integrator->advanceTime();
64 double time = integrator->getTime();
65 double timeFinal = pl->sublist(
"Default Integrator")
66 .sublist(
"Time Step Control")
67 .
get<
double>(
"Final Time");
73 model->getExactSolution(time).get_x();
76 std::ofstream ftmp(
"Tempus_HHTAlpha_BallParabolic.dat");
79 integrator->getSolutionHistory();
83 for (
int i = 0; i < solutionHistory->getNumStates(); i++) {
85 double time_i = solutionState->getTime();
87 x_exact_plot = model->getExactSolution(time_i).get_x();
88 ftmp << time_i <<
" " << get_ele(*(x_plot), 0) <<
" "
89 << get_ele(*(x_exact_plot), 0) << std::endl;
90 if (abs(get_ele(*(x_plot), 0) - get_ele(*(x_exact_plot), 0)) > err)
91 err = abs(get_ele(*(x_plot), 0) - get_ele(*(x_exact_plot), 0));
94 out <<
"Max error = " << err <<
"\n \n";
95 if (err > tolerance) passed =
false;
98 !passed, std::logic_error,
99 "\n Test failed! Max error = " << err <<
" > tolerance = " << tolerance
111 getParametersFromXmlFile(
"Tempus_HHTAlpha_SinCos_SecondOrder.xml");
120 stepper->setModel(model);
121 stepper->initialize();
127 timeStepControl->setInitIndex(tscPL.
get<
int>(
"Initial Time Index"));
128 timeStepControl->setInitTime(tscPL.
get<
double>(
"Initial Time"));
129 timeStepControl->setFinalTime(tscPL.
get<
double>(
"Final Time"));
130 timeStepControl->setInitTimeStep(dt);
131 timeStepControl->initialize();
134 auto inArgsIC = model->getNominalValues();
140 icState->setTime(timeStepControl->getInitTime());
141 icState->setIndex(timeStepControl->getInitIndex());
142 icState->setTimeStep(0.0);
143 icState->setOrder(stepper->getOrder());
148 solutionHistory->setName(
"Forward States");
150 solutionHistory->setStorageLimit(2);
151 solutionHistory->addState(icState);
155 Tempus::createIntegratorBasic<double>();
156 integrator->setStepper(stepper);
157 integrator->setTimeStepControl(timeStepControl);
158 integrator->setSolutionHistory(solutionHistory);
160 integrator->initialize();
163 bool integratorStatus = integrator->advanceTime();
167 double time = integrator->getTime();
168 double timeFinal = pl->
sublist(
"Default Integrator")
170 .
get<
double>(
"Final Time");
176 model->getExactSolution(time).get_x();
180 Thyra::V_StVpStV(xdiff.
ptr(), 1.0, *x_exact, -1.0, *(x));
183 out <<
" Stepper = " << stepper->description() << std::endl;
184 out <<
" =========================" << std::endl;
185 out <<
" Exact solution : " << get_ele(*(x_exact), 0) << std::endl;
186 out <<
" Computed solution: " << get_ele(*(x), 0) << std::endl;
187 out <<
" Difference : " << get_ele(*(xdiff), 0) << std::endl;
188 out <<
" =========================" << std::endl;
189 TEST_FLOATING_EQUALITY(get_ele(*(x), 0), 0.144918, 1.0e-4);
196 std::vector<RCP<Thyra::VectorBase<double>>> solutions;
197 std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
198 std::vector<double> StepSize;
199 std::vector<double> xErrorNorm;
200 std::vector<double> xDotErrorNorm;
201 const int nTimeStepSizes = 7;
206 getParametersFromXmlFile(
"Tempus_HHTAlpha_SinCos_SecondOrder.xml");
213 double k = hom_pl->
get<
double>(
"x coeff k");
214 double m = hom_pl->
get<
double>(
"Mass coeff m");
222 double dt = pl->sublist(
"Default Integrator")
223 .sublist(
"Time Step Control")
224 .get<
double>(
"Initial Time Step");
227 for (
int n = 0; n < nTimeStepSizes; n++) {
230 out <<
"\n \n time step #" << n <<
" (out of " << nTimeStepSizes - 1
231 <<
"), dt = " << dt <<
"\n";
232 pl->sublist(
"Default Integrator")
233 .sublist(
"Time Step Control")
234 .set(
"Initial Time Step", dt);
235 integrator = Tempus::createIntegratorBasic<double>(pl, model);
238 bool integratorStatus = integrator->advanceTime();
242 time = integrator->getTime();
243 double timeFinal = pl->sublist(
"Default Integrator")
244 .sublist(
"Time Step Control")
245 .
get<
double>(
"Final Time");
251 model->getExactSolution(time).get_x();
254 if (n == nTimeStepSizes - 1) {
256 integrator->getSolutionHistory();
257 writeSolution(
"Tempus_HHTAlpha_SinCos_SecondOrder.dat", solutionHistory);
260 for (
int i = 0; i < solutionHistory->getNumStates(); i++) {
261 double time_i = (*solutionHistory)[i]->getTime();
264 model->getExactSolution(time_i).get_x()),
266 model->getExactSolution(time_i).get_x_dot()));
267 state->setTime((*solutionHistory)[i]->getTime());
268 solnHistExact->addState(state);
275 std::ofstream ftmp(
"Tempus_HHTAlpha_SinCos_SecondOrder-Energy.dat");
278 for (
int i = 0; i < solutionHistory->getNumStates(); i++) {
280 (*solutionHistory)[i];
281 double time_i = solutionState->getTime();
284 solutionState->getXDot();
285 x_exact_plot = model->getExactSolution(time_i).get_x();
287 double ke = Thyra::dot(*x_dot_plot, *x_dot_plot);
290 double pe = Thyra::dot(*x_plot, *x_plot);
295 ftmp << time_i <<
" " << get_ele(*(x_plot), 0) <<
" "
296 << get_ele(*(x_exact_plot), 0) <<
" "
297 << get_ele(*(x_dot_plot), 0) <<
" " << ke <<
" " << pe
298 <<
" " << te << std::endl;
305 StepSize.push_back(dt);
306 auto solution = Thyra::createMember(model->get_x_space());
307 Thyra::copy(*(integrator->getX()), solution.ptr());
308 solutions.push_back(solution);
309 auto solutionDot = Thyra::createMember(model->get_x_space());
310 Thyra::copy(*(integrator->getXDot()), solutionDot.ptr());
311 solutionsDot.push_back(solutionDot);
312 if (n == nTimeStepSizes - 1) {
313 StepSize.push_back(0.0);
314 auto solutionExact = Thyra::createMember(model->get_x_space());
315 Thyra::copy(*(model->getExactSolution(time).get_x()),
316 solutionExact.ptr());
317 solutions.push_back(solutionExact);
318 auto solutionDotExact = Thyra::createMember(model->get_x_space());
319 Thyra::copy(*(model->getExactSolution(time).get_x_dot()),
320 solutionDotExact.ptr());
321 solutionsDot.push_back(solutionDotExact);
327 double xDotSlope = 0.0;
329 double order = stepper->getOrder();
330 writeOrderError(
"Tempus_HHTAlpha_SinCos_SecondOrder-Error.dat", stepper,
331 StepSize, solutions, xErrorNorm, xSlope, solutionsDot,
332 xDotErrorNorm, xDotSlope, out);
345 std::vector<RCP<Thyra::VectorBase<double>>> solutions;
346 std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
347 std::vector<double> StepSize;
348 std::vector<double> xErrorNorm;
349 std::vector<double> xDotErrorNorm;
350 const int nTimeStepSizes = 7;
355 getParametersFromXmlFile(
"Tempus_HHTAlpha_SinCos_FirstOrder.xml");
362 double k = hom_pl->
get<
double>(
"x coeff k");
363 double m = hom_pl->
get<
double>(
"Mass coeff m");
371 double dt = pl->sublist(
"Default Integrator")
372 .sublist(
"Time Step Control")
373 .get<
double>(
"Initial Time Step");
376 for (
int n = 0; n < nTimeStepSizes; n++) {
379 out <<
"\n \n time step #" << n <<
" (out of " << nTimeStepSizes - 1
380 <<
"), dt = " << dt <<
"\n";
381 pl->sublist(
"Default Integrator")
382 .sublist(
"Time Step Control")
383 .set(
"Initial Time Step", dt);
384 integrator = Tempus::createIntegratorBasic<double>(pl, model);
387 bool integratorStatus = integrator->advanceTime();
391 time = integrator->getTime();
392 double timeFinal = pl->sublist(
"Default Integrator")
393 .sublist(
"Time Step Control")
394 .
get<
double>(
"Final Time");
400 model->getExactSolution(time).get_x();
403 if (n == nTimeStepSizes - 1) {
405 integrator->getSolutionHistory();
406 writeSolution(
"Tempus_HHTAlpha_SinCos_FirstOrder.dat", solutionHistory);
409 for (
int i = 0; i < solutionHistory->getNumStates(); i++) {
410 double time_i = (*solutionHistory)[i]->getTime();
413 model->getExactSolution(time_i).get_x()),
415 model->getExactSolution(time_i).get_x_dot()));
416 state->setTime((*solutionHistory)[i]->getTime());
417 solnHistExact->addState(state);
419 writeSolution(
"Tempus_HHTAlpha_SinCos_FirstOrder-Ref.dat", solnHistExact);
423 std::ofstream ftmp(
"Tempus_HHTAlpha_SinCos_FirstOrder-Energy.dat");
426 for (
int i = 0; i < solutionHistory->getNumStates(); i++) {
428 (*solutionHistory)[i];
429 double time_i = solutionState->getTime();
432 solutionState->getXDot();
433 x_exact_plot = model->getExactSolution(time_i).get_x();
435 double ke = Thyra::dot(*x_dot_plot, *x_dot_plot);
438 double pe = Thyra::dot(*x_plot, *x_plot);
443 ftmp << time_i <<
" " << get_ele(*(x_plot), 0) <<
" "
444 << get_ele(*(x_exact_plot), 0) <<
" "
445 << get_ele(*(x_dot_plot), 0) <<
" " << ke <<
" " << pe
446 <<
" " << te << std::endl;
453 StepSize.push_back(dt);
454 auto solution = Thyra::createMember(model->get_x_space());
455 Thyra::copy(*(integrator->getX()), solution.ptr());
456 solutions.push_back(solution);
457 auto solutionDot = Thyra::createMember(model->get_x_space());
458 Thyra::copy(*(integrator->getXDot()), solutionDot.ptr());
459 solutionsDot.push_back(solutionDot);
460 if (n == nTimeStepSizes - 1) {
461 StepSize.push_back(0.0);
462 auto solutionExact = Thyra::createMember(model->get_x_space());
463 Thyra::copy(*(model->getExactSolution(time).get_x()),
464 solutionExact.ptr());
465 solutions.push_back(solutionExact);
466 auto solutionDotExact = Thyra::createMember(model->get_x_space());
467 Thyra::copy(*(model->getExactSolution(time).get_x_dot()),
468 solutionDotExact.ptr());
469 solutionsDot.push_back(solutionDotExact);
475 double xDotSlope = 0.0;
478 writeOrderError(
"Tempus_HHTAlpha_SinCos_FirstOrder-Error.dat", stepper,
479 StepSize, solutions, xErrorNorm, xSlope, solutionsDot,
480 xDotErrorNorm, xDotSlope, out);
493 std::vector<RCP<Thyra::VectorBase<double>>> solutions;
494 std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
495 std::vector<double> StepSize;
496 std::vector<double> xErrorNorm;
497 std::vector<double> xDotErrorNorm;
498 const int nTimeStepSizes = 7;
503 getParametersFromXmlFile(
"Tempus_HHTAlpha_SinCos_ExplicitCD.xml");
510 double k = hom_pl->
get<
double>(
"x coeff k");
511 double m = hom_pl->
get<
double>(
"Mass coeff m");
519 double dt = pl->sublist(
"Default Integrator")
520 .sublist(
"Time Step Control")
521 .get<
double>(
"Initial Time Step");
524 for (
int n = 0; n < nTimeStepSizes; n++) {
527 out <<
"\n \n time step #" << n <<
" (out of " << nTimeStepSizes - 1
528 <<
"), dt = " << dt <<
"\n";
529 pl->sublist(
"Default Integrator")
530 .sublist(
"Time Step Control")
531 .set(
"Initial Time Step", dt);
532 integrator = Tempus::createIntegratorBasic<double>(pl, model);
535 bool integratorStatus = integrator->advanceTime();
539 time = integrator->getTime();
540 double timeFinal = pl->sublist(
"Default Integrator")
541 .sublist(
"Time Step Control")
542 .
get<
double>(
"Final Time");
548 model->getExactSolution(time).get_x();
551 if (n == nTimeStepSizes - 1) {
553 integrator->getSolutionHistory();
554 writeSolution(
"Tempus_HHTAlpha_SinCos_ExplicitCD.dat", solutionHistory);
557 for (
int i = 0; i < solutionHistory->getNumStates(); i++) {
558 double time_i = (*solutionHistory)[i]->getTime();
561 model->getExactSolution(time_i).get_x()),
563 model->getExactSolution(time_i).get_x_dot()));
564 state->setTime((*solutionHistory)[i]->getTime());
565 solnHistExact->addState(state);
567 writeSolution(
"Tempus_HHTAlpha_SinCos_ExplicitCD-Ref.dat", solnHistExact);
571 std::ofstream ftmp(
"Tempus_HHTAlpha_SinCos_ExplicitCD-Energy.dat");
574 for (
int i = 0; i < solutionHistory->getNumStates(); i++) {
576 (*solutionHistory)[i];
577 double time_i = solutionState->getTime();
580 solutionState->getXDot();
581 x_exact_plot = model->getExactSolution(time_i).get_x();
583 double ke = Thyra::dot(*x_dot_plot, *x_dot_plot);
586 double pe = Thyra::dot(*x_plot, *x_plot);
591 ftmp << time_i <<
" " << get_ele(*(x_plot), 0) <<
" "
592 << get_ele(*(x_exact_plot), 0) <<
" "
593 << get_ele(*(x_dot_plot), 0) <<
" " << ke <<
" " << pe
594 <<
" " << te << std::endl;
601 StepSize.push_back(dt);
602 auto solution = Thyra::createMember(model->get_x_space());
603 Thyra::copy(*(integrator->getX()), solution.ptr());
604 solutions.push_back(solution);
605 auto solutionDot = Thyra::createMember(model->get_x_space());
606 Thyra::copy(*(integrator->getXDot()), solutionDot.ptr());
607 solutionsDot.push_back(solutionDot);
608 if (n == nTimeStepSizes - 1) {
609 StepSize.push_back(0.0);
610 auto solutionExact = Thyra::createMember(model->get_x_space());
611 Thyra::copy(*(model->getExactSolution(time).get_x()),
612 solutionExact.ptr());
613 solutions.push_back(solutionExact);
614 auto solutionDotExact = Thyra::createMember(model->get_x_space());
615 Thyra::copy(*(model->getExactSolution(time).get_x_dot()),
616 solutionDotExact.ptr());
617 solutionsDot.push_back(solutionDotExact);
623 double xDotSlope = 0.0;
625 double order = stepper->getOrder();
626 writeOrderError(
"Tempus_HHTAlpha_SinCos_ExplicitCD-Error.dat", stepper,
627 StepSize, solutions, xErrorNorm, xSlope, solutionsDot,
628 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.