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"
37 using Teuchos::ParameterList;
38 using Teuchos::sublist;
39 using Teuchos::getParametersFromXmlFile;
47 #define TEST_CONSTRUCTING_FROM_DEFAULTS
51 #ifdef TEST_CONSTRUCTING_FROM_DEFAULTS
59 RCP<ParameterList> pList =
60 getParametersFromXmlFile(
"Tempus_Leapfrog_SinCos.xml");
61 RCP<ParameterList> pl = sublist(pList,
"Tempus",
true);
64 RCP<ParameterList> hom_pl = sublist(pList,
"HarmonicOscillatorModel",
true);
65 RCP<HarmonicOscillatorModel<double> > model =
69 RCP<Tempus::StepperLeapfrog<double> > stepper =
73 RCP<Tempus::TimeStepControl<double> > timeStepControl =
75 ParameterList tscPL = pl->sublist(
"Default Integrator")
76 .sublist(
"Time Step Control");
77 timeStepControl->setStepType (tscPL.get<std::string>(
"Integrator Step Type"));
78 timeStepControl->setInitIndex(tscPL.get<
int> (
"Initial Time Index"));
79 timeStepControl->setInitTime (tscPL.get<
double>(
"Initial Time"));
80 timeStepControl->setFinalTime(tscPL.get<
double>(
"Final Time"));
81 timeStepControl->setInitTimeStep(dt);
82 timeStepControl->initialize();
85 using Teuchos::rcp_const_cast;
86 Thyra::ModelEvaluatorBase::InArgs<double> inArgsIC =
87 stepper->getModel()->getNominalValues();
88 RCP<Thyra::VectorBase<double> > icX =
89 rcp_const_cast<Thyra::VectorBase<double> > (inArgsIC.get_x());
90 RCP<Thyra::VectorBase<double> > icXDot =
91 rcp_const_cast<Thyra::VectorBase<double> > (inArgsIC.get_x_dot());
92 RCP<Thyra::VectorBase<double> > icXDotDot =
93 rcp_const_cast<Thyra::VectorBase<double> > (inArgsIC.get_x_dot_dot());
94 RCP<Tempus::SolutionState<double> > icState =
96 icState->setTime (timeStepControl->getInitTime());
97 icState->setIndex (timeStepControl->getInitIndex());
98 icState->setTimeStep(0.0);
99 icState->setOrder (stepper->getOrder());
111 RCP<Tempus::IntegratorBasic<double> > integrator =
112 Tempus::integratorBasic<double>();
113 integrator->setStepperWStepper(stepper);
114 integrator->setTimeStepControl(timeStepControl);
117 integrator->initialize();
121 bool integratorStatus = integrator->advanceTime();
122 TEST_ASSERT(integratorStatus)
126 double time = integrator->getTime();
127 double timeFinal =pl->sublist(
"Default Integrator")
128 .sublist(
"Time Step Control").get<
double>(
"Final Time");
129 TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
132 RCP<Thyra::VectorBase<double> > x = integrator->getX();
133 RCP<const Thyra::VectorBase<double> > x_exact =
134 model->getExactSolution(time).get_x();
137 RCP<Thyra::VectorBase<double> > xdiff = x->clone_v();
138 Thyra::V_StVpStV(xdiff.ptr(), 1.0, *x_exact, -1.0, *(x));
141 std::cout <<
" Stepper = " << stepper->description() << std::endl;
142 std::cout <<
" =========================" << std::endl;
143 std::cout <<
" Exact solution : " << get_ele(*(x_exact), 0) << std::endl;
144 std::cout <<
" Computed solution: " << get_ele(*(x ), 0) << std::endl;
145 std::cout <<
" Difference : " << get_ele(*(xdiff ), 0) << std::endl;
146 std::cout <<
" =========================" << std::endl;
147 TEST_FLOATING_EQUALITY(get_ele(*(x), 0), 0.167158, 1.0e-4 );
149 #endif // TEST_CONSTRUCTING_FROM_DEFAULTS
157 RCP<Tempus::IntegratorBasic<double> > integrator;
158 std::vector<RCP<Thyra::VectorBase<double>>> solutions;
159 std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
160 std::vector<double> StepSize;
161 std::vector<double> xErrorNorm;
162 std::vector<double> xDotErrorNorm;
163 const int nTimeStepSizes = 9;
167 RCP<ParameterList> pList =
168 getParametersFromXmlFile(
"Tempus_Leapfrog_SinCos.xml");
171 RCP<ParameterList> hom_pl = sublist(pList,
"HarmonicOscillatorModel",
true);
172 RCP<HarmonicOscillatorModel<double> > model =
177 RCP<ParameterList> pl = sublist(pList,
"Tempus",
true);
180 double dt =pl->sublist(
"Default Integrator")
181 .sublist(
"Time Step Control").get<
double>(
"Initial Time Step");
184 for (
int n=0; n<nTimeStepSizes; n++) {
188 std::cout <<
"\n \n time step #" << n
189 <<
" (out of " << nTimeStepSizes-1 <<
"), dt = " << dt <<
"\n";
190 pl->sublist(
"Default Integrator")
191 .sublist(
"Time Step Control").set(
"Initial Time Step", dt);
192 integrator = Tempus::integratorBasic<double>(pl, model);
195 bool integratorStatus = integrator->advanceTime();
196 TEST_ASSERT(integratorStatus)
199 time = integrator->getTime();
200 double timeFinal =pl->sublist(
"Default Integrator")
201 .sublist(
"Time Step Control").get<
double>(
"Final Time");
202 TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
205 if (n == nTimeStepSizes-1) {
207 integrator->getSolutionHistory();
210 RCP<Tempus::SolutionHistory<double> > solnHistExact =
213 double time = (*solutionHistory)[i]->getTime();
214 RCP<Tempus::SolutionState<double> > state =
216 model->getExactSolution(time).get_x(),
217 model->getExactSolution(time).get_x_dot()));
219 solnHistExact->addState(state);
221 writeSolution(
"Tempus_Leapfrog_SinCos-Ref.dat", solnHistExact);
227 StepSize.push_back(dt);
228 auto solution = Thyra::createMember(model->get_x_space());
229 Thyra::copy(*(integrator->getX()),solution.ptr());
230 solutions.push_back(solution);
231 auto solutionDot = Thyra::createMember(model->get_x_space());
232 Thyra::copy(*(integrator->getXdot()),solutionDot.ptr());
233 solutionsDot.push_back(solutionDot);
234 if (n == nTimeStepSizes-1) {
235 StepSize.push_back(0.0);
236 auto solution = Thyra::createMember(model->get_x_space());
237 Thyra::copy(*(model->getExactSolution(time).get_x()),solution.ptr());
238 solutions.push_back(solution);
239 auto solutionDot = Thyra::createMember(model->get_x_space());
240 Thyra::copy(*(model->getExactSolution(time).get_x_dot()),
242 solutionsDot.push_back(solutionDot);
248 double xDotSlope = 0.0;
249 RCP<Tempus::Stepper<double> > stepper = integrator->getStepper();
250 double order = stepper->getOrder();
253 solutions, xErrorNorm, xSlope,
254 solutionsDot, xDotErrorNorm, xDotSlope);
256 TEST_FLOATING_EQUALITY( xSlope, order, 0.02 );
257 TEST_FLOATING_EQUALITY( xErrorNorm[0], 0.0157928, 1.0e-4 );
258 TEST_FLOATING_EQUALITY( xDotSlope, 1.09387, 0.01 );
259 TEST_FLOATING_EQUALITY( xDotErrorNorm[0], 0.563002, 1.0e-4 );
261 Teuchos::TimeMonitor::summarize();
263 #endif // TEST_SINCOS