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"
16 #include "Tempus_IntegratorObserverSubcycling.hpp"
19 #include "Tempus_StepperSubcycling.hpp"
22 #include "../TestModels/SinCosModel.hpp"
23 #include "../TestModels/VanDerPol_IMEX_ExplicitModel.hpp"
24 #include "../TestModels/VanDerPol_IMEX_ImplicitModel.hpp"
25 #include "../TestUtils/Tempus_ConvergenceTestUtils.hpp"
30 namespace Tempus_Test {
34 using Teuchos::rcp_const_cast;
35 using Teuchos::ParameterList;
36 using Teuchos::sublist;
37 using Teuchos::getParametersFromXmlFile;
49 RCP<ParameterList> pList =
50 getParametersFromXmlFile(
"Tempus_Subcycling_SinCos.xml");
57 RCP<ParameterList> scm_pl = sublist(pList,
"SinCosModel",
true);
60 RCP<ParameterList> tempusPL = sublist(pList,
"Tempus",
true);
64 RCP<Tempus::IntegratorBasic<double> > integrator =
65 Tempus::integratorBasic<double>(tempusPL, model);
67 RCP<ParameterList> stepperPL = sublist(tempusPL,
"Demo Stepper",
true);
68 RCP<const ParameterList> defaultPL =
69 integrator->getStepper()->getValidParameters();
71 bool pass = haveSameValues(*stepperPL, *defaultPL,
true);
73 std::cout << std::endl;
74 std::cout <<
"stepperPL -------------- \n" << *stepperPL << std::endl;
75 std::cout <<
"defaultPL -------------- \n" << *defaultPL << std::endl;
82 RCP<Tempus::IntegratorBasic<double> > integrator =
83 Tempus::integratorBasic<double>(model,
"Forward Euler");
85 RCP<ParameterList> stepperPL = sublist(tempusPL,
"Demo Stepper",
true);
86 RCP<const ParameterList> defaultPL =
87 integrator->getStepper()->getValidParameters();
89 bool pass = haveSameValues(*stepperPL, *defaultPL,
true);
91 std::cout << std::endl;
92 std::cout <<
"stepperPL -------------- \n" << *stepperPL << std::endl;
93 std::cout <<
"defaultPL -------------- \n" << *defaultPL << std::endl;
112 auto stepperFE = sf->createStepperForwardEuler(model, Teuchos::null);
113 stepper->setSubcyclingStepper(stepperFE);
115 stepper->setSubcyclingMinTimeStep (0.1);
116 stepper->setSubcyclingInitTimeStep (0.1);
117 stepper->setSubcyclingMaxTimeStep (0.1);
118 stepper->setSubcyclingStepType (
"Constant");
119 stepper->setSubcyclingMaxFailures (10);
120 stepper->setSubcyclingMaxConsecFailures(5);
121 stepper->setSubcyclingScreenOutputIndexInterval(1);
122 stepper->setSubcyclingIntegratorObserver(
124 stepper->setSubcyclingPrintDtChanges (
true);
126 stepper->initialize();
130 timeStepControl->setStepType (
"Constant");
131 timeStepControl->setInitIndex(0);
132 timeStepControl->setInitTime (0.0);
133 timeStepControl->setFinalTime(1.0);
134 timeStepControl->setInitTimeStep(dt);
135 timeStepControl->initialize();
138 Thyra::ModelEvaluatorBase::InArgs<double> inArgsIC =
139 stepper->getModel()->getNominalValues();
140 auto icSolution = rcp_const_cast<Thyra::VectorBase<double> > (inArgsIC.get_x());
142 icState->setTime (timeStepControl->getInitTime());
143 icState->setIndex (timeStepControl->getInitIndex());
144 icState->setTimeStep(0.0);
149 solutionHistory->setName(
"Forward States");
151 solutionHistory->setStorageLimit(2);
152 solutionHistory->addState(icState);
155 RCP<Tempus::IntegratorBasic<double> > integrator =
156 Tempus::integratorBasic<double>();
157 integrator->setStepperWStepper(stepper);
158 integrator->setTimeStepControl(timeStepControl);
159 integrator->setSolutionHistory(solutionHistory);
160 integrator->setScreenOutputIndexInterval(10);
162 integrator->initialize();
166 bool integratorStatus = integrator->advanceTime();
167 TEST_ASSERT(integratorStatus)
171 double time = integrator->getTime();
172 double timeFinal = 1.0;
173 TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
176 RCP<Thyra::VectorBase<double> > x = integrator->getX();
177 RCP<const Thyra::VectorBase<double> > x_exact =
178 model->getExactSolution(time).get_x();
181 RCP<Thyra::VectorBase<double> > xdiff = x->clone_v();
182 Thyra::V_StVpStV(xdiff.ptr(), 1.0, *x_exact, -1.0, *(x));
185 std::cout <<
" Stepper = " << stepper->description() << std::endl;
186 std::cout <<
" =========================" << std::endl;
187 std::cout <<
" Exact solution : " << get_ele(*(x_exact), 0) <<
" "
188 << get_ele(*(x_exact), 1) << std::endl;
189 std::cout <<
" Computed solution: " << get_ele(*(x ), 0) <<
" "
190 << get_ele(*(x ), 1) << std::endl;
191 std::cout <<
" Difference : " << get_ele(*(xdiff ), 0) <<
" "
192 << get_ele(*(xdiff ), 1) << std::endl;
193 std::cout <<
" =========================" << std::endl;
194 TEST_FLOATING_EQUALITY(get_ele(*(x), 0), 0.882508, 1.0e-4 );
195 TEST_FLOATING_EQUALITY(get_ele(*(x), 1), 0.570790, 1.0e-4 );
203 RCP<Tempus::IntegratorBasic<double> > integrator;
204 std::vector<RCP<Thyra::VectorBase<double>>> solutions;
205 std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
206 std::vector<double> StepSize;
211 const int nTimeStepSizes = 2;
212 std::string output_file_string =
"Tempus_Subcycling_SinCos";
213 std::string output_file_name = output_file_string +
".dat";
214 std::string err_out_file_name = output_file_string +
"-Error.dat";
216 for (
int n=0; n<nTimeStepSizes; n++) {
226 auto stepperFE = sf->createStepperForwardEuler(model, Teuchos::null);
227 stepper->setSubcyclingStepper(stepperFE);
229 stepper->setSubcyclingMinTimeStep (dt/10.0);
230 stepper->setSubcyclingInitTimeStep (dt/10.0);
231 stepper->setSubcyclingMaxTimeStep (dt);
232 stepper->setSubcyclingStepType (
"Variable");
233 stepper->setSubcyclingMaxFailures (10);
234 stepper->setSubcyclingMaxConsecFailures(5);
235 stepper->setSubcyclingScreenOutputIndexInterval(1);
242 strategy->setMinEta(0.02);
243 strategy->setMaxEta(0.04);
244 stepper->setSubcyclingTimeStepControlStrategy(strategy);
246 stepper->initialize();
250 timeStepControl->setStepType (
"Constant");
251 timeStepControl->setInitIndex(0);
252 timeStepControl->setInitTime (0.0);
253 timeStepControl->setFinalTime(1.0);
254 timeStepControl->setMinTimeStep (dt);
255 timeStepControl->setInitTimeStep(dt);
256 timeStepControl->setMaxTimeStep (dt);
257 timeStepControl->initialize();
260 Thyra::ModelEvaluatorBase::InArgs<double> inArgsIC =
261 stepper->getModel()->getNominalValues();
262 auto icSolution = rcp_const_cast<Thyra::VectorBase<double> > (inArgsIC.get_x());
264 icState->setTime (timeStepControl->getInitTime());
265 icState->setIndex (timeStepControl->getInitIndex());
266 icState->setTimeStep(0.0);
271 solutionHistory->setName(
"Forward States");
273 solutionHistory->setStorageLimit(2);
274 solutionHistory->addState(icState);
277 integrator = Tempus::integratorBasic<double>();
278 integrator->setStepperWStepper(stepper);
279 integrator->setTimeStepControl(timeStepControl);
280 integrator->setSolutionHistory(solutionHistory);
281 integrator->setScreenOutputIndexInterval(10);
283 integrator->initialize();
287 bool integratorStatus = integrator->advanceTime();
288 TEST_ASSERT(integratorStatus)
291 time = integrator->getTime();
292 double timeFinal = 1.0;
293 TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
296 RCP<Thyra::VectorBase<double> > x = integrator->getX();
297 RCP<const Thyra::VectorBase<double> > x_exact =
298 model->getExactSolution(time).get_x();
329 StepSize.push_back(dt);
330 auto solution = Thyra::createMember(model->get_x_space());
331 Thyra::copy(*(integrator->getX()),solution.ptr());
332 solutions.push_back(solution);
333 if (n == nTimeStepSizes-1) {
334 StepSize.push_back(0.0);
335 auto solutionExact = Thyra::createMember(model->get_x_space());
336 Thyra::copy(*(model->getExactSolution(time).get_x()),solutionExact.ptr());
337 solutions.push_back(solutionExact);
342 if (nTimeStepSizes > 1) {
344 double xDotSlope = 0.0;
345 std::vector<double> xErrorNorm;
346 std::vector<double> xDotErrorNorm;
347 RCP<Tempus::Stepper<double> > stepper = integrator->getStepper();
351 solutions, xErrorNorm, xSlope,
352 solutionsDot, xDotErrorNorm, xDotSlope);
354 TEST_FLOATING_EQUALITY( xSlope, 1.00137, 0.01 );
356 TEST_FLOATING_EQUALITY( xErrorNorm[0], 0.00387948, 1.0e-4 );
360 Teuchos::TimeMonitor::summarize();
368 RCP<Tempus::IntegratorBasic<double> > integrator;
369 std::vector<RCP<Thyra::VectorBase<double>>> solutions;
370 std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
371 std::vector<double> StepSize;
372 std::vector<double> xErrorNorm;
373 std::vector<double> xDotErrorNorm;
374 const int nTimeStepSizes = 4;
377 for (
int n=0; n<nTimeStepSizes; n++) {
381 if (n == nTimeStepSizes-1) dt /= 10.0;
385 auto pl = Teuchos::rcp_const_cast<Teuchos::ParameterList> (
386 tmpModel->getValidParameters());
387 pl->set(
"Coeff epsilon", 0.1);
396 auto stepperFE = sf->createStepperForwardEuler(explicitModel,Teuchos::null);
397 stepperFE->setUseFSAL(
false);
398 stepperFE->initialize();
399 stepperSC->setSubcyclingStepper(stepperFE);
401 stepperSC->setSubcyclingMinTimeStep (0.00001);
402 stepperSC->setSubcyclingInitTimeStep (dt/10.0);
403 stepperSC->setSubcyclingMaxTimeStep (dt/10.0);
404 stepperSC->setSubcyclingMaxFailures (10);
405 stepperSC->setSubcyclingMaxConsecFailures(5);
406 stepperSC->setSubcyclingScreenOutputIndexInterval(1);
412 stepperSC->setSubcyclingStepType (
"Variable");
414 strategySC->setMinEta(0.000001);
415 strategySC->setMaxEta(0.01);
416 stepperSC->setSubcyclingTimeStepControlStrategy(strategySC);
417 stepperSC->initialize();
421 sf->createStepperBackwardEuler(implicitModel, Teuchos::null);
425 stepper->addStepper(stepperSC);
426 stepper->addStepper(stepperBE);
427 stepper->initialize();
431 timeStepControl->setInitIndex(0);
432 timeStepControl->setInitTime (0.0);
434 timeStepControl->setFinalTime(2.0);
435 timeStepControl->setMinTimeStep (0.000001);
436 timeStepControl->setStepType (
"Constant");
437 timeStepControl->setInitTimeStep(dt);
438 timeStepControl->setMaxTimeStep (dt);
449 timeStepControl->initialize();
452 Thyra::ModelEvaluatorBase::InArgs<double> inArgsIC =
453 stepper->getModel()->getNominalValues();
454 auto icX = rcp_const_cast<Thyra::VectorBase<double> > (inArgsIC.get_x());
455 auto icXDot = rcp_const_cast<Thyra::VectorBase<double> > (inArgsIC.get_x_dot());
457 icState->setTime (timeStepControl->getInitTime());
458 icState->setIndex (timeStepControl->getInitIndex());
459 icState->setTimeStep(0.0);
460 icState->setOrder (stepper->getOrder());
465 solutionHistory->setName(
"Forward States");
468 solutionHistory->setStorageLimit(3);
469 solutionHistory->addState(icState);
472 integrator = Tempus::integratorBasic<double>();
473 integrator->setStepperWStepper(stepper);
474 integrator->setTimeStepControl(timeStepControl);
475 integrator->setSolutionHistory(solutionHistory);
476 integrator->setScreenOutputIndexInterval(10);
478 integrator->initialize();
482 bool integratorStatus = integrator->advanceTime();
483 TEST_ASSERT(integratorStatus)
486 time = integrator->getTime();
487 double timeFinal = 2.0;
488 double tol = 100.0 * std::numeric_limits<double>::epsilon();
489 TEST_FLOATING_EQUALITY(time, timeFinal, tol);
492 StepSize.push_back(dt);
493 auto solution = Thyra::createMember(implicitModel->get_x_space());
494 Thyra::copy(*(integrator->getX()),solution.ptr());
495 solutions.push_back(solution);
496 auto solutionDot = Thyra::createMember(implicitModel->get_x_space());
497 Thyra::copy(*(integrator->getXdot()),solutionDot.ptr());
498 solutionsDot.push_back(solutionDot);
502 if ((n == 0) or (n == nTimeStepSizes-1)) {
503 std::string fname =
"Tempus_Subcycling_VanDerPol-Ref.dat";
504 if (n == 0) fname =
"Tempus_Subcycling_VanDerPol.dat";
512 double xDotSlope = 0.0;
513 RCP<Tempus::Stepper<double> > stepper = integrator->getStepper();
517 solutions, xErrorNorm, xSlope,
518 solutionsDot, xDotErrorNorm, xDotSlope);
520 TEST_FLOATING_EQUALITY( xSlope, 1.25708, 0.05 );
521 TEST_FLOATING_EQUALITY( xDotSlope, 1.20230, 0.05 );
522 TEST_FLOATING_EQUALITY( xErrorNorm[0], 0.37156, 1.0e-4 );
523 TEST_FLOATING_EQUALITY( xDotErrorNorm[0], 3.11651, 1.0e-4 );
525 Teuchos::TimeMonitor::summarize();
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.
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...
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.
IntegratorObserverSubcycling class for time integrators. This basic class has simple no-op functions...
Grow the history as needed.
Solution state for integrators and steppers. SolutionState contains the metadata for solutions and th...