Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Tempus_TimeStepControlStrategyPID.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_PID_hpp
10 #define Tempus_TimeStepControlStrategy_PID_hpp
11 
12 #include "Tempus_TimeStepControl.hpp"
14 #include "Tempus_SolutionState.hpp"
15 #include "Tempus_SolutionStateMetaData.hpp"
16 #include "Tempus_SolutionHistory.hpp"
17 #include "Tempus_StepperState.hpp"
18 
19 
20 namespace Tempus {
21 
22 /** \brief StepControlStrategy class for TimeStepControl
23  *
24  */
25 template<class Scalar>
27  : virtual public TimeStepControlStrategy<Scalar>
28 {
29 public:
30 
31  /// Constructor
32  TimeStepControlStrategyPID(Teuchos::RCP<Teuchos::ParameterList> pList = Teuchos::null){
33  this->setParameterList(pList);
34  }
35 
36  /// Destructor
38 
39  /** \brief Determine the time step size.*/
40  virtual void getNextTimeStep(const TimeStepControl<Scalar> tsc,
42  Status & integratorStatus) override
43  {
44 
45  // Take first step with initial time step provided
47  firstSuccessfulStep_ = true;
48  return;
49  }
50 
51  Teuchos::RCP<SolutionState<Scalar> > workingState=solutionHistory->getWorkingState();
52  Teuchos::RCP<SolutionStateMetaData<Scalar> > metaData = workingState->getMetaData();
53  const Scalar errorAbs = metaData->getErrorAbs();
54  volatile const int iStep = metaData->getIStep();
55  volatile const Scalar errorRel = metaData->getErrorRel();
56  int order = metaData->getOrder();
57  Scalar dt = metaData->getDt();
58  bool printChanges = solutionHistory->getVerbLevel() !=
59  Teuchos::as<int>(Teuchos::VERB_NONE);
60 
61  Teuchos::RCP<Teuchos::FancyOStream> out = tsc.getOStream();
62  Teuchos::OSTab ostab(out,1,"getNextTimeStep");
63 
64  // For now, only time step is changed (not order)
65  /*
66  auto changeOrder = [] (int order_old, int order_new, std::string reason) {
67  std::stringstream message;
68  message << " (order = " << std::setw(2) << order_old
69  << ", new = " << std::setw(2) << order_new
70  << ") " << reason << std::endl;
71  return message.str();
72  };
73  */
74 
75  // update errors
76  errNm2_ = errNm1_;
77  errNm1_ = errN_;
78  errN_ = errorRel;
79 
80  Scalar k1 = Teuchos::as<Scalar>(-k1_ / order);
81  Scalar k2 = Teuchos::as<Scalar>(k2_ / order);
82  Scalar k3 = Teuchos::as<Scalar>(-k3_ / order);
83 
84  k1 = std::pow(errN_, k1);
85  k2 = std::pow(errNm1_, k2);
86  k3 = std::pow(errNm2_, k3);
87  Scalar beta = safetyFactor_*k1*k2*k3;
88  beta = std::max(facMin_, beta);
89  beta = std::min(facMax_, beta);
90 
91  // new (optimal) suggested time step
92  dt = beta * dt;
93 
94  if (workingState->getSolutionStatus() == Status::PASSED or
95  workingState->getSolutionStatus() == Status::WORKING) {
97  dt = std::min(dt, metaData->getDt());
98  } else {
99  facMax_ = tscsPL_->get<Scalar>("Maximum Safety Factor");
100  }
101  lastStepRejected_ = false;
102  } else {
103  facMax_ = 0.90;
104  lastStepRejected_ = true;
105  }
106 
107  // update dt
108  metaData->setDt(dt);
109  }
110 
111  /// \name Overridden from Teuchos::ParameterListAcceptor
112  //@{
113  void setParameterList(const Teuchos::RCP<Teuchos::ParameterList> & pList){
114 
115  if (pList == Teuchos::null) {
116  // Create default parameters if null, otherwise keep current parameters.
117  if (tscsPL_ == Teuchos::null) {
118  tscsPL_ = Teuchos::parameterList("TimeStepControlStrategyPID");
119  *tscsPL_= *(this->getValidParameters());
120  }
121  } else {
122  tscsPL_ = pList;
123  }
124  tscsPL_->validateParametersAndSetDefaults(*this->getValidParameters());
125 
126  errN_ = Scalar(1.0);
127  errNm1_ = Scalar(1.0);
128  errNm2_ = Scalar(1.0);
129  k1_ = tscsPL_->get<Scalar>("K1");
130  k2_ = tscsPL_->get<Scalar>("K2");
131  k3_ = tscsPL_->get<Scalar>("K3");
132  safetyFactor_ = tscsPL_->get<Scalar>("Safety Factor");
133  facMax_ = tscsPL_->get<Scalar>("Maximum Safety Factor");
134  facMin_ = tscsPL_->get<Scalar>("Minimum Safety Factor");
135 
136  TEUCHOS_TEST_FOR_EXCEPTION(safetyFactor_ <= 0.0, std::out_of_range,
137  "Error - Invalid value of Safety Factory= " << safetyFactor_ << "! \n"
138  << "Safety Factor must be > 0.0.\n");
139 
140  TEUCHOS_TEST_FOR_EXCEPTION(facMax_ <= 0.0, std::out_of_range,
141  "Error - Invalid value of Maximum Safety Factory= " << facMax_ << "! \n"
142  << "Maximum Safety Factor must be > 0.0.\n");
143 
144  TEUCHOS_TEST_FOR_EXCEPTION(facMax_<= 0.0, std::out_of_range,
145  "Error - Invalid value of Minimum Safety Factory= " << facMin_ << "! \n"
146  << "Minimum Safety Factor must be > 0.0.\n");
147 
148  }
149 
150  Teuchos::RCP<const Teuchos::ParameterList> getValidParameters() const {
151  Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
152 
153  pl->set<std::string>("Name","PID");
154  pl->set<Scalar>("K1" , 0.58, "");
155  pl->set<Scalar>("K2" , 0.21, "");
156  pl->set<Scalar>("K3" , 0.10, "");
157  pl->set<Scalar>("Safety Factor" , 0.90, "Safety Factor");
158  pl->set<Scalar>("Maximum Safety Factor" , 5.0, "Maximum Safety Factor");
159  pl->set<Scalar>("Minimum Safety Factor" , 0.5, "Minimum Safety Factor");
160  return pl;
161  }
162 
163  Teuchos::RCP<Teuchos::ParameterList> getNonconstParameterList() {
164  return tscsPL_;
165  }
166 
167  Teuchos::RCP<Teuchos::ParameterList> unsetParameterList() {
168  Teuchos::RCP<Teuchos::ParameterList> temp_plist = tscsPL_;
169  tscsPL_ = Teuchos::null;
170  return(temp_plist);
171  }
172  //@}
173 
174 private:
175  Teuchos::RCP<Teuchos::ParameterList> tscsPL_;
176  Scalar k1_;
177  Scalar k2_;
178  Scalar k3_;
179  Scalar errN_;
180  Scalar errNm1_;
181  Scalar errNm2_;
183  Scalar facMax_;
184  Scalar facMin_;
185  bool firstSuccessfulStep_ = false;
186  bool lastStepRejected_ = false;
187 
188 };
189 } // namespace Tempus
190 #endif // Tempus_TimeStepControlStrategy_PID_hpp
virtual void getNextTimeStep(const TimeStepControl< Scalar > tsc, Teuchos::RCP< SolutionHistory< Scalar > > solutionHistory, Status &integratorStatus) override
Determine the time step size.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
TimeStepControlStrategyPID(Teuchos::RCP< Teuchos::ParameterList > pList=Teuchos::null)
Constructor.
StepControlStrategy class for TimeStepControl.
Teuchos::RCP< Teuchos::ParameterList > getNonconstParameterList()
Status
Status for the Integrator, the Stepper and the SolutionState.
void setParameterList(const Teuchos::RCP< Teuchos::ParameterList > &pList)
TimeStepControl manages the time step size. There several mechanicisms that effect the time step size...
Teuchos::RCP< SolutionHistory< Scalar > > solutionHistory(Teuchos::RCP< Teuchos::ParameterList > pList=Teuchos::null)
Nonmember constructor.
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
Teuchos::RCP< Teuchos::ParameterList > unsetParameterList()
Teuchos::RCP< Teuchos::ParameterList > tscsPL_
StepControlStrategy class for TimeStepControl.