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 
21 namespace Tempus {
22 
103 template<class Scalar>
105  : virtual public TimeStepControlStrategy<Scalar>
106 {
107 public:
108 
111  : rho_(1.75), sigma_(0.5), minEta_(0.0), maxEta_(1.0e+16)
112  {
113  this->setStrategyType("Basic VS");
114  this->setStepType("Variable");
115  this->setName("Basic VS");
116  this->initialize();
117  }
118 
120  TimeStepControlStrategyBasicVS(Scalar rho, Scalar sigma, Scalar minEta,
121  Scalar maxEta, std::string name = "Basic VS")
122  : rho_(rho), sigma_(sigma), minEta_(minEta), maxEta_(maxEta)
123  {
124  this->setStrategyType("Basic VS");
125  this->setStepType("Variable");
126  this->setName(name);
127  this->initialize();
128  }
129 
132 
134  virtual void setNextTimeStep(
135  const TimeStepControl<Scalar> & tsc,
136  Teuchos::RCP<SolutionHistory<Scalar> > solutionHistory,
137  Status & /* integratorStatus */) override
138  {
139  using Teuchos::RCP;
140 
141  this->checkInitialized();
142 
143  RCP<SolutionState<Scalar> > workingState=solutionHistory->getWorkingState();
144  const Scalar errorAbs = workingState->getErrorAbs();
145  const Scalar errorRel = workingState->getErrorRel();
146  const int iStep = workingState->getIndex();
147  Scalar dt = workingState->getTimeStep();
148 
149  Scalar rho = getAmplFactor();
150  Scalar sigma = getReductFactor();
151  Scalar eta = solutionHistory->getCurrentState()->getDxNormL2Rel();
152  if (iStep == 1) eta = getMinEta(); // For first step use initial dt.
153 
154  // General rule: only increase/decrease dt once for any given reason.
155  if (workingState->getSolutionStatus() == Status::FAILED) {
156  tsc.printDtChanges(iStep, dt, dt*sigma,
157  "Stepper failure - Decreasing dt.");
158  dt *= sigma;
159  }
160  else { //Stepper passed
161  if (eta < getMinEta()) { // increase dt
162  tsc.printDtChanges(iStep, dt, dt*rho,
163  "Change too small ("
164  + std::to_string(eta) + " < " + std::to_string(getMinEta())
165  + "). Increasing dt.");
166  dt *= rho;
167  }
168  else if (eta > getMaxEta()) { // reduce dt
169  tsc.printDtChanges(iStep, dt, dt*sigma,
170  "Change too large ("
171  + std::to_string(eta) + " > " + std::to_string(getMaxEta())
172  + "). Decreasing dt.");
173  dt *= sigma;
174  }
175  else if (errorAbs > tsc.getMaxAbsError()) { // reduce dt
176  tsc.printDtChanges(iStep, dt, dt*sigma,
177  "Absolute error is too large ("
178  + std::to_string(errorAbs)+" > "+std::to_string(tsc.getMaxAbsError())
179  + "). Decreasing dt.");
180  dt *= sigma;
181  }
182  else if (errorRel > tsc.getMaxRelError()) { // reduce dt
183  tsc.printDtChanges(iStep, dt, dt*sigma,
184  "Relative error is too large ("
185  + std::to_string(errorRel)+" > "+std::to_string(tsc.getMaxRelError())
186  + "). Decreasing dt.");
187  dt *= sigma;
188  }
189  }
190 
191  if (dt < tsc.getMinTimeStep()) { // decreased below minimum dt
192  tsc.printDtChanges(iStep, dt, tsc.getMinTimeStep(),
193  "dt is too small. Resetting to minimum dt.");
194  dt = tsc.getMinTimeStep();
195  }
196  if (dt > tsc.getMaxTimeStep()) { // increased above maximum dt
197  tsc.printDtChanges(iStep, dt, tsc.getMaxTimeStep(),
198  "dt is too large. Resetting to maximum dt.");
199  dt = tsc.getMaxTimeStep();
200  }
201 
202  workingState->setTimeStep(dt);
203  workingState->setTime(solutionHistory->getCurrentState()->getTime() + dt);
204  workingState->setComputeNorms(true);
205  }
206 
208 
209  std::string description() const override
210  { return "Tempus::TimeStepControlStrategyBasicVS"; }
211 
213  const Teuchos::EVerbosityLevel verbLevel) const override
214  {
215  Teuchos::OSTab ostab(out,2,"describe");
216  out << description() << "::describe:" << std::endl
217  << "StrategyType = " << this->getStrategyType()<< std::endl
218  << "Amplification Factor = " << getAmplFactor() << std::endl
219  << "Reduction Factor = " << getReductFactor() << std::endl
220  << "Minimum Value Monitoring Function = " << getMinEta() << std::endl
221  << "Maximum Value Monitoring Function = " << getMaxEta() << std::endl;
222  }
224 
227  {
229  Teuchos::parameterList("Time Step Control Strategy");
230 
231  pl->set<std::string>("Strategy Type", this->getStrategyType(), "Basic VS");
232  pl->set<double>("Amplification Factor", getAmplFactor(), "Amplification factor");
233  pl->set<double>("Reduction Factor" , getReductFactor() , "Reduction factor");
234  pl->set<double>("Minimum Value Monitoring Function", getMinEta(), "Min value eta");
235  pl->set<double>("Maximum Value Monitoring Function", getMaxEta(), "Max value eta");
236  return pl;
237  }
238 
239 
240  virtual void initialize() const override
241  {
242  TEUCHOS_TEST_FOR_EXCEPTION(getAmplFactor() <= 1.0, std::out_of_range,
243  "Error - Invalid value of Amplification Factor = " << getAmplFactor()
244  << "! \n" << "Amplification Factor must be > 1.0.\n");
245 
246  TEUCHOS_TEST_FOR_EXCEPTION(getReductFactor() >= 1.0, std::out_of_range,
247  "Error - Invalid value of Reduction Factor = " << getReductFactor()
248  << "! \n" << "Reduction Factor must be < 1.0.\n");
249 
250  TEUCHOS_TEST_FOR_EXCEPTION(getMinEta() > getMaxEta(), std::out_of_range,
251  "Error - Invalid values of 'Minimum Value Monitoring Function' = "
252  << getMinEta() << "\n and 'Maximum Value Monitoring Function' = "
253  << getMaxEta() <<"! \n Mininum Value cannot be > Maximum Value! \n");
254 
255  this->isInitialized_ = true; // Only place where this is set to true!
256  }
257 
258 
259  virtual Scalar getAmplFactor() const { return rho_; }
260  virtual Scalar getReductFactor() const { return sigma_;}
261  virtual Scalar getMinEta() const { return minEta_; }
262  virtual Scalar getMaxEta() const { return maxEta_; }
263 
264  virtual void setAmplFactor(Scalar rho) { rho_ = rho; this->isInitialized_ = false; }
265  virtual void setReductFactor(Scalar sigma) { sigma_ = sigma; this->isInitialized_ = false; }
266  virtual void setMinEta(Scalar minEta) { minEta_ = minEta; this->isInitialized_ = false; }
267  virtual void setMaxEta(Scalar maxEta) { maxEta_ = maxEta; this->isInitialized_ = false; }
268 
269 
270 private:
271 
272  Scalar rho_;
273  Scalar sigma_;
274  Scalar minEta_;
275  Scalar maxEta_;
276 
277 };
278 
279 
281 template <class Scalar>
285  std::string name = "Basic VS")
286 {
288  if (pList == Teuchos::null) return tscs;
289 
291  pList->get<std::string>("Strategy Type", "Basic VS") != "Basic VS",
292  std::logic_error,
293  "Error - Strategy Type != 'Basic VS'. (='"
294  +pList->get<std::string>("Strategy Type")+"')\n");
295 
296  pList->validateParametersAndSetDefaults(*tscs->getValidParameters(), 0);
297 
298  tscs->setAmplFactor( pList->get<double>("Amplification Factor"));
299  tscs->setReductFactor(pList->get<double>("Reduction Factor"));
300  tscs->setMinEta(pList->get<double>("Minimum Value Monitoring Function"));
301  tscs->setMaxEta(pList->get<double>("Maximum Value Monitoring Function"));
302 
303  tscs->setName(name);
304  tscs->initialize();
305 
306  return tscs;
307 }
308 
309 
311 template<class Scalar>
313 {
315  return Teuchos::rcp_const_cast<Teuchos::ParameterList> (t->getValidParameters());
316 }
317 
318 
319 } // namespace Tempus
320 #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.
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
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.