9 #include "Teuchos_UnitTestHarness.hpp" 10 #include "Teuchos_XMLParameterListHelpers.hpp" 11 #include "Teuchos_TimeMonitor.hpp" 13 #include "Thyra_VectorStdOps.hpp" 14 #include "Thyra_DetachedVectorView.hpp" 16 #include "Tempus_IntegratorBasic.hpp" 18 #include "Tempus_StepperForwardEuler.hpp" 20 #include "../TestModels/SinCosModel.hpp" 21 #include "../TestModels/VanDerPolModel.hpp" 22 #include "../TestUtils/Tempus_ConvergenceTestUtils.hpp" 31 using Teuchos::rcp_const_cast;
32 using Teuchos::ParameterList;
33 using Teuchos::sublist;
34 using Teuchos::getParametersFromXmlFile;
46 RCP<ParameterList> pList =
47 getParametersFromXmlFile(
"Tempus_ForwardEuler_SinCos.xml");
54 RCP<ParameterList> scm_pl = sublist(pList,
"SinCosModel",
true);
57 RCP<ParameterList> tempusPL = sublist(pList,
"Tempus",
true);
61 RCP<Tempus::IntegratorBasic<double> > integrator =
62 Tempus::createIntegratorBasic<double>(tempusPL, model);
64 RCP<ParameterList> stepperPL = sublist(tempusPL,
"Demo Stepper",
true);
65 RCP<const ParameterList> defaultPL =
66 integrator->getStepper()->getValidParameters();
68 bool pass = haveSameValuesSorted(*stepperPL, *defaultPL,
true);
70 std::cout << std::endl;
71 std::cout <<
"stepperPL -------------- \n" << *stepperPL << std::endl;
72 std::cout <<
"defaultPL -------------- \n" << *defaultPL << std::endl;
79 RCP<Tempus::IntegratorBasic<double> > integrator =
80 Tempus::createIntegratorBasic<double>(model,
"Forward Euler");
82 RCP<ParameterList> stepperPL = sublist(tempusPL,
"Demo Stepper",
true);
83 RCP<const ParameterList> defaultPL =
84 integrator->getStepper()->getValidParameters();
86 bool pass = haveSameValuesSorted(*stepperPL, *defaultPL,
true);
88 std::cout << std::endl;
89 std::cout <<
"stepperPL -------------- \n" << *stepperPL << std::endl;
90 std::cout <<
"defaultPL -------------- \n" << *defaultPL << std::endl;
102 std::vector<std::string> options;
103 options.push_back(
"useFSAL=true");
104 options.push_back(
"useFSAL=false");
105 options.push_back(
"ICConsistency and Check");
107 for(
const auto& option: options) {
110 RCP<ParameterList> pList =
111 getParametersFromXmlFile(
"Tempus_ForwardEuler_SinCos.xml");
112 RCP<ParameterList> pl = sublist(pList,
"Tempus",
true);
115 RCP<ParameterList> scm_pl = sublist(pList,
"SinCosModel",
true);
121 stepper->setModel(model);
122 if (option ==
"useFSAL=true") stepper->setUseFSAL(
true);
123 else if (option ==
"useFSAL=false") stepper->setUseFSAL(
false);
124 else if ( option ==
"ICConsistency and Check") {
125 stepper->setICConsistency(
"Consistent");
126 stepper->setICConsistencyCheck(
true);
128 stepper->initialize();
132 ParameterList tscPL = pl->sublist(
"Demo Integrator")
133 .sublist(
"Time Step Control");
134 timeStepControl->setInitIndex(tscPL.get<
int> (
"Initial Time Index"));
135 timeStepControl->setInitTime (tscPL.get<
double>(
"Initial Time"));
136 timeStepControl->setFinalTime(tscPL.get<
double>(
"Final Time"));
137 timeStepControl->setInitTimeStep(dt);
138 timeStepControl->initialize();
141 auto inArgsIC = model()->getNominalValues();
144 icState->setTime (timeStepControl->getInitTime());
145 icState->setIndex (timeStepControl->getInitIndex());
146 icState->setTimeStep(0.0);
151 solutionHistory->setName(
"Forward States");
153 solutionHistory->setStorageLimit(2);
154 solutionHistory->addState(icState);
157 stepper->setInitialConditions(solutionHistory);
160 RCP<Tempus::IntegratorBasic<double> > integrator =
161 Tempus::createIntegratorBasic<double>();
162 integrator->setStepper(stepper);
163 integrator->setTimeStepControl(timeStepControl);
164 integrator->setSolutionHistory(solutionHistory);
166 integrator->initialize();
170 bool integratorStatus = integrator->advanceTime();
171 TEST_ASSERT(integratorStatus)
175 double time = integrator->getTime();
176 double timeFinal =pl->sublist(
"Demo Integrator")
177 .sublist(
"Time Step Control").get<
double>(
"Final Time");
178 TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
181 RCP<Thyra::VectorBase<double> > x = integrator->getX();
182 RCP<const Thyra::VectorBase<double> > x_exact =
183 model->getExactSolution(time).get_x();
186 RCP<Thyra::VectorBase<double> > xdiff = x->clone_v();
187 Thyra::V_StVpStV(xdiff.ptr(), 1.0, *x_exact, -1.0, *(x));
190 std::cout <<
" Stepper = " << stepper->description()
191 <<
"\n with " << option << std::endl;
192 std::cout <<
" =========================" << std::endl;
193 std::cout <<
" Exact solution : " << get_ele(*(x_exact), 0) <<
" " 194 << get_ele(*(x_exact), 1) << std::endl;
195 std::cout <<
" Computed solution: " << get_ele(*(x ), 0) <<
" " 196 << get_ele(*(x ), 1) << std::endl;
197 std::cout <<
" Difference : " << get_ele(*(xdiff ), 0) <<
" " 198 << get_ele(*(xdiff ), 1) << std::endl;
199 std::cout <<
" =========================" << std::endl;
200 TEST_FLOATING_EQUALITY(get_ele(*(x), 0), 0.882508, 1.0e-4 );
201 TEST_FLOATING_EQUALITY(get_ele(*(x), 1), 0.570790, 1.0e-4 );
210 RCP<Tempus::IntegratorBasic<double> > integrator;
211 std::vector<RCP<Thyra::VectorBase<double>>> solutions;
212 std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
213 std::vector<double> StepSize;
214 std::vector<double> xErrorNorm;
215 std::vector<double> xDotErrorNorm;
216 const int nTimeStepSizes = 7;
219 for (
int n=0; n<nTimeStepSizes; n++) {
222 RCP<ParameterList> pList =
223 getParametersFromXmlFile(
"Tempus_ForwardEuler_SinCos.xml");
230 RCP<ParameterList> scm_pl = sublist(pList,
"SinCosModel",
true);
237 RCP<ParameterList> pl = sublist(pList,
"Tempus",
true);
238 pl->sublist(
"Demo Integrator")
239 .sublist(
"Time Step Control").set(
"Initial Time Step", dt);
240 integrator = Tempus::createIntegratorBasic<double>(pl, model);
246 RCP<Thyra::VectorBase<double> > x0 =
247 model->getNominalValues().get_x()->clone_v();
248 integrator->initializeSolutionHistory(0.0, x0);
249 integrator->initialize();
252 bool integratorStatus = integrator->advanceTime();
253 TEST_ASSERT(integratorStatus)
256 RCP<Tempus::PhysicsState<double> > physicsState =
257 integrator->getSolutionHistory()->getCurrentState()->getPhysicsState();
258 TEST_EQUALITY(physicsState->getName(),
"Tempus::PhysicsState");
261 time = integrator->getTime();
262 double timeFinal = pl->sublist(
"Demo Integrator")
263 .sublist(
"Time Step Control").get<
double>(
"Final Time");
264 TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
267 RCP<Thyra::VectorBase<double> > x = integrator->getX();
268 RCP<const Thyra::VectorBase<double> > x_exact =
269 model->getExactSolution(time).get_x();
273 RCP<const SolutionHistory<double> > solutionHistory =
274 integrator->getSolutionHistory();
275 writeSolution(
"Tempus_ForwardEuler_SinCos.dat", solutionHistory);
278 for (
int i=0; i<solutionHistory->getNumStates(); i++) {
279 double time_i = (*solutionHistory)[i]->getTime();
282 model->getExactSolution(time_i).get_x()),
284 model->getExactSolution(time_i).get_x_dot()));
285 state->setTime((*solutionHistory)[i]->getTime());
286 solnHistExact->addState(state);
288 writeSolution(
"Tempus_ForwardEuler_SinCos-Ref.dat", solnHistExact);
292 StepSize.push_back(dt);
293 auto solution = Thyra::createMember(model->get_x_space());
294 Thyra::copy(*(integrator->getX()),solution.ptr());
295 solutions.push_back(solution);
296 auto solutionDot = Thyra::createMember(model->get_x_space());
297 Thyra::copy(*(integrator->getXDot()),solutionDot.ptr());
298 solutionsDot.push_back(solutionDot);
299 if (n == nTimeStepSizes-1) {
300 StepSize.push_back(0.0);
301 auto solutionExact = Thyra::createMember(model->get_x_space());
302 Thyra::copy(*(model->getExactSolution(time).get_x()),solutionExact.ptr());
303 solutions.push_back(solutionExact);
304 auto solutionDotExact = Thyra::createMember(model->get_x_space());
305 Thyra::copy(*(model->getExactSolution(time).get_x_dot()),
306 solutionDotExact.ptr());
307 solutionsDot.push_back(solutionDotExact);
313 double xDotSlope = 0.0;
314 RCP<Tempus::Stepper<double> > stepper = integrator->getStepper();
315 double order = stepper->getOrder();
318 solutions, xErrorNorm, xSlope,
319 solutionsDot, xDotErrorNorm, xDotSlope);
321 TEST_FLOATING_EQUALITY( xSlope, order, 0.01 );
322 TEST_FLOATING_EQUALITY( xErrorNorm[0], 0.051123, 1.0e-4 );
327 Teuchos::TimeMonitor::summarize();
335 RCP<Tempus::IntegratorBasic<double> > integrator;
336 std::vector<RCP<Thyra::VectorBase<double>>> solutions;
337 std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
338 std::vector<double> StepSize;
339 std::vector<double> xErrorNorm;
340 std::vector<double> xDotErrorNorm;
341 const int nTimeStepSizes = 7;
343 for (
int n=0; n<nTimeStepSizes; n++) {
346 RCP<ParameterList> pList =
347 getParametersFromXmlFile(
"Tempus_ForwardEuler_VanDerPol.xml");
350 RCP<ParameterList> vdpm_pl = sublist(pList,
"VanDerPolModel",
true);
355 if (n == nTimeStepSizes-1) dt /= 10.0;
358 RCP<ParameterList> pl = sublist(pList,
"Tempus",
true);
359 pl->sublist(
"Demo Integrator")
360 .sublist(
"Time Step Control").set(
"Initial Time Step", dt);
361 integrator = Tempus::createIntegratorBasic<double>(pl, model);
364 bool integratorStatus = integrator->advanceTime();
365 TEST_ASSERT(integratorStatus)
368 double time = integrator->getTime();
369 double timeFinal =pl->sublist(
"Demo Integrator")
370 .sublist(
"Time Step Control").get<
double>(
"Final Time");
371 double tol = 100.0 * std::numeric_limits<double>::epsilon();
372 TEST_FLOATING_EQUALITY(time, timeFinal, tol);
375 StepSize.push_back(dt);
376 auto solution = Thyra::createMember(model->get_x_space());
377 Thyra::copy(*(integrator->getX()),solution.ptr());
378 solutions.push_back(solution);
379 auto solutionDot = Thyra::createMember(model->get_x_space());
380 Thyra::copy(*(integrator->getXDot()),solutionDot.ptr());
381 solutionsDot.push_back(solutionDot);
385 if ((n == 0) || (n == nTimeStepSizes-1)) {
386 std::string fname =
"Tempus_ForwardEuler_VanDerPol-Ref.dat";
387 if (n == 0) fname =
"Tempus_ForwardEuler_VanDerPol.dat";
388 RCP<const SolutionHistory<double> > solutionHistory =
389 integrator->getSolutionHistory();
396 double xDotSlope = 0.0;
397 RCP<Tempus::Stepper<double> > stepper = integrator->getStepper();
398 double order = stepper->getOrder();
401 solutions, xErrorNorm, xSlope,
402 solutionsDot, xDotErrorNorm, xDotSlope);
404 TEST_FLOATING_EQUALITY( xSlope, order, 0.10 );
405 TEST_FLOATING_EQUALITY( xErrorNorm[0], 0.387476, 1.0e-4 );
410 Teuchos::TimeMonitor::summarize();
419 std::vector<double> StepSize;
420 std::vector<double> ErrorNorm;
426 RCP<ParameterList> pList =
427 getParametersFromXmlFile(
"Tempus_ForwardEuler_NumberOfTimeSteps.xml");
430 RCP<ParameterList> vdpm_pl = sublist(pList,
"VanDerPolModel",
true);
434 RCP<ParameterList> pl = sublist(pList,
"Tempus",
true);
439 const int numTimeSteps = pl->sublist(
"Demo Integrator")
440 .sublist(
"Time Step Control")
441 .get<
int>(
"Number of Time Steps");
443 RCP<Tempus::IntegratorBasic<double> > integrator =
444 Tempus::createIntegratorBasic<double>(pl, model);
447 bool integratorStatus = integrator->advanceTime();
448 TEST_ASSERT(integratorStatus)
452 TEST_EQUALITY(numTimeSteps, integrator->getIndex());
461 RCP<ParameterList> pList =
462 getParametersFromXmlFile(
"Tempus_ForwardEuler_VanDerPol.xml");
465 RCP<ParameterList> vdpm_pl = sublist(pList,
"VanDerPolModel",
true);
469 RCP<ParameterList> pl = sublist(pList,
"Tempus",
true);
472 pl->sublist(
"Demo Integrator")
473 .sublist(
"Time Step Control").set(
"Initial Time Step", 0.01);
475 pl->sublist(
"Demo Integrator")
476 .sublist(
"Time Step Control")
477 .sublist(
"Time Step Control Strategy").set(
"Reduction Factor", 0.9);
478 pl->sublist(
"Demo Integrator")
479 .sublist(
"Time Step Control")
480 .sublist(
"Time Step Control Strategy").set(
"Amplification Factor", 1.15);
481 pl->sublist(
"Demo Integrator")
482 .sublist(
"Time Step Control")
483 .sublist(
"Time Step Control Strategy").set(
"Minimum Value Monitoring Function", 0.05);
484 pl->sublist(
"Demo Integrator")
485 .sublist(
"Time Step Control")
486 .sublist(
"Time Step Control Strategy").set(
"Maximum Value Monitoring Function", 0.1);
488 pl->sublist(
"Demo Integrator")
489 .sublist(
"Solution History").set(
"Storage Type",
"Static");
490 pl->sublist(
"Demo Integrator")
491 .sublist(
"Solution History").set(
"Storage Limit", 3);
493 RCP<Tempus::IntegratorBasic<double> > integrator =
494 Tempus::createIntegratorBasic<double>(pl, model);
497 bool integratorStatus = integrator->advanceTime();
498 TEST_ASSERT(integratorStatus)
501 double time = integrator->getTime();
502 double timeFinal =pl->sublist(
"Demo Integrator")
503 .sublist(
"Time Step Control").get<
double>(
"Final Time");
504 TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
507 auto state = integrator->getCurrentState();
508 double dt = state->getTimeStep();
509 TEST_FLOATING_EQUALITY(dt, 0.008310677297208358, 1.0e-12);
512 const int numTimeSteps = 60;
513 TEST_EQUALITY(numTimeSteps, integrator->getIndex());
516 RCP<Thyra::VectorBase<double> > x = integrator->getX();
517 RCP<Thyra::VectorBase<double> > x_ref = x->clone_v();
519 Thyra::DetachedVectorView<double> x_ref_view( *x_ref );
520 x_ref_view[0] = -1.931946840284863;
521 x_ref_view[1] = 0.645346748303107;
525 RCP<Thyra::VectorBase<double> > xdiff = x->clone_v();
526 Thyra::V_StVpStV(xdiff.ptr(), 1.0, *x_ref, -1.0, *(x));
529 std::cout <<
" Stepper = ForwardEuler" << std::endl;
530 std::cout <<
" =========================" << std::endl;
531 std::cout <<
" Reference solution: " << get_ele(*(x_ref), 0) <<
" " 532 << get_ele(*(x_ref), 1) << std::endl;
533 std::cout <<
" Computed solution : " << get_ele(*(x ), 0) <<
" " 534 << get_ele(*(x ), 1) << std::endl;
535 std::cout <<
" Difference : " << get_ele(*(xdiff), 0) <<
" " 536 << get_ele(*(xdiff), 1) << std::endl;
537 std::cout <<
" =========================" << std::endl;
538 TEST_FLOATING_EQUALITY(get_ele(*(x), 0), get_ele(*(x_ref), 0), 1.0e-12);
539 TEST_FLOATING_EQUALITY(get_ele(*(x), 1), get_ele(*(x_ref), 1), 1.0e-12);
Teuchos::RCP< SolutionState< Scalar > > createSolutionStateX(const Teuchos::RCP< Thyra::VectorBase< Scalar > > &x, const Teuchos::RCP< Thyra::VectorBase< Scalar > > &xdot=Teuchos::null, const Teuchos::RCP< Thyra::VectorBase< Scalar > > &xdotdot=Teuchos::null)
Nonmember constructor from non-const solution vectors, x.
Forward Euler 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 mechanisms that effect the time step size a...
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...