Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_TimeStepControlStrategyBasicVS.hpp
Go to the documentation of this file.
1 //@HEADER
2 // *****************************************************************************
3 // Tempus: Time Integration and Sensitivity Analysis Package
4 //
5 // Copyright 2017 NTESS and the Tempus contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 //@HEADER
9 
10 #ifndef Tempus_TimeStepControlStrategy_BasicVS_hpp
11 #define Tempus_TimeStepControlStrategy_BasicVS_hpp
12 
13 #include "Thyra_VectorStdOps.hpp"
14 
15 #include "Tempus_config.hpp"
17 #include "Tempus_SolutionState.hpp"
18 #include "Tempus_SolutionHistory.hpp"
19 #include "Tempus_StepperState.hpp"
20 
21 namespace Tempus {
22 
103 template <class Scalar>
105  : virtual public TimeStepControlStrategy<Scalar> {
106  public:
109  : rho_(1.75), sigma_(0.5), minEta_(0.0), maxEta_(1.0e+16)
110  {
111  this->setStrategyType("Basic VS");
112  this->setStepType("Variable");
113  this->setName("Basic VS");
114  this->initialize();
115  }
116 
118  TimeStepControlStrategyBasicVS(Scalar rho, Scalar sigma, Scalar minEta,
119  Scalar maxEta, std::string name = "Basic VS")
120  : rho_(rho), sigma_(sigma), minEta_(minEta), maxEta_(maxEta)
121  {
122  this->setStrategyType("Basic VS");
123  this->setStepType("Variable");
124  this->setName(name);
125  this->initialize();
126  }
127 
130 
132  virtual void setNextTimeStep(
133  const TimeStepControl<Scalar> &tsc,
134  Teuchos::RCP<SolutionHistory<Scalar> > solutionHistory,
135  Status & /* integratorStatus */) override
136  {
137  using Teuchos::RCP;
138 
139  this->checkInitialized();
140 
141  RCP<SolutionState<Scalar> > workingState =
142  solutionHistory->getWorkingState();
143  const Scalar errorAbs = workingState->getErrorAbs();
144  const Scalar errorRel = workingState->getErrorRel();
145  const int iStep = workingState->getIndex();
146  Scalar dt = workingState->getTimeStep();
147 
148  Scalar rho = getAmplFactor();
149  Scalar sigma = getReductFactor();
150  Scalar eta = solutionHistory->getCurrentState()->getDxNormL2Rel();
151  if (iStep == 1) eta = getMinEta(); // For first step use initial dt.
152 
153  // General rule: only increase/decrease dt once for any given reason.
154  if (workingState->getSolutionStatus() == Status::FAILED) {
155  tsc.printDtChanges(iStep, dt, dt * sigma,
156  "Stepper failure - Decreasing dt.");
157  dt *= sigma;
158  }
159  else { // Stepper passed
160  if (eta < getMinEta()) { // increase dt
161  tsc.printDtChanges(iStep, dt, dt * rho,
162  "Change too small (" + std::to_string(eta) + " < " +
163  std::to_string(getMinEta()) +
164  "). Increasing dt.");
165  dt *= rho;
166  }
167  else if (eta > getMaxEta()) { // reduce dt
168  tsc.printDtChanges(iStep, dt, dt * sigma,
169  "Change too large (" + std::to_string(eta) + " > " +
170  std::to_string(getMaxEta()) +
171  "). Decreasing dt.");
172  dt *= sigma;
173  }
174  else if (errorAbs > tsc.getMaxAbsError()) { // reduce dt
175  tsc.printDtChanges(
176  iStep, dt, dt * sigma,
177  "Absolute error is too large (" + std::to_string(errorAbs) + " > " +
178  std::to_string(tsc.getMaxAbsError()) + "). Decreasing dt.");
179  dt *= sigma;
180  }
181  else if (errorRel > tsc.getMaxRelError()) { // reduce dt
182  tsc.printDtChanges(
183  iStep, dt, dt * sigma,
184  "Relative error is too large (" + std::to_string(errorRel) + " > " +
185  std::to_string(tsc.getMaxRelError()) + "). Decreasing dt.");
186  dt *= sigma;
187  }
188  }
189 
190  if (dt < tsc.getMinTimeStep()) { // decreased below minimum dt
191  tsc.printDtChanges(iStep, dt, tsc.getMinTimeStep(),
192  "dt is too small. Resetting to minimum dt.");
193  dt = tsc.getMinTimeStep();
194  }
195  if (dt > tsc.getMaxTimeStep()) { // increased above maximum dt
196  tsc.printDtChanges(iStep, dt, tsc.getMaxTimeStep(),
197  "dt is too large. Resetting to maximum dt.");
198  dt = tsc.getMaxTimeStep();
199  }
200 
201  workingState->setTimeStep(dt);
202  workingState->setTime(solutionHistory->getCurrentState()->getTime() + dt);
203  workingState->setComputeNorms(true);
204  }
205 
207 
208  std::string description() const override
209  {
210  return "Tempus::TimeStepControlStrategyBasicVS";
211  }
212 
214  const Teuchos::EVerbosityLevel verbLevel) const override
215  {
216  auto l_out = Teuchos::fancyOStream(out.getOStream());
217  Teuchos::OSTab ostab(*l_out, 2, this->description());
218  l_out->setOutputToRootOnly(0);
219 
220  *l_out << "\n--- " << this->description() << " ---" << std::endl;
221 
222  if (Teuchos::as<int>(verbLevel) >= Teuchos::as<int>(Teuchos::VERB_MEDIUM)) {
223  *l_out << " StrategyType = "
224  << this->getStrategyType() << std::endl
225  << " Step Type = " << this->getStepType()
226  << std::endl
227  << " Amplification Factor = " << getAmplFactor()
228  << std::endl
229  << " Reduction Factor = " << getReductFactor()
230  << std::endl
231  << " Minimum Value Monitoring Function = " << getMinEta()
232  << std::endl
233  << " Maximum Value Monitoring Function = " << getMaxEta()
234  << std::endl;
235  *l_out << std::string(this->description().length() + 8, '-') << std::endl;
236  }
237  }
239 
242  const override
243  {
245  Teuchos::parameterList("Time Step Control Strategy");
246 
247  pl->set<std::string>("Strategy Type", this->getStrategyType(), "Basic VS");
248  pl->set<double>("Amplification Factor", getAmplFactor(),
249  "Amplification factor");
250  pl->set<double>("Reduction Factor", getReductFactor(), "Reduction factor");
251  pl->set<double>("Minimum Value Monitoring Function", getMinEta(),
252  "Min value eta");
253  pl->set<double>("Maximum Value Monitoring Function", getMaxEta(),
254  "Max value eta");
255  return pl;
256  }
257 
258  virtual void initialize() const override
259  {
261  getAmplFactor() <= 1.0, std::out_of_range,
262  "Error - Invalid value of Amplification Factor = "
263  << getAmplFactor() << "! \n"
264  << "Amplification Factor must be > 1.0.\n");
265 
266  TEUCHOS_TEST_FOR_EXCEPTION(getReductFactor() >= 1.0, std::out_of_range,
267  "Error - Invalid value of Reduction Factor = "
268  << getReductFactor() << "! \n"
269  << "Reduction Factor must be < 1.0.\n");
270 
272  getMinEta() > getMaxEta(), std::out_of_range,
273  "Error - Invalid values of 'Minimum Value Monitoring Function' = "
274  << getMinEta()
275  << "\n and 'Maximum Value Monitoring Function' = " << getMaxEta()
276  << "! \n Mininum Value cannot be > Maximum Value! \n");
277 
278  this->isInitialized_ = true; // Only place where this is set to true!
279  }
280 
281  virtual Scalar getAmplFactor() const { return rho_; }
282  virtual Scalar getReductFactor() const { return sigma_; }
283  virtual Scalar getMinEta() const { return minEta_; }
284  virtual Scalar getMaxEta() const { return maxEta_; }
285 
286  virtual void setAmplFactor(Scalar rho)
287  {
288  rho_ = rho;
289  this->isInitialized_ = false;
290  }
291  virtual void setReductFactor(Scalar sigma)
292  {
293  sigma_ = sigma;
294  this->isInitialized_ = false;
295  }
296  virtual void setMinEta(Scalar minEta)
297  {
298  minEta_ = minEta;
299  this->isInitialized_ = false;
300  }
301  virtual void setMaxEta(Scalar maxEta)
302  {
303  maxEta_ = maxEta;
304  this->isInitialized_ = false;
305  }
306 
307  private:
308  Scalar rho_;
309  Scalar sigma_;
310  Scalar minEta_;
311  Scalar maxEta_;
312 };
313 
315 template <class Scalar>
319  std::string name = "Basic VS")
320 {
322  if (pList == Teuchos::null || pList->numParams() == 0) return tscs;
323 
325  pList->get<std::string>("Strategy Type") != "Basic VS", std::logic_error,
326  "Error - Strategy Type != 'Basic VS'. (='" +
327  pList->get<std::string>("Strategy Type") + "')\n");
328 
329  pList->validateParametersAndSetDefaults(*tscs->getValidParameters());
330 
331  tscs->setAmplFactor(pList->get<double>("Amplification Factor"));
332  tscs->setReductFactor(pList->get<double>("Reduction Factor"));
333  tscs->setMinEta(pList->get<double>("Minimum Value Monitoring Function"));
334  tscs->setMaxEta(pList->get<double>("Maximum Value Monitoring Function"));
335 
336  tscs->setName(name);
337  tscs->initialize();
338 
339  return tscs;
340 }
341 
343 template <class Scalar>
345 {
347  return Teuchos::rcp_const_cast<Teuchos::ParameterList>(
348  t->getValidParameters());
349 }
350 
351 } // namespace Tempus
352 #endif // Tempus_TimeStepControlStrategy_BasicVS_hpp
Teuchos::RCP< Teuchos::ParameterList > getTimeStepControlStrategyBasicVS_PL()
Nonmember function to return ParameterList with default values.
T & get(const std::string &name, T def_value)
StepControlStrategy class for TimeStepControl.
virtual Scalar getMaxRelError() const
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
virtual Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
Return ParameterList with current values.
Ordinal numParams() const
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
virtual void printDtChanges(int istep, Scalar dt_old, Scalar dt_new, std::string reason) const
Teuchos::RCP< TimeStepControlStrategyBasicVS< Scalar > > createTimeStepControlStrategyBasicVS(const Teuchos::RCP< Teuchos::ParameterList > &pList, std::string name="Basic VS")
Nonmember constructor.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Status
Status for the Integrator, the Stepper and the SolutionState.
virtual void setNextTimeStep(const TimeStepControl< Scalar > &tsc, Teuchos::RCP< SolutionHistory< Scalar > > solutionHistory, Status &) override
Set the time step size.
bool isInitialized_
Bool if strategy is initialized.
virtual Scalar getMaxAbsError() const
TimeStepControl manages the time step size. There several mechanisms that effect the time step size a...
void validateParametersAndSetDefaults(ParameterList const &validParamList, int const depth=1000)
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
virtual Scalar getMaxTimeStep() const
virtual Scalar getMinTimeStep() const
RCP< std::basic_ostream< char_type, traits_type > > getOStream()
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const override
TimeStepControlStrategy class for TimeStepControl.
TimeStepControlStrategyBasicVS(Scalar rho, Scalar sigma, Scalar minEta, Scalar maxEta, std::string name="Basic VS")
Full Constructor.