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"
36 namespace Tempus_Test {
40 using Teuchos::rcp_const_cast;
41 using Teuchos::ParameterList;
42 using Teuchos::sublist;
43 using Teuchos::getParametersFromXmlFile;
50 #define TEST_PARAMETERLIST
51 #define TEST_CONSTRUCTING_FROM_DEFAULTS
53 #define TEST_VANDERPOL
56 #ifdef TEST_PARAMETERLIST
62 RCP<ParameterList> pList =
63 getParametersFromXmlFile(
"Tempus_Trapezoidal_SinCos.xml");
66 RCP<ParameterList> scm_pl = sublist(pList,
"SinCosModel",
true);
69 RCP<ParameterList> tempusPL = sublist(pList,
"Tempus",
true);
73 RCP<Tempus::IntegratorBasic<double> > integrator =
74 Tempus::integratorBasic<double>(tempusPL, model);
76 RCP<ParameterList> stepperPL = sublist(tempusPL,
"Default Stepper",
true);
78 RCP<ParameterList> defaultPL =
79 integrator->getStepper()->getDefaultParameters();
80 bool pass = haveSameValues(*stepperPL, *defaultPL,
true);
82 std::cout << std::endl;
83 std::cout <<
"stepperPL -------------- \n" << *stepperPL << std::endl;
84 std::cout <<
"defaultPL -------------- \n" << *defaultPL << std::endl;
91 RCP<Tempus::IntegratorBasic<double> > integrator =
92 Tempus::integratorBasic<double>(model,
"Trapezoidal Method");
94 RCP<ParameterList> stepperPL = sublist(tempusPL,
"Default Stepper",
true);
95 RCP<ParameterList> defaultPL =
96 integrator->getStepper()->getDefaultParameters();
98 bool pass = haveSameValues(*stepperPL, *defaultPL,
true);
100 std::cout << std::endl;
101 std::cout <<
"stepperPL -------------- \n" << *stepperPL << std::endl;
102 std::cout <<
"defaultPL -------------- \n" << *defaultPL << std::endl;
107 #endif // TEST_PARAMETERLIST
110 #ifdef TEST_CONSTRUCTING_FROM_DEFAULTS
118 RCP<ParameterList> pList =
119 getParametersFromXmlFile(
"Tempus_Trapezoidal_SinCos.xml");
120 RCP<ParameterList> pl = sublist(pList,
"Tempus",
true);
123 RCP<ParameterList> scm_pl = sublist(pList,
"SinCosModel",
true);
142 stepper->setModel(model);
143 stepper->initialize();
147 ParameterList tscPL = pl->sublist(
"Default Integrator")
148 .sublist(
"Time Step Control");
149 timeStepControl->setStepType (tscPL.get<std::string>(
"Integrator Step Type"));
150 timeStepControl->setInitIndex(tscPL.get<
int> (
"Initial Time Index"));
151 timeStepControl->setInitTime (tscPL.get<
double>(
"Initial Time"));
152 timeStepControl->setFinalTime(tscPL.get<
double>(
"Final Time"));
153 timeStepControl->setInitTimeStep(dt);
154 timeStepControl->initialize();
157 Thyra::ModelEvaluatorBase::InArgs<double> inArgsIC =
158 stepper->getModel()->getNominalValues();
159 auto icSoln = rcp_const_cast<Thyra::VectorBase<double> > (inArgsIC.get_x());
161 rcp_const_cast<Thyra::VectorBase<double> > (inArgsIC.get_x_dot());
163 icState->setTime (timeStepControl->getInitTime());
164 icState->setIndex (timeStepControl->getInitIndex());
165 icState->setTimeStep(0.0);
166 icState->setOrder (stepper->getOrder());
177 RCP<Tempus::IntegratorBasic<double> > integrator =
178 Tempus::integratorBasic<double>();
179 integrator->setStepperWStepper(stepper);
180 integrator->setTimeStepControl(timeStepControl);
183 integrator->initialize();
187 bool integratorStatus = integrator->advanceTime();
188 TEST_ASSERT(integratorStatus)
192 double time = integrator->getTime();
193 double timeFinal =pl->sublist(
"Default Integrator")
194 .sublist(
"Time Step Control").get<
double>(
"Final Time");
195 TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
198 RCP<Thyra::VectorBase<double> > x = integrator->getX();
199 RCP<const Thyra::VectorBase<double> > x_exact =
200 model->getExactSolution(time).get_x();
203 RCP<Thyra::VectorBase<double> > xdiff = x->clone_v();
204 Thyra::V_StVpStV(xdiff.ptr(), 1.0, *x_exact, -1.0, *(x));
207 std::cout <<
" Stepper = Trapezoidal" << std::endl;
208 std::cout <<
" =========================" << std::endl;
209 std::cout <<
" Exact solution : " << get_ele(*(x_exact), 0) <<
" "
210 << get_ele(*(x_exact), 1) << std::endl;
211 std::cout <<
" Computed solution: " << get_ele(*(x ), 0) <<
" "
212 << get_ele(*(x ), 1) << std::endl;
213 std::cout <<
" Difference : " << get_ele(*(xdiff ), 0) <<
" "
214 << get_ele(*(xdiff ), 1) << std::endl;
215 std::cout <<
" =========================" << std::endl;
216 TEST_FLOATING_EQUALITY(get_ele(*(x), 0), 0.841021, 1.0e-4 );
217 TEST_FLOATING_EQUALITY(get_ele(*(x), 1), 0.541002, 1.0e-4 );
219 #endif // TEST_CONSTRUCTING_FROM_DEFAULTS
227 RCP<Tempus::IntegratorBasic<double> > integrator;
228 std::vector<RCP<Thyra::VectorBase<double>>> solutions;
229 std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
230 std::vector<double> StepSize;
231 std::vector<double> xErrorNorm;
232 std::vector<double> xDotErrorNorm;
233 const int nTimeStepSizes = 7;
236 for (
int n=0; n<nTimeStepSizes; n++) {
239 RCP<ParameterList> pList =
240 getParametersFromXmlFile(
"Tempus_Trapezoidal_SinCos.xml");
247 RCP<ParameterList> scm_pl = sublist(pList,
"SinCosModel",
true);
254 RCP<ParameterList> pl = sublist(pList,
"Tempus",
true);
255 pl->sublist(
"Default Integrator")
256 .sublist(
"Time Step Control").set(
"Initial Time Step", dt);
257 integrator = Tempus::integratorBasic<double>(pl, model);
264 RCP<Thyra::VectorBase<double> > x0 =
265 model->getNominalValues().get_x()->clone_v();
266 RCP<Thyra::VectorBase<double> > xdot0 =
267 model->getNominalValues().get_x_dot()->clone_v();
268 integrator->initializeSolutionHistory(0.0, x0, xdot0);
272 bool integratorStatus = integrator->advanceTime();
273 TEST_ASSERT(integratorStatus)
276 time = integrator->getTime();
277 double timeFinal =pl->sublist(
"Default Integrator")
278 .sublist(
"Time Step Control").get<
double>(
"Final Time");
279 TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
284 integrator->getSolutionHistory();
285 writeSolution(
"Tempus_Trapezoidal_SinCos.dat", solutionHistory);
288 for (
int i=0; i<solutionHistory->getNumStates(); i++) {
289 double time_i = (*solutionHistory)[i]->getTime();
291 model->getExactSolution(time_i).get_x(),
292 model->getExactSolution(time_i).get_x_dot()));
293 state->setTime((*solutionHistory)[i]->getTime());
294 solnHistExact->addState(state);
296 writeSolution(
"Tempus_Trapezoidal_SinCos-Ref.dat", solnHistExact);
300 StepSize.push_back(dt);
301 auto solution = Thyra::createMember(model->get_x_space());
302 Thyra::copy(*(integrator->getX()),solution.ptr());
303 solutions.push_back(solution);
304 auto solutionDot = Thyra::createMember(model->get_x_space());
305 Thyra::copy(*(integrator->getXdot()),solutionDot.ptr());
306 solutionsDot.push_back(solutionDot);
307 if (n == nTimeStepSizes-1) {
308 StepSize.push_back(0.0);
309 auto solutionExact = Thyra::createMember(model->get_x_space());
310 Thyra::copy(*(model->getExactSolution(time).get_x()),solutionExact.ptr());
311 solutions.push_back(solutionExact);
312 auto solutionDotExact = Thyra::createMember(model->get_x_space());
313 Thyra::copy(*(model->getExactSolution(time).get_x_dot()),
314 solutionDotExact.ptr());
315 solutionsDot.push_back(solutionDotExact);
320 double xDotSlope = 0.0;
321 RCP<Tempus::Stepper<double> > stepper = integrator->getStepper();
322 double order = stepper->getOrder();
325 solutions, xErrorNorm, xSlope,
326 solutionsDot, xDotErrorNorm, xDotSlope);
328 TEST_FLOATING_EQUALITY( xSlope, order, 0.01 );
329 TEST_FLOATING_EQUALITY( xErrorNorm[0], 0.000832086, 1.0e-4 );
330 TEST_FLOATING_EQUALITY( xDotSlope, order, 0.01 );
331 TEST_FLOATING_EQUALITY( xDotErrorNorm[0], 0.000832086, 1.0e-4 );
333 Teuchos::TimeMonitor::summarize();
335 #endif // TEST_SINCOS
338 #ifdef TEST_VANDERPOL
343 RCP<Tempus::IntegratorBasic<double> > integrator;
344 std::vector<RCP<Thyra::VectorBase<double>>> solutions;
345 std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
346 std::vector<double> StepSize;
347 std::vector<double> xErrorNorm;
348 std::vector<double> xDotErrorNorm;
349 const int nTimeStepSizes = 4;
352 for (
int n=0; n<nTimeStepSizes; n++) {
355 RCP<ParameterList> pList =
356 getParametersFromXmlFile(
"Tempus_Trapezoidal_VanDerPol.xml");
359 RCP<ParameterList> vdpm_pl = sublist(pList,
"VanDerPolModel",
true);
364 if (n == nTimeStepSizes-1) dt /= 10.0;
367 RCP<ParameterList> pl = sublist(pList,
"Tempus",
true);
368 pl->sublist(
"Demo Integrator")
369 .sublist(
"Time Step Control").set(
"Initial Time Step", dt);
370 integrator = Tempus::integratorBasic<double>(pl, model);
373 bool integratorStatus = integrator->advanceTime();
374 TEST_ASSERT(integratorStatus)
377 time = integrator->getTime();
378 double timeFinal =pl->sublist(
"Demo Integrator")
379 .sublist(
"Time Step Control").get<
double>(
"Final Time");
380 double tol = 100.0 * std::numeric_limits<double>::epsilon();
381 TEST_FLOATING_EQUALITY(time, timeFinal, tol);
384 StepSize.push_back(dt);
385 auto solution = Thyra::createMember(model->get_x_space());
386 Thyra::copy(*(integrator->getX()),solution.ptr());
387 solutions.push_back(solution);
388 auto solutionDot = Thyra::createMember(model->get_x_space());
389 Thyra::copy(*(integrator->getXdot()),solutionDot.ptr());
390 solutionsDot.push_back(solutionDot);
394 if ((n == 0) or (n == nTimeStepSizes-1)) {
395 std::string fname =
"Tempus_Trapezoidal_VanDerPol-Ref.dat";
396 if (n == 0) fname =
"Tempus_Trapezoidal_VanDerPol.dat";
398 integrator->getSolutionHistory();
404 double xDotSlope = 0.0;
405 RCP<Tempus::Stepper<double> > stepper = integrator->getStepper();
406 double order = stepper->getOrder();
409 solutions, xErrorNorm, xSlope,
410 solutionsDot, xDotErrorNorm, xDotSlope);
412 TEST_FLOATING_EQUALITY( xSlope, order, 0.10 );
413 TEST_FLOATING_EQUALITY( xDotSlope, order, 0.10 );
414 TEST_FLOATING_EQUALITY( xErrorNorm[0], 0.00085855, 1.0e-4 );
415 TEST_FLOATING_EQUALITY( xDotErrorNorm[0], 0.00120695, 1.0e-4 );
417 Teuchos::TimeMonitor::summarize();
419 #endif // TEST_VANDERPOL
Trapezoidal method time stepper.
Sine-Cosine model problem from Rythmos. This is a canonical Sine-Cosine differential equation with a...
void writeSolution(const std::string filename, Teuchos::RCP< const Tempus::SolutionHistory< Scalar > > solutionHistory)
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.
van der Pol model problem for nonlinear electrical circuit.
Solution state for integrators and steppers. SolutionState contains the metadata for solutions and th...