Tempus  Version of the Day
Time Integration
Tempus_TimeStepControlStrategyComposite.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_TimeStepControlStrategyComposite_hpp
10 #define Tempus_TimeStepControlStrategyComposite_hpp
11 
12 #include "Tempus_config.hpp"
17 #include "Tempus_SolutionHistory.hpp"
18 
19 
20 namespace Tempus {
21 
41 template<class Scalar>
43  : virtual public TimeStepControlStrategy<Scalar>
44 {
45 public:
46 
49  {
50  this->setStrategyType("Composite");
51  this->setStepType("Variable");
52  this->isInitialized_ = false;
53  }
54 
57 
59  virtual void setNextTimeStep(const TimeStepControl<Scalar> & tsc,
60  Teuchos::RCP<SolutionHistory<Scalar> > sh,
61  Status & integratorStatus) override
62  {
63  for(auto& s : strategies_)
64  s->setNextTimeStep(tsc, sh, integratorStatus);
65  }
66 
68 
69  std::string description() const override
70  { return "Tempus::TimeStepControlComposite"; }
71 
72  void describe(Teuchos::FancyOStream &out,
73  const Teuchos::EVerbosityLevel verbLevel) const override
74  {
75  auto l_out = Teuchos::fancyOStream( out.getOStream() );
76  Teuchos::OSTab ostab(*l_out, 2, this->description());
77  l_out->setOutputToRootOnly(0);
78 
79  *l_out << "\n--- " << this->description() << " ---" << std::endl;
80 
81  if (Teuchos::as<int>(verbLevel) >= Teuchos::as<int>(Teuchos::VERB_MEDIUM)) {
82  *l_out << " Strategy Type = " << this->getStrategyType()<< std::endl
83  << " Step Type = " << this->getStepType()<< std::endl;
84 
85  std::stringstream sList;
86  for(std::size_t i = 0; i < strategies_.size(); ++i) {
87  sList << strategies_[i]->getStrategyType();
88  if (i < strategies_.size()-1) sList << ", ";
89  }
90  *l_out << " Strategy List = " << sList.str() << std::endl;
91 
92  for(auto& s : strategies_)
93  s->describe(*l_out, verbLevel);
94 
95  *l_out << std::string(this->description().length()+8, '-') <<std::endl;
96  }
97  }
99 
102  const Teuchos::RCP<TimeStepControlStrategy<Scalar> > &strategy)
103  {
104  if (Teuchos::nonnull(strategy)) {
105  if (this->size() == 0) this->setStepType(strategy->getStepType());
106 
107  TEUCHOS_TEST_FOR_EXCEPTION(this->getStepType() != strategy->getStepType(),
108  std::logic_error,
109  "Error - Cannot mix 'Constant' and 'Variable' step types.\n"
110  "strategies in composite! Need at least one.\n");
111 
112  strategies_.push_back(strategy);
113  }
114  }
115 
117  virtual int size() const { return strategies_.size(); }
118 
120  virtual std::vector<Teuchos::RCP<TimeStepControlStrategy<Scalar>>>
121  getStrategies() const { return strategies_; }
122 
124  void clearStrategies() { strategies_.clear(); }
125 
126  virtual void initialize() const override
127  {
128  TEUCHOS_TEST_FOR_EXCEPTION( strategies_.size() == 0, std::logic_error,
129  "Error - No strategies in composite! Need at least one.\n");
130 
131  for(auto& s : strategies_)
132  s->initialize();
133 
134  auto strategy0 = strategies_[0];
135  for (auto& s : strategies_) {
136  TEUCHOS_TEST_FOR_EXCEPTION(s->isInitialized() == false, std::logic_error,
137  "Error - Composite strategy, "<< s->getName() <<" is not initialized!\n");
138 
139  if (strategy0->getStepType() != s->getStepType()) {
140  std::ostringstream msg;
141  msg << "Error - All the Strategy Step Types must match.\n";
142  for(std::size_t i = 0; i < strategies_.size(); ++i) {
143  msg << " Strategy[" << i << "] = "
144  << strategies_[i]->getStepType()
145  << " (" << strategies_[i]->getName() << ")\n";
146  }
147  TEUCHOS_TEST_FOR_EXCEPTION( true, std::logic_error, msg.str());
148  }
149  }
150 
151  this->isInitialized_ = true; // Only place where this is set to true!
152  }
153 
155  virtual Teuchos::RCP<const Teuchos::ParameterList> getValidParameters() const override
156  {
157  Teuchos::RCP<Teuchos::ParameterList> pl =
158  Teuchos::parameterList("Time Step Control Strategy");
159 
160  pl->set<std::string>("Strategy Type", this->getStrategyType(), "Composite");
161 
162  std::stringstream sList;
163  for(std::size_t i = 0; i < strategies_.size(); ++i) {
164  sList << strategies_[i]->getStrategyType();
165  if (i < strategies_.size()-1) sList << ", ";
166  }
167  pl->set<std::string>("Strategy List", sList.str());
168 
169  for(auto& s : strategies_)
170  pl->set(s->getName(), *s->getValidParameters());
171 
172  return pl;
173  }
174 
175 private:
176 
177  std::vector<Teuchos::RCP<TimeStepControlStrategy<Scalar > > > strategies_;
178 
179 };
180 
181 
182 // Nonmember constructor - ParameterList
183 // ------------------------------------------------------------------------
184 template <class Scalar>
185 Teuchos::RCP<TimeStepControlStrategyComposite<Scalar> >
187  Teuchos::RCP<Teuchos::ParameterList> const& pList,
188  std::string name = "Composite")
189 {
190  using Teuchos::RCP;
191  using Teuchos::ParameterList;
192 
193  std::vector<std::string> tscsList;
194 
195  TEUCHOS_TEST_FOR_EXCEPTION(
196  pList->get<std::string>("Strategy Type", "Composite") !=
197  "Composite", std::logic_error,
198  "Error - Strategy Type != 'Composite'. (='"
199  +pList->get<std::string>("Strategy Type")+"')\n");
200 
201  // string tokenizer
202  tscsList.clear();
203  std::string str = pList->get<std::string>("Strategy List");
204  std::string delimiters(",");
205  const char* WhiteSpace = " \t\v\r\n";
206  // Skip delimiters at the beginning
207  std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
208  // Find the first delimiter
209  std::string::size_type pos = str.find_first_of(delimiters, lastPos);
210  while ((pos != std::string::npos) || (lastPos != std::string::npos)) {
211  // Found a token, add it to the vector
212  std::string token = str.substr(lastPos,pos-lastPos);
213 
214  std::size_t start = token.find_first_not_of(WhiteSpace);
215  std::size_t end = token.find_last_not_of(WhiteSpace);
216  token = (start == end ? std::string() : token.substr(start, end-start+1));
217 
218  tscsList.push_back(token);
219  if(pos==std::string::npos) break;
220 
221  lastPos = str.find_first_not_of(delimiters, pos); // Skip delimiters
222  pos = str.find_first_of(delimiters, lastPos); // Find next delimiter
223  }
224 
225  auto tscsc = Teuchos::rcp(new TimeStepControlStrategyComposite<Scalar>());
226 
227  // For each sublist name tokenized, add the TSCS
228  for ( auto tscsName: tscsList) {
229  RCP<ParameterList> pl =
230  Teuchos::rcp(new ParameterList(pList->sublist(tscsName)));
231 
232  auto strategyType = pl->get<std::string>("Strategy Type", "Unknown");
233  if (strategyType == "Constant") {
234  tscsc->addStrategy(
235  createTimeStepControlStrategyConstant<Scalar>(pl, tscsName));
236  } else if (strategyType == "Basic VS") {
237  tscsc->addStrategy(
238  createTimeStepControlStrategyBasicVS<Scalar>(pl, tscsName));
239  } else if (strategyType == "Integral Controller") {
240  tscsc->addStrategy(
241  createTimeStepControlStrategyIntegralController<Scalar>(pl, tscsName));
242  } else if (strategyType == "Composite") {
243  tscsc->addStrategy(
244  createTimeStepControlStrategyComposite<Scalar>(pl, tscsName));
245  } else {
246  RCP<Teuchos::FancyOStream> out =
247  Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
248  out->setOutputToRootOnly(0);
249  Teuchos::OSTab ostab(out,1, "createTimeStepControlStrategyComposite()");
250  *out << "Warning -- Unknown strategy type!\n"
251  << "'Strategy Type' = '" << strategyType << "'\n"
252  << "Should call addStrategy() with this\n"
253  << "(app-specific?) strategy, and initialize().\n" << std::endl;
254  }
255 
256  }
257 
258  tscsc->setName(name);
259 
260  if (tscsc->size() == 0) {
261  RCP<Teuchos::FancyOStream> out =
262  Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
263  out->setOutputToRootOnly(0);
264  Teuchos::OSTab ostab(out,1, "createTimeStepControlStrategyComposite()");
265  *out << "Warning -- Did not find a Tempus strategy to create!\n"
266  << "Should call addStrategy() with (app-specific?) strategy(ies),\n"
267  << "and initialize().\n" << std::endl;
268  } else {
269  tscsc->initialize();
270  }
271 
272  return tscsc;
273 
274 }
275 
276 
278 template<class Scalar>
279 Teuchos::RCP<Teuchos::ParameterList> getTimeStepControlStrategyCompositePL()
280 {
283  t->addStrategy(tscs);
284  return Teuchos::rcp_const_cast<Teuchos::ParameterList> (t->getValidParameters());
285 }
286 
287 
288 } // namespace Tempus
289 #endif // Tempus_TimeStepControlStrategy_hpp
virtual Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
Return ParameterList with current values.
virtual std::vector< Teuchos::RCP< TimeStepControlStrategy< Scalar > > > getStrategies() const
Return composite list.
std::vector< Teuchos::RCP< TimeStepControlStrategy< Scalar > > > strategies_
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const override
Teuchos::RCP< TimeStepControlStrategyComposite< Scalar > > createTimeStepControlStrategyComposite(Teuchos::RCP< Teuchos::ParameterList > const &pList, std::string name="Composite")
Status
Status for the Integrator, the Stepper and the SolutionState.
bool isInitialized_
Bool if strategy is initialized.
TimeStepControl manages the time step size. There several mechanisms that effect the time step size a...
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
virtual void setNextTimeStep(const TimeStepControl< Scalar > &tsc, Teuchos::RCP< SolutionHistory< Scalar > > sh, Status &integratorStatus) override
Determine the time step size.
StepControlStrategy class for TimeStepControl.
TimeStepControlStrategyComposite loops over a vector of TimeStepControlStrategies.
void addStrategy(const Teuchos::RCP< TimeStepControlStrategy< Scalar > > &strategy)
Append strategy to the composite list.
TimeStepControlStrategy class for TimeStepControl.
Teuchos::RCP< Teuchos::ParameterList > getTimeStepControlStrategyCompositePL()
Nonmember function to return ParameterList with default values.