Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_UnitTest_TimeStepControlStrategyIntegralController.cpp
Go to the documentation of this file.
1 // @HEADER
2 // ****************************************************************************
3 // Tempus: Copyright (2017) Sandia Corporation
4 //
5 // Distributed under BSD 3-clause license (See accompanying file Copyright.txt)
6 // ****************************************************************************
7 // @HEADER
8 
10 
11 #include "Tempus_TimeStepControl.hpp"
13 
14 #include "../TestModels/DahlquistTestModel.hpp"
15 
16 
17 namespace Tempus_Unit_Test {
18 
19 using Teuchos::RCP;
20 using Teuchos::rcp;
21 using Teuchos::rcp_const_cast;
22 using Teuchos::rcp_dynamic_cast;
24 using Teuchos::sublist;
25 
26 
27 // ************************************************************
28 // ************************************************************
29 TEUCHOS_UNIT_TEST(TimeStepControlStrategyIntegralController, Default_Construction)
30 {
32  TEUCHOS_TEST_FOR_EXCEPT(!tscs->isInitialized());
33 
34  // Test the get functions (i.e., defaults).
35  TEUCHOS_TEST_FOR_EXCEPT(tscs->getStepType() != "Variable");
36  TEUCHOS_TEST_FOR_EXCEPT(tscs->getController() != "PID");
37  TEUCHOS_TEST_FOR_EXCEPT(tscs->getKI() != 0.58);
38  TEUCHOS_TEST_FOR_EXCEPT(tscs->getKP() != 0.21);
39  TEUCHOS_TEST_FOR_EXCEPT(tscs->getKD() != 0.10);
40  TEUCHOS_TEST_FOR_EXCEPT(tscs->getSafetyFactor() != 0.90);
41  TEUCHOS_TEST_FOR_EXCEPT(tscs->getSafetyFactorAfterReject() != 0.90);
42  TEUCHOS_TEST_FOR_EXCEPT(tscs->getFacMax() != 5.0);
43  TEUCHOS_TEST_FOR_EXCEPT(tscs->getFacMin() != 0.5);
44 
45  // Test the set functions.
46  tscs->setController("I"); tscs->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!tscs->isInitialized());
47  tscs->setKI(0.6); tscs->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!tscs->isInitialized());
48  tscs->setKP(0.0); tscs->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!tscs->isInitialized());
49  tscs->setKD(0.0); tscs->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!tscs->isInitialized());
50  tscs->setSafetyFactor(0.8); tscs->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!tscs->isInitialized());
51  tscs->setSafetyFactorAfterReject(0.8); tscs->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!tscs->isInitialized());
52  tscs->setFacMax(4.0); tscs->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!tscs->isInitialized());
53  tscs->setFacMin(0.4); tscs->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!tscs->isInitialized());
54 
55  TEUCHOS_TEST_FOR_EXCEPT(tscs->getStepType() != "Variable");
56  TEUCHOS_TEST_FOR_EXCEPT(tscs->getController() != "I");
57  TEUCHOS_TEST_FOR_EXCEPT(tscs->getKI() != 0.6);
58  TEUCHOS_TEST_FOR_EXCEPT(tscs->getKP() != 0.0);
59  TEUCHOS_TEST_FOR_EXCEPT(tscs->getKD() != 0.0);
60  TEUCHOS_TEST_FOR_EXCEPT(tscs->getSafetyFactor() != 0.8);
61  TEUCHOS_TEST_FOR_EXCEPT(tscs->getSafetyFactorAfterReject() != 0.8);
62  TEUCHOS_TEST_FOR_EXCEPT(tscs->getFacMax() != 4.0);
63  TEUCHOS_TEST_FOR_EXCEPT(tscs->getFacMin() != 0.4);
64 }
65 
66 
67 // ************************************************************
68 // ************************************************************
69 TEUCHOS_UNIT_TEST(TimeStepControlStrategyIntegralController, Full_Construction)
70 {
72  "I", 0.6, 0.0, 0.0, 0.8, 0.8, 4.0, 0.4));
73  TEUCHOS_TEST_FOR_EXCEPT(!tscs->isInitialized());
74 
75  TEUCHOS_TEST_FOR_EXCEPT(tscs->getStepType() != "Variable");
76  TEUCHOS_TEST_FOR_EXCEPT(tscs->getController() != "I");
77  TEUCHOS_TEST_FOR_EXCEPT(tscs->getKI() != 0.6);
78  TEUCHOS_TEST_FOR_EXCEPT(tscs->getKP() != 0.0);
79  TEUCHOS_TEST_FOR_EXCEPT(tscs->getKD() != 0.0);
80  TEUCHOS_TEST_FOR_EXCEPT(tscs->getSafetyFactor() != 0.8);
81  TEUCHOS_TEST_FOR_EXCEPT(tscs->getSafetyFactorAfterReject() != 0.8);
82  TEUCHOS_TEST_FOR_EXCEPT(tscs->getFacMax() != 4.0);
83  TEUCHOS_TEST_FOR_EXCEPT(tscs->getFacMin() != 0.4);
84 }
85 
86 
87 // ************************************************************
88 // ************************************************************
89 TEUCHOS_UNIT_TEST(TimeStepControlStrategyIntegralController, Create_Construction)
90 {
91  auto pl = Tempus::getTimeStepControlStrategyIntegralControllerPL<double>();
92 
93  pl->set<std::string>("Controller Type", "I");
94  pl->set<double>("KI", 0.6);
95  pl->set<double>("KP", 0.0);
96  pl->set<double>("KD", 0.0);
97  pl->set<double>("Safety Factor", 0.8);
98  pl->set<double>("Safety Factor After Step Rejection", 0.8);
99  pl->set<double>("Maximum Safety Factor", 4.0);
100  pl->set<double>("Minimum Safety Factor", 0.4);
101 
102  auto tscs = Tempus::createTimeStepControlStrategyIntegralController<double>(pl);
103 
104  TEUCHOS_TEST_FOR_EXCEPT(tscs->getStepType() != "Variable");
105  TEUCHOS_TEST_FOR_EXCEPT(tscs->getController() != "I");
106  TEUCHOS_TEST_FOR_EXCEPT(tscs->getKI() != 0.6);
107  TEUCHOS_TEST_FOR_EXCEPT(tscs->getKP() != 0.0);
108  TEUCHOS_TEST_FOR_EXCEPT(tscs->getKD() != 0.0);
109  TEUCHOS_TEST_FOR_EXCEPT(tscs->getSafetyFactor() != 0.8);
110  TEUCHOS_TEST_FOR_EXCEPT(tscs->getSafetyFactorAfterReject() != 0.8);
111  TEUCHOS_TEST_FOR_EXCEPT(tscs->getFacMax() != 4.0);
112  TEUCHOS_TEST_FOR_EXCEPT(tscs->getFacMin() != 0.4);
113 }
114 
115 
116 // ************************************************************
117 // ************************************************************
118 TEUCHOS_UNIT_TEST(TimeStepControlStrategyIntegralController, setNextTimeStep)
119 {
120  double KI = 0.5;
121  double KP = 0.25;
122  double KD = 0.15;
123  double safetyFactor = 0.9;
124  double safetyFactorAfterReject = 0.9;
125  double facMax = 5.0;
126  double facMin = 0.5;
127 
128  auto tscs =
130  "PID", KI, KP, KD, safetyFactor, safetyFactorAfterReject,
131  facMax, facMin));
132 
133  // Setup the TimeStepControl --------------------------------
134  auto tsc = rcp(new Tempus::TimeStepControl<double>());
135  tsc->setTimeStepControlStrategy(tscs);
136  tsc->setInitTime(0.0);
137  tsc->setFinalTime(10.0);
138  tsc->setMinTimeStep (0.01);
139  tsc->setInitTimeStep(1.0);
140  tsc->setMaxTimeStep (10.0);
141  tsc->setFinalIndex(100);
142  tsc->initialize();
143  TEUCHOS_TEST_FOR_EXCEPT(!tsc->isInitialized());
145 
146  // Setup the SolutionHistory --------------------------------
147  auto model = rcp(new Tempus_Test::DahlquistTestModel<double>());
148  auto inArgsIC = model->getNominalValues();
149  auto icSolution = rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
150  auto icState = Tempus::createSolutionStateX<double>(icSolution);
151  auto solutionHistory = rcp(new Tempus::SolutionHistory<double>());
152 
153  double order = 2.0;
154  solutionHistory->addState(icState);
155  solutionHistory->getCurrentState()->setTimeStep(1.0);
156  solutionHistory->getCurrentState()->setTime(0.0);
157  solutionHistory->getCurrentState()->setIndex(0);
158  solutionHistory->getCurrentState()->setOrder(order);
159 
160 
161  // Mock Integrator
162 
163  // -- First Time Step
164  solutionHistory->initWorkingState();
165  auto currentState = solutionHistory->getCurrentState();
166  auto workingState = solutionHistory->getWorkingState();
167 
168  TEST_FLOATING_EQUALITY(workingState->getErrorRel() , 0.0, 1.0e-14);
169  TEST_FLOATING_EQUALITY(workingState->getErrorRelNm1(), 0.0, 1.0e-14);
170  TEST_FLOATING_EQUALITY(workingState->getErrorRelNm2(), 0.0, 1.0e-14);
171 
172  tsc->setNextTimeStep(solutionHistory, status);
173 
174  // First time step should cause no change to dt because
175  // internal relative errors = 1.
176  TEST_FLOATING_EQUALITY(workingState->getTimeStep(), 1.0, 1.0e-14);
177 
178  // Mock takeStep
179  double errN = 0.1;
180  workingState->setErrorRel(errN);
181  workingState->setSolutionStatus(Tempus::Status::PASSED);
182 
183 
184 
185  // -- Second Time Step
186  solutionHistory->initWorkingState();
187  currentState = solutionHistory->getCurrentState();
188  workingState = solutionHistory->getWorkingState();
189  double dt = workingState->getTimeStep();
190 
191  TEST_FLOATING_EQUALITY(workingState->getErrorRel() , 0.1, 1.0e-14);
192  TEST_FLOATING_EQUALITY(workingState->getErrorRelNm1(), 0.0, 1.0e-14);
193  TEST_FLOATING_EQUALITY(workingState->getErrorRelNm2(), 0.0, 1.0e-14);
194 
195  tsc->setNextTimeStep(solutionHistory, status);
196 
197  double p = order - 1.0;
198  double dtNew = dt*safetyFactor*std::pow(errN, -KI/p);
199  TEST_FLOATING_EQUALITY(workingState->getTimeStep(), dtNew, 1.0e-14);
200 
201  // Mock takeStep
202  errN = 0.2;
203  double errNm1 = 0.1;
204  workingState->setErrorRel(errN);
205  workingState->setSolutionStatus(Tempus::Status::PASSED);
206 
207 
208 
209  // -- Third Time Step
210  solutionHistory->initWorkingState();
211  currentState = solutionHistory->getCurrentState();
212  workingState = solutionHistory->getWorkingState();
213  dt = workingState->getTimeStep();
214 
215  TEST_FLOATING_EQUALITY(workingState->getErrorRel() , 0.2, 1.0e-14);
216  TEST_FLOATING_EQUALITY(workingState->getErrorRelNm1(), 0.1, 1.0e-14);
217  TEST_FLOATING_EQUALITY(workingState->getErrorRelNm2(), 0.0, 1.0e-14);
218 
219  tsc->setNextTimeStep(solutionHistory, status);
220 
221  dtNew = dt*safetyFactor*std::pow(errN, -KI/p)
222  *std::pow(errNm1, KP/p);
223  TEST_FLOATING_EQUALITY(workingState->getTimeStep(), dtNew, 1.0e-14);
224 
225  // Mock takeStep
226  errN = 0.3;
227  errNm1 = 0.2;
228  double errNm2 = 0.1;
229  workingState->setErrorRel(errN);
230  workingState->setSolutionStatus(Tempus::Status::PASSED);
231 
232 
233 
234  // -- Fourth Time Step
235  solutionHistory->initWorkingState();
236  currentState = solutionHistory->getCurrentState();
237  workingState = solutionHistory->getWorkingState();
238  dt = workingState->getTimeStep();
239 
240  TEST_FLOATING_EQUALITY(workingState->getErrorRel() , 0.3, 1.0e-14);
241  TEST_FLOATING_EQUALITY(workingState->getErrorRelNm1(), 0.2, 1.0e-14);
242  TEST_FLOATING_EQUALITY(workingState->getErrorRelNm2(), 0.1, 1.0e-14);
243 
244  tsc->setNextTimeStep(solutionHistory, status);
245 
246  dtNew = dt*safetyFactor*std::pow(errN, -KI/p)
247  *std::pow(errNm1, KP/p)
248  *std::pow(errNm2, -KD/p);
249  TEST_FLOATING_EQUALITY(workingState->getTimeStep(), dtNew, 1.0e-14);
250 
251 }
252 
253 
254 // ************************************************************
255 // ************************************************************
256 TEUCHOS_UNIT_TEST(TimeStepControlStrategyIntegralController, getValidParameters)
257 {
259 
260  auto pl = tscs->getValidParameters();
261 
262  TEST_COMPARE ( pl->get<std::string>("Strategy Type"), ==,"Integral Controller");
263  TEST_COMPARE ( pl->get<std::string>("Controller Type"), ==,"PID");
264  TEST_FLOATING_EQUALITY( pl->get<double>("KI"), 0.58, 1.0e-14);
265  TEST_FLOATING_EQUALITY( pl->get<double>("KP"), 0.21, 1.0e-14);
266  TEST_FLOATING_EQUALITY( pl->get<double>("KD"), 0.10, 1.0e-14);
267  TEST_FLOATING_EQUALITY( pl->get<double>("Safety Factor"), 0.9, 1.0e-14);
268  TEST_FLOATING_EQUALITY( pl->get<double>("Safety Factor After Step Rejection"), 0.9, 1.0e-14);
269  TEST_FLOATING_EQUALITY( pl->get<double>("Maximum Safety Factor"), 5.0, 1.0e-14);
270  TEST_FLOATING_EQUALITY( pl->get<double>("Minimum Safety Factor"), 0.5, 1.0e-14);
271 
272  { // Ensure that parameters are "used", excluding sublists.
273  std::ostringstream unusedParameters;
274  pl->unused(unusedParameters);
275  TEST_COMPARE ( unusedParameters.str(), ==, "");
276  }
277 }
278 
279 
280 } // namespace Tempus_Unit_Test
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...
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)