Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_TimeStepControlStrategyConstant.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_Constant_hpp
10 #define Tempus_TimeStepControlStrategy_Constant_hpp
11 
12 #include "Tempus_config.hpp"
14 #include "Tempus_SolutionState.hpp"
15 #include "Tempus_SolutionHistory.hpp"
16 #include "Tempus_StepperState.hpp"
17 
18 namespace Tempus {
19 
23 template <class Scalar>
25  : virtual public TimeStepControlStrategy<Scalar> {
26  public:
29  {
30  this->setStrategyType("Constant");
31  this->setStepType("Constant");
32  this->setName("Constant");
33  this->initialize();
34  }
35 
37  TimeStepControlStrategyConstant(Scalar constantTimeStep,
38  std::string name = "Constant")
39  : constantTimeStep_(constantTimeStep)
40  {
41  this->setStrategyType("Constant");
42  this->setStepType("Constant");
43  this->setName(name);
44  this->initialize();
45  }
46 
49 
51  virtual void setNextTimeStep(
52  const TimeStepControl<Scalar> &tsc,
53  Teuchos::RCP<SolutionHistory<Scalar> > solutionHistory,
54  Status &integratorStatus) override
55  {
56  using Teuchos::RCP;
57 
58  this->checkInitialized();
59 
60  RCP<SolutionState<Scalar> > workingState =
61  solutionHistory->getWorkingState();
62  const Scalar errorAbs = workingState->getErrorAbs();
63  const Scalar errorRel = workingState->getErrorRel();
64  Scalar dt = workingState->getTimeStep();
65 
66  RCP<Teuchos::FancyOStream> out = tsc.getOStream();
67  Teuchos::OSTab ostab(out, 1, "setNextTimeStep");
68  out->setOutputToRootOnly(0);
69 
70  // Check constant time step
71  if (dt != tsc.getInitTimeStep()) {
72  tsc.printDtChanges(workingState->getIndex(), dt, tsc.getInitTimeStep(),
73  "Resetting constant dt.");
74  dt = tsc.getInitTimeStep();
75  }
76 
77  // Stepper failure
78  if (workingState->getSolutionStatus() == Status::FAILED) {
79  *out << "Failure - Stepper failed and can not change time step size!\n"
80  << " Time step type == CONSTANT_STEP_SIZE\n"
81  << std::endl;
82  integratorStatus = FAILED;
83  return;
84  }
85 
86  // Absolute error failure
87  if (errorAbs > tsc.getMaxAbsError()) {
88  *out << "Failure - Absolute error failed and can not change time step!\n"
89  << " Time step type == CONSTANT_STEP_SIZE\n"
90  << " (errorAbs =" << errorAbs
91  << ") > (errorMaxAbs =" << tsc.getMaxAbsError() << ")" << std::endl;
92  integratorStatus = FAILED;
93  return;
94  }
95 
96  // Relative error failure
97  if (errorRel > tsc.getMaxRelError()) {
98  *out << "Failure - Relative error failed and can not change time step!\n"
99  << " Time step type == CONSTANT_STEP_SIZE\n"
100  << " (errorRel =" << errorRel
101  << ") > (errorMaxRel =" << tsc.getMaxRelError() << ")" << std::endl;
102  integratorStatus = FAILED;
103  return;
104  }
105 
106  // update dt
107  workingState->setTimeStep(dt);
108 
109  // Set time from initial time, dt, and index to avoid numerical roundoff.
110  const Scalar initTime = tsc.getInitTime();
111  const int initIndex = tsc.getInitIndex();
112  const int index = workingState->getIndex();
113  const Scalar time = (index - initIndex) * dt + initTime;
114  workingState->setTime(time);
115  }
116 
118 
119  std::string description() const override
120  {
121  return "Tempus::TimeStepControlStrategyConstant";
122  }
123 
125  const Teuchos::EVerbosityLevel verbLevel) const override
126  {
127  auto l_out = Teuchos::fancyOStream(out.getOStream());
128  Teuchos::OSTab ostab(*l_out, 2, this->description());
129  l_out->setOutputToRootOnly(0);
130 
131  *l_out << "\n--- " << this->description() << " ---" << std::endl;
132 
133  if (Teuchos::as<int>(verbLevel) >= Teuchos::as<int>(Teuchos::VERB_MEDIUM)) {
134  *l_out << " Strategy Type = " << this->getStrategyType() << std::endl
135  << " Step Type = " << this->getStepType() << std::endl
136  << " Time Step = " << getConstantTimeStep() << std::endl;
137 
138  *l_out << std::string(this->description().length() + 8, '-') << std::endl;
139  }
140  }
142 
145  const override
146  {
148  Teuchos::parameterList("Time Step Control Strategy");
149 
150  pl->set<std::string>("Strategy Type", this->getStrategyType(), "Constant");
151  pl->set<double>("Time Step", getConstantTimeStep());
152 
153  return pl;
154  }
155 
156  virtual void initialize() const override
157  {
158  this->isInitialized_ = true; // Only place where this is set to true!
159  }
160 
161  virtual Scalar getConstantTimeStep() const { return constantTimeStep_; }
162 
163  virtual void setConstantTimeStep(Scalar dt)
164  {
165  constantTimeStep_ = dt;
166  this->isInitialized_ = false;
167  }
168 
169  private:
171 };
172 
174 template <class Scalar>
178  std::string name = "Constant")
179 {
181  if (pList == Teuchos::null || pList->numParams() == 0) return tscs;
182 
184  pList->get<std::string>("Strategy Type") != "Constant", std::logic_error,
185  "Error - Strategy Type != 'Constant'. (='" +
186  pList->get<std::string>("Strategy Type") + "')\n");
187 
188  pList->validateParametersAndSetDefaults(*tscs->getValidParameters());
189 
190  tscs->setConstantTimeStep(pList->get<double>("Time Step"));
191 
192  tscs->setName(name);
193  tscs->initialize();
194 
195  return tscs;
196 }
197 
199 template <class Scalar>
201 {
203  return Teuchos::rcp_const_cast<Teuchos::ParameterList>(
204  t->getValidParameters());
205 }
206 
207 } // namespace Tempus
208 #endif // Tempus_TimeStepControlStrategy_Constant_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)
virtual Scalar getMaxRelError() const
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Ordinal numParams() const
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const override
TimeStepControlStrategyConstant(Scalar constantTimeStep, std::string name="Constant")
Full Constructor.
virtual Scalar getInitTimeStep() const
virtual void printDtChanges(int istep, Scalar dt_old, Scalar dt_new, std::string reason) const
virtual void setNextTimeStep(const TimeStepControl< Scalar > &tsc, Teuchos::RCP< SolutionHistory< Scalar > > solutionHistory, Status &integratorStatus) override
Determine the time step size.
Teuchos::RCP< TimeStepControlStrategyConstant< Scalar > > createTimeStepControlStrategyConstant(const Teuchos::RCP< Teuchos::ParameterList > &pList, std::string name="Constant")
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.
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)
Teuchos::RCP< Teuchos::ParameterList > getTimeStepControlStrategyConstantPL()
Nonmember function to return ParameterList with default values.
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
StepControlStrategy class for TimeStepControl.
RCP< std::basic_ostream< char_type, traits_type > > getOStream()
TimeStepControlStrategy class for TimeStepControl.
virtual Scalar getInitTime() const
virtual RCP< FancyOStream > getOStream() const