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: 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_BasicVS_hpp
10 #define Tempus_TimeStepControlStrategy_BasicVS_hpp
11 
12 #include "Thyra_VectorStdOps.hpp"
13 
14 #include "Tempus_config.hpp"
16 #include "Tempus_SolutionState.hpp"
17 #include "Tempus_SolutionHistory.hpp"
18 #include "Tempus_StepperState.hpp"
19 
20 namespace Tempus {
21 
102 template <class Scalar>
104  : virtual public TimeStepControlStrategy<Scalar> {
105  public:
108  : rho_(1.75), sigma_(0.5), minEta_(0.0), maxEta_(1.0e+16)
109  {
110  this->setStrategyType("Basic VS");
111  this->setStepType("Variable");
112  this->setName("Basic VS");
113  this->initialize();
114  }
115 
117  TimeStepControlStrategyBasicVS(Scalar rho, Scalar sigma, Scalar minEta,
118  Scalar maxEta, std::string name = "Basic VS")
119  : rho_(rho), sigma_(sigma), minEta_(minEta), maxEta_(maxEta)
120  {
121  this->setStrategyType("Basic VS");
122  this->setStepType("Variable");
123  this->setName(name);
124  this->initialize();
125  }
126 
129 
131  virtual void setNextTimeStep(
132  const TimeStepControl<Scalar> &tsc,
133  Teuchos::RCP<SolutionHistory<Scalar> > solutionHistory,
134  Status & /* integratorStatus */) override
135  {
136  using Teuchos::RCP;
137 
138  this->checkInitialized();
139 
140  RCP<SolutionState<Scalar> > workingState =
141  solutionHistory->getWorkingState();
142  const Scalar errorAbs = workingState->getErrorAbs();
143  const Scalar errorRel = workingState->getErrorRel();
144  const int iStep = workingState->getIndex();
145  Scalar dt = workingState->getTimeStep();
146 
147  Scalar rho = getAmplFactor();
148  Scalar sigma = getReductFactor();
149  Scalar eta = solutionHistory->getCurrentState()->getDxNormL2Rel();
150  if (iStep == 1) eta = getMinEta(); // For first step use initial dt.
151 
152  // General rule: only increase/decrease dt once for any given reason.
153  if (workingState->getSolutionStatus() == Status::FAILED) {
154  tsc.printDtChanges(iStep, dt, dt * sigma,
155  "Stepper failure - Decreasing dt.");
156  dt *= sigma;
157  }
158  else { // Stepper passed
159  if (eta < getMinEta()) { // increase dt
160  tsc.printDtChanges(iStep, dt, dt * rho,
161  "Change too small (" + std::to_string(eta) + " < " +
162  std::to_string(getMinEta()) +
163  "). Increasing dt.");
164  dt *= rho;
165  }
166  else if (eta > getMaxEta()) { // reduce dt
167  tsc.printDtChanges(iStep, dt, dt * sigma,
168  "Change too large (" + std::to_string(eta) + " > " +
169  std::to_string(getMaxEta()) +
170  "). Decreasing dt.");
171  dt *= sigma;
172  }
173  else if (errorAbs > tsc.getMaxAbsError()) { // reduce dt
174  tsc.printDtChanges(
175  iStep, dt, dt * sigma,
176  "Absolute error is too large (" + std::to_string(errorAbs) + " > " +
177  std::to_string(tsc.getMaxAbsError()) + "). Decreasing dt.");
178  dt *= sigma;
179  }
180  else if (errorRel > tsc.getMaxRelError()) { // reduce dt
181  tsc.printDtChanges(
182  iStep, dt, dt * sigma,
183  "Relative error is too large (" + std::to_string(errorRel) + " > " +
184  std::to_string(tsc.getMaxRelError()) + "). Decreasing dt.");
185  dt *= sigma;
186  }
187  }
188 
189  if (dt < tsc.getMinTimeStep()) { // decreased below minimum dt
190  tsc.printDtChanges(iStep, dt, tsc.getMinTimeStep(),
191  "dt is too small. Resetting to minimum dt.");
192  dt = tsc.getMinTimeStep();
193  }
194  if (dt > tsc.getMaxTimeStep()) { // increased above maximum dt
195  tsc.printDtChanges(iStep, dt, tsc.getMaxTimeStep(),
196  "dt is too large. Resetting to maximum dt.");
197  dt = tsc.getMaxTimeStep();
198  }
199 
200  workingState->setTimeStep(dt);
201  workingState->setTime(solutionHistory->getCurrentState()->getTime() + dt);
202  workingState->setComputeNorms(true);
203  }
204 
206 
207  std::string description() const override
208  {
209  return "Tempus::TimeStepControlStrategyBasicVS";
210  }
211 
213  const Teuchos::EVerbosityLevel verbLevel) const override
214  {
215  auto l_out = Teuchos::fancyOStream(out.getOStream());
216  Teuchos::OSTab ostab(*l_out, 2, this->description());
217  l_out->setOutputToRootOnly(0);
218 
219  *l_out << "\n--- " << this->description() << " ---" << std::endl;
220 
221  if (Teuchos::as<int>(verbLevel) >= Teuchos::as<int>(Teuchos::VERB_MEDIUM)) {
222  *l_out << " StrategyType = "
223  << this->getStrategyType() << std::endl
224  << " Step Type = " << this->getStepType()
225  << std::endl
226  << " Amplification Factor = " << getAmplFactor()
227  << std::endl
228  << " Reduction Factor = " << getReductFactor()
229  << std::endl
230  << " Minimum Value Monitoring Function = " << getMinEta()
231  << std::endl
232  << " Maximum Value Monitoring Function = " << getMaxEta()
233  << std::endl;
234  *l_out << std::string(this->description().length() + 8, '-') << std::endl;
235  }
236  }
238 
241  const override
242  {
244  Teuchos::parameterList("Time Step Control Strategy");
245 
246  pl->set<std::string>("Strategy Type", this->getStrategyType(), "Basic VS");
247  pl->set<double>("Amplification Factor", getAmplFactor(),
248  "Amplification factor");
249  pl->set<double>("Reduction Factor", getReductFactor(), "Reduction factor");
250  pl->set<double>("Minimum Value Monitoring Function", getMinEta(),
251  "Min value eta");
252  pl->set<double>("Maximum Value Monitoring Function", getMaxEta(),
253  "Max value eta");
254  return pl;
255  }
256 
257  virtual void initialize() const override
258  {
260  getAmplFactor() <= 1.0, std::out_of_range,
261  "Error - Invalid value of Amplification Factor = "
262  << getAmplFactor() << "! \n"
263  << "Amplification Factor must be > 1.0.\n");
264 
265  TEUCHOS_TEST_FOR_EXCEPTION(getReductFactor() >= 1.0, std::out_of_range,
266  "Error - Invalid value of Reduction Factor = "
267  << getReductFactor() << "! \n"
268  << "Reduction Factor must be < 1.0.\n");
269 
271  getMinEta() > getMaxEta(), std::out_of_range,
272  "Error - Invalid values of 'Minimum Value Monitoring Function' = "
273  << getMinEta()
274  << "\n and 'Maximum Value Monitoring Function' = " << getMaxEta()
275  << "! \n Mininum Value cannot be > Maximum Value! \n");
276 
277  this->isInitialized_ = true; // Only place where this is set to true!
278  }
279 
280  virtual Scalar getAmplFactor() const { return rho_; }
281  virtual Scalar getReductFactor() const { return sigma_; }
282  virtual Scalar getMinEta() const { return minEta_; }
283  virtual Scalar getMaxEta() const { return maxEta_; }
284 
285  virtual void setAmplFactor(Scalar rho)
286  {
287  rho_ = rho;
288  this->isInitialized_ = false;
289  }
290  virtual void setReductFactor(Scalar sigma)
291  {
292  sigma_ = sigma;
293  this->isInitialized_ = false;
294  }
295  virtual void setMinEta(Scalar minEta)
296  {
297  minEta_ = minEta;
298  this->isInitialized_ = false;
299  }
300  virtual void setMaxEta(Scalar maxEta)
301  {
302  maxEta_ = maxEta;
303  this->isInitialized_ = false;
304  }
305 
306  private:
307  Scalar rho_;
308  Scalar sigma_;
309  Scalar minEta_;
310  Scalar maxEta_;
311 };
312 
314 template <class Scalar>
318  std::string name = "Basic VS")
319 {
321  if (pList == Teuchos::null || pList->numParams() == 0) return tscs;
322 
324  pList->get<std::string>("Strategy Type") != "Basic VS", std::logic_error,
325  "Error - Strategy Type != 'Basic VS'. (='" +
326  pList->get<std::string>("Strategy Type") + "')\n");
327 
328  pList->validateParametersAndSetDefaults(*tscs->getValidParameters());
329 
330  tscs->setAmplFactor(pList->get<double>("Amplification Factor"));
331  tscs->setReductFactor(pList->get<double>("Reduction Factor"));
332  tscs->setMinEta(pList->get<double>("Minimum Value Monitoring Function"));
333  tscs->setMaxEta(pList->get<double>("Maximum Value Monitoring Function"));
334 
335  tscs->setName(name);
336  tscs->initialize();
337 
338  return tscs;
339 }
340 
342 template <class Scalar>
344 {
346  return Teuchos::rcp_const_cast<Teuchos::ParameterList>(
347  t->getValidParameters());
348 }
349 
350 } // namespace Tempus
351 #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.
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
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
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.