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  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 = " << this->getStrategyType()<< std::endl
223  << " Step Type = " << this->getStepType() << std::endl
224  << " Amplification Factor = " << getAmplFactor() << std::endl
225  << " Reduction Factor = " << getReductFactor() << std::endl
226  << " Minimum Value Monitoring Function = " << getMinEta() << std::endl
227  << " Maximum Value Monitoring Function = " << getMaxEta() << std::endl;
228  *l_out << std::string(this->description().length()+8, '-') <<std::endl;
229  }
230  }
232 
235  {
237  Teuchos::parameterList("Time Step Control Strategy");
238 
239  pl->set<std::string>("Strategy Type", this->getStrategyType(), "Basic VS");
240  pl->set<double>("Amplification Factor", getAmplFactor(), "Amplification factor");
241  pl->set<double>("Reduction Factor" , getReductFactor() , "Reduction factor");
242  pl->set<double>("Minimum Value Monitoring Function", getMinEta(), "Min value eta");
243  pl->set<double>("Maximum Value Monitoring Function", getMaxEta(), "Max value eta");
244  return pl;
245  }
246 
247 
248  virtual void initialize() const override
249  {
250  TEUCHOS_TEST_FOR_EXCEPTION(getAmplFactor() <= 1.0, std::out_of_range,
251  "Error - Invalid value of Amplification Factor = " << getAmplFactor()
252  << "! \n" << "Amplification Factor must be > 1.0.\n");
253 
254  TEUCHOS_TEST_FOR_EXCEPTION(getReductFactor() >= 1.0, std::out_of_range,
255  "Error - Invalid value of Reduction Factor = " << getReductFactor()
256  << "! \n" << "Reduction Factor must be < 1.0.\n");
257 
258  TEUCHOS_TEST_FOR_EXCEPTION(getMinEta() > getMaxEta(), std::out_of_range,
259  "Error - Invalid values of 'Minimum Value Monitoring Function' = "
260  << getMinEta() << "\n and 'Maximum Value Monitoring Function' = "
261  << getMaxEta() <<"! \n Mininum Value cannot be > Maximum Value! \n");
262 
263  this->isInitialized_ = true; // Only place where this is set to true!
264  }
265 
266 
267  virtual Scalar getAmplFactor() const { return rho_; }
268  virtual Scalar getReductFactor() const { return sigma_;}
269  virtual Scalar getMinEta() const { return minEta_; }
270  virtual Scalar getMaxEta() const { return maxEta_; }
271 
272  virtual void setAmplFactor(Scalar rho) { rho_ = rho; this->isInitialized_ = false; }
273  virtual void setReductFactor(Scalar sigma) { sigma_ = sigma; this->isInitialized_ = false; }
274  virtual void setMinEta(Scalar minEta) { minEta_ = minEta; this->isInitialized_ = false; }
275  virtual void setMaxEta(Scalar maxEta) { maxEta_ = maxEta; this->isInitialized_ = false; }
276 
277 
278 private:
279 
280  Scalar rho_;
281  Scalar sigma_;
282  Scalar minEta_;
283  Scalar maxEta_;
284 
285 };
286 
287 
289 template <class Scalar>
293  std::string name = "Basic VS")
294 {
296  if (pList == Teuchos::null || pList->numParams() == 0) return tscs;
297 
299  pList->get<std::string>("Strategy Type") != "Basic VS", std::logic_error,
300  "Error - Strategy Type != 'Basic VS'. (='"
301  +pList->get<std::string>("Strategy Type")+"')\n");
302 
303  pList->validateParametersAndSetDefaults(*tscs->getValidParameters());
304 
305  tscs->setAmplFactor( pList->get<double>("Amplification Factor"));
306  tscs->setReductFactor(pList->get<double>("Reduction Factor"));
307  tscs->setMinEta(pList->get<double>("Minimum Value Monitoring Function"));
308  tscs->setMaxEta(pList->get<double>("Maximum Value Monitoring Function"));
309 
310  tscs->setName(name);
311  tscs->initialize();
312 
313  return tscs;
314 }
315 
316 
318 template<class Scalar>
320 {
322  return Teuchos::rcp_const_cast<Teuchos::ParameterList> (t->getValidParameters());
323 }
324 
325 
326 } // namespace Tempus
327 #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.