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;
42 using Teuchos::sublist;
43 using Teuchos::getParametersFromXmlFile;
56 getParametersFromXmlFile(
"Tempus_Trapezoidal_SinCos.xml");
67 Tempus::createIntegratorBasic<double>(tempusPL, model);
72 integrator->getStepper()->getValidParameters();
73 bool pass = haveSameValuesSorted(*stepperPL, *defaultPL,
true);
75 std::cout << std::endl;
76 std::cout <<
"stepperPL -------------- \n" << *stepperPL << std::endl;
77 std::cout <<
"defaultPL -------------- \n" << *defaultPL << std::endl;
85 Tempus::createIntegratorBasic<double>(model,
"Trapezoidal Method");
89 integrator->getStepper()->getValidParameters();
91 bool pass = haveSameValuesSorted(*stepperPL, *defaultPL,
true);
93 std::cout << std::endl;
94 std::cout <<
"stepperPL -------------- \n" << *stepperPL << std::endl;
95 std::cout <<
"defaultPL -------------- \n" << *defaultPL << std::endl;
107 std::vector<std::string> options;
108 options.push_back(
"Default Parameters");
109 options.push_back(
"ICConsistency and Check");
111 for(
const auto& option: options) {
115 getParametersFromXmlFile(
"Tempus_Trapezoidal_SinCos.xml");
138 stepper->setModel(model);
139 if ( option ==
"ICConsistency and Check") {
140 stepper->setICConsistency(
"Consistent");
141 stepper->setICConsistencyCheck(
true);
143 stepper->initialize();
149 timeStepControl->setInitIndex(tscPL.
get<
int> (
"Initial Time Index"));
150 timeStepControl->setInitTime (tscPL.
get<
double>(
"Initial Time"));
151 timeStepControl->setFinalTime(tscPL.
get<
double>(
"Final Time"));
152 timeStepControl->setInitTimeStep(dt);
153 timeStepControl->initialize();
156 auto inArgsIC = model->getNominalValues();
161 icState->setTime (timeStepControl->getInitTime());
162 icState->setIndex (timeStepControl->getInitIndex());
163 icState->setTimeStep(0.0);
164 icState->setOrder (stepper->getOrder());
169 solutionHistory->setName(
"Forward States");
171 solutionHistory->setStorageLimit(2);
172 solutionHistory->addState(icState);
176 Tempus::createIntegratorBasic<double>();
177 integrator->setStepper(stepper);
178 integrator->setTimeStepControl(timeStepControl);
179 integrator->setSolutionHistory(solutionHistory);
181 integrator->initialize();
185 bool integratorStatus = integrator->advanceTime();
190 double time = integrator->getTime();
191 double timeFinal =pl->
sublist(
"Default Integrator")
192 .
sublist(
"Time Step Control").
get<
double>(
"Final Time");
198 model->getExactSolution(time).get_x();
202 Thyra::V_StVpStV(xdiff.
ptr(), 1.0, *x_exact, -1.0, *(x));
205 std::cout <<
" Stepper = " << stepper->description()
206 <<
"\n with " << option << std::endl;
207 std::cout <<
" =========================" << std::endl;
208 std::cout <<
" Exact solution : " << get_ele(*(x_exact), 0) <<
" "
209 << get_ele(*(x_exact), 1) << std::endl;
210 std::cout <<
" Computed solution: " << get_ele(*(x ), 0) <<
" "
211 << get_ele(*(x ), 1) << std::endl;
212 std::cout <<
" Difference : " << get_ele(*(xdiff ), 0) <<
" "
213 << get_ele(*(xdiff ), 1) << std::endl;
214 std::cout <<
" =========================" << std::endl;
215 TEST_FLOATING_EQUALITY(get_ele(*(x), 0), 0.841021, 1.0e-4 );
216 TEST_FLOATING_EQUALITY(get_ele(*(x), 1), 0.541002, 1.0e-4 );
226 std::vector<RCP<Thyra::VectorBase<double>>> solutions;
227 std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
228 std::vector<double> StepSize;
229 std::vector<double> xErrorNorm;
230 std::vector<double> xDotErrorNorm;
231 const int nTimeStepSizes = 7;
234 for (
int n=0; n<nTimeStepSizes; n++) {
238 getParametersFromXmlFile(
"Tempus_Trapezoidal_SinCos.xml");
253 pl->
sublist(
"Default Integrator")
254 .
sublist(
"Time Step Control").
set(
"Initial Time Step", dt);
255 integrator = Tempus::createIntegratorBasic<double>(pl, model);
263 model->getNominalValues().get_x()->clone_v();
265 model->getNominalValues().get_x_dot()->clone_v();
266 integrator->initializeSolutionHistory(0.0, x0, xdot0);
270 bool integratorStatus = integrator->advanceTime();
274 time = integrator->getTime();
275 double timeFinal =pl->sublist(
"Default Integrator")
276 .sublist(
"Time Step Control").
get<
double>(
"Final Time");
282 integrator->getSolutionHistory();
283 writeSolution(
"Tempus_Trapezoidal_SinCos.dat", solutionHistory);
286 for (
int i=0; i<solutionHistory->getNumStates(); i++) {
287 double time_i = (*solutionHistory)[i]->getTime();
290 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;
322 double order = stepper->getOrder();
325 solutions, xErrorNorm, xSlope,
326 solutionsDot, xDotErrorNorm, xDotSlope);
342 std::vector<RCP<Thyra::VectorBase<double>>> solutions;
343 std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
344 std::vector<double> StepSize;
345 std::vector<double> xErrorNorm;
346 std::vector<double> xDotErrorNorm;
347 const int nTimeStepSizes = 4;
350 for (
int n=0; n<nTimeStepSizes; n++) {
354 getParametersFromXmlFile(
"Tempus_Trapezoidal_VanDerPol.xml");
362 if (n == nTimeStepSizes-1) dt /= 10.0;
367 .
sublist(
"Time Step Control").
set(
"Initial Time Step", dt);
368 integrator = Tempus::createIntegratorBasic<double>(pl, model);
371 bool integratorStatus = integrator->advanceTime();
375 time = integrator->getTime();
376 double timeFinal =pl->sublist(
"Demo Integrator")
377 .sublist(
"Time Step Control").
get<
double>(
"Final Time");
378 double tol = 100.0 * std::numeric_limits<double>::epsilon();
382 StepSize.push_back(dt);
383 auto solution = Thyra::createMember(model->get_x_space());
384 Thyra::copy(*(integrator->getX()),solution.ptr());
385 solutions.push_back(solution);
386 auto solutionDot = Thyra::createMember(model->get_x_space());
387 Thyra::copy(*(integrator->getXDot()),solutionDot.ptr());
388 solutionsDot.push_back(solutionDot);
392 if ((n == 0) || (n == nTimeStepSizes-1)) {
393 std::string fname =
"Tempus_Trapezoidal_VanDerPol-Ref.dat";
394 if (n == 0) fname =
"Tempus_Trapezoidal_VanDerPol.dat";
396 integrator->getSolutionHistory();
402 double xDotSlope = 0.0;
404 double order = stepper->getOrder();
407 solutions, xErrorNorm, xSlope,
408 solutionsDot, xDotErrorNorm, xDotSlope);
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.
Trapezoidal method time stepper.
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)
#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="")
Solution state for integrators and steppers. SolutionState contains the metadata for solutions and th...