12 #include "Teuchos_DefaultComm.hpp"
14 #include "Thyra_VectorStdOps.hpp"
16 #include "Tempus_StepperFactory.hpp"
17 #include "Tempus_TimeStepControl.hpp"
20 #include "../TestModels/SinCosModel.hpp"
21 #include "../TestModels/DahlquistTestModel.hpp"
22 #include "../TestUtils/Tempus_ConvergenceTestUtils.hpp"
27 namespace Tempus_Unit_Test {
31 using Teuchos::rcp_const_cast;
32 using Teuchos::rcp_dynamic_cast;
34 using Teuchos::sublist;
35 using Teuchos::getParametersFromXmlFile;
49 TEUCHOS_TEST_FOR_EXCEPT(tscs->getKP() != 0.21);
50 TEUCHOS_TEST_FOR_EXCEPT(tscs->getKD() != 0.10);
51 TEUCHOS_TEST_FOR_EXCEPT(tscs->getSafetyFactor() != 0.90);
52 TEUCHOS_TEST_FOR_EXCEPT(tscs->getSafetyFactorAfterReject() != 0.90);
53 TEUCHOS_TEST_FOR_EXCEPT(tscs->getFacMax() != 5.0);
54 TEUCHOS_TEST_FOR_EXCEPT(tscs->getFacMin() != 0.5);
57 tscs->setController(
"I"); tscs->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!tscs->isInitialized());
58 tscs->setKI(0.6); tscs->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!tscs->isInitialized());
59 tscs->setKP(0.0); tscs->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!tscs->isInitialized());
60 tscs->setKD(0.0); tscs->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!tscs->isInitialized());
61 tscs->setSafetyFactor(0.8); tscs->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!tscs->isInitialized());
62 tscs->setSafetyFactorAfterReject(0.8); tscs->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!tscs->isInitialized());
63 tscs->setFacMax(4.0); tscs->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!tscs->isInitialized());
64 tscs->setFacMin(0.4); tscs->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!tscs->isInitialized());
66 TEUCHOS_TEST_FOR_EXCEPT(tscs->getStepType() !=
"Variable");
67 TEUCHOS_TEST_FOR_EXCEPT(tscs->getController() !=
"I");
68 TEUCHOS_TEST_FOR_EXCEPT(tscs->getKI() != 0.6);
69 TEUCHOS_TEST_FOR_EXCEPT(tscs->getKP() != 0.0);
70 TEUCHOS_TEST_FOR_EXCEPT(tscs->getKD() != 0.0);
71 TEUCHOS_TEST_FOR_EXCEPT(tscs->getSafetyFactor() != 0.8);
72 TEUCHOS_TEST_FOR_EXCEPT(tscs->getSafetyFactorAfterReject() != 0.8);
73 TEUCHOS_TEST_FOR_EXCEPT(tscs->getFacMax() != 4.0);
74 TEUCHOS_TEST_FOR_EXCEPT(tscs->getFacMin() != 0.4);
83 "I", 0.6, 0.0, 0.0, 0.8, 0.8, 4.0, 0.4));
89 TEUCHOS_TEST_FOR_EXCEPT(tscs->getKP() != 0.0);
90 TEUCHOS_TEST_FOR_EXCEPT(tscs->getKD() != 0.0);
91 TEUCHOS_TEST_FOR_EXCEPT(tscs->getSafetyFactor() != 0.8);
92 TEUCHOS_TEST_FOR_EXCEPT(tscs->getSafetyFactorAfterReject() != 0.8);
93 TEUCHOS_TEST_FOR_EXCEPT(tscs->getFacMax() != 4.0);
94 TEUCHOS_TEST_FOR_EXCEPT(tscs->getFacMin() != 0.4);
102 auto pl = Tempus::getTimeStepControlStrategyIntegralControllerPL<double>();
104 pl->set<std::string>(
"Controller Type",
"I");
105 pl->set<
double>(
"KI", 0.6);
106 pl->set<
double>(
"KP", 0.0);
107 pl->set<
double>(
"KD", 0.0);
108 pl->set<
double>(
"Safety Factor", 0.8);
109 pl->set<
double>(
"Safety Factor After Step Rejection", 0.8);
110 pl->set<
double>(
"Maximum Safety Factor", 4.0);
111 pl->set<
double>(
"Minimum Safety Factor", 0.4);
113 auto tscs = Tempus::createTimeStepControlStrategyIntegralController<double>(pl);
118 TEUCHOS_TEST_FOR_EXCEPT(tscs->getKP() != 0.0);
119 TEUCHOS_TEST_FOR_EXCEPT(tscs->getKD() != 0.0);
120 TEUCHOS_TEST_FOR_EXCEPT(tscs->getSafetyFactor() != 0.8);
121 TEUCHOS_TEST_FOR_EXCEPT(tscs->getSafetyFactorAfterReject() != 0.8);
122 TEUCHOS_TEST_FOR_EXCEPT(tscs->getFacMax() != 4.0);
123 TEUCHOS_TEST_FOR_EXCEPT(tscs->getFacMin() != 0.4);
134 double safetyFactor = 0.9;
135 double safetyFactorAfterReject = 0.9;
141 "PID", KI, KP, KD, safetyFactor, safetyFactorAfterReject,
146 tsc->setTimeStepControlStrategy(tscs);
147 tsc->setInitTime(0.0);
148 tsc->setFinalTime(10.0);
149 tsc->setMinTimeStep (0.01);
150 tsc->setInitTimeStep(1.0);
151 tsc->setMaxTimeStep (10.0);
152 tsc->setFinalIndex(100);
159 auto inArgsIC = model->getNominalValues();
161 auto icState = Tempus::createSolutionStateX<double>(icSolution);
165 solutionHistory->addState(icState);
166 solutionHistory->getCurrentState()->setTimeStep(1.0);
167 solutionHistory->getCurrentState()->setTime(0.0);
168 solutionHistory->getCurrentState()->setIndex(0);
169 solutionHistory->getCurrentState()->setOrder(order);
174 solutionHistory->initWorkingState();
175 auto currentState = solutionHistory->getCurrentState();
176 auto workingState = solutionHistory->getWorkingState();
178 tsc->setNextTimeStep(solutionHistory, status);
186 workingState->setErrorRel(errN);
192 solutionHistory->initWorkingState();
193 currentState = solutionHistory->getCurrentState();
194 workingState = solutionHistory->getWorkingState();
195 double dt = workingState->getTimeStep();
197 tsc->setNextTimeStep(solutionHistory, status);
199 double p = order - 1.0;
200 double dtNew = dt*safetyFactor*std::pow(errN, -KI/p);
206 workingState->setErrorRel(errN);
212 solutionHistory->initWorkingState();
213 currentState = solutionHistory->getCurrentState();
214 workingState = solutionHistory->getWorkingState();
215 dt = workingState->getTimeStep();
217 tsc->setNextTimeStep(solutionHistory, status);
219 dtNew = dt*safetyFactor*std::pow(errN, -KI/p)
220 *std::pow(errNm1, KP/p);
227 workingState->setErrorRel(errN);
233 solutionHistory->initWorkingState();
234 currentState = solutionHistory->getCurrentState();
235 workingState = solutionHistory->getWorkingState();
236 dt = workingState->getTimeStep();
238 tsc->setNextTimeStep(solutionHistory, status);
240 dtNew = dt*safetyFactor*std::pow(errN, -KI/p)
241 *std::pow(errNm1, KP/p)
242 *std::pow(errNm2, -KD/p);
254 auto pl = tscs->getValidParameters();
256 TEST_COMPARE ( pl->get<std::string>(
"Strategy Type"), ==,
"Integral Controller");
257 TEST_COMPARE ( pl->get<std::string>(
"Controller Type"), ==,
"PID");
267 std::ostringstream unusedParameters;
268 pl->unused(unusedParameters);
The classic Dahlquist Test Problem.
#define TEST_COMPARE(v1, comp, v2)
#define TEST_FLOATING_EQUALITY(v1, v2, tol)
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.
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)