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"
20 #include "Stratimikos_DefaultLinearSolverBuilder.hpp"
21 #include "Thyra_LinearOpWithSolveFactoryHelpers.hpp"
22 #include "Thyra_DetachedVectorView.hpp"
23 #include "Thyra_DetachedMultiVectorView.hpp"
24 #include "NOX_Thyra.H"
27 #ifdef Tempus_ENABLE_MPI
28 #include "Epetra_MpiComm.h"
30 #include "Epetra_SerialComm.h"
38 namespace Tempus_Test {
42 using Teuchos::rcp_const_cast;
44 using Teuchos::sublist;
45 using Teuchos::getParametersFromXmlFile;
57 double tolerance = 1.0e-14;
60 getParametersFromXmlFile(
"Tempus_HHTAlpha_BallParabolic.xml");
70 Tempus::createIntegratorBasic<double>(pl, model);
73 bool integratorStatus = integrator->advanceTime();
77 double time = integrator->getTime();
78 double timeFinal =pl->sublist(
"Default Integrator")
79 .sublist(
"Time Step Control").
get<
double>(
"Final Time");
85 model->getExactSolution(time).get_x();
88 std::ofstream ftmp(
"Tempus_HHTAlpha_BallParabolic.dat");
91 integrator->getSolutionHistory();
95 for (
int i=0; i<solutionHistory->getNumStates(); i++) {
97 double time_i = solutionState->getTime();
99 x_exact_plot = model->getExactSolution(time_i).get_x();
100 ftmp << time_i <<
" "
101 << get_ele(*(x_plot), 0) <<
" "
102 << get_ele(*(x_exact_plot), 0) << std::endl;
103 if (abs(get_ele(*(x_plot),0) - get_ele(*(x_exact_plot), 0)) > err)
104 err = abs(get_ele(*(x_plot),0) - get_ele(*(x_exact_plot), 0));
107 std::cout <<
"Max error = " << err <<
"\n \n";
112 "\n Test failed! Max error = " << err <<
" > tolerance = " << tolerance <<
"\n!");
125 getParametersFromXmlFile(
"Tempus_HHTAlpha_SinCos_SecondOrder.xml");
134 stepper->setModel(model);
135 stepper->initialize();
141 timeStepControl->setInitIndex(tscPL.
get<
int> (
"Initial Time Index"));
142 timeStepControl->setInitTime (tscPL.
get<
double>(
"Initial Time"));
143 timeStepControl->setFinalTime(tscPL.
get<
double>(
"Final Time"));
144 timeStepControl->setInitTimeStep(dt);
145 timeStepControl->initialize();
148 auto inArgsIC = model->getNominalValues();
153 icState->setTime (timeStepControl->getInitTime());
154 icState->setIndex (timeStepControl->getInitIndex());
155 icState->setTimeStep(0.0);
156 icState->setOrder (stepper->getOrder());
161 solutionHistory->setName(
"Forward States");
163 solutionHistory->setStorageLimit(2);
164 solutionHistory->addState(icState);
168 Tempus::createIntegratorBasic<double>();
169 integrator->setStepper(stepper);
170 integrator->setTimeStepControl(timeStepControl);
171 integrator->setSolutionHistory(solutionHistory);
173 integrator->initialize();
177 bool integratorStatus = integrator->advanceTime();
182 double time = integrator->getTime();
183 double timeFinal =pl->
sublist(
"Default Integrator")
184 .
sublist(
"Time Step Control").
get<
double>(
"Final Time");
190 model->getExactSolution(time).get_x();
194 Thyra::V_StVpStV(xdiff.
ptr(), 1.0, *x_exact, -1.0, *(x));
197 std::cout <<
" Stepper = " << stepper->description() << std::endl;
198 std::cout <<
" =========================" << std::endl;
199 std::cout <<
" Exact solution : " << get_ele(*(x_exact), 0) << std::endl;
200 std::cout <<
" Computed solution: " << get_ele(*(x ), 0) << std::endl;
201 std::cout <<
" Difference : " << get_ele(*(xdiff ), 0) << std::endl;
202 std::cout <<
" =========================" << std::endl;
203 TEST_FLOATING_EQUALITY(get_ele(*(x), 0), 0.144918, 1.0e-4 );
211 std::vector<RCP<Thyra::VectorBase<double>>> solutions;
212 std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
213 std::vector<double> StepSize;
214 std::vector<double> xErrorNorm;
215 std::vector<double> xDotErrorNorm;
216 const int nTimeStepSizes = 7;
221 getParametersFromXmlFile(
"Tempus_HHTAlpha_SinCos_SecondOrder.xml");
228 double k = hom_pl->
get<
double>(
"x coeff k");
229 double m = hom_pl->
get<
double>(
"Mass coeff m");
236 double dt =pl->sublist(
"Default Integrator")
237 .sublist(
"Time Step Control").get<
double>(
"Initial Time Step");
240 for (
int n=0; n<nTimeStepSizes; n++) {
244 std::cout <<
"\n \n time step #" << n <<
" (out of "
245 << nTimeStepSizes-1 <<
"), dt = " << dt <<
"\n";
246 pl->sublist(
"Default Integrator")
247 .sublist(
"Time Step Control").set(
"Initial Time Step", dt);
248 integrator = Tempus::createIntegratorBasic<double>(pl, model);
251 bool integratorStatus = integrator->advanceTime();
255 time = integrator->getTime();
256 double timeFinal =pl->sublist(
"Default Integrator")
257 .sublist(
"Time Step Control").
get<
double>(
"Final Time");
263 model->getExactSolution(time).get_x();
266 if (n == nTimeStepSizes-1) {
268 integrator->getSolutionHistory();
269 writeSolution(
"Tempus_HHTAlpha_SinCos_SecondOrder.dat", solutionHistory);
272 for (
int i=0; i<solutionHistory->getNumStates(); i++) {
273 double time_i = (*solutionHistory)[i]->getTime();
276 model->getExactSolution(time_i).get_x()),
278 model->getExactSolution(time_i).get_x_dot()));
279 state->setTime((*solutionHistory)[i]->getTime());
280 solnHistExact->addState(state);
282 writeSolution(
"Tempus_HHTAlpha_SinCos_SecondOrder-Ref.dat", solnHistExact);
286 std::ofstream ftmp(
"Tempus_HHTAlpha_SinCos_SecondOrder-Energy.dat");
289 for (
int i=0; i<solutionHistory->getNumStates(); i++) {
291 double time_i = solutionState->getTime();
294 x_exact_plot = model->getExactSolution(time_i).get_x();
296 double ke = Thyra::dot(*x_dot_plot, *x_dot_plot);
299 double pe = Thyra::dot(*x_plot, *x_plot);
304 ftmp << time_i <<
" "
305 << get_ele(*(x_plot), 0) <<
" "
306 << get_ele(*(x_exact_plot), 0) <<
" "
307 << get_ele(*(x_dot_plot), 0) <<
" "
308 << ke <<
" " << pe <<
" " << te << std::endl;
315 StepSize.push_back(dt);
316 auto solution = Thyra::createMember(model->get_x_space());
317 Thyra::copy(*(integrator->getX()),solution.ptr());
318 solutions.push_back(solution);
319 auto solutionDot = Thyra::createMember(model->get_x_space());
320 Thyra::copy(*(integrator->getXDot()),solutionDot.ptr());
321 solutionsDot.push_back(solutionDot);
322 if (n == nTimeStepSizes-1) {
323 StepSize.push_back(0.0);
324 auto solutionExact = Thyra::createMember(model->get_x_space());
325 Thyra::copy(*(model->getExactSolution(time).get_x()),solutionExact.ptr());
326 solutions.push_back(solutionExact);
327 auto solutionDotExact = Thyra::createMember(model->get_x_space());
328 Thyra::copy(*(model->getExactSolution(time).get_x_dot()),
329 solutionDotExact.ptr());
330 solutionsDot.push_back(solutionDotExact);
336 double xDotSlope = 0.0;
338 double order = stepper->getOrder();
341 solutions, xErrorNorm, xSlope,
342 solutionsDot, xDotErrorNorm, xDotSlope);
356 std::vector<RCP<Thyra::VectorBase<double>>> solutions;
357 std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
358 std::vector<double> StepSize;
359 std::vector<double> xErrorNorm;
360 std::vector<double> xDotErrorNorm;
361 const int nTimeStepSizes = 7;
366 getParametersFromXmlFile(
"Tempus_HHTAlpha_SinCos_FirstOrder.xml");
373 double k = hom_pl->
get<
double>(
"x coeff k");
374 double m = hom_pl->
get<
double>(
"Mass coeff m");
381 double dt =pl->sublist(
"Default Integrator")
382 .sublist(
"Time Step Control").get<
double>(
"Initial Time Step");
385 for (
int n=0; n<nTimeStepSizes; n++) {
389 std::cout <<
"\n \n time step #" << n <<
" (out of "
390 << nTimeStepSizes-1 <<
"), dt = " << dt <<
"\n";
391 pl->sublist(
"Default Integrator")
392 .sublist(
"Time Step Control").set(
"Initial Time Step", dt);
393 integrator = Tempus::createIntegratorBasic<double>(pl, model);
396 bool integratorStatus = integrator->advanceTime();
400 time = integrator->getTime();
401 double timeFinal =pl->sublist(
"Default Integrator")
402 .sublist(
"Time Step Control").
get<
double>(
"Final Time");
408 model->getExactSolution(time).get_x();
411 if (n == nTimeStepSizes-1) {
413 integrator->getSolutionHistory();
414 writeSolution(
"Tempus_HHTAlpha_SinCos_FirstOrder.dat", solutionHistory);
417 for (
int i=0; i<solutionHistory->getNumStates(); i++) {
418 double time_i = (*solutionHistory)[i]->getTime();
421 model->getExactSolution(time_i).get_x()),
423 model->getExactSolution(time_i).get_x_dot()));
424 state->setTime((*solutionHistory)[i]->getTime());
425 solnHistExact->addState(state);
427 writeSolution(
"Tempus_HHTAlpha_SinCos_FirstOrder-Ref.dat", solnHistExact);
431 std::ofstream ftmp(
"Tempus_HHTAlpha_SinCos_FirstOrder-Energy.dat");
434 for (
int i=0; i<solutionHistory->getNumStates(); i++) {
436 double time_i = solutionState->getTime();
439 x_exact_plot = model->getExactSolution(time_i).get_x();
441 double ke = Thyra::dot(*x_dot_plot, *x_dot_plot);
444 double pe = Thyra::dot(*x_plot, *x_plot);
449 ftmp << time_i <<
" "
450 << get_ele(*(x_plot), 0) <<
" "
451 << get_ele(*(x_exact_plot), 0) <<
" "
452 << get_ele(*(x_dot_plot), 0) <<
" "
453 << ke <<
" " << pe <<
" " << te << std::endl;
460 StepSize.push_back(dt);
461 auto solution = Thyra::createMember(model->get_x_space());
462 Thyra::copy(*(integrator->getX()),solution.ptr());
463 solutions.push_back(solution);
464 auto solutionDot = Thyra::createMember(model->get_x_space());
465 Thyra::copy(*(integrator->getXDot()),solutionDot.ptr());
466 solutionsDot.push_back(solutionDot);
467 if (n == nTimeStepSizes-1) {
468 StepSize.push_back(0.0);
469 auto solutionExact = Thyra::createMember(model->get_x_space());
470 Thyra::copy(*(model->getExactSolution(time).get_x()),solutionExact.ptr());
471 solutions.push_back(solutionExact);
472 auto solutionDotExact = Thyra::createMember(model->get_x_space());
473 Thyra::copy(*(model->getExactSolution(time).get_x_dot()),
474 solutionDotExact.ptr());
475 solutionsDot.push_back(solutionDotExact);
481 double xDotSlope = 0.0;
486 solutions, xErrorNorm, xSlope,
487 solutionsDot, xDotErrorNorm, xDotSlope);
502 std::vector<RCP<Thyra::VectorBase<double>>> solutions;
503 std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
504 std::vector<double> StepSize;
505 std::vector<double> xErrorNorm;
506 std::vector<double> xDotErrorNorm;
507 const int nTimeStepSizes = 7;
512 getParametersFromXmlFile(
"Tempus_HHTAlpha_SinCos_ExplicitCD.xml");
519 double k = hom_pl->
get<
double>(
"x coeff k");
520 double m = hom_pl->
get<
double>(
"Mass coeff m");
527 double dt =pl->sublist(
"Default Integrator")
528 .sublist(
"Time Step Control").get<
double>(
"Initial Time Step");
531 for (
int n=0; n<nTimeStepSizes; n++) {
535 std::cout <<
"\n \n time step #" << n <<
" (out of "
536 << nTimeStepSizes-1 <<
"), dt = " << dt <<
"\n";
537 pl->sublist(
"Default Integrator")
538 .sublist(
"Time Step Control").set(
"Initial Time Step", dt);
539 integrator = Tempus::createIntegratorBasic<double>(pl, model);
542 bool integratorStatus = integrator->advanceTime();
546 time = integrator->getTime();
547 double timeFinal =pl->sublist(
"Default Integrator")
548 .sublist(
"Time Step Control").
get<
double>(
"Final Time");
554 model->getExactSolution(time).get_x();
557 if (n == nTimeStepSizes-1) {
559 integrator->getSolutionHistory();
560 writeSolution(
"Tempus_HHTAlpha_SinCos_ExplicitCD.dat", solutionHistory);
563 for (
int i=0; i<solutionHistory->getNumStates(); i++) {
564 double time_i = (*solutionHistory)[i]->getTime();
567 model->getExactSolution(time_i).get_x()),
569 model->getExactSolution(time_i).get_x_dot()));
570 state->setTime((*solutionHistory)[i]->getTime());
571 solnHistExact->addState(state);
573 writeSolution(
"Tempus_HHTAlpha_SinCos_ExplicitCD-Ref.dat", solnHistExact);
577 std::ofstream ftmp(
"Tempus_HHTAlpha_SinCos_ExplicitCD-Energy.dat");
580 for (
int i=0; i<solutionHistory->getNumStates(); i++) {
582 double time_i = solutionState->getTime();
585 x_exact_plot = model->getExactSolution(time_i).get_x();
587 double ke = Thyra::dot(*x_dot_plot, *x_dot_plot);
590 double pe = Thyra::dot(*x_plot, *x_plot);
595 ftmp << time_i <<
" "
596 << get_ele(*(x_plot), 0) <<
" "
597 << get_ele(*(x_exact_plot), 0) <<
" "
598 << get_ele(*(x_dot_plot), 0) <<
" "
599 << ke <<
" " << pe <<
" " << te << std::endl;
606 StepSize.push_back(dt);
607 auto solution = Thyra::createMember(model->get_x_space());
608 Thyra::copy(*(integrator->getX()),solution.ptr());
609 solutions.push_back(solution);
610 auto solutionDot = Thyra::createMember(model->get_x_space());
611 Thyra::copy(*(integrator->getXDot()),solutionDot.ptr());
612 solutionsDot.push_back(solutionDot);
613 if (n == nTimeStepSizes-1) {
614 StepSize.push_back(0.0);
615 auto solutionExact = Thyra::createMember(model->get_x_space());
616 Thyra::copy(*(model->getExactSolution(time).get_x()),solutionExact.ptr());
617 solutions.push_back(solutionExact);
618 auto solutionDotExact = Thyra::createMember(model->get_x_space());
619 Thyra::copy(*(model->getExactSolution(time).get_x_dot()),
620 solutionDotExact.ptr());
621 solutionsDot.push_back(solutionDotExact);
627 double xDotSlope = 0.0;
629 double order = stepper->getOrder();
632 solutions, xErrorNorm, xSlope,
633 solutionsDot, xDotErrorNorm, xDotSlope);
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)
#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...
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)
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. SolutionState contains the metadata for solutions and th...