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  out->setOutputToRootOnly(0);
71 
72 
73  // Check constant time step
74  if ( dt != tsc.getInitTimeStep() ) {
75  tsc.printDtChanges(workingState->getIndex(), dt, tsc.getInitTimeStep(),
76  "Resetting constant dt.");
77  dt = tsc.getInitTimeStep();
78  }
79 
80  // Stepper failure
81  if (workingState->getSolutionStatus() == Status::FAILED) {
82  *out << "Failure - Stepper failed and can not change time step size!\n"
83  << " Time step type == CONSTANT_STEP_SIZE\n" << std::endl;
84  integratorStatus = FAILED;
85  return;
86  }
87 
88  // Absolute error failure
89  if (errorAbs > tsc.getMaxAbsError()) {
90  *out << "Failure - Absolute error failed and can not change time step!\n"
91  << " Time step type == CONSTANT_STEP_SIZE\n"
92  << " (errorAbs ="<<errorAbs<<") > (errorMaxAbs ="
93  << tsc.getMaxAbsError() << ")" << std::endl;
94  integratorStatus = FAILED;
95  return;
96  }
97 
98  // Relative error failure
99  if (errorRel > tsc.getMaxRelError()) {
100  *out << "Failure - Relative error failed and can not change time step!\n"
101  << " Time step type == CONSTANT_STEP_SIZE\n"
102  << " (errorRel ="<<errorRel<<") > (errorMaxRel ="
103  << tsc.getMaxRelError() << ")" << std::endl;
104  integratorStatus = FAILED;
105  return;
106  }
107 
108  // update dt
109  workingState->setTimeStep(dt);
110 
111  // Set time from initial time, dt, and index to avoid numerical roundoff.
112  const Scalar initTime = tsc.getInitTime();
113  const int initIndex = tsc.getInitIndex();
114  const int index = workingState->getIndex();
115  const Scalar time = (index-initIndex)*dt + initTime;
116  workingState->setTime(time);
117  }
118 
119 
121 
122  std::string description() const override
123  { return "Tempus::TimeStepControlStrategyConstant"; }
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  {
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 
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  { constantTimeStep_ = dt; this->isInitialized_ = false; }
166 
167 
168 private:
169 
171 
172 };
173 
174 
176 template <class Scalar>
180  std::string name = "Constant")
181 {
183  if (pList == Teuchos::null || pList->numParams() == 0) return tscs;
184 
186  pList->get<std::string>("Strategy Type") != "Constant", std::logic_error,
187  "Error - Strategy Type != 'Constant'. (='"
188  +pList->get<std::string>("Strategy Type")+"')\n");
189 
190  pList->validateParametersAndSetDefaults(*tscs->getValidParameters());
191 
192  tscs->setConstantTimeStep(pList->get<double>("Time Step"));
193 
194  tscs->setName(name);
195  tscs->initialize();
196 
197  return tscs;
198 }
199 
200 
202 template<class Scalar>
204 {
206  return Teuchos::rcp_const_cast<Teuchos::ParameterList> (t->getValidParameters());
207 }
208 
209 
210 } // namespace Tempus
211 #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 RCP< FancyOStream > getOStream() const