11 #include "Tempus_TimeStepControl.hpp"
17 namespace Tempus_Unit_Test {
22 using Teuchos::rcp_const_cast;
23 using Teuchos::rcp_dynamic_cast;
24 using Teuchos::sublist;
49 TEST_COMPARE(tsc->getOutputIndexInterval(), ==, 1000000);
51 auto tec = tsc->getTimeEvents();
54 "Output Index Interval, Output Time Interval");
57 tsc->setInitTime(1.0);
60 tsc->setFinalTime(100.0);
63 tsc->setMinTimeStep(0.01);
66 tsc->setInitTimeStep(0.02);
69 tsc->setMaxTimeStep(0.05);
72 tsc->setInitIndex(-100);
75 tsc->setFinalIndex(100);
78 tsc->setMaxAbsError(1.0e-06);
81 tsc->setMaxRelError(1.0e-06);
84 tsc->setMaxFailures(8);
87 tsc->setMaxConsecFailures(4);
90 tsc->setNumTimeSteps(-1);
93 tsc->setPrintDtChanges(
false);
96 tsc->setOutputExactly(
false);
99 tsc->setOutputIndexInterval(9);
102 tsc->setOutputTimeInterval(0.1);
127 ter->setName(
"Test Range");
128 tel->setName(
"Test List");
131 tsc->setTimeEvents(tecTmp);
132 tec = tsc->getTimeEvents();
134 TEST_COMPARE(tec->getTimeEventNames(), ==,
"Test Range, Test List");
141 std::vector<int> outputIndices;
142 outputIndices.push_back(7);
143 outputIndices.push_back(11);
144 outputIndices.push_back(13);
146 std::vector<double> outputTimes;
147 outputTimes.push_back(0.3);
148 outputTimes.push_back(0.7);
149 outputTimes.push_back(1.3);
150 outputTimes.push_back(1.7);
157 ter->setName(
"Test Range");
158 teri->setName(
"Test Range Index");
159 tel->setName(
"Test List");
160 teli->setName(
"Test List Index");
210 auto tec = tsc->getTimeEvents();
213 tec->getTimeEventNames(), ==,
214 "Test Range, Test Range Index, Test List, Test List Index, Output Time "
215 "Interval, Output Time List, Output Index Interval, Output Index List");
223 Tempus::getTimeStepControlPL<double>();
225 pl->
set<
double>(
"Initial Time", 1.0);
226 pl->
set<
double>(
"Final Time", 100.0);
227 pl->
set<
double>(
"Minimum Time Step", 0.01);
228 pl->
set<
double>(
"Initial Time Step", 0.02);
229 pl->
set<
double>(
"Maximum Time Step", 0.05);
230 pl->
set<
int>(
"Initial Time Index", -100);
231 pl->
set<
int>(
"Final Time Index", 100);
232 pl->
set<
double>(
"Maximum Absolute Error", 1.0e-06);
233 pl->
set<
double>(
"Maximum Relative Error", 1.0e-06);
234 pl->
set<
int>(
"Maximum Number of Stepper Failures", 8);
235 pl->
set<
int>(
"Maximum Number of Consecutive Stepper Failures", 4);
236 pl->
set<
int>(
"Number of Time Steps", -1);
237 pl->
set<
bool>(
"Print Time Step Changes",
false);
238 pl->
set<
bool>(
"Output Exactly On Output Times",
false);
239 pl->
set<std::string>(
"Output Index List",
"7, 11, 13");
240 pl->
set<std::string>(
"Output Time List",
"0.3, 0.7, 1.3, 1.7");
241 pl->
set<
int>(
"Output Index Interval", 9);
242 pl->
set<
double>(
"Output Time Interval", 0.011);
245 auto tscsPL = tscs->getValidParameters();
246 pl->
set(
"Time Step Control Strategy", *tscsPL);
253 ter->setName(
"Test Range");
254 teri->setName(
"Test Range Index");
255 tel->setName(
"Test List");
256 teli->setName(
"Test List Index");
261 auto tecPL = rcp_const_cast<
ParameterList>(tec->getValidParameters());
262 pl->
set(
"Time Step Control Events", *tecPL);
264 auto tsc = Tempus::createTimeStepControl<double>(pl);
294 tec = tsc->getTimeEvents();
297 tec->getTimeEventNames(), ==,
298 "Output Index List, Output Index Interval, Output Time List, Output Time "
299 "Interval, Test Range, Test Range Index, Test List, Test List Index");
311 double dLast = 0.989;
314 tsc->setInitTime(dFirst);
316 tsc->setFinalTime(dLast);
318 tsc->setMinTimeStep(dStep);
320 tsc->setInitTimeStep(dStep);
322 tsc->setMaxTimeStep(dLast);
324 tsc->setInitIndex(iFirst);
326 tsc->setFinalIndex(iLast);
328 tsc->setMaxAbsError(dStep);
330 tsc->setMaxRelError(dStep);
332 tsc->setOutputExactly(
false);
334 tsc->setOutputExactly(
true);
337 std::vector<int> iVSet{0, 1, 2, 3, 5, 8, 13, 21, 34};
338 tsc->setOutputIndices(iVSet);
341 tsc->setOutputIndexInterval(iStep);
343 tsc->setOutputTimeInterval(dStep);
353 std::vector<double> times_in;
354 times_in.push_back(0.0000000000000000e-11);
355 times_in.push_back(0.1001384570000000e-11);
356 times_in.push_back(0.2002769140000000e-11);
357 times_in.push_back(0.3004153710000000e-11);
358 times_in.push_back(0.4005538280000000e-11);
359 times_in.push_back(0.5006922850000000e-11);
360 times_in.push_back(0.6008307420000000e-11);
361 times_in.push_back(0.7009691990000000e-11);
362 times_in.push_back(0.8011076560000000e-11);
363 times_in.push_back(0.9012461130000000e-11);
364 times_in.push_back(1.0013845700000000e-11);
366 tsc->setOutputTimes(times_in);
368 auto times_out = tsc->getOutputTimes();
369 double maxDiff = 0.0;
372 for (
size_t i = 0; i < times_in.size(); ++i) {
376 maxDiff = std::max(std::fabs(times_in[i] - times_out[i]), maxDiff);
383 times_in.push_back(0.00000000000000000000000000000000);
384 times_in.push_back(0.00000000000100138457000000009381);
385 times_in.push_back(0.00000000000200276914000000018762);
386 times_in.push_back(0.00000000000300415371000000007949);
387 times_in.push_back(0.00000000000400553828000000037525);
388 times_in.push_back(0.00000000000500692284999999986321);
389 times_in.push_back(0.00000000000600830742000000015898);
390 times_in.push_back(0.00000000000700969198999999964694);
391 times_in.push_back(0.00000000000801107656000000075050);
392 times_in.push_back(0.00000000000901246112999999943067);
393 times_in.push_back(0.00000000001001384569999999972643);
395 tsc->setOutputTimes(times_in);
397 times_out = tsc->getOutputTimes();
401 for (
size_t i = 0; i < times_in.size(); ++i) {
405 maxDiff = std::max(std::fabs(times_in[i] - times_out[i]), maxDiff);
417 int setOutputTimeIndex = 17;
418 double setOutputTimeInterval = 1.101001000100001e-7;
420 tsc->setFinalTime(1.0);
421 tsc->setOutputIndexInterval(setOutputTimeIndex);
422 tsc->setOutputTimeInterval(setOutputTimeInterval);
424 int getOutputTimeIndex = tsc->getOutputIndexInterval();
425 double getOutputTimeInterval = tsc->getOutputTimeInterval();
426 TEST_COMPARE(getOutputTimeInterval, ==, setOutputTimeInterval);
427 TEST_COMPARE(getOutputTimeIndex, ==, setOutputTimeIndex);
438 auto testTimeInRange = [=](
double initTime,
double finalTime) {
439 tsc->setInitTime(initTime);
440 tsc->setFinalTime(finalTime);
444 const int i = (initTime == 0)
446 : 1 + (
int)std::floor(std::log10(std::fabs(initTime)));
447 const double absTolInit10 = std::pow(10, i - 10);
448 const double absTolInit15 = std::pow(10, i - 15);
449 const int j = (finalTime == 0)
451 : 1 + (
int)std::floor(std::log10(std::fabs(finalTime)));
452 const double absTolFinal10 = std::pow(10, j - 10);
453 const double absTolFinal15 = std::pow(10, j - 15);
456 if (initTime == 0.0) {
461 tsc->timeInRange(initTime - 0.1 * std::fabs(initTime)));
469 !tsc->timeInRange(initTime + 0.3 * (std::fabs(finalTime - initTime))));
475 if (finalTime == 0.0) {
480 tsc->timeInRange(finalTime + 0.1 * std::fabs(finalTime)));
485 testTimeInRange(0.0, 1.0);
488 testTimeInRange(-1.0, 0.0);
491 testTimeInRange(9.9e-20, 3.3e+20);
492 testTimeInRange(-1.9e+20, 2.3e-20);
503 auto testIndexInRange = [=](
double initIndex,
double finalIndex) {
504 tsc->setInitIndex(initIndex);
505 tsc->setFinalIndex(finalIndex);
515 initIndex + (
int)0.3 * (std::fabs(finalIndex - initIndex))));
523 testIndexInRange(0, 10);
526 testIndexInRange(-10, 0);
529 testIndexInRange(-190000, 20);
530 testIndexInRange(-19, 200000);
537 std::vector<int> outputIndices;
538 outputIndices.push_back(7);
539 outputIndices.push_back(11);
540 outputIndices.push_back(13);
542 std::vector<double> outputTimes;
543 outputTimes.push_back(0.3);
544 outputTimes.push_back(0.7);
545 outputTimes.push_back(1.3);
546 outputTimes.push_back(1.7);
553 1.0, 100.0, 0.01, 0.02, 0.05, -100, 100, 1.0e-06, 1.0e-06, 8, 4, -1,
554 false,
false, outputIndices, outputTimes, 9, 0.011, tec, tscsc));
557 auto pl = tsc->getValidParameters();
564 TEST_COMPARE(pl->get<
int>(
"Initial Time Index"), ==, -100);
565 TEST_COMPARE(pl->get<
int>(
"Final Time Index"), ==, 100);
570 TEST_COMPARE(pl->get<
int>(
"Maximum Number of Stepper Failures"), ==, 8);
571 TEST_COMPARE(pl->get<
int>(
"Maximum Number of Consecutive Stepper Failures"),
573 TEST_COMPARE(pl->get<
int>(
"Number of Time Steps"), ==, -1);
574 TEST_COMPARE(pl->get<
bool>(
"Print Time Step Changes"), ==,
false);
575 TEST_COMPARE(pl->get<
bool>(
"Output Exactly On Output Times"), ==,
false);
576 TEST_COMPARE(pl->get<std::string>(
"Output Index List"), ==,
"7, 11, 13");
577 TEST_COMPARE(pl->get<std::string>(
"Output Time List"), ==,
578 "0.3, 0.7, 1.3, 1.7");
579 TEST_COMPARE(pl->get<
int>(
"Output Index Interval"), ==, 9);
584 std::ostringstream unusedParameters;
585 pl->unused(unusedParameters);
587 "WARNING: Parameter \"Time Step Control Strategy\" "
588 "[unused] is unused\n");
591 auto tscs_PL = pl->sublist(
"Time Step Control Strategy");
592 TEST_COMPARE(tscs_PL.get<std::string>(
"Strategy Type"), ==,
"Constant");
596 std::ostringstream unusedParameters;
597 tscs_PL.unused(unusedParameters);
609 tsc->setInitTime(0.0);
610 tsc->setFinalTime(100.0);
611 tsc->setMinTimeStep(0.01);
612 tsc->setInitTimeStep(0.02);
613 tsc->setMaxTimeStep(0.05);
614 tsc->setNumTimeSteps(-1);
625 tsc->setNumTimeSteps(100);
642 auto inArgsIC = model->getNominalValues();
644 auto icState = Tempus::createSolutionStateX<double>(icSolution);
646 solutionHistory->addState(icState);
648 solutionHistory->getCurrentState()->setTimeStep(dt);
652 std::vector<double> outputTimes;
653 double outputTime = 0.8;
654 outputTimes.push_back(outputTime);
655 tsc->setOutputTimes(outputTimes);
656 tsc->setOutputExactly(
true);
658 tsc->setTimeStepControlStrategy(tscs);
659 tsc->setMinTimeStep(dt / 2.0);
660 tsc->setInitTimeStep(dt);
661 tsc->setMaxTimeStep(2.0 * dt);
662 tsc->setPrintDtChanges(
true);
669 solutionHistory->initWorkingState();
670 auto currentState = solutionHistory->getCurrentState();
671 auto workingState = solutionHistory->getWorkingState();
673 tsc->setNextTimeStep(solutionHistory, status);
682 solutionHistory->promoteWorkingState();
686 solutionHistory->initWorkingState();
687 currentState = solutionHistory->getCurrentState();
688 workingState = solutionHistory->getWorkingState();
690 tsc->setNextTimeStep(solutionHistory, status);
694 workingState->getTime(), 1.0e-14);
705 auto inArgsIC = model->getNominalValues();
707 auto icState = Tempus::createSolutionStateX<double>(icSolution);
709 solutionHistory->addState(icState);
711 solutionHistory->getCurrentState()->setTimeStep(dt);
715 std::vector<double> outputTimes;
716 double outputTime = 0.8;
717 outputTimes.push_back(outputTime);
718 tsc->setOutputTimes(outputTimes);
719 tsc->setMinTimeStep(dt / 2.0);
720 tsc->setInitTimeStep(dt);
721 tsc->setMaxTimeStep(2.0 * dt);
722 tsc->setOutputExactly(
false);
728 solutionHistory->initWorkingState();
729 auto currentState = solutionHistory->getCurrentState();
730 auto workingState = solutionHistory->getWorkingState();
732 tsc->setNextTimeStep(solutionHistory, status);
733 double timeN = workingState->getTime();
741 solutionHistory->promoteWorkingState();
744 solutionHistory->initWorkingState();
746 currentState = solutionHistory->getCurrentState();
747 workingState = solutionHistory->getWorkingState();
749 tsc->setNextTimeStep(solutionHistory, status);
750 timeN = workingState->getTime();
753 double dtN = workingState->getTimeStep();
765 auto inArgsIC = model->getNominalValues();
767 auto icState = Tempus::createSolutionStateX<double>(icSolution);
769 solutionHistory->addState(icState);
771 solutionHistory->getCurrentState()->setTimeStep(dt);
775 std::vector<double> outputTimes;
776 double outputTime = 0.8;
777 outputTimes.push_back(outputTime);
778 tsc->setOutputTimes(outputTimes);
779 tsc->setOutputExactly(
true);
780 tsc->setTimeStepControlStrategy();
781 tsc->setInitTimeStep(dt);
786 for (
int i = 0; i < 10000; ++i) {
787 solutionHistory->initWorkingState();
788 tsc->setNextTimeStep(solutionHistory, status);
791 solutionHistory->getWorkingState()->setSolutionStatus(
794 solutionHistory->promoteWorkingState();
797 auto currentState = solutionHistory->getCurrentState();
798 double time = currentState->getTime();
807 auto pl = Tempus::getTimeStepControlPL<double>();
808 pl->remove(
"Time Step Control Strategy");
810 auto tsc = Tempus::createTimeStepControl<double>(pl,
false);
817 TEUCHOS_TEST_FOR_EXCEPT(
818 !(tsc->getTimeStepControlStrategy()->getStrategyType() ==
"Constant"));
822 auto pl = Tempus::getTimeStepControlPL<double>();
823 pl->remove(
"Time Step Control Strategy");
824 pl->set(
"Time Step Control Strategy",
825 *(Tempus::getTimeStepControlStrategyBasicVS_PL<double>()));
827 auto tsc = Tempus::createTimeStepControl<double>(pl);
832 TEUCHOS_TEST_FOR_EXCEPT(
833 !(tsc->getTimeStepControlStrategy()->getStrategyType() ==
"Basic VS"));
837 auto pl = Tempus::getTimeStepControlPL<double>();
838 pl->remove(
"Time Step Control Strategy");
840 "Time Step Control Strategy",
841 *(Tempus::getTimeStepControlStrategyIntegralControllerPL<double>()));
843 auto tsc = Tempus::createTimeStepControl<double>(pl);
848 TEUCHOS_TEST_FOR_EXCEPT(
849 !(tsc->getTimeStepControlStrategy()->getStrategyType() ==
850 "Integral Controller"));
854 auto pl = Tempus::getTimeStepControlPL<double>();
855 pl->remove(
"Time Step Control Strategy");
856 pl->set(
"Time Step Control Strategy",
857 *(Tempus::getTimeStepControlStrategyCompositePL<double>()));
859 auto tsc = Tempus::createTimeStepControl<double>(pl);
864 TEUCHOS_TEST_FOR_EXCEPT(
865 !(tsc->getTimeStepControlStrategy()->getStrategyType() ==
"Composite"));
869 auto pl = Tempus::getTimeStepControlPL<double>();
870 pl->remove(
"Time Step Control Strategy");
872 auto nonTempusStrategyPL =
873 Teuchos::parameterList(
"Time Step Control Strategy");
874 nonTempusStrategyPL->set<std::string>(
"Strategy Type",
875 "Application Strategy");
876 nonTempusStrategyPL->set<
double>(
"Secret Sauce", 1.2345);
878 pl->set(
"Time Step Control Strategy", *nonTempusStrategyPL);
880 auto tsc = Tempus::createTimeStepControl<double>(pl);
886 TEUCHOS_TEST_FOR_EXCEPT(
887 !(tsc->getTimeStepControlStrategy()->getStrategyType() ==
"Constant"));
898 temp->addStrategy(tscsBasicVS);
901 temp->addStrategy(tscsIntCtrl);
906 tsc->setTimeStepControlStrategy(temp);
911 tsc->getTimeStepControlStrategy();
920 std::vector<Teuchos::RCP<Tempus::TimeStepControlStrategy<double>>>
921 strategies = tscsc->getStrategies();
923 auto strategyBasicVS =
929 TEUCHOS_TEST_FOR_EXCEPT(strategyBasicVS->getReductFactor() != 0.5);
930 TEUCHOS_TEST_FOR_EXCEPT(strategyBasicVS->getMinEta() != 0.0);
931 TEUCHOS_TEST_FOR_EXCEPT(strategyBasicVS->getMaxEta() != 1.0e+16);
933 auto strategyIC = Teuchos::rcp_dynamic_cast<
936 TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getStepType() !=
"Variable");
937 TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getController() !=
"PID");
938 TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getKI() != 0.58);
939 TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getKP() != 0.21);
940 TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getKD() != 0.10);
941 TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getSafetyFactor() != 0.90);
942 TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getSafetyFactorAfterReject() != 0.90);
943 TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getFacMax() != 5.0);
944 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.
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
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...
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)