9 #include "Teuchos_UnitTestHarness.hpp"
10 #include "Teuchos_XMLParameterListHelpers.hpp"
11 #include "Teuchos_TimeMonitor.hpp"
13 #include "Thyra_VectorStdOps.hpp"
15 #include "Tempus_IntegratorBasic.hpp"
18 #include "Tempus_StepperSubcycling.hpp"
21 #include "../TestModels/SinCosModel.hpp"
22 #include "../TestModels/VanDerPol_IMEX_ExplicitModel.hpp"
23 #include "../TestModels/VanDerPol_IMEX_ImplicitModel.hpp"
24 #include "../TestUtils/Tempus_ConvergenceTestUtils.hpp"
29 namespace Tempus_Test {
33 using Teuchos::rcp_const_cast;
34 using Teuchos::ParameterList;
35 using Teuchos::sublist;
36 using Teuchos::getParametersFromXmlFile;
45 #define TEST_CONSTRUCTING_FROM_DEFAULTS
46 #define TEST_SINCOS_ADAPT
47 #define TEST_VANDERPOL_OPERATOR_SPLIT
50 #ifdef TEST_PARAMETERLIST
56 RCP<ParameterList> pList =
57 getParametersFromXmlFile(
"Tempus_Subcycling_SinCos.xml");
64 RCP<ParameterList> scm_pl = sublist(pList,
"SinCosModel",
true);
65 auto model = rcp(
new SinCosModel<double> (scm_pl));
67 RCP<ParameterList> tempusPL = sublist(pList,
"Tempus",
true);
71 RCP<Tempus::IntegratorBasic<double> > integrator =
72 Tempus::integratorBasic<double>(tempusPL, model);
74 RCP<ParameterList> stepperPL = sublist(tempusPL,
"Demo Stepper",
true);
75 RCP<const ParameterList> defaultPL =
76 integrator->getStepper()->getValidParameters();
78 bool pass = haveSameValues(*stepperPL, *defaultPL,
true);
80 std::cout << std::endl;
81 std::cout <<
"stepperPL -------------- \n" << *stepperPL << std::endl;
82 std::cout <<
"defaultPL -------------- \n" << *defaultPL << std::endl;
89 RCP<Tempus::IntegratorBasic<double> > integrator =
90 Tempus::integratorBasic<double>(model,
"Forward Euler");
92 RCP<ParameterList> stepperPL = sublist(tempusPL,
"Demo Stepper",
true);
93 RCP<const ParameterList> defaultPL =
94 integrator->getStepper()->getValidParameters();
96 bool pass = haveSameValues(*stepperPL, *defaultPL,
true);
98 std::cout << std::endl;
99 std::cout <<
"stepperPL -------------- \n" << *stepperPL << std::endl;
100 std::cout <<
"defaultPL -------------- \n" << *defaultPL << std::endl;
105 #endif // TEST_PARAMETERLIST
108 #ifdef TEST_CONSTRUCTING_FROM_DEFAULTS
121 auto stepperFE = sf->createStepperForwardEuler(model, Teuchos::null);
122 stepper->setSubcyclingStepper(stepperFE);
124 stepper->setSubcyclingMinTimeStep (0.1);
125 stepper->setSubcyclingInitTimeStep (0.1);
126 stepper->setSubcyclingMaxTimeStep (0.1);
127 stepper->setSubcyclingStepType (
"Constant");
128 stepper->setSubcyclingMaxFailures (10);
129 stepper->setSubcyclingMaxConsecFailures(5);
130 stepper->setSubcyclingScreenOutputIndexInterval(1);
132 stepper->initialize();
136 timeStepControl->setStepType (
"Constant");
137 timeStepControl->setInitIndex(0);
138 timeStepControl->setInitTime (0.0);
139 timeStepControl->setFinalTime(1.0);
140 timeStepControl->setInitTimeStep(dt);
141 timeStepControl->initialize();
144 Thyra::ModelEvaluatorBase::InArgs<double> inArgsIC =
145 stepper->getModel()->getNominalValues();
146 auto icSolution = rcp_const_cast<Thyra::VectorBase<double> > (inArgsIC.get_x());
148 icState->setTime (timeStepControl->getInitTime());
149 icState->setIndex (timeStepControl->getInitIndex());
150 icState->setTimeStep(0.0);
161 RCP<Tempus::IntegratorBasic<double> > integrator =
162 Tempus::integratorBasic<double>();
163 integrator->setStepperWStepper(stepper);
164 integrator->setTimeStepControl(timeStepControl);
166 integrator->setScreenOutputIndexInterval(1);
168 integrator->initialize();
172 bool integratorStatus = integrator->advanceTime();
173 TEST_ASSERT(integratorStatus)
177 double time = integrator->getTime();
178 double timeFinal = 1.0;
179 TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
182 RCP<Thyra::VectorBase<double> > x = integrator->getX();
183 RCP<const Thyra::VectorBase<double> > x_exact =
184 model->getExactSolution(time).get_x();
187 RCP<Thyra::VectorBase<double> > xdiff = x->clone_v();
188 Thyra::V_StVpStV(xdiff.ptr(), 1.0, *x_exact, -1.0, *(x));
191 std::cout <<
" Stepper = " << stepper->description() << 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 );
203 #endif // TEST_CONSTRUCTING_FROM_DEFAULTS
206 #ifdef TEST_SINCOS_ADAPT
211 RCP<Tempus::IntegratorBasic<double> > integrator;
212 std::vector<RCP<Thyra::VectorBase<double>>> solutions;
213 std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
214 std::vector<double> StepSize;
219 const int nTimeStepSizes = 2;
220 std::string output_file_string =
"Tempus_Subcycling_SinCos";
221 std::string output_file_name = output_file_string +
".dat";
222 std::string err_out_file_name = output_file_string +
"-Error.dat";
224 for (
int n=0; n<nTimeStepSizes; n++) {
234 auto stepperFE = sf->createStepperForwardEuler(model, Teuchos::null);
235 stepper->setSubcyclingStepper(stepperFE);
237 stepper->setSubcyclingMinTimeStep (dt/10.0);
238 stepper->setSubcyclingInitTimeStep (dt/10.0);
239 stepper->setSubcyclingMaxTimeStep (dt);
240 stepper->setSubcyclingStepType (
"Variable");
241 stepper->setSubcyclingMaxFailures (10);
242 stepper->setSubcyclingMaxConsecFailures(5);
243 stepper->setSubcyclingScreenOutputIndexInterval(1);
247 strategy->setMinEta(0.02);
248 strategy->setMaxEta(0.04);
249 stepper->setSubcyclingTimeStepControlStrategy(strategy);
251 stepper->initialize();
255 timeStepControl->setStepType (
"Constant");
256 timeStepControl->setInitIndex(0);
257 timeStepControl->setInitTime (0.0);
258 timeStepControl->setFinalTime(1.0);
259 timeStepControl->setMinTimeStep (dt);
260 timeStepControl->setInitTimeStep(dt);
261 timeStepControl->setMaxTimeStep (dt);
262 timeStepControl->initialize();
265 Thyra::ModelEvaluatorBase::InArgs<double> inArgsIC =
266 stepper->getModel()->getNominalValues();
267 auto icSolution = rcp_const_cast<Thyra::VectorBase<double> > (inArgsIC.get_x());
269 icState->setTime (timeStepControl->getInitTime());
270 icState->setIndex (timeStepControl->getInitIndex());
271 icState->setTimeStep(0.0);
282 integrator = Tempus::integratorBasic<double>();
283 integrator->setStepperWStepper(stepper);
284 integrator->setTimeStepControl(timeStepControl);
286 integrator->setScreenOutputIndexInterval(1);
288 integrator->initialize();
292 bool integratorStatus = integrator->advanceTime();
293 TEST_ASSERT(integratorStatus)
296 time = integrator->getTime();
297 double timeFinal = 1.0;
298 TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
301 RCP<Thyra::VectorBase<double> > x = integrator->getX();
302 RCP<const Thyra::VectorBase<double> > x_exact =
303 model->getExactSolution(time).get_x();
334 StepSize.push_back(dt);
335 auto solution = Thyra::createMember(model->get_x_space());
336 Thyra::copy(*(integrator->getX()),solution.ptr());
337 solutions.push_back(solution);
338 if (n == nTimeStepSizes-1) {
339 StepSize.push_back(0.0);
340 auto solutionExact = Thyra::createMember(model->get_x_space());
341 Thyra::copy(*(model->getExactSolution(time).get_x()),solutionExact.ptr());
342 solutions.push_back(solutionExact);
347 if (nTimeStepSizes > 1) {
349 double xDotSlope = 0.0;
350 std::vector<double> xErrorNorm;
351 std::vector<double> xDotErrorNorm;
352 RCP<Tempus::Stepper<double> > stepper = integrator->getStepper();
356 solutions, xErrorNorm, xSlope,
357 solutionsDot, xDotErrorNorm, xDotSlope);
359 TEST_FLOATING_EQUALITY( xSlope, 1.00137, 0.01 );
361 TEST_FLOATING_EQUALITY( xErrorNorm[0], 0.00387948, 1.0e-4 );
365 Teuchos::TimeMonitor::summarize();
367 #endif // TEST_SINCOS_ADAPT
370 #ifdef TEST_VANDERPOL_OPERATOR_SPLIT
375 RCP<Tempus::IntegratorBasic<double> > integrator;
376 std::vector<RCP<Thyra::VectorBase<double>>> solutions;
377 std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
378 std::vector<double> StepSize;
379 std::vector<double> xErrorNorm;
380 std::vector<double> xDotErrorNorm;
381 const int nTimeStepSizes = 4;
384 for (
int n=0; n<nTimeStepSizes; n++) {
388 if (n == nTimeStepSizes-1) dt /= 10.0;
392 auto pl = Teuchos::rcp_const_cast<Teuchos::ParameterList> (
393 tmpModel->getValidParameters());
394 pl->set(
"Coeff epsilon", 0.1);
403 auto stepperFE = sf->createStepperForwardEuler(explicitModel,Teuchos::null);
404 stepperFE->setUseFSAL(
false);
405 stepperSC->setSubcyclingStepper(stepperFE);
407 stepperSC->setSubcyclingMinTimeStep (0.00001);
408 stepperSC->setSubcyclingInitTimeStep (dt/10.0);
409 stepperSC->setSubcyclingMaxTimeStep (dt/10.0);
410 stepperSC->setSubcyclingMaxFailures (10);
411 stepperSC->setSubcyclingMaxConsecFailures(5);
412 stepperSC->setSubcyclingScreenOutputIndexInterval(1);
414 stepperSC->setSubcyclingStepType (
"Variable");
416 strategySC->setMinEta(0.000001);
417 strategySC->setMaxEta(0.01);
418 stepperSC->setSubcyclingTimeStepControlStrategy(strategySC);
419 stepperSC->initialize();
423 sf->createStepperBackwardEuler(implicitModel, Teuchos::null);
427 stepper->addStepper(stepperSC);
428 stepper->addStepper(stepperBE);
429 stepper->initialize();
433 timeStepControl->setInitIndex(0);
434 timeStepControl->setInitTime (0.0);
436 timeStepControl->setFinalTime(2.0);
437 timeStepControl->setMinTimeStep (0.000001);
438 timeStepControl->setStepType (
"Constant");
439 timeStepControl->setInitTimeStep(dt);
440 timeStepControl->setMaxTimeStep (dt);
451 timeStepControl->initialize();
454 Thyra::ModelEvaluatorBase::InArgs<double> inArgsIC =
455 stepper->getModel()->getNominalValues();
456 auto icX = rcp_const_cast<Thyra::VectorBase<double> > (inArgsIC.get_x());
457 auto icXDot = rcp_const_cast<Thyra::VectorBase<double> > (inArgsIC.get_x_dot());
459 icState->setTime (timeStepControl->getInitTime());
460 icState->setIndex (timeStepControl->getInitIndex());
461 icState->setTimeStep(0.0);
462 icState->setOrder (stepper->getOrder());
474 integrator = Tempus::integratorBasic<double>();
475 integrator->setStepperWStepper(stepper);
476 integrator->setTimeStepControl(timeStepControl);
478 integrator->setScreenOutputIndexInterval(1);
480 integrator->initialize();
484 bool integratorStatus = integrator->advanceTime();
485 TEST_ASSERT(integratorStatus)
488 time = integrator->getTime();
489 double timeFinal = 2.0;
490 double tol = 100.0 * std::numeric_limits<double>::epsilon();
491 TEST_FLOATING_EQUALITY(time, timeFinal, tol);
494 StepSize.push_back(dt);
495 auto solution = Thyra::createMember(implicitModel->get_x_space());
496 Thyra::copy(*(integrator->getX()),solution.ptr());
497 solutions.push_back(solution);
498 auto solutionDot = Thyra::createMember(implicitModel->get_x_space());
499 Thyra::copy(*(integrator->getXdot()),solutionDot.ptr());
500 solutionsDot.push_back(solutionDot);
504 if ((n == 0) or (n == nTimeStepSizes-1)) {
505 std::string fname =
"Tempus_Subcycling_VanDerPol-Ref.dat";
506 if (n == 0) fname =
"Tempus_Subcycling_VanDerPol.dat";
508 integrator->getSolutionHistory();
516 double xDotSlope = 0.0;
517 RCP<Tempus::Stepper<double> > stepper = integrator->getStepper();
521 solutions, xErrorNorm, xSlope,
522 solutionsDot, xDotErrorNorm, xDotSlope);
524 TEST_FLOATING_EQUALITY( xSlope, 1.25708, 0.05 );
525 TEST_FLOATING_EQUALITY( xDotSlope, 1.20230, 0.05 );
526 TEST_FLOATING_EQUALITY( xErrorNorm[0], 0.37156, 1.0e-4 );
527 TEST_FLOATING_EQUALITY( xDotErrorNorm[0], 3.11651, 1.0e-4 );
529 Teuchos::TimeMonitor::summarize();
531 #endif // TEST_VANDERPOL_OPERATOR_SPLIT
van der Pol model formulated for IMEX-RK.
StepControlStrategy class for TimeStepControl.
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)
OperatorSplit stepper loops through the Stepper list.
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.
van der Pol model formulated for IMEX.
Grow the history as needed.
Solution state for integrators and steppers. SolutionState contains the metadata for solutions and th...