9 #include "Teuchos_UnitTestHarness.hpp"
10 #include "Teuchos_XMLParameterListHelpers.hpp"
11 #include "Teuchos_TimeMonitor.hpp"
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"
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;
42 using Teuchos::ParameterList;
43 using Teuchos::sublist;
44 using Teuchos::getParametersFromXmlFile;
51 #define TEST_BALL_PARABOLIC
52 #define TEST_CONSTRUCTING_FROM_DEFAULTS
53 #define TEST_SINCOS_SECONDORDER
54 #define TEST_SINCOS_FIRSTORDER
55 #define TEST_SINCOS_CD
58 #ifdef TEST_BALL_PARABOLIC
64 double tolerance = 1.0e-14;
66 RCP<ParameterList> pList =
67 getParametersFromXmlFile(
"Tempus_HHTAlpha_BallParabolic.xml");
70 RCP<ParameterList> hom_pl = sublist(pList,
"HarmonicOscillatorModel",
true);
74 RCP<ParameterList> pl = sublist(pList,
"Tempus",
true);
76 RCP<Tempus::IntegratorBasic<double> > integrator =
77 Tempus::integratorBasic<double>(pl, model);
80 bool integratorStatus = integrator->advanceTime();
81 TEST_ASSERT(integratorStatus)
84 double time = integrator->getTime();
85 double timeFinal =pl->sublist(
"Default Integrator")
86 .sublist(
"Time Step Control").get<
double>(
"Final Time");
87 TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
90 RCP<Thyra::VectorBase<double> > x = integrator->getX();
91 RCP<const Thyra::VectorBase<double> > x_exact =
92 model->getExactSolution(time).get_x();
95 std::ofstream ftmp(
"Tempus_HHTAlpha_BallParabolic.dat");
98 integrator->getSolutionHistory();
101 RCP<const Thyra::VectorBase<double> > x_exact_plot;
102 for (
int i=0; i<solutionHistory->getNumStates(); i++) {
103 RCP<const SolutionState<double> > solutionState = (*solutionHistory)[i];
104 double time_i = solutionState->getTime();
105 RCP<const Thyra::VectorBase<double> > x_plot = solutionState->getX();
106 x_exact_plot = model->getExactSolution(time_i).get_x();
107 ftmp << time_i <<
" "
108 << get_ele(*(x_plot), 0) <<
" "
109 << get_ele(*(x_exact_plot), 0) << std::endl;
110 if (abs(get_ele(*(x_plot),0) - get_ele(*(x_exact_plot), 0)) > err)
111 err = abs(get_ele(*(x_plot),0) - get_ele(*(x_exact_plot), 0));
114 std::cout <<
"Max error = " << err <<
"\n \n";
118 TEUCHOS_TEST_FOR_EXCEPTION(!passed, std::logic_error,
119 "\n Test failed! Max error = " << err <<
" > tolerance = " << tolerance <<
"\n!");
122 #endif // TEST_BALL_PARABOLIC
125 #ifdef TEST_CONSTRUCTING_FROM_DEFAULTS
133 RCP<ParameterList> pList =
134 getParametersFromXmlFile(
"Tempus_HHTAlpha_SinCos_SecondOrder.xml");
135 RCP<ParameterList> pl = sublist(pList,
"Tempus",
true);
138 RCP<ParameterList> hom_pl = sublist(pList,
"HarmonicOscillatorModel",
true);
143 stepper->setModel(model);
144 stepper->setSolver();
145 stepper->initialize();
149 ParameterList tscPL = pl->sublist(
"Default Integrator")
150 .sublist(
"Time Step Control");
151 timeStepControl->setStepType (tscPL.get<std::string>(
"Integrator Step Type"));
152 timeStepControl->setInitIndex(tscPL.get<
int> (
"Initial Time Index"));
153 timeStepControl->setInitTime (tscPL.get<
double>(
"Initial Time"));
154 timeStepControl->setFinalTime(tscPL.get<
double>(
"Final Time"));
155 timeStepControl->setInitTimeStep(dt);
156 timeStepControl->initialize();
159 Thyra::ModelEvaluatorBase::InArgs<double> inArgsIC =
160 stepper->getModel()->getNominalValues();
161 auto icX = rcp_const_cast<Thyra::VectorBase<double> > (inArgsIC.get_x());
162 auto icXDot = rcp_const_cast<Thyra::VectorBase<double> > (inArgsIC.get_x_dot());
163 auto icXDotDot = rcp_const_cast<Thyra::VectorBase<double> > (inArgsIC.get_x_dot_dot());
165 icState->setTime (timeStepControl->getInitTime());
166 icState->setIndex (timeStepControl->getInitIndex());
167 icState->setTimeStep(0.0);
168 icState->setOrder (stepper->getOrder());
179 RCP<Tempus::IntegratorBasic<double> > integrator =
180 Tempus::integratorBasic<double>();
181 integrator->setStepperWStepper(stepper);
182 integrator->setTimeStepControl(timeStepControl);
185 integrator->initialize();
189 bool integratorStatus = integrator->advanceTime();
190 TEST_ASSERT(integratorStatus)
194 double time = integrator->getTime();
195 double timeFinal =pl->sublist(
"Default Integrator")
196 .sublist(
"Time Step Control").get<
double>(
"Final Time");
197 TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
200 RCP<Thyra::VectorBase<double> > x = integrator->getX();
201 RCP<const Thyra::VectorBase<double> > x_exact =
202 model->getExactSolution(time).get_x();
205 RCP<Thyra::VectorBase<double> > xdiff = x->clone_v();
206 Thyra::V_StVpStV(xdiff.ptr(), 1.0, *x_exact, -1.0, *(x));
209 std::cout <<
" Stepper = " << stepper->description() << std::endl;
210 std::cout <<
" =========================" << std::endl;
211 std::cout <<
" Exact solution : " << get_ele(*(x_exact), 0) << std::endl;
212 std::cout <<
" Computed solution: " << get_ele(*(x ), 0) << std::endl;
213 std::cout <<
" Difference : " << get_ele(*(xdiff ), 0) << std::endl;
214 std::cout <<
" =========================" << std::endl;
215 TEST_FLOATING_EQUALITY(get_ele(*(x), 0), 0.144918, 1.0e-4 );
217 #endif // TEST_CONSTRUCTING_FROM_DEFAULTS
220 #ifdef TEST_SINCOS_SECONDORDER
224 RCP<Tempus::IntegratorBasic<double> > integrator;
225 std::vector<RCP<Thyra::VectorBase<double>>> solutions;
226 std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
227 std::vector<double> StepSize;
228 std::vector<double> xErrorNorm;
229 std::vector<double> xDotErrorNorm;
230 const int nTimeStepSizes = 8;
234 RCP<ParameterList> pList =
235 getParametersFromXmlFile(
"Tempus_HHTAlpha_SinCos_SecondOrder.xml");
238 RCP<ParameterList> hom_pl = sublist(pList,
"HarmonicOscillatorModel",
true);
242 double k = hom_pl->get<
double>(
"x coeff k");
243 double m = hom_pl->get<
double>(
"Mass coeff m");
246 RCP<ParameterList> pl = sublist(pList,
"Tempus",
true);
250 double dt =pl->sublist(
"Default Integrator")
251 .sublist(
"Time Step Control").get<
double>(
"Initial Time Step");
254 for (
int n=0; n<nTimeStepSizes; n++) {
258 std::cout <<
"\n \n time step #" << n <<
" (out of "
259 << nTimeStepSizes-1 <<
"), dt = " << dt <<
"\n";
260 pl->sublist(
"Default Integrator")
261 .sublist(
"Time Step Control").set(
"Initial Time Step", dt);
262 integrator = Tempus::integratorBasic<double>(pl, model);
265 bool integratorStatus = integrator->advanceTime();
266 TEST_ASSERT(integratorStatus)
269 time = integrator->getTime();
270 double timeFinal =pl->sublist(
"Default Integrator")
271 .sublist(
"Time Step Control").get<
double>(
"Final Time");
272 TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
275 RCP<Thyra::VectorBase<double> > x = integrator->getX();
276 RCP<const Thyra::VectorBase<double> > x_exact =
277 model->getExactSolution(time).get_x();
280 if (n == nTimeStepSizes-1) {
282 integrator->getSolutionHistory();
283 writeSolution(
"Tempus_HHTAlpha_SinCos_SecondOrder.dat", solutionHistory);
286 for (
int i=0; i<solutionHistory->getNumStates(); i++) {
287 double time_i = (*solutionHistory)[i]->getTime();
289 model->getExactSolution(time_i).get_x(),
290 model->getExactSolution(time_i).get_x_dot()));
291 state->setTime((*solutionHistory)[i]->getTime());
292 solnHistExact->addState(state);
294 writeSolution(
"Tempus_HHTAlpha_SinCos_SecondOrder-Ref.dat", solnHistExact);
298 std::ofstream ftmp(
"Tempus_HHTAlpha_SinCos_SecondOrder-Energy.dat");
300 RCP<const Thyra::VectorBase<double> > x_exact_plot;
301 for (
int i=0; i<solutionHistory->getNumStates(); i++) {
302 RCP<const SolutionState<double> > solutionState = (*solutionHistory)[i];
303 double time_i = solutionState->getTime();
304 RCP<const Thyra::VectorBase<double> > x_plot = solutionState->getX();
305 RCP<const Thyra::VectorBase<double> > x_dot_plot = solutionState->getXDot();
306 x_exact_plot = model->getExactSolution(time_i).get_x();
308 double ke = Thyra::dot(*x_dot_plot, *x_dot_plot);
311 double pe = Thyra::dot(*x_plot, *x_plot);
316 ftmp << time_i <<
" "
317 << get_ele(*(x_plot), 0) <<
" "
318 << get_ele(*(x_exact_plot), 0) <<
" "
319 << get_ele(*(x_dot_plot), 0) <<
" "
320 << ke <<
" " << pe <<
" " << te << std::endl;
327 StepSize.push_back(dt);
328 auto solution = Thyra::createMember(model->get_x_space());
329 Thyra::copy(*(integrator->getX()),solution.ptr());
330 solutions.push_back(solution);
331 auto solutionDot = Thyra::createMember(model->get_x_space());
332 Thyra::copy(*(integrator->getXdot()),solutionDot.ptr());
333 solutionsDot.push_back(solutionDot);
334 if (n == nTimeStepSizes-1) {
335 StepSize.push_back(0.0);
336 auto solutionExact = Thyra::createMember(model->get_x_space());
337 Thyra::copy(*(model->getExactSolution(time).get_x()),solutionExact.ptr());
338 solutions.push_back(solutionExact);
339 auto solutionDotExact = Thyra::createMember(model->get_x_space());
340 Thyra::copy(*(model->getExactSolution(time).get_x_dot()),
341 solutionDotExact.ptr());
342 solutionsDot.push_back(solutionDotExact);
348 double xDotSlope = 0.0;
349 RCP<Tempus::Stepper<double> > stepper = integrator->getStepper();
350 double order = stepper->getOrder();
353 solutions, xErrorNorm, xSlope,
354 solutionsDot, xDotErrorNorm, xDotSlope);
356 TEST_FLOATING_EQUALITY( xSlope, order, 0.02 );
357 TEST_FLOATING_EQUALITY( xErrorNorm[0], 0.00644755, 1.0e-4 );
358 TEST_FLOATING_EQUALITY( xDotSlope, order, 0.01 );
359 TEST_FLOATING_EQUALITY( xDotErrorNorm[0], 0.104392, 1.0e-4 );
361 #endif // TEST_SINCOS_SECONDORDER
364 #ifdef TEST_SINCOS_FIRSTORDER
369 RCP<Tempus::IntegratorBasic<double> > integrator;
370 std::vector<RCP<Thyra::VectorBase<double>>> solutions;
371 std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
372 std::vector<double> StepSize;
373 std::vector<double> xErrorNorm;
374 std::vector<double> xDotErrorNorm;
375 const int nTimeStepSizes = 8;
379 RCP<ParameterList> pList =
380 getParametersFromXmlFile(
"Tempus_HHTAlpha_SinCos_FirstOrder.xml");
383 RCP<ParameterList> hom_pl = sublist(pList,
"HarmonicOscillatorModel",
true);
387 double k = hom_pl->get<
double>(
"x coeff k");
388 double m = hom_pl->get<
double>(
"Mass coeff m");
391 RCP<ParameterList> pl = sublist(pList,
"Tempus",
true);
395 double dt =pl->sublist(
"Default Integrator")
396 .sublist(
"Time Step Control").get<
double>(
"Initial Time Step");
399 for (
int n=0; n<nTimeStepSizes; n++) {
403 std::cout <<
"\n \n time step #" << n <<
" (out of "
404 << nTimeStepSizes-1 <<
"), dt = " << dt <<
"\n";
405 pl->sublist(
"Default Integrator")
406 .sublist(
"Time Step Control").set(
"Initial Time Step", dt);
407 integrator = Tempus::integratorBasic<double>(pl, model);
410 bool integratorStatus = integrator->advanceTime();
411 TEST_ASSERT(integratorStatus)
414 time = integrator->getTime();
415 double timeFinal =pl->sublist(
"Default Integrator")
416 .sublist(
"Time Step Control").get<
double>(
"Final Time");
417 TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
420 RCP<Thyra::VectorBase<double> > x = integrator->getX();
421 RCP<const Thyra::VectorBase<double> > x_exact =
422 model->getExactSolution(time).get_x();
425 if (n == nTimeStepSizes-1) {
427 integrator->getSolutionHistory();
428 writeSolution(
"Tempus_HHTAlpha_SinCos_FirstOrder.dat", solutionHistory);
431 for (
int i=0; i<solutionHistory->getNumStates(); i++) {
432 double time_i = (*solutionHistory)[i]->getTime();
434 model->getExactSolution(time_i).get_x(),
435 model->getExactSolution(time_i).get_x_dot()));
436 state->setTime((*solutionHistory)[i]->getTime());
437 solnHistExact->addState(state);
439 writeSolution(
"Tempus_HHTAlpha_SinCos_FirstOrder-Ref.dat", solnHistExact);
443 std::ofstream ftmp(
"Tempus_HHTAlpha_SinCos_FirstOrder-Energy.dat");
445 RCP<const Thyra::VectorBase<double> > x_exact_plot;
446 for (
int i=0; i<solutionHistory->getNumStates(); i++) {
447 RCP<const SolutionState<double> > solutionState = (*solutionHistory)[i];
448 double time_i = solutionState->getTime();
449 RCP<const Thyra::VectorBase<double> > x_plot = solutionState->getX();
450 RCP<const Thyra::VectorBase<double> > x_dot_plot = solutionState->getXDot();
451 x_exact_plot = model->getExactSolution(time_i).get_x();
453 double ke = Thyra::dot(*x_dot_plot, *x_dot_plot);
456 double pe = Thyra::dot(*x_plot, *x_plot);
461 ftmp << time_i <<
" "
462 << get_ele(*(x_plot), 0) <<
" "
463 << get_ele(*(x_exact_plot), 0) <<
" "
464 << get_ele(*(x_dot_plot), 0) <<
" "
465 << ke <<
" " << pe <<
" " << te << std::endl;
472 StepSize.push_back(dt);
473 auto solution = Thyra::createMember(model->get_x_space());
474 Thyra::copy(*(integrator->getX()),solution.ptr());
475 solutions.push_back(solution);
476 auto solutionDot = Thyra::createMember(model->get_x_space());
477 Thyra::copy(*(integrator->getXdot()),solutionDot.ptr());
478 solutionsDot.push_back(solutionDot);
479 if (n == nTimeStepSizes-1) {
480 StepSize.push_back(0.0);
481 auto solutionExact = Thyra::createMember(model->get_x_space());
482 Thyra::copy(*(model->getExactSolution(time).get_x()),solutionExact.ptr());
483 solutions.push_back(solutionExact);
484 auto solutionDotExact = Thyra::createMember(model->get_x_space());
485 Thyra::copy(*(model->getExactSolution(time).get_x_dot()),
486 solutionDotExact.ptr());
487 solutionsDot.push_back(solutionDotExact);
493 double xDotSlope = 0.0;
494 RCP<Tempus::Stepper<double> > stepper = integrator->getStepper();
495 double order = stepper->getOrder();
498 solutions, xErrorNorm, xSlope,
499 solutionsDot, xDotErrorNorm, xDotSlope);
501 TEST_FLOATING_EQUALITY( xSlope, order, 0.02 );
502 TEST_FLOATING_EQUALITY( xErrorNorm[0], 0.048932, 1.0e-4 );
503 TEST_FLOATING_EQUALITY( xDotSlope, 1.18873, 0.01 );
504 TEST_FLOATING_EQUALITY( xDotErrorNorm[0], 0.393504, 1.0e-4 );
507 #endif // TEST_SINCOS_FIRSTORDER
510 #ifdef TEST_SINCOS_CD
515 RCP<Tempus::IntegratorBasic<double> > integrator;
516 std::vector<RCP<Thyra::VectorBase<double>>> solutions;
517 std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
518 std::vector<double> StepSize;
519 std::vector<double> xErrorNorm;
520 std::vector<double> xDotErrorNorm;
521 const int nTimeStepSizes = 8;
525 RCP<ParameterList> pList =
526 getParametersFromXmlFile(
"Tempus_HHTAlpha_SinCos_ExplicitCD.xml");
529 RCP<ParameterList> hom_pl = sublist(pList,
"HarmonicOscillatorModel",
true);
533 double k = hom_pl->get<
double>(
"x coeff k");
534 double m = hom_pl->get<
double>(
"Mass coeff m");
537 RCP<ParameterList> pl = sublist(pList,
"Tempus",
true);
541 double dt =pl->sublist(
"Default Integrator")
542 .sublist(
"Time Step Control").get<
double>(
"Initial Time Step");
545 for (
int n=0; n<nTimeStepSizes; n++) {
549 std::cout <<
"\n \n time step #" << n <<
" (out of "
550 << nTimeStepSizes-1 <<
"), dt = " << dt <<
"\n";
551 pl->sublist(
"Default Integrator")
552 .sublist(
"Time Step Control").set(
"Initial Time Step", dt);
553 integrator = Tempus::integratorBasic<double>(pl, model);
556 bool integratorStatus = integrator->advanceTime();
557 TEST_ASSERT(integratorStatus)
560 time = integrator->getTime();
561 double timeFinal =pl->sublist(
"Default Integrator")
562 .sublist(
"Time Step Control").get<
double>(
"Final Time");
563 TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
566 RCP<Thyra::VectorBase<double> > x = integrator->getX();
567 RCP<const Thyra::VectorBase<double> > x_exact =
568 model->getExactSolution(time).get_x();
571 if (n == nTimeStepSizes-1) {
573 integrator->getSolutionHistory();
574 writeSolution(
"Tempus_HHTAlpha_SinCos_ExplicitCD.dat", solutionHistory);
577 for (
int i=0; i<solutionHistory->getNumStates(); i++) {
578 double time_i = (*solutionHistory)[i]->getTime();
580 model->getExactSolution(time_i).get_x(),
581 model->getExactSolution(time_i).get_x_dot()));
582 state->setTime((*solutionHistory)[i]->getTime());
583 solnHistExact->addState(state);
585 writeSolution(
"Tempus_HHTAlpha_SinCos_ExplicitCD-Ref.dat", solnHistExact);
589 std::ofstream ftmp(
"Tempus_HHTAlpha_SinCos_ExplicitCD-Energy.dat");
591 RCP<const Thyra::VectorBase<double> > x_exact_plot;
592 for (
int i=0; i<solutionHistory->getNumStates(); i++) {
593 RCP<const SolutionState<double> > solutionState = (*solutionHistory)[i];
594 double time_i = solutionState->getTime();
595 RCP<const Thyra::VectorBase<double> > x_plot = solutionState->getX();
596 RCP<const Thyra::VectorBase<double> > x_dot_plot = solutionState->getXDot();
597 x_exact_plot = model->getExactSolution(time_i).get_x();
599 double ke = Thyra::dot(*x_dot_plot, *x_dot_plot);
602 double pe = Thyra::dot(*x_plot, *x_plot);
607 ftmp << time_i <<
" "
608 << get_ele(*(x_plot), 0) <<
" "
609 << get_ele(*(x_exact_plot), 0) <<
" "
610 << get_ele(*(x_dot_plot), 0) <<
" "
611 << ke <<
" " << pe <<
" " << te << std::endl;
618 StepSize.push_back(dt);
619 auto solution = Thyra::createMember(model->get_x_space());
620 Thyra::copy(*(integrator->getX()),solution.ptr());
621 solutions.push_back(solution);
622 auto solutionDot = Thyra::createMember(model->get_x_space());
623 Thyra::copy(*(integrator->getXdot()),solutionDot.ptr());
624 solutionsDot.push_back(solutionDot);
625 if (n == nTimeStepSizes-1) {
626 StepSize.push_back(0.0);
627 auto solutionExact = Thyra::createMember(model->get_x_space());
628 Thyra::copy(*(model->getExactSolution(time).get_x()),solutionExact.ptr());
629 solutions.push_back(solutionExact);
630 auto solutionDotExact = Thyra::createMember(model->get_x_space());
631 Thyra::copy(*(model->getExactSolution(time).get_x_dot()),
632 solutionDotExact.ptr());
633 solutionsDot.push_back(solutionDotExact);
639 double xDotSlope = 0.0;
640 RCP<Tempus::Stepper<double> > stepper = integrator->getStepper();
641 double order = stepper->getOrder();
644 solutions, xErrorNorm, xSlope,
645 solutionsDot, xDotErrorNorm, xDotSlope);
647 TEST_FLOATING_EQUALITY( xSlope, order, 0.02 );
648 TEST_FLOATING_EQUALITY( xErrorNorm[0], 0.00451069, 1.0e-4 );
649 TEST_FLOATING_EQUALITY( xDotSlope, order, 0.01 );
650 TEST_FLOATING_EQUALITY( xDotErrorNorm[0], 0.0551522, 1.0e-4 );
652 #endif // TEST_SINCOS_CD
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)
TimeStepControl manages the time step size. There several mechanicisms that effect the time step size...
Teuchos::RCP< SolutionHistory< Scalar > > solutionHistory(Teuchos::RCP< Teuchos::ParameterList > pList=Teuchos::null)
Nonmember constructor.
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
Keep a fix number of states.
Solution state for integrators and steppers. SolutionState contains the metadata for solutions and th...