12 #include "Teuchos_DefaultComm.hpp"
14 #include "Thyra_VectorStdOps.hpp"
16 #include "Tempus_TimeStepControl.hpp"
22 #include "../TestModels/SinCosModel.hpp"
23 #include "../TestUtils/Tempus_ConvergenceTestUtils.hpp"
28 namespace Tempus_Unit_Test {
32 using Teuchos::rcp_const_cast;
33 using Teuchos::rcp_dynamic_cast;
35 using Teuchos::sublist;
36 using Teuchos::getParametersFromXmlFile;
62 TEST_COMPARE ( tsc->getOutputIndexInterval(), ==, 1000000 );
107 std::vector<int> outputIndices;
108 outputIndices.push_back(7);
109 outputIndices.push_back(11);
110 outputIndices.push_back(13);
112 std::vector<double> outputTimes;
113 outputTimes.push_back(0.3);
114 outputTimes.push_back(0.7);
115 outputTimes.push_back(1.3);
116 outputTimes.push_back(1.7);
122 1.0, 100.0, 0.01, 0.02, 0.05, -100,
123 100, 1.0e-06, 1.0e-06, 8, 4, -1,
false,
false,
124 outputIndices, outputTimes, 9, 0.011, tscsc));
154 Tempus::getTimeStepControlPL<double>();
156 pl->
set<
double> (
"Initial Time" , 1.0);
157 pl->
set<
double> (
"Final Time" , 100.0);
158 pl->
set<
double> (
"Minimum Time Step" , 0.01);
159 pl->
set<
double> (
"Initial Time Step" , 0.02);
160 pl->
set<
double> (
"Maximum Time Step" , 0.05);
161 pl->
set<
int> (
"Initial Time Index" , -100);
162 pl->
set<
int> (
"Final Time Index" , 100);
163 pl->
set<
double> (
"Maximum Absolute Error", 1.0e-06);
164 pl->
set<
double> (
"Maximum Relative Error", 1.0e-06);
165 pl->
set<
int> (
"Maximum Number of Stepper Failures", 8);
166 pl->
set<
int> (
"Maximum Number of Consecutive Stepper Failures", 4);
167 pl->
set<
int> (
"Number of Time Steps" , -1);
168 pl->
set<
bool> (
"Print Time Step Changes",
false);
169 pl->
set<
bool> (
"Output Exactly On Output Times",
false);
170 pl->
set<std::string>(
"Output Index List" ,
"7, 11, 13" );
171 pl->
set<std::string>(
"Output Time List" ,
"0.3, 0.7, 1.3, 1.7");
172 pl->
set<
int> (
"Output Index Interval" , 9);
173 pl->
set<
double> (
"Output Time Interval" , 0.011);
176 auto tscsPL = tscs->getValidParameters();
177 pl->
set(
"Time Step Control Strategy", *tscsPL);
180 auto tsc = Tempus::createTimeStepControl<double>(pl);
223 tsc->setInitTime(dSet);
TEST_COMPARE( tsc->getInitTime(), ==, dSet);
224 tsc->setFinalTime(dSet);
TEST_COMPARE( tsc->getFinalTime(), ==, dSet);
225 tsc->setMinTimeStep(dSet);
TEST_COMPARE( tsc->getMinTimeStep(), ==, dSet);
226 tsc->setInitTimeStep(dSet);
TEST_COMPARE( tsc->getInitTimeStep(), ==, dSet);
227 tsc->setMaxTimeStep(dSet);
TEST_COMPARE( tsc->getMaxTimeStep(), ==, dSet);
228 tsc->setInitIndex(iSet);
TEST_COMPARE( tsc->getInitIndex(), ==, iSet);
229 tsc->setFinalIndex(iSet);
TEST_COMPARE( tsc->getFinalIndex(), ==, iSet);
230 tsc->setMaxAbsError(dSet);
TEST_COMPARE( tsc->getMaxAbsError(), ==, dSet);
231 tsc->setMaxRelError(dSet);
TEST_COMPARE( tsc->getMaxRelError(), ==, dSet);
232 tsc->setOutputExactly(
false);
TEST_COMPARE( tsc->getOutputExactly(), ==,
false);
233 tsc->setOutputExactly(
true);
TEST_COMPARE( tsc->getOutputExactly(), ==,
true);
235 std::vector<int> iVSet{ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 };
239 tsc->setOutputIndexInterval(iSet);
TEST_COMPARE( tsc->getOutputIndexInterval(), ==, iSet);
240 tsc->setOutputTimeInterval(dSet);
TEST_COMPARE( tsc->getOutputTimeInterval(), ==, dSet);
250 std::vector<double> times_in;
251 times_in.push_back(0.0000000000000000e-11);
252 times_in.push_back(0.1001384570000000e-11);
253 times_in.push_back(0.2002769140000000e-11);
254 times_in.push_back(0.3004153710000000e-11);
255 times_in.push_back(0.4005538280000000e-11);
256 times_in.push_back(0.5006922850000000e-11);
257 times_in.push_back(0.6008307420000000e-11);
258 times_in.push_back(0.7009691990000000e-11);
259 times_in.push_back(0.8011076560000000e-11);
260 times_in.push_back(0.9012461130000000e-11);
261 times_in.push_back(1.0013845700000000e-11);
263 tsc->setOutputTimes(times_in);
265 auto times_out = tsc->getOutputTimes();
266 double maxDiff = 0.0;
269 for (
size_t i=0; i < times_in.size(); ++i) {
273 maxDiff = std::max(std::fabs(times_in[i] - times_out[i]), maxDiff);
281 times_in.push_back(0.00000000000000000000000000000000);
282 times_in.push_back(0.00000000000100138457000000009381);
283 times_in.push_back(0.00000000000200276914000000018762);
284 times_in.push_back(0.00000000000300415371000000007949);
285 times_in.push_back(0.00000000000400553828000000037525);
286 times_in.push_back(0.00000000000500692284999999986321);
287 times_in.push_back(0.00000000000600830742000000015898);
288 times_in.push_back(0.00000000000700969198999999964694);
289 times_in.push_back(0.00000000000801107656000000075050);
290 times_in.push_back(0.00000000000901246112999999943067);
291 times_in.push_back(0.00000000001001384569999999972643);
293 tsc->setOutputTimes(times_in);
295 times_out = tsc->getOutputTimes();
299 for (
size_t i=0; i < times_in.size(); ++i) {
303 maxDiff = std::max(std::fabs(times_in[i] - times_out[i]), maxDiff);
316 int setOutputTimeIndex = 17;
317 double setOutputTimeInterval = 1.101001000100001e-7;
319 tsc->setOutputIndexInterval(setOutputTimeIndex);
320 tsc->setOutputTimeInterval(setOutputTimeInterval);
322 int getOutputTimeIndex = tsc->getOutputIndexInterval();
323 double getOutputTimeInterval = tsc->getOutputTimeInterval();
324 TEST_COMPARE(getOutputTimeInterval, ==, setOutputTimeInterval);
325 TEST_COMPARE(getOutputTimeIndex, ==, setOutputTimeIndex);
337 auto testTimeInRange = [=] (
double initTime,
double finalTime)
339 tsc->setInitTime (initTime);
340 tsc->setFinalTime(finalTime);
344 const int i = (initTime == 0) ? 0 : 1 + (
int)std::floor(std::log10(std::fabs(initTime) ) );
345 const double absTolInit10 = std::pow(10, i-10);
346 const double absTolInit15 = std::pow(10, i-15);
347 const int j = (finalTime == 0) ? 0 : 1 + (
int)std::floor(std::log10(std::fabs(finalTime) ) );
348 const double absTolFinal10 = std::pow(10, j-10);
349 const double absTolFinal15 = std::pow(10, j-15);
353 if ( initTime == 0.0 ) {
369 if ( finalTime == 0.0 ) {
377 testTimeInRange (0.0, 1.0);
380 testTimeInRange (-1.0, 0.0);
383 testTimeInRange ( 9.9e-20, 3.3e+20);
384 testTimeInRange (-1.9e+20, 2.3e-20);
396 auto testIndexInRange = [=] (
double initIndex,
double finalIndex)
398 tsc->setInitIndex (initIndex);
399 tsc->setFinalIndex(finalIndex);
416 testIndexInRange (0, 10);
419 testIndexInRange (-10, 0);
422 testIndexInRange (-190000, 20);
423 testIndexInRange (-19, 200000);
431 std::vector<int> outputIndices;
432 outputIndices.push_back(7);
433 outputIndices.push_back(11);
434 outputIndices.push_back(13);
436 std::vector<double> outputTimes;
437 outputTimes.push_back(0.3);
438 outputTimes.push_back(0.7);
439 outputTimes.push_back(1.3);
440 outputTimes.push_back(1.7);
446 1.0, 100.0, 0.01, 0.02, 0.05, -100,
447 100, 1.0e-06, 1.0e-06, 8, 4, -1,
false,
false,
448 outputIndices, outputTimes, 9, 0.011, tscsc));
451 auto pl = tsc->getValidParameters();
458 TEST_COMPARE ( pl->get<
int> (
"Initial Time Index") , ==, -100 );
459 TEST_COMPARE ( pl->get<
int> (
"Final Time Index") , ==, 100 );
462 TEST_COMPARE ( pl->get<
int> (
"Maximum Number of Stepper Failures") , ==, 8);
463 TEST_COMPARE ( pl->get<
int> (
"Maximum Number of Consecutive Stepper Failures"), ==, 4);
464 TEST_COMPARE ( pl->get<
int> (
"Number of Time Steps") , ==, -1 );
465 TEST_COMPARE ( pl->get<
bool> (
"Print Time Step Changes") , ==,
false );
466 TEST_COMPARE ( pl->get<
bool> (
"Output Exactly On Output Times"), ==,
false );
467 TEST_COMPARE ( pl->get<std::string>(
"Output Index List") , ==,
"7, 11, 13" );
468 TEST_COMPARE ( pl->get<std::string>(
"Output Time List") , ==,
"0.3, 0.7, 1.3, 1.7");
469 TEST_COMPARE ( pl->get<
int> (
"Output Index Interval") , ==, 9 );
473 std::ostringstream unusedParameters;
474 pl->unused(unusedParameters);
476 "WARNING: Parameter \"Time Step Control Strategy\" [unused] is unused\n");
479 auto tscs_PL = pl->sublist(
"Time Step Control Strategy");
480 TEST_COMPARE ( tscs_PL.get<std::string>(
"Strategy Type") , ==,
"Constant");
484 std::ostringstream unusedParameters;
485 tscs_PL.unused(unusedParameters);
499 tsc->setInitTime(0.0);
500 tsc->setFinalTime(100.0);
501 tsc->setMinTimeStep(0.01);
502 tsc->setInitTimeStep(0.02);
503 tsc->setMaxTimeStep(0.05);
504 tsc->setNumTimeSteps(-1);
515 tsc->setNumTimeSteps(100);
534 auto inArgsIC = model->getNominalValues();
536 auto icState = Tempus::createSolutionStateX<double>(icSolution);
538 solutionHistory->addState(icState);
540 solutionHistory->getCurrentState()->setTimeStep(dt);
544 std::vector<double> outputTimes;
545 double outputTime = 0.8;
546 outputTimes.push_back(outputTime);
547 tsc->setOutputTimes(outputTimes);
548 tsc->setOutputExactly(
true);
550 tsc->setTimeStepControlStrategy(tscs);
551 tsc->setMinTimeStep (dt/2.0);
552 tsc->setInitTimeStep(dt);
553 tsc->setMaxTimeStep (2.0*dt);
554 tsc->setPrintDtChanges(
true);
561 solutionHistory->initWorkingState();
562 auto currentState = solutionHistory->getCurrentState();
563 auto workingState = solutionHistory->getWorkingState();
565 tsc->setNextTimeStep(solutionHistory, status);
574 solutionHistory->promoteWorkingState();
578 solutionHistory->initWorkingState();
579 currentState = solutionHistory->getCurrentState();
580 workingState = solutionHistory->getWorkingState();
582 tsc->setNextTimeStep(solutionHistory, status);
586 currentState->getTime() + workingState->getTimeStep(),
587 workingState->getTime(), 1.0e-14);
599 auto inArgsIC = model->getNominalValues();
601 auto icState = Tempus::createSolutionStateX<double>(icSolution);
603 solutionHistory->addState(icState);
605 solutionHistory->getCurrentState()->setTimeStep(dt);
609 std::vector<double> outputTimes;
610 double outputTime = 0.8;
611 outputTimes.push_back(outputTime);
612 tsc->setOutputTimes(outputTimes);
613 tsc->setOutputExactly(
true);
614 tsc->setMinTimeStep (dt/2.0);
615 tsc->setInitTimeStep(dt);
616 tsc->setMaxTimeStep (2.0*dt);
617 tsc->setOutputExactly(
false);
623 solutionHistory->initWorkingState();
624 auto currentState = solutionHistory->getCurrentState();
625 auto workingState = solutionHistory->getWorkingState();
627 tsc->setNextTimeStep(solutionHistory, status);
628 double timeN = workingState->getTime();
636 solutionHistory->promoteWorkingState();
639 solutionHistory->initWorkingState();
641 currentState = solutionHistory->getCurrentState();
642 workingState = solutionHistory->getWorkingState();
644 tsc->setNextTimeStep(solutionHistory, status);
645 timeN = workingState->getTime();
648 double dtN = workingState->getTimeStep();
661 auto inArgsIC = model->getNominalValues();
663 auto icState = Tempus::createSolutionStateX<double>(icSolution);
665 solutionHistory->addState(icState);
667 solutionHistory->getCurrentState()->setTimeStep(dt);
671 std::vector<double> outputTimes;
672 double outputTime = 0.8;
673 outputTimes.push_back(outputTime);
674 tsc->setOutputTimes(outputTimes);
675 tsc->setOutputExactly(
true);
676 tsc->setTimeStepControlStrategy();
677 tsc->setInitTimeStep(dt);
683 for (
int i=0; i < 10000; ++i) {
684 solutionHistory->initWorkingState();
685 tsc->setNextTimeStep(solutionHistory, status);
690 solutionHistory->promoteWorkingState();
693 auto currentState = solutionHistory->getCurrentState();
694 double time = currentState->getTime();
705 auto pl = Tempus::getTimeStepControlPL<double>();
706 pl->remove(
"Time Step Control Strategy");
708 auto tsc = Tempus::createTimeStepControl<double>(pl,
false);
715 TEUCHOS_TEST_FOR_EXCEPT(!(tsc->getTimeStepControlStrategy()->getStrategyType() ==
"Constant"));
719 auto pl = Tempus::getTimeStepControlPL<double>();
720 pl->remove(
"Time Step Control Strategy");
721 pl->set(
"Time Step Control Strategy",
722 *(Tempus::getTimeStepControlStrategyBasicVS_PL<double>()));
724 auto tsc = Tempus::createTimeStepControl<double>(pl);
729 TEUCHOS_TEST_FOR_EXCEPT(!(tsc->getTimeStepControlStrategy()->getStrategyType() ==
"Basic VS"));
733 auto pl = Tempus::getTimeStepControlPL<double>();
734 pl->remove(
"Time Step Control Strategy");
735 pl->set(
"Time Step Control Strategy",
736 *(Tempus::getTimeStepControlStrategyIntegralControllerPL<double>()));
738 auto tsc = Tempus::createTimeStepControl<double>(pl);
743 TEUCHOS_TEST_FOR_EXCEPT(!(tsc->getTimeStepControlStrategy()->getStrategyType() ==
"Integral Controller"));
747 auto pl = Tempus::getTimeStepControlPL<double>();
748 pl->remove(
"Time Step Control Strategy");
749 pl->set(
"Time Step Control Strategy",
750 *(Tempus::getTimeStepControlStrategyCompositePL<double>()));
752 auto tsc = Tempus::createTimeStepControl<double>(pl);
757 TEUCHOS_TEST_FOR_EXCEPT(!(tsc->getTimeStepControlStrategy()->getStrategyType() ==
"Composite"));
761 auto pl = Tempus::getTimeStepControlPL<double>();
762 pl->remove(
"Time Step Control Strategy");
764 auto nonTempusStrategyPL =
765 Teuchos::parameterList(
"Time Step Control Strategy");
766 nonTempusStrategyPL->set<std::string>(
"Strategy Type",
"Application Strategy");
767 nonTempusStrategyPL->set<
double>(
"Secret Sauce", 1.2345);
769 pl->set(
"Time Step Control Strategy", *nonTempusStrategyPL);
771 auto tsc = Tempus::createTimeStepControl<double>(pl);
776 TEUCHOS_TEST_FOR_EXCEPT(!(tsc->getTimeStepControlStrategy()->getStrategyType() ==
"Constant"));
789 temp->addStrategy(tscsBasicVS);
791 temp->addStrategy(tscsIntCtrl);
796 tsc->setTimeStepControlStrategy(temp);
801 tsc->getTimeStepControlStrategy();
808 std::vector<Teuchos::RCP<Tempus::TimeStepControlStrategy<double>>>
809 strategies = tscsc->getStrategies();
815 TEUCHOS_TEST_FOR_EXCEPT(strategyBasicVS->getReductFactor() != 0.5);
816 TEUCHOS_TEST_FOR_EXCEPT(strategyBasicVS->getMinEta() != 0.0);
817 TEUCHOS_TEST_FOR_EXCEPT(strategyBasicVS->getMaxEta() != 1.0e+16);
821 TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getStepType() !=
"Variable");
822 TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getController() !=
"PID");
823 TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getKI() != 0.58);
824 TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getKP() != 0.21);
825 TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getKD() != 0.10);
826 TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getSafetyFactor() != 0.90);
827 TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getSafetyFactorAfterReject() != 0.90);
828 TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getFacMax() != 5.0);
829 TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getFacMin() != 0.5);
StepControlStrategy class for TimeStepControl.
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
#define TEST_COMPARE(v1, comp, v2)
#define TEST_FLOATING_EQUALITY(v1, v2, tol)
Sine-Cosine model problem from Rythmos. This is a canonical Sine-Cosine differential equation with a...
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Status
Status for the Integrator, the Stepper and the SolutionState.
TEUCHOS_UNIT_TEST(BackwardEuler, Default_Construction)
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...
StepControlStrategy class for TimeStepControl.
TimeStepControlStrategyComposite loops over a vector of TimeStepControlStrategies.
StepControlStrategy class for TimeStepControl.
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)