9 #include "Teuchos_UnitTestHarness.hpp"
10 #include "Teuchos_XMLParameterListHelpers.hpp"
11 #include "Teuchos_TimeMonitor.hpp"
12 #include "Teuchos_DefaultComm.hpp"
14 #include "Tempus_config.hpp"
15 #include "Tempus_IntegratorBasic.hpp"
16 #include "Tempus_StepperTrapezoidal.hpp"
18 #include "../TestModels/SinCosModel.hpp"
19 #include "../TestModels/VanDerPolModel.hpp"
20 #include "../TestUtils/Tempus_ConvergenceTestUtils.hpp"
22 #include "Stratimikos_DefaultLinearSolverBuilder.hpp"
23 #include "Thyra_LinearOpWithSolveFactoryHelpers.hpp"
25 #ifdef Tempus_ENABLE_MPI
26 #include "Epetra_MpiComm.h"
28 #include "Epetra_SerialComm.h"
39 using Teuchos::ParameterList;
40 using Teuchos::sublist;
41 using Teuchos::getParametersFromXmlFile;
48 #define TEST_PARAMETERLIST
49 #define TEST_CONSTRUCTING_FROM_DEFAULTS
51 #define TEST_VANDERPOL
54 #ifdef TEST_PARAMETERLIST
60 RCP<ParameterList> pList =
61 getParametersFromXmlFile(
"Tempus_Trapezoidal_SinCos.xml");
64 RCP<ParameterList> scm_pl = sublist(pList,
"SinCosModel",
true);
65 RCP<SinCosModel<double> > model =
68 RCP<ParameterList> tempusPL = sublist(pList,
"Tempus",
true);
72 RCP<Tempus::IntegratorBasic<double> > integrator =
73 Tempus::integratorBasic<double>(tempusPL, model);
75 RCP<ParameterList> stepperPL = sublist(tempusPL,
"Default Stepper",
true);
76 RCP<ParameterList> defaultPL =
77 integrator->getStepper()->getDefaultParameters();
78 TEST_ASSERT(haveSameValues(*stepperPL, *defaultPL,
true))
83 RCP<Tempus::IntegratorBasic<double> > integrator =
84 Tempus::integratorBasic<double>(model,
"Trapezoidal Method");
86 RCP<ParameterList> stepperPL = sublist(tempusPL,
"Default Stepper",
true);
87 RCP<ParameterList> defaultPL =
88 integrator->getStepper()->getDefaultParameters();
90 TEST_ASSERT(haveSameValues(*stepperPL, *defaultPL,
true))
93 #endif // TEST_PARAMETERLIST
96 #ifdef TEST_CONSTRUCTING_FROM_DEFAULTS
104 RCP<ParameterList> pList =
105 getParametersFromXmlFile(
"Tempus_Trapezoidal_SinCos.xml");
106 RCP<ParameterList> pl = sublist(pList,
"Tempus",
true);
109 RCP<ParameterList> scm_pl = sublist(pList,
"SinCosModel",
true);
111 RCP<SinCosModel<double> > model =
115 RCP<Tempus::StepperTrapezoidal<double> > stepper =
127 RCP<Tempus::TimeStepControl<double> > timeStepControl =
129 ParameterList tscPL = pl->sublist(
"Default Integrator")
130 .sublist(
"Time Step Control");
131 timeStepControl->setStepType (tscPL.get<std::string>(
"Integrator Step Type"));
132 timeStepControl->setInitIndex(tscPL.get<
int> (
"Initial Time Index"));
133 timeStepControl->setInitTime (tscPL.get<
double>(
"Initial Time"));
134 timeStepControl->setFinalTime(tscPL.get<
double>(
"Final Time"));
135 timeStepControl->setInitTimeStep(dt);
136 timeStepControl->initialize();
139 Thyra::ModelEvaluatorBase::InArgs<double> inArgsIC =
140 stepper->getModel()->getNominalValues();
141 RCP<Thyra::VectorBase<double> > icSoln =
142 Teuchos::rcp_const_cast<Thyra::VectorBase<double> > (inArgsIC.get_x());
143 RCP<Thyra::VectorBase<double> > icSolnDot =
144 Teuchos::rcp_const_cast<Thyra::VectorBase<double> > (inArgsIC.get_x_dot());
145 RCP<Tempus::SolutionState<double> > icState =
147 icState->setTime (timeStepControl->getInitTime());
148 icState->setIndex (timeStepControl->getInitIndex());
149 icState->setTimeStep(0.0);
150 icState->setOrder (stepper->getOrder());
162 RCP<Tempus::IntegratorBasic<double> > integrator =
163 Tempus::integratorBasic<double>();
164 integrator->setStepperWStepper(stepper);
165 integrator->setTimeStepControl(timeStepControl);
168 integrator->initialize();
172 bool integratorStatus = integrator->advanceTime();
173 TEST_ASSERT(integratorStatus)
177 double time = integrator->getTime();
178 double timeFinal =pl->sublist(
"Default Integrator")
179 .sublist(
"Time Step Control").get<
double>(
"Final Time");
180 TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
183 RCP<Thyra::VectorBase<double> > x = integrator->getX();
184 RCP<const Thyra::VectorBase<double> > x_exact =
185 model->getExactSolution(time).get_x();
188 RCP<Thyra::VectorBase<double> > xdiff = x->clone_v();
189 Thyra::V_StVpStV(xdiff.ptr(), 1.0, *x_exact, -1.0, *(x));
192 std::cout <<
" Stepper = Trapezoidal" << std::endl;
193 std::cout <<
" =========================" << std::endl;
194 std::cout <<
" Exact solution : " << get_ele(*(x_exact), 0) <<
" "
195 << get_ele(*(x_exact), 1) << std::endl;
196 std::cout <<
" Computed solution: " << get_ele(*(x ), 0) <<
" "
197 << get_ele(*(x ), 1) << std::endl;
198 std::cout <<
" Difference : " << get_ele(*(xdiff ), 0) <<
" "
199 << get_ele(*(xdiff ), 1) << std::endl;
200 std::cout <<
" =========================" << std::endl;
201 TEST_FLOATING_EQUALITY(get_ele(*(x), 0), 0.841021, 1.0e-4 );
202 TEST_FLOATING_EQUALITY(get_ele(*(x), 1), 0.541002, 1.0e-4 );
204 #endif // TEST_CONSTRUCTING_FROM_DEFAULTS
212 RCP<Tempus::IntegratorBasic<double> > integrator;
213 std::vector<RCP<Thyra::VectorBase<double>>> solutions;
214 std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
215 std::vector<double> StepSize;
216 std::vector<double> xErrorNorm;
217 std::vector<double> xDotErrorNorm;
218 const int nTimeStepSizes = 7;
221 for (
int n=0; n<nTimeStepSizes; n++) {
224 RCP<ParameterList> pList =
225 getParametersFromXmlFile(
"Tempus_Trapezoidal_SinCos.xml");
232 RCP<ParameterList> scm_pl = sublist(pList,
"SinCosModel",
true);
234 RCP<SinCosModel<double> > model =
240 RCP<ParameterList> pl = sublist(pList,
"Tempus",
true);
241 pl->sublist(
"Default Integrator")
242 .sublist(
"Time Step Control").set(
"Initial Time Step", dt);
243 integrator = Tempus::integratorBasic<double>(pl, model);
250 RCP<Thyra::VectorBase<double> > x0 =
251 model->getNominalValues().get_x()->clone_v();
252 RCP<Thyra::VectorBase<double> > xdot0 =
253 model->getNominalValues().get_x_dot()->clone_v();
254 integrator->setInitialState(0.0, x0, xdot0);
258 bool integratorStatus = integrator->advanceTime();
259 TEST_ASSERT(integratorStatus)
262 time = integrator->getTime();
263 double timeFinal =pl->sublist(
"Default Integrator")
264 .sublist(
"Time Step Control").get<
double>(
"Final Time");
265 TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
270 integrator->getSolutionHistory();
273 RCP<Tempus::SolutionHistory<double> > solnHistExact =
276 double time = (*solutionHistory)[i]->getTime();
277 RCP<Tempus::SolutionState<double> > state =
279 model->getExactSolution(time).get_x(),
280 model->getExactSolution(time).get_x_dot()));
282 solnHistExact->addState(state);
284 writeSolution(
"Tempus_Trapezoidal_SinCos-Ref.dat", solnHistExact);
288 StepSize.push_back(dt);
289 auto solution = Thyra::createMember(model->get_x_space());
290 Thyra::copy(*(integrator->getX()),solution.ptr());
291 solutions.push_back(solution);
292 auto solutionDot = Thyra::createMember(model->get_x_space());
293 Thyra::copy(*(integrator->getXdot()),solutionDot.ptr());
294 solutionsDot.push_back(solutionDot);
295 if (n == nTimeStepSizes-1) {
296 StepSize.push_back(0.0);
297 auto solution = Thyra::createMember(model->get_x_space());
298 Thyra::copy(*(model->getExactSolution(time).get_x()),solution.ptr());
299 solutions.push_back(solution);
300 auto solutionDot = Thyra::createMember(model->get_x_space());
301 Thyra::copy(*(model->getExactSolution(time).get_x_dot()),
303 solutionsDot.push_back(solutionDot);
308 double xDotSlope = 0.0;
309 RCP<Tempus::Stepper<double> > stepper = integrator->getStepper();
310 double order = stepper->getOrder();
313 solutions, xErrorNorm, xSlope,
314 solutionsDot, xDotErrorNorm, xDotSlope);
316 TEST_FLOATING_EQUALITY( xSlope, order, 0.01 );
317 TEST_FLOATING_EQUALITY( xErrorNorm[0], 0.000832086, 1.0e-4 );
318 TEST_FLOATING_EQUALITY( xDotSlope, order, 0.01 );
319 TEST_FLOATING_EQUALITY( xDotErrorNorm[0], 0.000832086, 1.0e-4 );
321 Teuchos::TimeMonitor::summarize();
323 #endif // TEST_SINCOS
326 #ifdef TEST_VANDERPOL
331 RCP<Tempus::IntegratorBasic<double> > integrator;
332 std::vector<RCP<Thyra::VectorBase<double>>> solutions;
333 std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
334 std::vector<double> StepSize;
335 std::vector<double> xErrorNorm;
336 std::vector<double> xDotErrorNorm;
337 const int nTimeStepSizes = 4;
340 for (
int n=0; n<nTimeStepSizes; n++) {
343 RCP<ParameterList> pList =
344 getParametersFromXmlFile(
"Tempus_Trapezoidal_VanDerPol.xml");
347 RCP<ParameterList> vdpm_pl = sublist(pList,
"VanDerPolModel",
true);
348 RCP<VanDerPolModel<double> > model =
353 if (n == nTimeStepSizes-1) dt /= 10.0;
356 RCP<ParameterList> pl = sublist(pList,
"Tempus",
true);
357 pl->sublist(
"Demo Integrator")
358 .sublist(
"Time Step Control").set(
"Initial Time Step", dt);
359 integrator = Tempus::integratorBasic<double>(pl, model);
362 bool integratorStatus = integrator->advanceTime();
363 TEST_ASSERT(integratorStatus)
366 time = integrator->getTime();
367 double timeFinal =pl->sublist(
"Demo Integrator")
368 .sublist(
"Time Step Control").get<
double>(
"Final Time");
369 double tol = 100.0 * std::numeric_limits<double>::epsilon();
370 TEST_FLOATING_EQUALITY(time, timeFinal, tol);
373 StepSize.push_back(dt);
374 auto solution = Thyra::createMember(model->get_x_space());
375 Thyra::copy(*(integrator->getX()),solution.ptr());
376 solutions.push_back(solution);
377 auto solutionDot = Thyra::createMember(model->get_x_space());
378 Thyra::copy(*(integrator->getXdot()),solutionDot.ptr());
379 solutionsDot.push_back(solutionDot);
383 if ((n == 0) or (n == nTimeStepSizes-1)) {
384 std::string fname =
"Tempus_Trapezoidal_VanDerPol-Ref.dat";
385 if (n == 0) fname =
"Tempus_Trapezoidal_VanDerPol.dat";
387 integrator->getSolutionHistory();
393 double xDotSlope = 0.0;
394 RCP<Tempus::Stepper<double> > stepper = integrator->getStepper();
395 double order = stepper->getOrder();
398 solutions, xErrorNorm, xSlope,
399 solutionsDot, xDotErrorNorm, xDotSlope);
401 TEST_FLOATING_EQUALITY( xSlope, order, 0.10 );
402 TEST_FLOATING_EQUALITY( xDotSlope, order, 0.10 );
403 TEST_FLOATING_EQUALITY( xErrorNorm[0], 0.00085855, 1.0e-4 );
404 TEST_FLOATING_EQUALITY( xDotErrorNorm[0], 0.00120695, 1.0e-4 );
406 Teuchos::TimeMonitor::summarize();
408 #endif // TEST_VANDERPOL