12 #include "Tempus_TimeStepControl.hpp"
18 namespace Tempus_Unit_Test {
23 using Teuchos::rcp_const_cast;
24 using Teuchos::rcp_dynamic_cast;
25 using Teuchos::sublist;
50 TEST_COMPARE(tsc->getOutputIndexInterval(), ==, 1000000);
52 auto tec = tsc->getTimeEvents();
55 "Output Index Interval, Output Time Interval");
58 tsc->setInitTime(1.0);
61 tsc->setFinalTime(100.0);
64 tsc->setMinTimeStep(0.01);
67 tsc->setInitTimeStep(0.02);
70 tsc->setMaxTimeStep(0.05);
73 tsc->setInitIndex(-100);
76 tsc->setFinalIndex(100);
79 tsc->setMaxAbsError(1.0e-06);
82 tsc->setMaxRelError(1.0e-06);
85 tsc->setMaxFailures(8);
88 tsc->setMaxConsecFailures(4);
91 tsc->setNumTimeSteps(-1);
94 tsc->setPrintDtChanges(
false);
97 tsc->setOutputExactly(
false);
100 tsc->setOutputIndexInterval(9);
103 tsc->setOutputTimeInterval(0.1);
128 ter->setName(
"Test Range");
129 tel->setName(
"Test List");
132 tsc->setTimeEvents(tecTmp);
133 tec = tsc->getTimeEvents();
135 TEST_COMPARE(tec->getTimeEventNames(), ==,
"Test Range, Test List");
142 std::vector<int> outputIndices;
143 outputIndices.push_back(7);
144 outputIndices.push_back(11);
145 outputIndices.push_back(13);
147 std::vector<double> outputTimes;
148 outputTimes.push_back(0.3);
149 outputTimes.push_back(0.7);
150 outputTimes.push_back(1.3);
151 outputTimes.push_back(1.7);
158 ter->setName(
"Test Range");
159 teri->setName(
"Test Range Index");
160 tel->setName(
"Test List");
161 teli->setName(
"Test List Index");
211 auto tec = tsc->getTimeEvents();
214 tec->getTimeEventNames(), ==,
215 "Test Range, Test Range Index, Test List, Test List Index, Output Time "
216 "Interval, Output Time List, Output Index Interval, Output Index List");
224 Tempus::getTimeStepControlPL<double>();
226 pl->
set<
double>(
"Initial Time", 1.0);
227 pl->
set<
double>(
"Final Time", 100.0);
228 pl->
set<
double>(
"Minimum Time Step", 0.01);
229 pl->
set<
double>(
"Initial Time Step", 0.02);
230 pl->
set<
double>(
"Maximum Time Step", 0.05);
231 pl->
set<
int>(
"Initial Time Index", -100);
232 pl->
set<
int>(
"Final Time Index", 100);
233 pl->
set<
double>(
"Maximum Absolute Error", 1.0e-06);
234 pl->
set<
double>(
"Maximum Relative Error", 1.0e-06);
235 pl->
set<
int>(
"Maximum Number of Stepper Failures", 8);
236 pl->
set<
int>(
"Maximum Number of Consecutive Stepper Failures", 4);
237 pl->
set<
int>(
"Number of Time Steps", -1);
238 pl->
set<
bool>(
"Print Time Step Changes",
false);
239 pl->
set<
bool>(
"Output Exactly On Output Times",
false);
240 pl->
set<std::string>(
"Output Index List",
"7, 11, 13");
241 pl->
set<std::string>(
"Output Time List",
"0.3, 0.7, 1.3, 1.7");
242 pl->
set<
int>(
"Output Index Interval", 9);
243 pl->
set<
double>(
"Output Time Interval", 0.011);
246 auto tscsPL = tscs->getValidParameters();
247 pl->
set(
"Time Step Control Strategy", *tscsPL);
254 ter->setName(
"Test Range");
255 teri->setName(
"Test Range Index");
256 tel->setName(
"Test List");
257 teli->setName(
"Test List Index");
262 auto tecPL = rcp_const_cast<
ParameterList>(tec->getValidParameters());
263 pl->
set(
"Time Step Control Events", *tecPL);
265 auto tsc = Tempus::createTimeStepControl<double>(pl);
295 tec = tsc->getTimeEvents();
298 tec->getTimeEventNames(), ==,
299 "Output Index List, Output Index Interval, Output Time List, Output Time "
300 "Interval, Test Range, Test Range Index, Test List, Test List Index");
312 double dLast = 0.989;
315 tsc->setInitTime(dFirst);
317 tsc->setFinalTime(dLast);
319 tsc->setMinTimeStep(dStep);
321 tsc->setInitTimeStep(dStep);
323 tsc->setMaxTimeStep(dLast);
325 tsc->setInitIndex(iFirst);
327 tsc->setFinalIndex(iLast);
329 tsc->setMaxAbsError(dStep);
331 tsc->setMaxRelError(dStep);
333 tsc->setOutputExactly(
false);
335 tsc->setOutputExactly(
true);
338 std::vector<int> iVSet{0, 1, 2, 3, 5, 8, 13, 21, 34};
339 tsc->setOutputIndices(iVSet);
342 tsc->setOutputIndexInterval(iStep);
344 tsc->setOutputTimeInterval(dStep);
354 std::vector<double> times_in;
355 times_in.push_back(0.0000000000000000e-11);
356 times_in.push_back(0.1001384570000000e-11);
357 times_in.push_back(0.2002769140000000e-11);
358 times_in.push_back(0.3004153710000000e-11);
359 times_in.push_back(0.4005538280000000e-11);
360 times_in.push_back(0.5006922850000000e-11);
361 times_in.push_back(0.6008307420000000e-11);
362 times_in.push_back(0.7009691990000000e-11);
363 times_in.push_back(0.8011076560000000e-11);
364 times_in.push_back(0.9012461130000000e-11);
365 times_in.push_back(1.0013845700000000e-11);
367 tsc->setOutputTimes(times_in);
369 auto times_out = tsc->getOutputTimes();
370 double maxDiff = 0.0;
373 for (
size_t i = 0; i < times_in.size(); ++i) {
377 maxDiff = std::max(std::fabs(times_in[i] - times_out[i]), maxDiff);
384 times_in.push_back(0.00000000000000000000000000000000);
385 times_in.push_back(0.00000000000100138457000000009381);
386 times_in.push_back(0.00000000000200276914000000018762);
387 times_in.push_back(0.00000000000300415371000000007949);
388 times_in.push_back(0.00000000000400553828000000037525);
389 times_in.push_back(0.00000000000500692284999999986321);
390 times_in.push_back(0.00000000000600830742000000015898);
391 times_in.push_back(0.00000000000700969198999999964694);
392 times_in.push_back(0.00000000000801107656000000075050);
393 times_in.push_back(0.00000000000901246112999999943067);
394 times_in.push_back(0.00000000001001384569999999972643);
396 tsc->setOutputTimes(times_in);
398 times_out = tsc->getOutputTimes();
402 for (
size_t i = 0; i < times_in.size(); ++i) {
406 maxDiff = std::max(std::fabs(times_in[i] - times_out[i]), maxDiff);
418 int setOutputTimeIndex = 17;
419 double setOutputTimeInterval = 1.101001000100001e-7;
421 tsc->setFinalTime(1.0);
422 tsc->setOutputIndexInterval(setOutputTimeIndex);
423 tsc->setOutputTimeInterval(setOutputTimeInterval);
425 int getOutputTimeIndex = tsc->getOutputIndexInterval();
426 double getOutputTimeInterval = tsc->getOutputTimeInterval();
427 TEST_COMPARE(getOutputTimeInterval, ==, setOutputTimeInterval);
428 TEST_COMPARE(getOutputTimeIndex, ==, setOutputTimeIndex);
439 auto testTimeInRange = [=](
double initTime,
double finalTime) {
440 tsc->setInitTime(initTime);
441 tsc->setFinalTime(finalTime);
445 const int i = (initTime == 0)
447 : 1 + (
int)std::floor(std::log10(std::fabs(initTime)));
448 const double absTolInit10 = std::pow(10, i - 10);
449 const double absTolInit15 = std::pow(10, i - 15);
450 const int j = (finalTime == 0)
452 : 1 + (
int)std::floor(std::log10(std::fabs(finalTime)));
453 const double absTolFinal10 = std::pow(10, j - 10);
454 const double absTolFinal15 = std::pow(10, j - 15);
457 if (initTime == 0.0) {
462 tsc->timeInRange(initTime - 0.1 * std::fabs(initTime)));
470 !tsc->timeInRange(initTime + 0.3 * (std::fabs(finalTime - initTime))));
476 if (finalTime == 0.0) {
481 tsc->timeInRange(finalTime + 0.1 * std::fabs(finalTime)));
486 testTimeInRange(0.0, 1.0);
489 testTimeInRange(-1.0, 0.0);
492 testTimeInRange(9.9e-20, 3.3e+20);
493 testTimeInRange(-1.9e+20, 2.3e-20);
504 auto testIndexInRange = [=](
double initIndex,
double finalIndex) {
505 tsc->setInitIndex(initIndex);
506 tsc->setFinalIndex(finalIndex);
516 initIndex + (
int)0.3 * (std::fabs(finalIndex - initIndex))));
524 testIndexInRange(0, 10);
527 testIndexInRange(-10, 0);
530 testIndexInRange(-190000, 20);
531 testIndexInRange(-19, 200000);
538 std::vector<int> outputIndices;
539 outputIndices.push_back(7);
540 outputIndices.push_back(11);
541 outputIndices.push_back(13);
543 std::vector<double> outputTimes;
544 outputTimes.push_back(0.3);
545 outputTimes.push_back(0.7);
546 outputTimes.push_back(1.3);
547 outputTimes.push_back(1.7);
554 1.0, 100.0, 0.01, 0.02, 0.05, -100, 100, 1.0e-06, 1.0e-06, 8, 4, -1,
555 false,
false, outputIndices, outputTimes, 9, 0.011, tec, tscsc));
558 auto pl = tsc->getValidParameters();
565 TEST_COMPARE(pl->get<
int>(
"Initial Time Index"), ==, -100);
566 TEST_COMPARE(pl->get<
int>(
"Final Time Index"), ==, 100);
571 TEST_COMPARE(pl->get<
int>(
"Maximum Number of Stepper Failures"), ==, 8);
572 TEST_COMPARE(pl->get<
int>(
"Maximum Number of Consecutive Stepper Failures"),
574 TEST_COMPARE(pl->get<
int>(
"Number of Time Steps"), ==, -1);
575 TEST_COMPARE(pl->get<
bool>(
"Print Time Step Changes"), ==,
false);
576 TEST_COMPARE(pl->get<
bool>(
"Output Exactly On Output Times"), ==,
false);
577 TEST_COMPARE(pl->get<std::string>(
"Output Index List"), ==,
"7, 11, 13");
578 TEST_COMPARE(pl->get<std::string>(
"Output Time List"), ==,
579 "0.3, 0.7, 1.3, 1.7");
580 TEST_COMPARE(pl->get<
int>(
"Output Index Interval"), ==, 9);
585 std::ostringstream unusedParameters;
586 pl->unused(unusedParameters);
588 "WARNING: Parameter \"Time Step Control Strategy\" "
589 "[unused] is unused\n");
592 auto tscs_PL = pl->sublist(
"Time Step Control Strategy");
593 TEST_COMPARE(tscs_PL.get<std::string>(
"Strategy Type"), ==,
"Constant");
597 std::ostringstream unusedParameters;
598 tscs_PL.unused(unusedParameters);
610 tsc->setInitTime(0.0);
611 tsc->setFinalTime(100.0);
612 tsc->setMinTimeStep(0.01);
613 tsc->setInitTimeStep(0.02);
614 tsc->setMaxTimeStep(0.05);
615 tsc->setNumTimeSteps(-1);
626 tsc->setNumTimeSteps(100);
643 auto inArgsIC = model->getNominalValues();
645 auto icState = Tempus::createSolutionStateX<double>(icSolution);
647 solutionHistory->addState(icState);
649 solutionHistory->getCurrentState()->setTimeStep(dt);
653 std::vector<double> outputTimes;
654 double outputTime = 0.8;
655 outputTimes.push_back(outputTime);
656 tsc->setOutputTimes(outputTimes);
657 tsc->setOutputExactly(
true);
659 tsc->setTimeStepControlStrategy(tscs);
660 tsc->setMinTimeStep(dt / 2.0);
661 tsc->setInitTimeStep(dt);
662 tsc->setMaxTimeStep(2.0 * dt);
663 tsc->setPrintDtChanges(
true);
670 solutionHistory->initWorkingState();
671 auto currentState = solutionHistory->getCurrentState();
672 auto workingState = solutionHistory->getWorkingState();
674 tsc->setNextTimeStep(solutionHistory, status);
683 solutionHistory->promoteWorkingState();
687 solutionHistory->initWorkingState();
688 currentState = solutionHistory->getCurrentState();
689 workingState = solutionHistory->getWorkingState();
691 tsc->setNextTimeStep(solutionHistory, status);
695 workingState->getTime(), 1.0e-14);
706 auto inArgsIC = model->getNominalValues();
708 auto icState = Tempus::createSolutionStateX<double>(icSolution);
710 solutionHistory->addState(icState);
712 solutionHistory->getCurrentState()->setTimeStep(dt);
716 std::vector<double> outputTimes;
717 double outputTime = 0.8;
718 outputTimes.push_back(outputTime);
719 tsc->setOutputTimes(outputTimes);
720 tsc->setMinTimeStep(dt / 2.0);
721 tsc->setInitTimeStep(dt);
722 tsc->setMaxTimeStep(2.0 * dt);
723 tsc->setOutputExactly(
false);
729 solutionHistory->initWorkingState();
730 auto currentState = solutionHistory->getCurrentState();
731 auto workingState = solutionHistory->getWorkingState();
733 tsc->setNextTimeStep(solutionHistory, status);
734 double timeN = workingState->getTime();
742 solutionHistory->promoteWorkingState();
745 solutionHistory->initWorkingState();
747 currentState = solutionHistory->getCurrentState();
748 workingState = solutionHistory->getWorkingState();
750 tsc->setNextTimeStep(solutionHistory, status);
751 timeN = workingState->getTime();
754 double dtN = workingState->getTimeStep();
766 auto inArgsIC = model->getNominalValues();
768 auto icState = Tempus::createSolutionStateX<double>(icSolution);
770 solutionHistory->addState(icState);
772 solutionHistory->getCurrentState()->setTimeStep(dt);
776 std::vector<double> outputTimes;
777 double outputTime = 0.8;
778 outputTimes.push_back(outputTime);
779 tsc->setOutputTimes(outputTimes);
780 tsc->setOutputExactly(
true);
781 tsc->setTimeStepControlStrategy();
782 tsc->setInitTimeStep(dt);
787 for (
int i = 0; i < 10000; ++i) {
788 solutionHistory->initWorkingState();
789 tsc->setNextTimeStep(solutionHistory, status);
792 solutionHistory->getWorkingState()->setSolutionStatus(
795 solutionHistory->promoteWorkingState();
798 auto currentState = solutionHistory->getCurrentState();
799 double time = currentState->getTime();
808 auto pl = Tempus::getTimeStepControlPL<double>();
809 pl->remove(
"Time Step Control Strategy");
811 auto tsc = Tempus::createTimeStepControl<double>(pl,
false);
818 TEUCHOS_TEST_FOR_EXCEPT(
819 !(tsc->getTimeStepControlStrategy()->getStrategyType() ==
"Constant"));
823 auto pl = Tempus::getTimeStepControlPL<double>();
824 pl->remove(
"Time Step Control Strategy");
825 pl->set(
"Time Step Control Strategy",
826 *(Tempus::getTimeStepControlStrategyBasicVS_PL<double>()));
828 auto tsc = Tempus::createTimeStepControl<double>(pl);
833 TEUCHOS_TEST_FOR_EXCEPT(
834 !(tsc->getTimeStepControlStrategy()->getStrategyType() ==
"Basic VS"));
838 auto pl = Tempus::getTimeStepControlPL<double>();
839 pl->remove(
"Time Step Control Strategy");
841 "Time Step Control Strategy",
842 *(Tempus::getTimeStepControlStrategyIntegralControllerPL<double>()));
844 auto tsc = Tempus::createTimeStepControl<double>(pl);
849 TEUCHOS_TEST_FOR_EXCEPT(
850 !(tsc->getTimeStepControlStrategy()->getStrategyType() ==
851 "Integral Controller"));
855 auto pl = Tempus::getTimeStepControlPL<double>();
856 pl->remove(
"Time Step Control Strategy");
857 pl->set(
"Time Step Control Strategy",
858 *(Tempus::getTimeStepControlStrategyCompositePL<double>()));
860 auto tsc = Tempus::createTimeStepControl<double>(pl);
865 TEUCHOS_TEST_FOR_EXCEPT(
866 !(tsc->getTimeStepControlStrategy()->getStrategyType() ==
"Composite"));
870 auto pl = Tempus::getTimeStepControlPL<double>();
871 pl->remove(
"Time Step Control Strategy");
873 auto nonTempusStrategyPL =
874 Teuchos::parameterList(
"Time Step Control Strategy");
875 nonTempusStrategyPL->set<std::string>(
"Strategy Type",
876 "Application Strategy");
877 nonTempusStrategyPL->set<
double>(
"Secret Sauce", 1.2345);
879 pl->set(
"Time Step Control Strategy", *nonTempusStrategyPL);
881 auto tsc = Tempus::createTimeStepControl<double>(pl);
887 TEUCHOS_TEST_FOR_EXCEPT(
888 !(tsc->getTimeStepControlStrategy()->getStrategyType() ==
"Constant"));
899 temp->addStrategy(tscsBasicVS);
902 temp->addStrategy(tscsIntCtrl);
907 tsc->setTimeStepControlStrategy(temp);
912 tsc->getTimeStepControlStrategy();
921 std::vector<Teuchos::RCP<Tempus::TimeStepControlStrategy<double>>>
922 strategies = tscsc->getStrategies();
924 auto strategyBasicVS =
930 TEUCHOS_TEST_FOR_EXCEPT(strategyBasicVS->getReductFactor() != 0.5);
931 TEUCHOS_TEST_FOR_EXCEPT(strategyBasicVS->getMinEta() != 0.0);
932 TEUCHOS_TEST_FOR_EXCEPT(strategyBasicVS->getMaxEta() != 1.0e+16);
934 auto strategyIC = Teuchos::rcp_dynamic_cast<
937 TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getStepType() !=
"Variable");
938 TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getController() !=
"PID");
939 TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getKI() != 0.58);
940 TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getKP() != 0.21);
941 TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getKD() != 0.10);
942 TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getSafetyFactor() != 0.90);
943 TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getSafetyFactorAfterReject() != 0.90);
944 TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getFacMax() != 5.0);
945 TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getFacMin() != 0.5);
TimeEventRangeIndex specifies a start, stop and stride index.
Teuchos::RCP< TimeStepControl< Scalar > > createTimeStepControl(Teuchos::RCP< Teuchos::ParameterList > const &pList, bool runInitialize=true)
Nonmember constructor from ParameterList.
StepControlStrategy class for TimeStepControl.
TimeEventListIndex specifies a list of index events.
#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...
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
TimeEventRange specifies a start, stop and stride time.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Status
Status for the Integrator, the Stepper and the SolutionState.
This composite TimeEvent loops over added TimeEvents.
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.
TimeEventList specifies a list of time events.
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)