9 #include "Teuchos_UnitTestHarness.hpp"
10 #include "Teuchos_XMLParameterListHelpers.hpp"
11 #include "Teuchos_TimeMonitor.hpp"
13 #include "Thyra_VectorStdOps.hpp"
15 #include "Tempus_config.hpp"
16 #include "Tempus_IntegratorBasic.hpp"
17 #include "Tempus_StepperLeapfrog.hpp"
19 #include "../TestModels/HarmonicOscillatorModel.hpp"
20 #include "../TestUtils/Tempus_ConvergenceTestUtils.hpp"
23 #ifdef Tempus_ENABLE_MPI
24 #include "Epetra_MpiComm.h"
26 #include "Epetra_SerialComm.h"
34 namespace Tempus_Test {
38 using Teuchos::rcp_const_cast;
39 using Teuchos::ParameterList;
40 using Teuchos::sublist;
41 using Teuchos::getParametersFromXmlFile;
49 #define TEST_CONSTRUCTING_FROM_DEFAULTS
53 #ifdef TEST_CONSTRUCTING_FROM_DEFAULTS
61 RCP<ParameterList> pList =
62 getParametersFromXmlFile(
"Tempus_Leapfrog_SinCos.xml");
63 RCP<ParameterList> pl = sublist(pList,
"Tempus",
true);
66 RCP<ParameterList> hom_pl = sublist(pList,
"HarmonicOscillatorModel",
true);
71 stepper->setModel(model);
72 stepper->initialize();
76 ParameterList tscPL = pl->sublist(
"Default Integrator")
77 .sublist(
"Time Step Control");
78 timeStepControl->setStepType (tscPL.get<std::string>(
"Integrator Step Type"));
79 timeStepControl->setInitIndex(tscPL.get<
int> (
"Initial Time Index"));
80 timeStepControl->setInitTime (tscPL.get<
double>(
"Initial Time"));
81 timeStepControl->setFinalTime(tscPL.get<
double>(
"Final Time"));
82 timeStepControl->setInitTimeStep(dt);
83 timeStepControl->initialize();
86 Thyra::ModelEvaluatorBase::InArgs<double> inArgsIC =
87 stepper->getModel()->getNominalValues();
88 auto icX = rcp_const_cast<Thyra::VectorBase<double> > (inArgsIC.get_x());
89 auto icXDot = rcp_const_cast<Thyra::VectorBase<double> > (inArgsIC.get_x_dot());
90 auto icXDotDot = rcp_const_cast<Thyra::VectorBase<double> > (inArgsIC.get_x_dot_dot());
92 icState->setTime (timeStepControl->getInitTime());
93 icState->setIndex (timeStepControl->getInitIndex());
94 icState->setTimeStep(0.0);
95 icState->setOrder (stepper->getOrder());
106 RCP<Tempus::IntegratorBasic<double> > integrator =
107 Tempus::integratorBasic<double>();
108 integrator->setStepperWStepper(stepper);
109 integrator->setTimeStepControl(timeStepControl);
112 integrator->initialize();
116 bool integratorStatus = integrator->advanceTime();
117 TEST_ASSERT(integratorStatus)
121 double time = integrator->getTime();
122 double timeFinal =pl->sublist(
"Default Integrator")
123 .sublist(
"Time Step Control").get<
double>(
"Final Time");
124 TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
127 RCP<Thyra::VectorBase<double> > x = integrator->getX();
128 RCP<const Thyra::VectorBase<double> > x_exact =
129 model->getExactSolution(time).get_x();
132 RCP<Thyra::VectorBase<double> > xdiff = x->clone_v();
133 Thyra::V_StVpStV(xdiff.ptr(), 1.0, *x_exact, -1.0, *(x));
136 std::cout <<
" Stepper = " << stepper->description() << std::endl;
137 std::cout <<
" =========================" << std::endl;
138 std::cout <<
" Exact solution : " << get_ele(*(x_exact), 0) << std::endl;
139 std::cout <<
" Computed solution: " << get_ele(*(x ), 0) << std::endl;
140 std::cout <<
" Difference : " << get_ele(*(xdiff ), 0) << std::endl;
141 std::cout <<
" =========================" << std::endl;
142 TEST_FLOATING_EQUALITY(get_ele(*(x), 0), 0.167158, 1.0e-4 );
144 #endif // TEST_CONSTRUCTING_FROM_DEFAULTS
152 RCP<Tempus::IntegratorBasic<double> > integrator;
153 std::vector<RCP<Thyra::VectorBase<double>>> solutions;
154 std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
155 std::vector<double> StepSize;
156 std::vector<double> xErrorNorm;
157 std::vector<double> xDotErrorNorm;
158 const int nTimeStepSizes = 9;
162 RCP<ParameterList> pList =
163 getParametersFromXmlFile(
"Tempus_Leapfrog_SinCos.xml");
166 RCP<ParameterList> hom_pl = sublist(pList,
"HarmonicOscillatorModel",
true);
171 RCP<ParameterList> pl = sublist(pList,
"Tempus",
true);
174 double dt =pl->sublist(
"Default Integrator")
175 .sublist(
"Time Step Control").get<
double>(
"Initial Time Step");
178 for (
int n=0; n<nTimeStepSizes; n++) {
182 std::cout <<
"\n \n time step #" << n
183 <<
" (out of " << nTimeStepSizes-1 <<
"), dt = " << dt <<
"\n";
184 pl->sublist(
"Default Integrator")
185 .sublist(
"Time Step Control").set(
"Initial Time Step", dt);
186 integrator = Tempus::integratorBasic<double>(pl, model);
189 bool integratorStatus = integrator->advanceTime();
190 TEST_ASSERT(integratorStatus)
193 time = integrator->getTime();
194 double timeFinal =pl->sublist(
"Default Integrator")
195 .sublist(
"Time Step Control").get<
double>(
"Final Time");
196 TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
199 if (n == nTimeStepSizes-1) {
201 integrator->getSolutionHistory();
202 writeSolution(
"Tempus_Leapfrog_SinCos.dat", solutionHistory);
205 for (
int i=0; i<solutionHistory->getNumStates(); i++) {
206 double time_i = (*solutionHistory)[i]->getTime();
208 model->getExactSolution(time_i).get_x(),
209 model->getExactSolution(time_i).get_x_dot()));
210 state->setTime((*solutionHistory)[i]->getTime());
211 solnHistExact->addState(state);
213 writeSolution(
"Tempus_Leapfrog_SinCos-Ref.dat", solnHistExact);
219 StepSize.push_back(dt);
220 auto solution = Thyra::createMember(model->get_x_space());
221 Thyra::copy(*(integrator->getX()),solution.ptr());
222 solutions.push_back(solution);
223 auto solutionDot = Thyra::createMember(model->get_x_space());
224 Thyra::copy(*(integrator->getXdot()),solutionDot.ptr());
225 solutionsDot.push_back(solutionDot);
226 if (n == nTimeStepSizes-1) {
227 StepSize.push_back(0.0);
228 auto solutionExact = Thyra::createMember(model->get_x_space());
229 Thyra::copy(*(model->getExactSolution(time).get_x()),solutionExact.ptr());
230 solutions.push_back(solutionExact);
231 auto solutionDotExact = Thyra::createMember(model->get_x_space());
232 Thyra::copy(*(model->getExactSolution(time).get_x_dot()),
233 solutionDotExact.ptr());
234 solutionsDot.push_back(solutionDotExact);
240 double xDotSlope = 0.0;
241 RCP<Tempus::Stepper<double> > stepper = integrator->getStepper();
242 double order = stepper->getOrder();
245 solutions, xErrorNorm, xSlope,
246 solutionsDot, xDotErrorNorm, xDotSlope);
248 TEST_FLOATING_EQUALITY( xSlope, order, 0.02 );
249 TEST_FLOATING_EQUALITY( xErrorNorm[0], 0.0157928, 1.0e-4 );
250 TEST_FLOATING_EQUALITY( xDotSlope, 1.09387, 0.01 );
251 TEST_FLOATING_EQUALITY( xDotErrorNorm[0], 0.563002, 1.0e-4 );
253 Teuchos::TimeMonitor::summarize();
255 #endif // TEST_SINCOS
void writeSolution(const std::string filename, Teuchos::RCP< const Tempus::SolutionHistory< Scalar > > solutionHistory)
Consider the ODE: where is a constant, is a constant damping parameter, is a constant forcing par...
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.
Solution state for integrators and steppers. SolutionState contains the metadata for solutions and th...