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 
19 namespace Tempus {
20 
24 template<class Scalar>
26  : virtual public TimeStepControlStrategy<Scalar>
27 {
28 public:
29 
32  : constantTimeStep_(0.0)
33  {
34  this->setStrategyType("Constant");
35  this->setStepType("Constant");
36  this->setName("Constant");
37  this->initialize();
38  }
39 
41  TimeStepControlStrategyConstant(Scalar constantTimeStep,
42  std::string name = "Constant")
43  : constantTimeStep_(constantTimeStep)
44  {
45  this->setStrategyType("Constant");
46  this->setStepType("Constant");
47  this->setName(name);
48  this->initialize();
49  }
50 
53 
55  virtual void setNextTimeStep(const TimeStepControl<Scalar> & tsc,
56  Teuchos::RCP<SolutionHistory<Scalar> > solutionHistory,
57  Status & integratorStatus) override
58  {
59  using Teuchos::RCP;
60 
61  this->checkInitialized();
62 
63  RCP<SolutionState<Scalar> >workingState=solutionHistory->getWorkingState();
64  const Scalar errorAbs = workingState->getErrorAbs();
65  const Scalar errorRel = workingState->getErrorRel();
66  Scalar dt = workingState->getTimeStep();
67 
68  RCP<Teuchos::FancyOStream> out = tsc.getOStream();
69  Teuchos::OSTab ostab(out,1,"setNextTimeStep");
70 
71 
72  // Check constant time step
73  if ( dt != tsc.getInitTimeStep() ) {
74  tsc.printDtChanges(workingState->getIndex(), dt, tsc.getInitTimeStep(),
75  "Resetting constant dt.");
76  dt = tsc.getInitTimeStep();
77  }
78 
79  // Stepper failure
80  if (workingState->getSolutionStatus() == Status::FAILED) {
81  *out << "Failure - Stepper failed and can not change time step size!\n"
82  << " Time step type == CONSTANT_STEP_SIZE\n" << 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<<") > (errorMaxAbs ="
92  << 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<<") > (errorMaxRel ="
102  << 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 
118 
120 
121  std::string description() const override
122  { return "Tempus::TimeStepControlStrategyConstant"; }
123 
125  const Teuchos::EVerbosityLevel verbLevel) const override
126  {
127  Teuchos::OSTab ostab(out,2,"describe");
128  out << description() << std::endl
129  << "Strategy Type = " << this->getStrategyType() << std::endl
130  << "Step Type = " << this->getStepType() << std::endl
131  << "Time Step = " << getConstantTimeStep() << std::endl;
132  }
134 
137  {
139  Teuchos::parameterList("Time Step Control Strategy");
140 
141  pl->set<std::string>("Strategy Type", this->getStrategyType(), "Constant");
142  pl->set<double>("Time Step", getConstantTimeStep());
143 
144  return pl;
145  }
146 
147 
148  virtual void initialize() const override
149  {
150  this->isInitialized_ = true; // Only place where this is set to true!
151  }
152 
153  virtual Scalar getConstantTimeStep() const { return constantTimeStep_; }
154 
155  virtual void setConstantTimeStep(Scalar dt)
156  { constantTimeStep_ = dt; this->isInitialized_ = false; }
157 
158 
159 private:
160 
162 
163 };
164 
165 
167 template <class Scalar>
171  std::string name = "Constant")
172 {
174  if (pList == Teuchos::null) return tscs;
175 
177  pList->get<std::string>("Strategy Type", "Constant") != "Constant",
178  std::logic_error,
179  "Error - Strategy Type != 'Constant'. (='"
180  +pList->get<std::string>("Strategy Type")+"')\n");
181 
182  pList->validateParametersAndSetDefaults(*tscs->getValidParameters(), 0);
183 
184  tscs->setConstantTimeStep(pList->get<double>("Time Step"));
185 
186  tscs->setName(name);
187  tscs->initialize();
188 
189  return tscs;
190 }
191 
192 
194 template<class Scalar>
196 {
198  return Teuchos::rcp_const_cast<Teuchos::ParameterList> (t->getValidParameters());
199 }
200 
201 
202 } // namespace Tempus
203 #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)
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.
TimeStepControlStrategy class for TimeStepControl.
virtual RCP< FancyOStream > getOStream() const