Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_TimeStepControlStrategyIntegralController.hpp
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 
9 #ifndef Tempus_TimeStepControlStrategy_IntegralController_hpp
10 #define Tempus_TimeStepControlStrategy_IntegralController_hpp
11 
12 #include "Tempus_config.hpp"
14 #include "Tempus_SolutionState.hpp"
15 #include "Tempus_SolutionHistory.hpp"
16 #include "Tempus_StepperState.hpp"
17 
18 
19 namespace Tempus {
20 
57 template<class Scalar>
59  : virtual public TimeStepControlStrategy<Scalar>
60 {
61 public:
62 
65  : controller_("PID"), KI_(0.58), KP_(0.21), KD_(0.10),
67  facMax_(5.0), facMin_(0.5)
68  {
69  errN_ = Scalar(1.0);
70  errNm1_ = Scalar(1.0);
71  errNm2_ = Scalar(1.0);
73 
74  this->setStrategyType("Integral Controller");
75  this->setStepType("Variable");
76  this->setName("Integral Controller");
77  this->initialize();
78  }
79 
82  Scalar KI, Scalar KP, Scalar KD,
83  Scalar safetyFactor, Scalar safetyFactorAfterReject,
84  Scalar facMax, Scalar facMin, std::string name = "Integral Controller")
85  : controller_(controller), KI_(KI), KP_(KP), KD_(KD),
86  safetyFactor_(safetyFactor),
87  safetyFactorAfterReject_(safetyFactorAfterReject),
88  facMax_(facMax), facMin_(facMin)
89  {
90  errN_ = Scalar(1.0);
91  errNm1_ = Scalar(1.0);
92  errNm2_ = Scalar(1.0);
94 
95  this->setStrategyType("Integral Controller");
96  this->setStepType("Variable");
97  this->setName(name);
98  this->initialize();
99  }
100 
101 
104 
106  virtual void setNextTimeStep(const TimeStepControl<Scalar> & tsc,
107  Teuchos::RCP<SolutionHistory<Scalar> > solutionHistory,
108  Status & /* integratorStatus */) override
109  {
110  using Teuchos::RCP;
111 
112  this->checkInitialized();
113 
114  // Take first step with initial time step provided
115  if (!firstSuccessfulStep_){
116  firstSuccessfulStep_ = true;
117  return;
118  }
119 
120  RCP<SolutionState<Scalar> > workingState=solutionHistory->getWorkingState();
121  const Scalar errorRel = workingState->getErrorRel();
122  Scalar beta = 1.0;
123 
124  // assumes the embedded solution is the low order solution
125  int order = workingState->getOrder() - 1;
126  Scalar dt = workingState->getTimeStep();
127 
128  // update errors
129  errNm2_ = errNm1_;
130  errNm1_ = errN_;
131  errN_ = errorRel;
132 
133  Scalar k1 = Teuchos::as<Scalar>(-KI_ / order);
134  Scalar k2 = Teuchos::as<Scalar>( KP_ / order);
135  Scalar k3 = Teuchos::as<Scalar>(-KD_ / order);
136 
137  k1 = std::pow(errN_, k1);
138  k2 = std::pow(errNm1_, k2);
139  k3 = std::pow(errNm2_, k3);
140 
141  if (controller_ == "I")
142  beta = safetyFactor_*k1;
143  else if (controller_ == "PI")
144  beta = safetyFactor_ *k1*k2;
145  else // (controller_ == "PID")
146  beta = safetyFactor_*k1*k2*k3;
147 
148  beta = std::max(facMin_, beta);
149  beta = std::min(facMax_, beta);
150 
151  // new (optimal) suggested time step
152  dt = beta * dt;
153 
154  if (workingState->getSolutionStatus() == Status::PASSED ||
155  workingState->getSolutionStatus() == Status::WORKING) {
156  if(lastStepRejected_){
157  dt = std::min(dt, workingState->getTimeStep());
158  } else {
160  }
161  lastStepRejected_ = false;
162  } else {
164  lastStepRejected_ = true;
165  }
166 
167  // update dt
168  workingState->setTimeStep(dt);
169  workingState->setTime(solutionHistory->getCurrentState()->getTime() + dt);
170  }
171 
172 
174 
175  std::string description() const override
176  { return "Tempus::TimeStepControlStrategyIntegralController"; }
177 
179  const Teuchos::EVerbosityLevel verbLevel) const override
180  {
181  Teuchos::OSTab ostab(out,2,"describe");
182  out << description() << "::describe:" << std::endl
183  << "Strategy Type = " << this->getStrategyType() << std::endl
184  << "Step Type = " << this->getStepType() << std::endl
185  << "Controller Type = " << getController() << std::endl
186  << "KI = " << getKI() << std::endl
187  << "KP = " << getKP() << std::endl
188  << "KD = " << getKD() << std::endl
189  << "errN_ = " << errN_ << std::endl
190  << "errNm1_ = " << errNm1_ << std::endl
191  << "errNm2_ = " << errNm2_ << std::endl
192  << "Safety Factor = " << getSafetyFactor() << std::endl
193  << "Safety Factor After Step Rejection = " << getSafetyFactorAfterReject() << std::endl
194  << "Maximum Safety Factor (INPUT) = " << facMaxINPUT_ << std::endl
195  << "Maximum Safety Factor = " << getFacMax() << std::endl
196  << "Minimum Safety Factor = " << getFacMin() << std::endl;
197  }
199 
202  {
204  Teuchos::parameterList("Time Step Control Strategy");
205 
206  pl->set<std::string>("Strategy Type", this->getStrategyType(), "Integral Controller");
207  pl->set<std::string>("Controller Type", getController(),
208  "Proportional-Integral-Derivative");
209  pl->set<Scalar>("KI" , getKI(), "Integral gain");
210  pl->set<Scalar>("KP" , getKP(), "Proportional gain");
211  pl->set<Scalar>("KD" , getKD(), "Derivative gain");
212  pl->set<Scalar>("Safety Factor" , getSafetyFactor(), "Safety Factor");
213  pl->set<Scalar>("Safety Factor After Step Rejection",
215  "Safety Factor Following Step Rejection");
216  pl->set<Scalar>("Maximum Safety Factor" , getFacMax(), "Maximum Safety Factor");
217  pl->set<Scalar>("Minimum Safety Factor" , getFacMin(), "Minimum Safety Factor");
218  return pl;
219  }
220 
221 
222  virtual void initialize() const override
223  {
224  TEUCHOS_TEST_FOR_EXCEPTION(safetyFactor_ <= 0.0, std::out_of_range,
225  "Error - Invalid value of Safety Factory= " << safetyFactor_ << "! \n"
226  << "Safety Factor must be > 0.0.\n");
227 
228  TEUCHOS_TEST_FOR_EXCEPTION(facMax_ <= 0.0, std::out_of_range,
229  "Error - Invalid value of Maximum Safety Factory= " << facMax_ << "! \n"
230  << "Maximum Safety Factor must be > 0.0.\n");
231 
232  TEUCHOS_TEST_FOR_EXCEPTION(facMax_<= 0.0, std::out_of_range,
233  "Error - Invalid value of Minimum Safety Factory= " << facMin_ << "! \n"
234  << "Minimum Safety Factor must be > 0.0.\n");
235 
237  (controller_ != "PI") &&
238  (controller_ != "PID")), std::invalid_argument,
239  "Error - Invalid choice of Controller Type = " << controller_ << "! \n"
240  << "Valid Choice are ['I', 'PI', 'PID'].\n");
241 
242  this->isInitialized_ = true; // Only place where this is set to true!
243  }
244 
245 
246  virtual std::string getController() const { return controller_; }
247  virtual Scalar getKI() const { return KI_; }
248  virtual Scalar getKP() const { return KP_; }
249  virtual Scalar getKD() const { return KD_; }
250  virtual Scalar getSafetyFactor() const { return safetyFactor_; }
251  virtual Scalar getSafetyFactorAfterReject() const { return safetyFactorAfterReject_; }
252  virtual Scalar getFacMax() const { return facMax_; }
253  virtual Scalar getFacMin() const { return facMin_; }
254 
255  virtual void setController(std::string c) { controller_ = c; this->isInitialized_ = false; }
256  virtual void setKI(Scalar k) { KI_ = k; this->isInitialized_ = false; }
257  virtual void setKP(Scalar k) { KP_ = k; this->isInitialized_ = false; }
258  virtual void setKD(Scalar k) { KD_ = k; this->isInitialized_ = false; }
259  virtual void setSafetyFactor(Scalar f) { safetyFactor_ = f; this->isInitialized_ = false; }
260  virtual void setSafetyFactorAfterReject(Scalar f) { safetyFactorAfterReject_ = f; this->isInitialized_ = false; }
261  virtual void setFacMax(Scalar f) { facMax_ = f; facMaxINPUT_ = f; this->isInitialized_ = false; }
262  virtual void setFacMin(Scalar f) { facMin_ = f; this->isInitialized_ = false; }
263 
264 private:
265 
266  std::string controller_;
267  Scalar KI_;
268  Scalar KP_;
269  Scalar KD_;
270  Scalar errN_;
271  Scalar errNm1_;
272  Scalar errNm2_;
273  Scalar safetyFactor_;
275  Scalar facMaxINPUT_;
276  Scalar facMax_;
277  Scalar facMin_;
278  bool firstSuccessfulStep_ = false;
279  bool lastStepRejected_ = false;
280 
281 };
282 
283 
284 // Nonmember constructor.
285 template <class Scalar>
289  std::string name = "Integral Controller")
290 {
291  using Teuchos::rcp;
293  if (pList == Teuchos::null) return tscs;
294 
296  pList->get<std::string>("Strategy Type", "Integral Controller") !=
297  "Integral Controller", std::logic_error,
298  "Error - Strategy Type != 'Integral Controller'. (='"
299  +pList->get<std::string>("Strategy Type")+"')\n");
300 
301  pList->validateParametersAndSetDefaults(*tscs->getValidParameters());
302 
303  tscs->setController (pList->get<std::string>("Controller Type"));
304  tscs->setKI (pList->get<Scalar>("KI"));
305  tscs->setKP (pList->get<Scalar>("KP"));
306  tscs->setKD (pList->get<Scalar>("KD"));
307  tscs->setSafetyFactor(pList->get<Scalar>("Safety Factor"));
308  tscs->setSafetyFactorAfterReject(pList->get<Scalar>("Safety Factor After Step Rejection"));
309  tscs->setFacMax (pList->get<Scalar>("Maximum Safety Factor"));
310  tscs->setFacMin (pList->get<Scalar>("Minimum Safety Factor"));
311 
312  tscs->setName(name);
313  tscs->initialize();
314 
315  return tscs;
316 }
317 
318 
320 template<class Scalar>
322 {
324  return Teuchos::rcp_const_cast<Teuchos::ParameterList> (t->getValidParameters());
325 }
326 
327 
328 } // namespace Tempus
329 #endif // Tempus_TimeStepControlStrategy_IntegralController_hpp
virtual Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
Return ParameterList with current values.
T & get(const std::string &name, T def_value)
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Status
Status for the Integrator, the Stepper and the SolutionState.
bool isInitialized_
Bool if strategy is initialized.
TimeStepControl manages the time step size. There several mechanisms that effect the time step size a...
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const override
void validateParametersAndSetDefaults(ParameterList const &validParamList, int const depth=1000)
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
std::string controller_
Control type [&#39;I&#39;, &#39;PI&#39;, &#39;PID&#39;].
Teuchos::RCP< Teuchos::ParameterList > getTimeStepControlStrategyIntegralControllerPL()
Nonmember function to return ParameterList with default values.
TimeStepControlStrategyIntegralController(std::string controller, Scalar KI, Scalar KP, Scalar KD, Scalar safetyFactor, Scalar safetyFactorAfterReject, Scalar facMax, Scalar facMin, std::string name="Integral Controller")
Full Constructor.
virtual void setNextTimeStep(const TimeStepControl< Scalar > &tsc, Teuchos::RCP< SolutionHistory< Scalar > > solutionHistory, Status &) override
Set the time step size.
Teuchos::RCP< TimeStepControlStrategyIntegralController< Scalar > > createTimeStepControlStrategyIntegralController(const Teuchos::RCP< Teuchos::ParameterList > pList, std::string name="Integral Controller")
TimeStepControlStrategy class for TimeStepControl.