44 double tolerance = 1.0e-14;
46 RCP<ParameterList> pList =
47 getParametersFromXmlFile(
"Tempus_HHTAlpha_BallParabolic.xml");
50 RCP<ParameterList> hom_pl = sublist(pList,
"HarmonicOscillatorModel",
true);
54 RCP<ParameterList> pl = sublist(pList,
"Tempus",
true);
56 RCP<Tempus::IntegratorBasic<double>> integrator =
57 Tempus::createIntegratorBasic<double>(pl, model);
60 bool integratorStatus = integrator->advanceTime();
61 TEST_ASSERT(integratorStatus)
64 double time = integrator->getTime();
65 double timeFinal = pl->sublist(
"Default Integrator")
66 .sublist(
"Time Step Control")
67 .get<
double>(
"Final Time");
68 TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
71 RCP<Thyra::VectorBase<double>> x = integrator->getX();
72 RCP<const Thyra::VectorBase<double>> x_exact =
73 model->getExactSolution(time).get_x();
76 std::ofstream ftmp(
"Tempus_HHTAlpha_BallParabolic.dat");
78 RCP<const SolutionHistory<double>> solutionHistory =
79 integrator->getSolutionHistory();
82 RCP<const Thyra::VectorBase<double>> x_exact_plot;
83 for (
int i = 0; i < solutionHistory->getNumStates(); i++) {
84 RCP<const SolutionState<double>> solutionState = (*solutionHistory)[i];
85 double time_i = solutionState->getTime();
86 RCP<const Thyra::VectorBase<double>> x_plot = solutionState->getX();
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;
97 TEUCHOS_TEST_FOR_EXCEPTION(
98 !passed, std::logic_error,
99 "\n Test failed! Max error = " << err <<
" > tolerance = " << tolerance
110 RCP<ParameterList> pList =
111 getParametersFromXmlFile(
"Tempus_HHTAlpha_SinCos_SecondOrder.xml");
112 RCP<ParameterList> pl = sublist(pList,
"Tempus",
true);
115 RCP<ParameterList> hom_pl = sublist(pList,
"HarmonicOscillatorModel",
true);
120 stepper->setModel(model);
121 stepper->initialize();
125 ParameterList tscPL =
126 pl->sublist(
"Default Integrator").sublist(
"Time Step Control");
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();
135 auto icX = rcp_const_cast<Thyra::VectorBase<double>>(inArgsIC.get_x());
136 auto icXDot = rcp_const_cast<Thyra::VectorBase<double>>(inArgsIC.get_x_dot());
138 rcp_const_cast<Thyra::VectorBase<double>>(inArgsIC.get_x_dot_dot());
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);
154 RCP<Tempus::IntegratorBasic<double>> integrator =
155 Tempus::createIntegratorBasic<double>();
156 integrator->setStepper(stepper);
157 integrator->setTimeStepControl(timeStepControl);
158 integrator->setSolutionHistory(solutionHistory);
160 integrator->initialize();
163 bool integratorStatus = integrator->advanceTime();
164 TEST_ASSERT(integratorStatus)
167 double time = integrator->getTime();
168 double timeFinal = pl->sublist(
"Default Integrator")
169 .sublist(
"Time Step Control")
170 .get<
double>(
"Final Time");
171 TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
174 RCP<Thyra::VectorBase<double>> x = integrator->getX();
175 RCP<const Thyra::VectorBase<double>> x_exact =
176 model->getExactSolution(time).get_x();
179 RCP<Thyra::VectorBase<double>> xdiff = x->clone_v();
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);
195 RCP<Tempus::IntegratorBasic<double>> integrator;
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;
205 RCP<ParameterList> pList =
206 getParametersFromXmlFile(
"Tempus_HHTAlpha_SinCos_SecondOrder.xml");
209 RCP<ParameterList> hom_pl = sublist(pList,
"HarmonicOscillatorModel",
true);
213 double k = hom_pl->get<
double>(
"x coeff k");
214 double m = hom_pl->get<
double>(
"Mass coeff m");
217 RCP<ParameterList> pl = sublist(pList,
"Tempus",
true);
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();
239 TEST_ASSERT(integratorStatus)
242 time = integrator->getTime();
243 double timeFinal = pl->sublist(
"Default Integrator")
244 .sublist(
"Time Step Control")
245 .get<
double>(
"Final Time");
246 TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
249 RCP<Thyra::VectorBase<double>> x = integrator->getX();
250 RCP<const Thyra::VectorBase<double>> x_exact =
251 model->getExactSolution(time).get_x();
254 if (n == nTimeStepSizes - 1) {
255 RCP<const SolutionHistory<double>> solutionHistory =
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");
277 RCP<const Thyra::VectorBase<double>> x_exact_plot;
278 for (
int i = 0; i < solutionHistory->getNumStates(); i++) {
279 RCP<const SolutionState<double>> solutionState =
280 (*solutionHistory)[i];
281 double time_i = solutionState->getTime();
282 RCP<const Thyra::VectorBase<double>> x_plot = solutionState->getX();
283 RCP<const Thyra::VectorBase<double>> x_dot_plot =
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;
328 RCP<Tempus::Stepper<double>> stepper = integrator->getStepper();
329 double order = stepper->getOrder();
330 writeOrderError(
"Tempus_HHTAlpha_SinCos_SecondOrder-Error.dat", stepper,
331 StepSize, solutions, xErrorNorm, xSlope, solutionsDot,
332 xDotErrorNorm, xDotSlope, out);
334 TEST_FLOATING_EQUALITY(xSlope, order, 0.02);
335 TEST_FLOATING_EQUALITY(xErrorNorm[0], 0.00644755, 1.0e-4);
336 TEST_FLOATING_EQUALITY(xDotSlope, order, 0.01);
337 TEST_FLOATING_EQUALITY(xDotErrorNorm[0], 0.104392, 1.0e-4);
344 RCP<Tempus::IntegratorBasic<double>> integrator;
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;
354 RCP<ParameterList> pList =
355 getParametersFromXmlFile(
"Tempus_HHTAlpha_SinCos_FirstOrder.xml");
358 RCP<ParameterList> hom_pl = sublist(pList,
"HarmonicOscillatorModel",
true);
362 double k = hom_pl->get<
double>(
"x coeff k");
363 double m = hom_pl->get<
double>(
"Mass coeff m");
366 RCP<ParameterList> pl = sublist(pList,
"Tempus",
true);
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();
388 TEST_ASSERT(integratorStatus)
391 time = integrator->getTime();
392 double timeFinal = pl->sublist(
"Default Integrator")
393 .sublist(
"Time Step Control")
394 .get<
double>(
"Final Time");
395 TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
398 RCP<Thyra::VectorBase<double>> x = integrator->getX();
399 RCP<const Thyra::VectorBase<double>> x_exact =
400 model->getExactSolution(time).get_x();
403 if (n == nTimeStepSizes - 1) {
404 RCP<const SolutionHistory<double>> solutionHistory =
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");
425 RCP<const Thyra::VectorBase<double>> x_exact_plot;
426 for (
int i = 0; i < solutionHistory->getNumStates(); i++) {
427 RCP<const SolutionState<double>> solutionState =
428 (*solutionHistory)[i];
429 double time_i = solutionState->getTime();
430 RCP<const Thyra::VectorBase<double>> x_plot = solutionState->getX();
431 RCP<const Thyra::VectorBase<double>> x_dot_plot =
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;
476 RCP<Tempus::Stepper<double>> stepper = integrator->getStepper();
478 writeOrderError(
"Tempus_HHTAlpha_SinCos_FirstOrder-Error.dat", stepper,
479 StepSize, solutions, xErrorNorm, xSlope, solutionsDot,
480 xDotErrorNorm, xDotSlope, out);
482 TEST_FLOATING_EQUALITY(xSlope, 0.977568, 0.02);
483 TEST_FLOATING_EQUALITY(xErrorNorm[0], 0.048932, 1.0e-4);
484 TEST_FLOATING_EQUALITY(xDotSlope, 1.2263, 0.01);
485 TEST_FLOATING_EQUALITY(xDotErrorNorm[0], 0.393504, 1.0e-4);
492 RCP<Tempus::IntegratorBasic<double>> integrator;
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;
502 RCP<ParameterList> pList =
503 getParametersFromXmlFile(
"Tempus_HHTAlpha_SinCos_ExplicitCD.xml");
506 RCP<ParameterList> hom_pl = sublist(pList,
"HarmonicOscillatorModel",
true);
510 double k = hom_pl->get<
double>(
"x coeff k");
511 double m = hom_pl->get<
double>(
"Mass coeff m");
514 RCP<ParameterList> pl = sublist(pList,
"Tempus",
true);
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();
536 TEST_ASSERT(integratorStatus)
539 time = integrator->getTime();
540 double timeFinal = pl->sublist(
"Default Integrator")
541 .sublist(
"Time Step Control")
542 .get<
double>(
"Final Time");
543 TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
546 RCP<Thyra::VectorBase<double>> x = integrator->getX();
547 RCP<const Thyra::VectorBase<double>> x_exact =
548 model->getExactSolution(time).get_x();
551 if (n == nTimeStepSizes - 1) {
552 RCP<const SolutionHistory<double>> solutionHistory =
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");
573 RCP<const Thyra::VectorBase<double>> x_exact_plot;
574 for (
int i = 0; i < solutionHistory->getNumStates(); i++) {
575 RCP<const SolutionState<double>> solutionState =
576 (*solutionHistory)[i];
577 double time_i = solutionState->getTime();
578 RCP<const Thyra::VectorBase<double>> x_plot = solutionState->getX();
579 RCP<const Thyra::VectorBase<double>> x_dot_plot =
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;
624 RCP<Tempus::Stepper<double>> stepper = integrator->getStepper();
625 double order = stepper->getOrder();
626 writeOrderError(
"Tempus_HHTAlpha_SinCos_ExplicitCD-Error.dat", stepper,
627 StepSize, solutions, xErrorNorm, xSlope, solutionsDot,
628 xDotErrorNorm, xDotSlope, out);
630 TEST_FLOATING_EQUALITY(xSlope, order, 0.02);
631 TEST_FLOATING_EQUALITY(xErrorNorm[0], 0.00451069, 1.0e-4);
632 TEST_FLOATING_EQUALITY(xDotSlope, order, 0.01);
633 TEST_FLOATING_EQUALITY(xDotErrorNorm[0], 0.0551522, 1.0e-4);
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
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)