11 #include "Tempus_TimeStepControl.hpp"
18 namespace Tempus_Unit_Test {
22 using Teuchos::rcp_const_cast;
23 using Teuchos::rcp_dynamic_cast;
25 using Teuchos::sublist;
51 TEST_COMPARE ( tsc->getOutputIndexInterval(), ==, 1000000 );
53 auto tec = tsc->getTimeEvents();
55 TEST_COMPARE ( tec->getTimeEventNames() , ==,
"Output Index Interval, Output Time Interval");
96 ter->setName(
"Test Range");
97 tel->setName(
"Test List");
100 tsc->setTimeEvents(tecTmp);
101 tec = tsc->getTimeEvents();
103 TEST_COMPARE ( tec->getTimeEventNames() , ==,
"Test Range, Test List");
111 std::vector<int> outputIndices;
112 outputIndices.push_back(7);
113 outputIndices.push_back(11);
114 outputIndices.push_back(13);
116 std::vector<double> outputTimes;
117 outputTimes.push_back(0.3);
118 outputTimes.push_back(0.7);
119 outputTimes.push_back(1.3);
120 outputTimes.push_back(1.7);
127 ter-> setName(
"Test Range");
128 teri->setName(
"Test Range Index");
129 tel-> setName(
"Test List");
130 teli->setName(
"Test List Index");
181 auto tec = tsc->getTimeEvents();
183 TEST_COMPARE ( tec->getTimeEventNames() , ==,
"Test Range, Test Range Index, Test List, Test List Index, Output Time Interval, Output Time List, Output Index Interval, Output Index List");
192 Tempus::getTimeStepControlPL<double>();
194 pl->
set<
double> (
"Initial Time" , 1.0);
195 pl->
set<
double> (
"Final Time" , 100.0);
196 pl->
set<
double> (
"Minimum Time Step" , 0.01);
197 pl->
set<
double> (
"Initial Time Step" , 0.02);
198 pl->
set<
double> (
"Maximum Time Step" , 0.05);
199 pl->
set<
int> (
"Initial Time Index" , -100);
200 pl->
set<
int> (
"Final Time Index" , 100);
201 pl->
set<
double> (
"Maximum Absolute Error", 1.0e-06);
202 pl->
set<
double> (
"Maximum Relative Error", 1.0e-06);
203 pl->
set<
int> (
"Maximum Number of Stepper Failures", 8);
204 pl->
set<
int> (
"Maximum Number of Consecutive Stepper Failures", 4);
205 pl->
set<
int> (
"Number of Time Steps" , -1);
206 pl->
set<
bool> (
"Print Time Step Changes",
false);
207 pl->
set<
bool> (
"Output Exactly On Output Times",
false);
208 pl->
set<std::string>(
"Output Index List" ,
"7, 11, 13" );
209 pl->
set<std::string>(
"Output Time List" ,
"0.3, 0.7, 1.3, 1.7");
210 pl->
set<
int> (
"Output Index Interval" , 9);
211 pl->
set<
double> (
"Output Time Interval" , 0.011);
214 auto tscsPL = tscs->getValidParameters();
215 pl->
set(
"Time Step Control Strategy", *tscsPL);
222 ter-> setName(
"Test Range");
223 teri->setName(
"Test Range Index");
224 tel-> setName(
"Test List");
225 teli->setName(
"Test List Index");
230 auto tecPL = rcp_const_cast<
ParameterList>(tec->getValidParameters());
231 pl->
set(
"Time Step Control Events", *tecPL);
233 auto tsc = Tempus::createTimeStepControl<double>(pl);
263 tec = tsc->getTimeEvents();
265 TEST_COMPARE ( tec->getTimeEventNames() , ==,
"Output Index List, Output Index Interval, Output Time List, Output Time Interval, Test Range, Test Range Index, Test List, Test List Index");
278 double dLast = 0.989;
281 tsc->setInitTime(dFirst);
TEST_COMPARE( tsc->getInitTime(), ==, dFirst);
282 tsc->setFinalTime(dLast);
TEST_COMPARE( tsc->getFinalTime(), ==, dLast);
283 tsc->setMinTimeStep(dStep);
TEST_COMPARE( tsc->getMinTimeStep(), ==, dStep);
284 tsc->setInitTimeStep(dStep);
TEST_COMPARE( tsc->getInitTimeStep(), ==, dStep);
285 tsc->setMaxTimeStep(dLast);
TEST_COMPARE( tsc->getMaxTimeStep(), ==, dLast);
286 tsc->setInitIndex(iFirst);
TEST_COMPARE( tsc->getInitIndex(), ==, iFirst);
287 tsc->setFinalIndex(iLast);
TEST_COMPARE( tsc->getFinalIndex(), ==, iLast);
288 tsc->setMaxAbsError(dStep);
TEST_COMPARE( tsc->getMaxAbsError(), ==, dStep);
289 tsc->setMaxRelError(dStep);
TEST_COMPARE( tsc->getMaxRelError(), ==, dStep);
290 tsc->setOutputExactly(
false);
TEST_COMPARE( tsc->getOutputExactly(), ==,
false);
291 tsc->setOutputExactly(
true);
TEST_COMPARE( tsc->getOutputExactly(), ==,
true);
293 std::vector<int> iVSet{ 0, 1, 2, 3, 5, 8, 13, 21, 34 };
296 tsc->setOutputIndexInterval(iStep);
TEST_COMPARE( tsc->getOutputIndexInterval(), ==, iStep);
297 tsc->setOutputTimeInterval(dStep);
TEST_COMPARE( tsc->getOutputTimeInterval(), ==, dStep);
307 std::vector<double> times_in;
308 times_in.push_back(0.0000000000000000e-11);
309 times_in.push_back(0.1001384570000000e-11);
310 times_in.push_back(0.2002769140000000e-11);
311 times_in.push_back(0.3004153710000000e-11);
312 times_in.push_back(0.4005538280000000e-11);
313 times_in.push_back(0.5006922850000000e-11);
314 times_in.push_back(0.6008307420000000e-11);
315 times_in.push_back(0.7009691990000000e-11);
316 times_in.push_back(0.8011076560000000e-11);
317 times_in.push_back(0.9012461130000000e-11);
318 times_in.push_back(1.0013845700000000e-11);
320 tsc->setOutputTimes(times_in);
322 auto times_out = tsc->getOutputTimes();
323 double maxDiff = 0.0;
326 for (
size_t i=0; i < times_in.size(); ++i) {
330 maxDiff = std::max(std::fabs(times_in[i] - times_out[i]), maxDiff);
338 times_in.push_back(0.00000000000000000000000000000000);
339 times_in.push_back(0.00000000000100138457000000009381);
340 times_in.push_back(0.00000000000200276914000000018762);
341 times_in.push_back(0.00000000000300415371000000007949);
342 times_in.push_back(0.00000000000400553828000000037525);
343 times_in.push_back(0.00000000000500692284999999986321);
344 times_in.push_back(0.00000000000600830742000000015898);
345 times_in.push_back(0.00000000000700969198999999964694);
346 times_in.push_back(0.00000000000801107656000000075050);
347 times_in.push_back(0.00000000000901246112999999943067);
348 times_in.push_back(0.00000000001001384569999999972643);
350 tsc->setOutputTimes(times_in);
352 times_out = tsc->getOutputTimes();
356 for (
size_t i=0; i < times_in.size(); ++i) {
360 maxDiff = std::max(std::fabs(times_in[i] - times_out[i]), maxDiff);
373 int setOutputTimeIndex = 17;
374 double setOutputTimeInterval = 1.101001000100001e-7;
376 tsc->setFinalTime(1.0);
377 tsc->setOutputIndexInterval(setOutputTimeIndex);
378 tsc->setOutputTimeInterval(setOutputTimeInterval);
380 int getOutputTimeIndex = tsc->getOutputIndexInterval();
381 double getOutputTimeInterval = tsc->getOutputTimeInterval();
382 TEST_COMPARE(getOutputTimeInterval, ==, setOutputTimeInterval);
383 TEST_COMPARE(getOutputTimeIndex, ==, setOutputTimeIndex);
395 auto testTimeInRange = [=] (
double initTime,
double finalTime)
397 tsc->setInitTime (initTime);
398 tsc->setFinalTime(finalTime);
402 const int i = (initTime == 0) ? 0 : 1 + (
int)std::floor(std::log10(std::fabs(initTime) ) );
403 const double absTolInit10 = std::pow(10, i-10);
404 const double absTolInit15 = std::pow(10, i-15);
405 const int j = (finalTime == 0) ? 0 : 1 + (
int)std::floor(std::log10(std::fabs(finalTime) ) );
406 const double absTolFinal10 = std::pow(10, j-10);
407 const double absTolFinal15 = std::pow(10, j-15);
411 if ( initTime == 0.0 ) {
427 if ( finalTime == 0.0 ) {
435 testTimeInRange (0.0, 1.0);
438 testTimeInRange (-1.0, 0.0);
441 testTimeInRange ( 9.9e-20, 3.3e+20);
442 testTimeInRange (-1.9e+20, 2.3e-20);
454 auto testIndexInRange = [=] (
double initIndex,
double finalIndex)
456 tsc->setInitIndex (initIndex);
457 tsc->setFinalIndex(finalIndex);
474 testIndexInRange (0, 10);
477 testIndexInRange (-10, 0);
480 testIndexInRange (-190000, 20);
481 testIndexInRange (-19, 200000);
489 std::vector<int> outputIndices;
490 outputIndices.push_back(7);
491 outputIndices.push_back(11);
492 outputIndices.push_back(13);
494 std::vector<double> outputTimes;
495 outputTimes.push_back(0.3);
496 outputTimes.push_back(0.7);
497 outputTimes.push_back(1.3);
498 outputTimes.push_back(1.7);
506 1.0, 100.0, 0.01, 0.02, 0.05, -100,
507 100, 1.0e-06, 1.0e-06, 8, 4, -1,
false,
false,
508 outputIndices, outputTimes, 9, 0.011, tec, tscsc));
511 auto pl = tsc->getValidParameters();
518 TEST_COMPARE ( pl->get<
int> (
"Initial Time Index") , ==, -100 );
519 TEST_COMPARE ( pl->get<
int> (
"Final Time Index") , ==, 100 );
522 TEST_COMPARE ( pl->get<
int> (
"Maximum Number of Stepper Failures") , ==, 8);
523 TEST_COMPARE ( pl->get<
int> (
"Maximum Number of Consecutive Stepper Failures"), ==, 4);
524 TEST_COMPARE ( pl->get<
int> (
"Number of Time Steps") , ==, -1 );
525 TEST_COMPARE ( pl->get<
bool> (
"Print Time Step Changes") , ==,
false );
526 TEST_COMPARE ( pl->get<
bool> (
"Output Exactly On Output Times"), ==,
false );
527 TEST_COMPARE ( pl->get<std::string>(
"Output Index List") , ==,
"7, 11, 13" );
528 TEST_COMPARE ( pl->get<std::string>(
"Output Time List") , ==,
"0.3, 0.7, 1.3, 1.7");
529 TEST_COMPARE ( pl->get<
int> (
"Output Index Interval") , ==, 9 );
533 std::ostringstream unusedParameters;
534 pl->unused(unusedParameters);
536 "WARNING: Parameter \"Time Step Control Strategy\" [unused] is unused\n");
539 auto tscs_PL = pl->sublist(
"Time Step Control Strategy");
540 TEST_COMPARE ( tscs_PL.get<std::string>(
"Strategy Type") , ==,
"Constant");
544 std::ostringstream unusedParameters;
545 tscs_PL.unused(unusedParameters);
559 tsc->setInitTime(0.0);
560 tsc->setFinalTime(100.0);
561 tsc->setMinTimeStep(0.01);
562 tsc->setInitTimeStep(0.02);
563 tsc->setMaxTimeStep(0.05);
564 tsc->setNumTimeSteps(-1);
575 tsc->setNumTimeSteps(100);
594 auto inArgsIC = model->getNominalValues();
596 auto icState = Tempus::createSolutionStateX<double>(icSolution);
598 solutionHistory->addState(icState);
600 solutionHistory->getCurrentState()->setTimeStep(dt);
604 std::vector<double> outputTimes;
605 double outputTime = 0.8;
606 outputTimes.push_back(outputTime);
607 tsc->setOutputTimes(outputTimes);
608 tsc->setOutputExactly(
true);
610 tsc->setTimeStepControlStrategy(tscs);
611 tsc->setMinTimeStep (dt/2.0);
612 tsc->setInitTimeStep(dt);
613 tsc->setMaxTimeStep (2.0*dt);
614 tsc->setPrintDtChanges(
true);
621 solutionHistory->initWorkingState();
622 auto currentState = solutionHistory->getCurrentState();
623 auto workingState = solutionHistory->getWorkingState();
625 tsc->setNextTimeStep(solutionHistory, status);
634 solutionHistory->promoteWorkingState();
638 solutionHistory->initWorkingState();
639 currentState = solutionHistory->getCurrentState();
640 workingState = solutionHistory->getWorkingState();
642 tsc->setNextTimeStep(solutionHistory, status);
646 currentState->getTime() + workingState->getTimeStep(),
647 workingState->getTime(), 1.0e-14);
659 auto inArgsIC = model->getNominalValues();
661 auto icState = Tempus::createSolutionStateX<double>(icSolution);
663 solutionHistory->addState(icState);
665 solutionHistory->getCurrentState()->setTimeStep(dt);
669 std::vector<double> outputTimes;
670 double outputTime = 0.8;
671 outputTimes.push_back(outputTime);
672 tsc->setOutputTimes(outputTimes);
673 tsc->setMinTimeStep (dt/2.0);
674 tsc->setInitTimeStep(dt);
675 tsc->setMaxTimeStep (2.0*dt);
676 tsc->setOutputExactly(
false);
682 solutionHistory->initWorkingState();
683 auto currentState = solutionHistory->getCurrentState();
684 auto workingState = solutionHistory->getWorkingState();
686 tsc->setNextTimeStep(solutionHistory, status);
687 double timeN = workingState->getTime();
695 solutionHistory->promoteWorkingState();
698 solutionHistory->initWorkingState();
700 currentState = solutionHistory->getCurrentState();
701 workingState = solutionHistory->getWorkingState();
703 tsc->setNextTimeStep(solutionHistory, status);
704 timeN = workingState->getTime();
707 double dtN = workingState->getTimeStep();
720 auto inArgsIC = model->getNominalValues();
722 auto icState = Tempus::createSolutionStateX<double>(icSolution);
724 solutionHistory->addState(icState);
726 solutionHistory->getCurrentState()->setTimeStep(dt);
730 std::vector<double> outputTimes;
731 double outputTime = 0.8;
732 outputTimes.push_back(outputTime);
733 tsc->setOutputTimes(outputTimes);
734 tsc->setOutputExactly(
true);
735 tsc->setTimeStepControlStrategy();
736 tsc->setInitTimeStep(dt);
742 for (
int i=0; i < 10000; ++i) {
743 solutionHistory->initWorkingState();
744 tsc->setNextTimeStep(solutionHistory, status);
749 solutionHistory->promoteWorkingState();
752 auto currentState = solutionHistory->getCurrentState();
753 double time = currentState->getTime();
764 auto pl = Tempus::getTimeStepControlPL<double>();
765 pl->remove(
"Time Step Control Strategy");
767 auto tsc = Tempus::createTimeStepControl<double>(pl,
false);
774 TEUCHOS_TEST_FOR_EXCEPT(!(tsc->getTimeStepControlStrategy()->getStrategyType() ==
"Constant"));
778 auto pl = Tempus::getTimeStepControlPL<double>();
779 pl->remove(
"Time Step Control Strategy");
780 pl->set(
"Time Step Control Strategy",
781 *(Tempus::getTimeStepControlStrategyBasicVS_PL<double>()));
783 auto tsc = Tempus::createTimeStepControl<double>(pl);
788 TEUCHOS_TEST_FOR_EXCEPT(!(tsc->getTimeStepControlStrategy()->getStrategyType() ==
"Basic VS"));
792 auto pl = Tempus::getTimeStepControlPL<double>();
793 pl->remove(
"Time Step Control Strategy");
794 pl->set(
"Time Step Control Strategy",
795 *(Tempus::getTimeStepControlStrategyIntegralControllerPL<double>()));
797 auto tsc = Tempus::createTimeStepControl<double>(pl);
802 TEUCHOS_TEST_FOR_EXCEPT(!(tsc->getTimeStepControlStrategy()->getStrategyType() ==
"Integral Controller"));
806 auto pl = Tempus::getTimeStepControlPL<double>();
807 pl->remove(
"Time Step Control Strategy");
808 pl->set(
"Time Step Control Strategy",
809 *(Tempus::getTimeStepControlStrategyCompositePL<double>()));
811 auto tsc = Tempus::createTimeStepControl<double>(pl);
816 TEUCHOS_TEST_FOR_EXCEPT(!(tsc->getTimeStepControlStrategy()->getStrategyType() ==
"Composite"));
820 auto pl = Tempus::getTimeStepControlPL<double>();
821 pl->remove(
"Time Step Control Strategy");
823 auto nonTempusStrategyPL =
824 Teuchos::parameterList(
"Time Step Control Strategy");
825 nonTempusStrategyPL->set<std::string>(
"Strategy Type",
"Application Strategy");
826 nonTempusStrategyPL->set<
double>(
"Secret Sauce", 1.2345);
828 pl->set(
"Time Step Control Strategy", *nonTempusStrategyPL);
830 auto tsc = Tempus::createTimeStepControl<double>(pl);
835 TEUCHOS_TEST_FOR_EXCEPT(!(tsc->getTimeStepControlStrategy()->getStrategyType() ==
"Constant"));
848 temp->addStrategy(tscsBasicVS);
850 temp->addStrategy(tscsIntCtrl);
855 tsc->setTimeStepControlStrategy(temp);
860 tsc->getTimeStepControlStrategy();
867 std::vector<Teuchos::RCP<Tempus::TimeStepControlStrategy<double>>>
868 strategies = tscsc->getStrategies();
874 TEUCHOS_TEST_FOR_EXCEPT(strategyBasicVS->getReductFactor() != 0.5);
875 TEUCHOS_TEST_FOR_EXCEPT(strategyBasicVS->getMinEta() != 0.0);
876 TEUCHOS_TEST_FOR_EXCEPT(strategyBasicVS->getMaxEta() != 1.0e+16);
880 TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getStepType() !=
"Variable");
881 TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getController() !=
"PID");
882 TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getKI() != 0.58);
883 TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getKP() != 0.21);
884 TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getKD() != 0.10);
885 TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getSafetyFactor() != 0.90);
886 TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getSafetyFactorAfterReject() != 0.90);
887 TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getFacMax() != 5.0);
888 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)