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