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 {
31 using Teuchos::rcp_const_cast;
33 using Teuchos::sublist;
34 using Teuchos::getParametersFromXmlFile;
47 getParametersFromXmlFile(
"Tempus_ForwardEuler_SinCos.xml");
62 Tempus::createIntegratorBasic<double>(tempusPL, model);
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;
80 Tempus::createIntegratorBasic<double>(model,
"Forward Euler");
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) {
111 getParametersFromXmlFile(
"Tempus_ForwardEuler_SinCos.xml");
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();
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);
161 Tempus::createIntegratorBasic<double>();
162 integrator->setStepper(stepper);
163 integrator->setTimeStepControl(timeStepControl);
164 integrator->setSolutionHistory(solutionHistory);
166 integrator->initialize();
170 bool integratorStatus = integrator->advanceTime();
175 double time = integrator->getTime();
176 double timeFinal =pl->
sublist(
"Demo Integrator")
177 .
sublist(
"Time Step Control").
get<
double>(
"Final Time");
183 model->getExactSolution(time).get_x();
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 );
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++) {
223 getParametersFromXmlFile(
"Tempus_ForwardEuler_SinCos.xml");
239 .
sublist(
"Time Step Control").
set(
"Initial Time Step", dt);
240 integrator = Tempus::createIntegratorBasic<double>(pl, model);
247 model->getNominalValues().get_x()->clone_v();
248 integrator->initializeSolutionHistory(0.0, x0);
249 integrator->initialize();
252 bool integratorStatus = integrator->advanceTime();
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");
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()),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;
315 double order = stepper->getOrder();
318 solutions, xErrorNorm, xSlope,
319 solutionsDot, xDotErrorNorm, xDotSlope);
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++) {
347 getParametersFromXmlFile(
"Tempus_ForwardEuler_VanDerPol.xml");
355 if (n == nTimeStepSizes-1) dt /= 10.0;
360 .
sublist(
"Time Step Control").
set(
"Initial Time Step", dt);
361 integrator = Tempus::createIntegratorBasic<double>(pl, model);
364 bool integratorStatus = integrator->advanceTime();
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();
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();
401 solutions, xErrorNorm, xSlope,
402 solutionsDot, xDotErrorNorm, xDotSlope);
419 std::vector<double> StepSize;
420 std::vector<double> ErrorNorm;
427 getParametersFromXmlFile(
"Tempus_ForwardEuler_NumberOfTimeSteps.xml");
439 const int numTimeSteps = pl->
sublist(
"Demo Integrator")
441 .
get<
int>(
"Number of Time Steps");
444 Tempus::createIntegratorBasic<double>(pl, model);
447 bool integratorStatus = integrator->advanceTime();
462 getParametersFromXmlFile(
"Tempus_ForwardEuler_VanDerPol.xml");
473 .
sublist(
"Time Step Control").
set(
"Initial Time Step", 0.01);
477 .
sublist(
"Time Step Control Strategy").
set(
"Reduction Factor", 0.9);
480 .
sublist(
"Time Step Control Strategy").
set(
"Amplification Factor", 1.15);
483 .
sublist(
"Time Step Control Strategy").
set(
"Minimum Value Monitoring Function", 0.05);
486 .
sublist(
"Time Step Control Strategy").
set(
"Maximum Value Monitoring Function", 0.1);
489 .
sublist(
"Solution History").
set(
"Storage Type",
"Static");
491 .
sublist(
"Solution History").
set(
"Storage Limit", 3);
494 Tempus::createIntegratorBasic<double>(pl, model);
497 bool integratorStatus = integrator->advanceTime();
501 double time = integrator->getTime();
502 double timeFinal =pl->sublist(
"Demo Integrator")
503 .sublist(
"Time Step Control").
get<
double>(
"Final Time");
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;
520 x_ref_view[0] = -1.931946840284863;
521 x_ref_view[1] = 0.645346748303107;
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.
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.
#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)
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)
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. SolutionState contains the metadata for solutions and th...