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: 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_Constant_hpp
11 #define Tempus_TimeStepControlStrategy_Constant_hpp
12 
13 #include "Tempus_config.hpp"
15 #include "Tempus_SolutionState.hpp"
16 #include "Tempus_SolutionHistory.hpp"
17 #include "Tempus_StepperState.hpp"
18 
19 namespace Tempus {
20 
24 template <class Scalar>
26  : virtual public TimeStepControlStrategy<Scalar> {
27  public:
30  {
31  this->setStrategyType("Constant");
32  this->setStepType("Constant");
33  this->setName("Constant");
34  this->initialize();
35  }
36 
38  TimeStepControlStrategyConstant(Scalar constantTimeStep,
39  std::string name = "Constant")
40  : constantTimeStep_(constantTimeStep)
41  {
42  this->setStrategyType("Constant");
43  this->setStepType("Constant");
44  this->setName(name);
45  this->initialize();
46  }
47 
50 
52  virtual void setNextTimeStep(
53  const TimeStepControl<Scalar> &tsc,
54  Teuchos::RCP<SolutionHistory<Scalar> > solutionHistory,
55  Status &integratorStatus) override
56  {
57  using Teuchos::RCP;
58 
59  this->checkInitialized();
60 
61  RCP<SolutionState<Scalar> > workingState =
62  solutionHistory->getWorkingState();
63  const Scalar errorAbs = workingState->getErrorAbs();
64  const Scalar errorRel = workingState->getErrorRel();
65  Scalar dt = workingState->getTimeStep();
66 
67  RCP<Teuchos::FancyOStream> out = tsc.getOStream();
68  Teuchos::OSTab ostab(out, 1, "setNextTimeStep");
69  out->setOutputToRootOnly(0);
70 
71  // Check constant time step
72  if (dt != tsc.getInitTimeStep()) {
73  tsc.printDtChanges(workingState->getIndex(), dt, tsc.getInitTimeStep(),
74  "Resetting constant dt.");
75  dt = tsc.getInitTimeStep();
76  }
77 
78  // Stepper failure
79  if (workingState->getSolutionStatus() == Status::FAILED) {
80  *out << "Failure - Stepper failed and can not change time step size!\n"
81  << " Time step type == CONSTANT_STEP_SIZE\n"
82  << std::endl;
83  integratorStatus = FAILED;
84  return;
85  }
86 
87  // Absolute error failure
88  if (errorAbs > tsc.getMaxAbsError()) {
89  *out << "Failure - Absolute error failed and can not change time step!\n"
90  << " Time step type == CONSTANT_STEP_SIZE\n"
91  << " (errorAbs =" << errorAbs
92  << ") > (errorMaxAbs =" << tsc.getMaxAbsError() << ")" << std::endl;
93  integratorStatus = FAILED;
94  return;
95  }
96 
97  // Relative error failure
98  if (errorRel > tsc.getMaxRelError()) {
99  *out << "Failure - Relative error failed and can not change time step!\n"
100  << " Time step type == CONSTANT_STEP_SIZE\n"
101  << " (errorRel =" << errorRel
102  << ") > (errorMaxRel =" << tsc.getMaxRelError() << ")" << std::endl;
103  integratorStatus = FAILED;
104  return;
105  }
106 
107  // update dt
108  workingState->setTimeStep(dt);
109 
110  // Set time from initial time, dt, and index to avoid numerical roundoff.
111  const Scalar initTime = tsc.getInitTime();
112  const int initIndex = tsc.getInitIndex();
113  const int index = workingState->getIndex();
114  const Scalar time = (index - initIndex) * dt + initTime;
115  workingState->setTime(time);
116  }
117 
119 
120  std::string description() const override
121  {
122  return "Tempus::TimeStepControlStrategyConstant";
123  }
124 
126  const Teuchos::EVerbosityLevel verbLevel) const override
127  {
128  auto l_out = Teuchos::fancyOStream(out.getOStream());
129  Teuchos::OSTab ostab(*l_out, 2, this->description());
130  l_out->setOutputToRootOnly(0);
131 
132  *l_out << "\n--- " << this->description() << " ---" << std::endl;
133 
134  if (Teuchos::as<int>(verbLevel) >= Teuchos::as<int>(Teuchos::VERB_MEDIUM)) {
135  *l_out << " Strategy Type = " << this->getStrategyType() << std::endl
136  << " Step Type = " << this->getStepType() << std::endl
137  << " Time Step = " << getConstantTimeStep() << std::endl;
138 
139  *l_out << std::string(this->description().length() + 8, '-') << std::endl;
140  }
141  }
143 
146  const override
147  {
149  Teuchos::parameterList("Time Step Control Strategy");
150 
151  pl->set<std::string>("Strategy Type", this->getStrategyType(), "Constant");
152  pl->set<double>("Time Step", getConstantTimeStep());
153 
154  return pl;
155  }
156 
157  virtual void initialize() const override
158  {
159  this->isInitialized_ = true; // Only place where this is set to true!
160  }
161 
162  virtual Scalar getConstantTimeStep() const { return constantTimeStep_; }
163 
164  virtual void setConstantTimeStep(Scalar dt)
165  {
166  constantTimeStep_ = dt;
167  this->isInitialized_ = false;
168  }
169 
170  private:
172 };
173 
175 template <class Scalar>
179  std::string name = "Constant")
180 {
182  if (pList == Teuchos::null || pList->numParams() == 0) return tscs;
183 
185  pList->get<std::string>("Strategy Type") != "Constant", std::logic_error,
186  "Error - Strategy Type != 'Constant'. (='" +
187  pList->get<std::string>("Strategy Type") + "')\n");
188 
189  pList->validateParametersAndSetDefaults(*tscs->getValidParameters());
190 
191  tscs->setConstantTimeStep(pList->get<double>("Time Step"));
192 
193  tscs->setName(name);
194  tscs->initialize();
195 
196  return tscs;
197 }
198 
200 template <class Scalar>
202 {
204  return Teuchos::rcp_const_cast<Teuchos::ParameterList>(
205  t->getValidParameters());
206 }
207 
208 } // namespace Tempus
209 #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)
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
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
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