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->initialize();
148 ParameterList tscPL = pl->sublist(
"Default Integrator")
149 .sublist(
"Time Step Control");
150 timeStepControl->setStepType (tscPL.get<std::string>(
"Integrator Step Type"));
151 timeStepControl->setInitIndex(tscPL.get<
int> (
"Initial Time Index"));
152 timeStepControl->setInitTime (tscPL.get<
double>(
"Initial Time"));
153 timeStepControl->setFinalTime(tscPL.get<
double>(
"Final Time"));
154 timeStepControl->setInitTimeStep(dt);
155 timeStepControl->initialize();
158 Thyra::ModelEvaluatorBase::InArgs<double> inArgsIC =
159 stepper->getModel()->getNominalValues();
160 auto icX = rcp_const_cast<Thyra::VectorBase<double> > (inArgsIC.get_x());
161 auto icXDot = rcp_const_cast<Thyra::VectorBase<double> > (inArgsIC.get_x_dot());
162 auto icXDotDot = rcp_const_cast<Thyra::VectorBase<double> > (inArgsIC.get_x_dot_dot());
164 icState->setTime (timeStepControl->getInitTime());
165 icState->setIndex (timeStepControl->getInitIndex());
166 icState->setTimeStep(0.0);
167 icState->setOrder (stepper->getOrder());
178 RCP<Tempus::IntegratorBasic<double> > integrator =
179 Tempus::integratorBasic<double>();
180 integrator->setStepperWStepper(stepper);
181 integrator->setTimeStepControl(timeStepControl);
184 integrator->initialize();
188 bool integratorStatus = integrator->advanceTime();
189 TEST_ASSERT(integratorStatus)
193 double time = integrator->getTime();
194 double timeFinal =pl->sublist(
"Default Integrator")
195 .sublist(
"Time Step Control").get<
double>(
"Final Time");
196 TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
199 RCP<Thyra::VectorBase<double> > x = integrator->getX();
200 RCP<const Thyra::VectorBase<double> > x_exact =
201 model->getExactSolution(time).get_x();
204 RCP<Thyra::VectorBase<double> > xdiff = x->clone_v();
205 Thyra::V_StVpStV(xdiff.ptr(), 1.0, *x_exact, -1.0, *(x));
208 std::cout <<
" Stepper = " << stepper->description() << std::endl;
209 std::cout <<
" =========================" << std::endl;
210 std::cout <<
" Exact solution : " << get_ele(*(x_exact), 0) << std::endl;
211 std::cout <<
" Computed solution: " << get_ele(*(x ), 0) << std::endl;
212 std::cout <<
" Difference : " << get_ele(*(xdiff ), 0) << std::endl;
213 std::cout <<
" =========================" << std::endl;
214 TEST_FLOATING_EQUALITY(get_ele(*(x), 0), 0.144918, 1.0e-4 );
216 #endif // TEST_CONSTRUCTING_FROM_DEFAULTS
219 #ifdef TEST_SINCOS_SECONDORDER
223 RCP<Tempus::IntegratorBasic<double> > integrator;
224 std::vector<RCP<Thyra::VectorBase<double>>> solutions;
225 std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
226 std::vector<double> StepSize;
227 std::vector<double> xErrorNorm;
228 std::vector<double> xDotErrorNorm;
229 const int nTimeStepSizes = 8;
233 RCP<ParameterList> pList =
234 getParametersFromXmlFile(
"Tempus_HHTAlpha_SinCos_SecondOrder.xml");
237 RCP<ParameterList> hom_pl = sublist(pList,
"HarmonicOscillatorModel",
true);
241 double k = hom_pl->get<
double>(
"x coeff k");
242 double m = hom_pl->get<
double>(
"Mass coeff m");
245 RCP<ParameterList> pl = sublist(pList,
"Tempus",
true);
249 double dt =pl->sublist(
"Default Integrator")
250 .sublist(
"Time Step Control").get<
double>(
"Initial Time Step");
253 for (
int n=0; n<nTimeStepSizes; n++) {
257 std::cout <<
"\n \n time step #" << n <<
" (out of "
258 << nTimeStepSizes-1 <<
"), dt = " << dt <<
"\n";
259 pl->sublist(
"Default Integrator")
260 .sublist(
"Time Step Control").set(
"Initial Time Step", dt);
261 integrator = Tempus::integratorBasic<double>(pl, model);
264 bool integratorStatus = integrator->advanceTime();
265 TEST_ASSERT(integratorStatus)
268 time = integrator->getTime();
269 double timeFinal =pl->sublist(
"Default Integrator")
270 .sublist(
"Time Step Control").get<
double>(
"Final Time");
271 TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
274 RCP<Thyra::VectorBase<double> > x = integrator->getX();
275 RCP<const Thyra::VectorBase<double> > x_exact =
276 model->getExactSolution(time).get_x();
279 if (n == nTimeStepSizes-1) {
281 integrator->getSolutionHistory();
282 writeSolution(
"Tempus_HHTAlpha_SinCos_SecondOrder.dat", solutionHistory);
285 for (
int i=0; i<solutionHistory->getNumStates(); i++) {
286 double time_i = (*solutionHistory)[i]->getTime();
288 model->getExactSolution(time_i).get_x(),
289 model->getExactSolution(time_i).get_x_dot()));
290 state->setTime((*solutionHistory)[i]->getTime());
291 solnHistExact->addState(state);
293 writeSolution(
"Tempus_HHTAlpha_SinCos_SecondOrder-Ref.dat", solnHistExact);
297 std::ofstream ftmp(
"Tempus_HHTAlpha_SinCos_SecondOrder-Energy.dat");
299 RCP<const Thyra::VectorBase<double> > x_exact_plot;
300 for (
int i=0; i<solutionHistory->getNumStates(); i++) {
301 RCP<const SolutionState<double> > solutionState = (*solutionHistory)[i];
302 double time_i = solutionState->getTime();
303 RCP<const Thyra::VectorBase<double> > x_plot = solutionState->getX();
304 RCP<const Thyra::VectorBase<double> > x_dot_plot = solutionState->getXDot();
305 x_exact_plot = model->getExactSolution(time_i).get_x();
307 double ke = Thyra::dot(*x_dot_plot, *x_dot_plot);
310 double pe = Thyra::dot(*x_plot, *x_plot);
315 ftmp << time_i <<
" "
316 << get_ele(*(x_plot), 0) <<
" "
317 << get_ele(*(x_exact_plot), 0) <<
" "
318 << get_ele(*(x_dot_plot), 0) <<
" "
319 << ke <<
" " << pe <<
" " << te << std::endl;
326 StepSize.push_back(dt);
327 auto solution = Thyra::createMember(model->get_x_space());
328 Thyra::copy(*(integrator->getX()),solution.ptr());
329 solutions.push_back(solution);
330 auto solutionDot = Thyra::createMember(model->get_x_space());
331 Thyra::copy(*(integrator->getXdot()),solutionDot.ptr());
332 solutionsDot.push_back(solutionDot);
333 if (n == nTimeStepSizes-1) {
334 StepSize.push_back(0.0);
335 auto solutionExact = Thyra::createMember(model->get_x_space());
336 Thyra::copy(*(model->getExactSolution(time).get_x()),solutionExact.ptr());
337 solutions.push_back(solutionExact);
338 auto solutionDotExact = Thyra::createMember(model->get_x_space());
339 Thyra::copy(*(model->getExactSolution(time).get_x_dot()),
340 solutionDotExact.ptr());
341 solutionsDot.push_back(solutionDotExact);
347 double xDotSlope = 0.0;
348 RCP<Tempus::Stepper<double> > stepper = integrator->getStepper();
349 double order = stepper->getOrder();
352 solutions, xErrorNorm, xSlope,
353 solutionsDot, xDotErrorNorm, xDotSlope);
355 TEST_FLOATING_EQUALITY( xSlope, order, 0.02 );
356 TEST_FLOATING_EQUALITY( xErrorNorm[0], 0.00644755, 1.0e-4 );
357 TEST_FLOATING_EQUALITY( xDotSlope, order, 0.01 );
358 TEST_FLOATING_EQUALITY( xDotErrorNorm[0], 0.104392, 1.0e-4 );
360 #endif // TEST_SINCOS_SECONDORDER
363 #ifdef TEST_SINCOS_FIRSTORDER
368 RCP<Tempus::IntegratorBasic<double> > integrator;
369 std::vector<RCP<Thyra::VectorBase<double>>> solutions;
370 std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
371 std::vector<double> StepSize;
372 std::vector<double> xErrorNorm;
373 std::vector<double> xDotErrorNorm;
374 const int nTimeStepSizes = 8;
378 RCP<ParameterList> pList =
379 getParametersFromXmlFile(
"Tempus_HHTAlpha_SinCos_FirstOrder.xml");
382 RCP<ParameterList> hom_pl = sublist(pList,
"HarmonicOscillatorModel",
true);
386 double k = hom_pl->get<
double>(
"x coeff k");
387 double m = hom_pl->get<
double>(
"Mass coeff m");
390 RCP<ParameterList> pl = sublist(pList,
"Tempus",
true);
394 double dt =pl->sublist(
"Default Integrator")
395 .sublist(
"Time Step Control").get<
double>(
"Initial Time Step");
398 for (
int n=0; n<nTimeStepSizes; n++) {
402 std::cout <<
"\n \n time step #" << n <<
" (out of "
403 << nTimeStepSizes-1 <<
"), dt = " << dt <<
"\n";
404 pl->sublist(
"Default Integrator")
405 .sublist(
"Time Step Control").set(
"Initial Time Step", dt);
406 integrator = Tempus::integratorBasic<double>(pl, model);
409 bool integratorStatus = integrator->advanceTime();
410 TEST_ASSERT(integratorStatus)
413 time = integrator->getTime();
414 double timeFinal =pl->sublist(
"Default Integrator")
415 .sublist(
"Time Step Control").get<
double>(
"Final Time");
416 TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
419 RCP<Thyra::VectorBase<double> > x = integrator->getX();
420 RCP<const Thyra::VectorBase<double> > x_exact =
421 model->getExactSolution(time).get_x();
424 if (n == nTimeStepSizes-1) {
426 integrator->getSolutionHistory();
427 writeSolution(
"Tempus_HHTAlpha_SinCos_FirstOrder.dat", solutionHistory);
430 for (
int i=0; i<solutionHistory->getNumStates(); i++) {
431 double time_i = (*solutionHistory)[i]->getTime();
433 model->getExactSolution(time_i).get_x(),
434 model->getExactSolution(time_i).get_x_dot()));
435 state->setTime((*solutionHistory)[i]->getTime());
436 solnHistExact->addState(state);
438 writeSolution(
"Tempus_HHTAlpha_SinCos_FirstOrder-Ref.dat", solnHistExact);
442 std::ofstream ftmp(
"Tempus_HHTAlpha_SinCos_FirstOrder-Energy.dat");
444 RCP<const Thyra::VectorBase<double> > x_exact_plot;
445 for (
int i=0; i<solutionHistory->getNumStates(); i++) {
446 RCP<const SolutionState<double> > solutionState = (*solutionHistory)[i];
447 double time_i = solutionState->getTime();
448 RCP<const Thyra::VectorBase<double> > x_plot = solutionState->getX();
449 RCP<const Thyra::VectorBase<double> > x_dot_plot = solutionState->getXDot();
450 x_exact_plot = model->getExactSolution(time_i).get_x();
452 double ke = Thyra::dot(*x_dot_plot, *x_dot_plot);
455 double pe = Thyra::dot(*x_plot, *x_plot);
460 ftmp << time_i <<
" "
461 << get_ele(*(x_plot), 0) <<
" "
462 << get_ele(*(x_exact_plot), 0) <<
" "
463 << get_ele(*(x_dot_plot), 0) <<
" "
464 << ke <<
" " << pe <<
" " << te << std::endl;
471 StepSize.push_back(dt);
472 auto solution = Thyra::createMember(model->get_x_space());
473 Thyra::copy(*(integrator->getX()),solution.ptr());
474 solutions.push_back(solution);
475 auto solutionDot = Thyra::createMember(model->get_x_space());
476 Thyra::copy(*(integrator->getXdot()),solutionDot.ptr());
477 solutionsDot.push_back(solutionDot);
478 if (n == nTimeStepSizes-1) {
479 StepSize.push_back(0.0);
480 auto solutionExact = Thyra::createMember(model->get_x_space());
481 Thyra::copy(*(model->getExactSolution(time).get_x()),solutionExact.ptr());
482 solutions.push_back(solutionExact);
483 auto solutionDotExact = Thyra::createMember(model->get_x_space());
484 Thyra::copy(*(model->getExactSolution(time).get_x_dot()),
485 solutionDotExact.ptr());
486 solutionsDot.push_back(solutionDotExact);
492 double xDotSlope = 0.0;
493 RCP<Tempus::Stepper<double> > stepper = integrator->getStepper();
494 double order = stepper->getOrder();
497 solutions, xErrorNorm, xSlope,
498 solutionsDot, xDotErrorNorm, xDotSlope);
500 TEST_FLOATING_EQUALITY( xSlope, order, 0.02 );
501 TEST_FLOATING_EQUALITY( xErrorNorm[0], 0.048932, 1.0e-4 );
502 TEST_FLOATING_EQUALITY( xDotSlope, 1.18873, 0.01 );
503 TEST_FLOATING_EQUALITY( xDotErrorNorm[0], 0.393504, 1.0e-4 );
506 #endif // TEST_SINCOS_FIRSTORDER
509 #ifdef TEST_SINCOS_CD
514 RCP<Tempus::IntegratorBasic<double> > integrator;
515 std::vector<RCP<Thyra::VectorBase<double>>> solutions;
516 std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
517 std::vector<double> StepSize;
518 std::vector<double> xErrorNorm;
519 std::vector<double> xDotErrorNorm;
520 const int nTimeStepSizes = 8;
524 RCP<ParameterList> pList =
525 getParametersFromXmlFile(
"Tempus_HHTAlpha_SinCos_ExplicitCD.xml");
528 RCP<ParameterList> hom_pl = sublist(pList,
"HarmonicOscillatorModel",
true);
532 double k = hom_pl->get<
double>(
"x coeff k");
533 double m = hom_pl->get<
double>(
"Mass coeff m");
536 RCP<ParameterList> pl = sublist(pList,
"Tempus",
true);
540 double dt =pl->sublist(
"Default Integrator")
541 .sublist(
"Time Step Control").get<
double>(
"Initial Time Step");
544 for (
int n=0; n<nTimeStepSizes; n++) {
548 std::cout <<
"\n \n time step #" << n <<
" (out of "
549 << nTimeStepSizes-1 <<
"), dt = " << dt <<
"\n";
550 pl->sublist(
"Default Integrator")
551 .sublist(
"Time Step Control").set(
"Initial Time Step", dt);
552 integrator = Tempus::integratorBasic<double>(pl, model);
555 bool integratorStatus = integrator->advanceTime();
556 TEST_ASSERT(integratorStatus)
559 time = integrator->getTime();
560 double timeFinal =pl->sublist(
"Default Integrator")
561 .sublist(
"Time Step Control").get<
double>(
"Final Time");
562 TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
565 RCP<Thyra::VectorBase<double> > x = integrator->getX();
566 RCP<const Thyra::VectorBase<double> > x_exact =
567 model->getExactSolution(time).get_x();
570 if (n == nTimeStepSizes-1) {
572 integrator->getSolutionHistory();
573 writeSolution(
"Tempus_HHTAlpha_SinCos_ExplicitCD.dat", solutionHistory);
576 for (
int i=0; i<solutionHistory->getNumStates(); i++) {
577 double time_i = (*solutionHistory)[i]->getTime();
579 model->getExactSolution(time_i).get_x(),
580 model->getExactSolution(time_i).get_x_dot()));
581 state->setTime((*solutionHistory)[i]->getTime());
582 solnHistExact->addState(state);
584 writeSolution(
"Tempus_HHTAlpha_SinCos_ExplicitCD-Ref.dat", solnHistExact);
588 std::ofstream ftmp(
"Tempus_HHTAlpha_SinCos_ExplicitCD-Energy.dat");
590 RCP<const Thyra::VectorBase<double> > x_exact_plot;
591 for (
int i=0; i<solutionHistory->getNumStates(); i++) {
592 RCP<const SolutionState<double> > solutionState = (*solutionHistory)[i];
593 double time_i = solutionState->getTime();
594 RCP<const Thyra::VectorBase<double> > x_plot = solutionState->getX();
595 RCP<const Thyra::VectorBase<double> > x_dot_plot = solutionState->getXDot();
596 x_exact_plot = model->getExactSolution(time_i).get_x();
598 double ke = Thyra::dot(*x_dot_plot, *x_dot_plot);
601 double pe = Thyra::dot(*x_plot, *x_plot);
606 ftmp << time_i <<
" "
607 << get_ele(*(x_plot), 0) <<
" "
608 << get_ele(*(x_exact_plot), 0) <<
" "
609 << get_ele(*(x_dot_plot), 0) <<
" "
610 << ke <<
" " << pe <<
" " << te << std::endl;
617 StepSize.push_back(dt);
618 auto solution = Thyra::createMember(model->get_x_space());
619 Thyra::copy(*(integrator->getX()),solution.ptr());
620 solutions.push_back(solution);
621 auto solutionDot = Thyra::createMember(model->get_x_space());
622 Thyra::copy(*(integrator->getXdot()),solutionDot.ptr());
623 solutionsDot.push_back(solutionDot);
624 if (n == nTimeStepSizes-1) {
625 StepSize.push_back(0.0);
626 auto solutionExact = Thyra::createMember(model->get_x_space());
627 Thyra::copy(*(model->getExactSolution(time).get_x()),solutionExact.ptr());
628 solutions.push_back(solutionExact);
629 auto solutionDotExact = Thyra::createMember(model->get_x_space());
630 Thyra::copy(*(model->getExactSolution(time).get_x_dot()),
631 solutionDotExact.ptr());
632 solutionsDot.push_back(solutionDotExact);
638 double xDotSlope = 0.0;
639 RCP<Tempus::Stepper<double> > stepper = integrator->getStepper();
640 double order = stepper->getOrder();
643 solutions, xErrorNorm, xSlope,
644 solutionsDot, xDotErrorNorm, xDotSlope);
646 TEST_FLOATING_EQUALITY( xSlope, order, 0.02 );
647 TEST_FLOATING_EQUALITY( xErrorNorm[0], 0.00451069, 1.0e-4 );
648 TEST_FLOATING_EQUALITY( xDotSlope, order, 0.01 );
649 TEST_FLOATING_EQUALITY( xDotErrorNorm[0], 0.0551522, 1.0e-4 );
651 #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...