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"
27 namespace Tempus_Test {
29 using Teuchos::getParametersFromXmlFile;
33 using Teuchos::rcp_const_cast;
34 using Teuchos::sublist;
46 getParametersFromXmlFile(
"Tempus_ForwardEuler_SinCos.xml");
57 Tempus::createIntegratorBasic<double>(tempusPL, model);
61 integrator->getStepper()->getValidParameters();
63 bool pass = haveSameValuesSorted(*stepperPL, *defaultPL,
true);
66 out <<
"stepperPL -------------- \n"
67 << *stepperPL << std::endl;
68 out <<
"defaultPL -------------- \n"
69 << *defaultPL << std::endl;
77 Tempus::createIntegratorBasic<double>(model,
78 std::string(
"Forward Euler"));
82 integrator->getStepper()->getValidParameters();
84 bool pass = haveSameValuesSorted(*stepperPL, *defaultPL,
true);
87 out <<
"stepperPL -------------- \n"
88 << *stepperPL << std::endl;
89 out <<
"defaultPL -------------- \n"
90 << *defaultPL << std::endl;
101 std::vector<std::string> options;
102 options.push_back(
"useFSAL=true");
103 options.push_back(
"useFSAL=false");
104 options.push_back(
"ICConsistency and Check");
106 for (
const auto& option : options) {
109 getParametersFromXmlFile(
"Tempus_ForwardEuler_SinCos.xml");
119 stepper->setModel(model);
120 if (option ==
"useFSAL=true")
121 stepper->setUseFSAL(
true);
122 else if (option ==
"useFSAL=false")
123 stepper->setUseFSAL(
false);
124 else if (option ==
"ICConsistency and Check") {
125 stepper->setICConsistency(
"Consistent");
126 stepper->setICConsistencyCheck(
true);
128 stepper->initialize();
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();
145 icState->setTime(timeStepControl->getInitTime());
146 icState->setIndex(timeStepControl->getInitIndex());
147 icState->setTimeStep(0.0);
152 solutionHistory->setName(
"Forward States");
154 solutionHistory->setStorageLimit(2);
155 solutionHistory->addState(icState);
158 stepper->setInitialConditions(solutionHistory);
162 Tempus::createIntegratorBasic<double>();
163 integrator->setStepper(stepper);
164 integrator->setTimeStepControl(timeStepControl);
165 integrator->setSolutionHistory(solutionHistory);
167 integrator->initialize();
170 bool integratorStatus = integrator->advanceTime();
174 double time = integrator->getTime();
175 double timeFinal = pl->
sublist(
"Demo Integrator")
177 .
get<
double>(
"Final Time");
183 model->getExactSolution(time).get_x();
187 Thyra::V_StVpStV(xdiff.
ptr(), 1.0, *x_exact, -1.0, *(x));
190 out <<
" Stepper = " << stepper->description() <<
"\n with "
191 << option << std::endl;
192 out <<
" =========================" << std::endl;
193 out <<
" Exact solution : " << get_ele(*(x_exact), 0) <<
" "
194 << get_ele(*(x_exact), 1) << std::endl;
195 out <<
" Computed solution: " << get_ele(*(x), 0) <<
" "
196 << get_ele(*(x), 1) << std::endl;
197 out <<
" Difference : " << get_ele(*(xdiff), 0) <<
" "
198 << get_ele(*(xdiff), 1) << std::endl;
199 out <<
" =========================" << 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 std::vector<RCP<Thyra::VectorBase<double>>> solutions;
211 std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
212 std::vector<double> StepSize;
213 std::vector<double> xErrorNorm;
214 std::vector<double> xDotErrorNorm;
215 const int nTimeStepSizes = 7;
218 for (
int n = 0; n < nTimeStepSizes; n++) {
221 getParametersFromXmlFile(
"Tempus_ForwardEuler_SinCos.xml");
238 .
set(
"Initial Time Step", dt);
239 integrator = Tempus::createIntegratorBasic<double>(pl, model);
246 model->getNominalValues().get_x()->clone_v();
247 integrator->initializeSolutionHistory(0.0, x0);
248 integrator->initialize();
251 bool integratorStatus = integrator->advanceTime();
256 integrator->getSolutionHistory()->getCurrentState()->getPhysicsState();
257 TEST_EQUALITY(physicsState->getName(),
"Tempus::PhysicsState");
260 time = integrator->getTime();
261 double timeFinal = pl->sublist(
"Demo Integrator")
262 .sublist(
"Time Step Control")
263 .
get<
double>(
"Final Time");
269 model->getExactSolution(time).get_x();
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()),
303 solutionExact.ptr());
304 solutions.push_back(solutionExact);
305 auto solutionDotExact = Thyra::createMember(model->get_x_space());
306 Thyra::copy(*(model->getExactSolution(time).get_x_dot()),
307 solutionDotExact.ptr());
308 solutionsDot.push_back(solutionDotExact);
314 double xDotSlope = 0.0;
316 double order = stepper->getOrder();
317 writeOrderError(
"Tempus_ForwardEuler_SinCos-Error.dat", stepper, StepSize,
318 solutions, xErrorNorm, xSlope, solutionsDot, xDotErrorNorm,
335 std::vector<RCP<Thyra::VectorBase<double>>> solutions;
336 std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
337 std::vector<double> StepSize;
338 std::vector<double> xErrorNorm;
339 std::vector<double> xDotErrorNorm;
340 const int nTimeStepSizes = 7;
342 for (
int n = 0; n < nTimeStepSizes; n++) {
345 getParametersFromXmlFile(
"Tempus_ForwardEuler_VanDerPol.xml");
353 if (n == nTimeStepSizes - 1) dt /= 10.0;
359 .
set(
"Initial Time Step", dt);
360 integrator = Tempus::createIntegratorBasic<double>(pl, model);
363 bool integratorStatus = integrator->advanceTime();
367 double time = integrator->getTime();
368 double timeFinal = pl->sublist(
"Demo Integrator")
369 .sublist(
"Time Step Control")
370 .
get<
double>(
"Final Time");
371 double tol = 100.0 * std::numeric_limits<double>::epsilon();
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";
389 integrator->getSolutionHistory();
396 double xDotSlope = 0.0;
398 double order = stepper->getOrder();
399 writeOrderError(
"Tempus_ForwardEuler_VanDerPol-Error.dat", stepper, StepSize,
400 solutions, xErrorNorm, xSlope, solutionsDot, xDotErrorNorm,
416 std::vector<double> StepSize;
417 std::vector<double> ErrorNorm;
424 getParametersFromXmlFile(
"Tempus_ForwardEuler_NumberOfTimeSteps.xml");
436 const int numTimeSteps = pl->
sublist(
"Demo Integrator")
438 .
get<
int>(
"Number of Time Steps");
441 Tempus::createIntegratorBasic<double>(pl, model);
444 bool integratorStatus = integrator->advanceTime();
458 getParametersFromXmlFile(
"Tempus_ForwardEuler_VanDerPol.xml");
470 .
set(
"Initial Time Step", 0.01);
474 .
sublist(
"Time Step Control Strategy")
475 .
set(
"Reduction Factor", 0.9);
478 .
sublist(
"Time Step Control Strategy")
479 .
set(
"Amplification Factor", 1.15);
482 .
sublist(
"Time Step Control Strategy")
483 .
set(
"Minimum Value Monitoring Function", 0.05);
486 .
sublist(
"Time Step Control Strategy")
487 .
set(
"Maximum Value Monitoring Function", 0.1);
491 .
set(
"Storage Type",
"Static");
494 .
set(
"Storage Limit", 3);
497 Tempus::createIntegratorBasic<double>(pl, model);
500 bool integratorStatus = integrator->advanceTime();
504 double time = integrator->getTime();
505 double timeFinal = pl->sublist(
"Demo Integrator")
506 .sublist(
"Time Step Control")
507 .
get<
double>(
"Final Time");
511 auto state = integrator->getCurrentState();
512 double dt = state->getTimeStep();
513 TEST_FLOATING_EQUALITY(dt, 0.008310677297208358, 1.0e-12);
516 const int numTimeSteps = 60;
524 x_ref_view[0] = -1.931946840284863;
525 x_ref_view[1] = 0.645346748303107;
530 Thyra::V_StVpStV(xdiff.
ptr(), 1.0, *x_ref, -1.0, *(x));
533 out <<
" Stepper = ForwardEuler" << std::endl;
534 out <<
" =========================" << std::endl;
535 out <<
" Reference solution: " << get_ele(*(x_ref), 0) <<
" "
536 << get_ele(*(x_ref), 1) << std::endl;
537 out <<
" Computed solution : " << get_ele(*(x), 0) <<
" "
538 << get_ele(*(x), 1) << std::endl;
539 out <<
" Difference : " << get_ele(*(xdiff), 0) <<
" "
540 << get_ele(*(xdiff), 1) << std::endl;
541 out <<
" =========================" << std::endl;
542 TEST_FLOATING_EQUALITY(get_ele(*(x), 0), get_ele(*(x_ref), 0), 1.0e-12);
543 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.
T & get(const std::string &name, T def_value)
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Forward Euler time stepper.
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)
#define TEST_FLOATING_EQUALITY(v1, v2, tol)
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)
TEUCHOS_UNIT_TEST(BackwardEuler, SinCos_ASA)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
static void summarize(Ptr< const Comm< int > > comm, std::ostream &out=std::cout, const bool alwaysWriteLocal=false, const bool writeGlobalStats=true, const bool writeZeroTimers=true, const ECounterSetOp setOp=Intersection, const std::string &filter="", const bool ignoreZeroTimers=false)
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.
ParameterList & sublist(const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
#define TEST_EQUALITY(v1, v2)
Solution state for integrators and steppers.