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 
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,
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 
73  const Teuchos::EVerbosityLevel verbLevel) const override
74  {
75  auto out = Teuchos::fancyOStream( in_out.getOStream() );
76  out->setOutputToRootOnly(0);
77  Teuchos::OSTab ostab(*out,2,"describe");
78  *out << description() << "::describe:" << std::endl
79  << "Strategy Type = " << this->getStrategyType()<< std::endl
80  << "Step Type = " << this->getStepType()<< std::endl;
81 
82  std::stringstream sList;
83  for(std::size_t i = 0; i < strategies_.size(); ++i) {
84  sList << strategies_[i]->getStrategyType();
85  if (i < strategies_.size()-1) sList << ", ";
86  }
87  *out << "Strategy List = " << sList.str() << std::endl;
88 
89  for(auto& s : strategies_)
90  s->describe(*out, verbLevel);
91  }
93 
97  {
98  if (Teuchos::nonnull(strategy)) {
99  if (this->size() == 0) this->setStepType(strategy->getStepType());
100 
101  TEUCHOS_TEST_FOR_EXCEPTION(this->getStepType() != strategy->getStepType(),
102  std::logic_error,
103  "Error - Cannot mix 'Constant' and 'Variable' step types.\n"
104  "strategies in composite! Need at least one.\n");
105 
106  strategies_.push_back(strategy);
107  }
108  }
109 
111  virtual int size() const { return strategies_.size(); }
112 
114  virtual std::vector<Teuchos::RCP<TimeStepControlStrategy<Scalar>>>
115  getStrategies() const { return strategies_; }
116 
118  void clearStrategies() { strategies_.clear(); }
119 
120  virtual void initialize() const override
121  {
122  TEUCHOS_TEST_FOR_EXCEPTION( strategies_.size() == 0, std::logic_error,
123  "Error - No strategies in composite! Need at least one.\n");
124 
125  for(auto& s : strategies_)
126  s->initialize();
127 
128  auto strategy0 = strategies_[0];
129  for (auto& s : strategies_) {
130  TEUCHOS_TEST_FOR_EXCEPTION(s->isInitialized() == false, std::logic_error,
131  "Error - Composite strategy, "<< s->getName() <<" is not initialized!\n");
132 
133  if (strategy0->getStepType() != s->getStepType()) {
134  std::ostringstream msg;
135  msg << "Error - All the Strategy Step Types must match.\n";
136  for(std::size_t i = 0; i < strategies_.size(); ++i) {
137  msg << " Strategy[" << i << "] = "
138  << strategies_[i]->getStepType()
139  << " (" << strategies_[i]->getName() << ")\n";
140  }
141  TEUCHOS_TEST_FOR_EXCEPTION( true, std::logic_error, msg.str());
142  }
143  }
144 
145  this->isInitialized_ = true; // Only place where this is set to true!
146  }
147 
150  {
152  Teuchos::parameterList("Time Step Control Strategy");
153 
154  pl->set<std::string>("Strategy Type", this->getStrategyType(), "Composite");
155 
156  std::stringstream sList;
157  for(std::size_t i = 0; i < strategies_.size(); ++i) {
158  sList << strategies_[i]->getStrategyType();
159  if (i < strategies_.size()-1) sList << ", ";
160  }
161  pl->set<std::string>("Strategy List", sList.str());
162 
163  for(auto& s : strategies_)
164  pl->set(s->getName(), *s->getValidParameters());
165 
166  return pl;
167  }
168 
169 private:
170 
171  std::vector<Teuchos::RCP<TimeStepControlStrategy<Scalar > > > strategies_;
172 
173 };
174 
175 
176 // Nonmember constructor - ParameterList
177 // ------------------------------------------------------------------------
178 template <class Scalar>
182  std::string name = "Composite")
183 {
184  using Teuchos::RCP;
186 
187  std::vector<std::string> tscsList;
188 
190  pList->get<std::string>("Strategy Type", "Composite") !=
191  "Composite", std::logic_error,
192  "Error - Strategy Type != 'Composite'. (='"
193  +pList->get<std::string>("Strategy Type")+"')\n");
194 
195  // string tokenizer
196  tscsList.clear();
197  std::string str = pList->get<std::string>("Strategy List");
198  std::string delimiters(",");
199  const char* WhiteSpace = " \t\v\r\n";
200  // Skip delimiters at the beginning
201  std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
202  // Find the first delimiter
203  std::string::size_type pos = str.find_first_of(delimiters, lastPos);
204  while ((pos != std::string::npos) || (lastPos != std::string::npos)) {
205  // Found a token, add it to the vector
206  std::string token = str.substr(lastPos,pos-lastPos);
207 
208  std::size_t start = token.find_first_not_of(WhiteSpace);
209  std::size_t end = token.find_last_not_of(WhiteSpace);
210  token = (start == end ? std::string() : token.substr(start, end-start+1));
211 
212  tscsList.push_back(token);
213  if(pos==std::string::npos) break;
214 
215  lastPos = str.find_first_not_of(delimiters, pos); // Skip delimiters
216  pos = str.find_first_of(delimiters, lastPos); // Find next delimiter
217  }
218 
220 
221  // For each sublist name tokenized, add the TSCS
222  for ( auto tscsName: tscsList) {
223  RCP<ParameterList> pl =
224  Teuchos::rcp(new ParameterList(pList->sublist(tscsName)));
225 
226  auto strategyType = pl->get<std::string>("Strategy Type", "Unknown");
227  if (strategyType == "Constant") {
228  tscsc->addStrategy(
229  createTimeStepControlStrategyConstant<Scalar>(pl, tscsName));
230  } else if (strategyType == "Basic VS") {
231  tscsc->addStrategy(
232  createTimeStepControlStrategyBasicVS<Scalar>(pl, tscsName));
233  } else if (strategyType == "Integral Controller") {
234  tscsc->addStrategy(
235  createTimeStepControlStrategyIntegralController<Scalar>(pl, tscsName));
236  } else if (strategyType == "Composite") {
237  tscsc->addStrategy(
238  createTimeStepControlStrategyComposite<Scalar>(pl, tscsName));
239  } else {
240  RCP<Teuchos::FancyOStream> out =
241  Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
242  out->setOutputToRootOnly(0);
243  Teuchos::OSTab ostab(out,1, "createTimeStepControlStrategyComposite()");
244  *out << "Warning -- Unknown strategy type!\n"
245  << "'Strategy Type' = '" << strategyType << "'\n"
246  << "Should call addStrategy() with this\n"
247  << "(app-specific?) strategy, and initialize().\n" << std::endl;
248  }
249 
250  }
251 
252  tscsc->setName(name);
253 
254  if (tscsc->size() == 0) {
255  RCP<Teuchos::FancyOStream> out =
256  Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
257  out->setOutputToRootOnly(0);
258  Teuchos::OSTab ostab(out,1, "createTimeStepControlStrategyComposite()");
259  *out << "Warning -- Did not find a Tempus strategy to create!\n"
260  << "Should call addStrategy() with (app-specific?) strategy(ies),\n"
261  << "and initialize().\n" << std::endl;
262  } else {
263  tscsc->initialize();
264  }
265 
266  return tscsc;
267 
268 }
269 
270 
272 template<class Scalar>
274 {
277  t->addStrategy(tscs);
278  return Teuchos::rcp_const_cast<Teuchos::ParameterList> (t->getValidParameters());
279 }
280 
281 
282 } // namespace Tempus
283 #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 &in_out, const Teuchos::EVerbosityLevel verbLevel) const override
std::vector< Teuchos::RCP< TimeStepControlStrategy< Scalar > > > strategies_
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)
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.
RCP< std::basic_ostream< char_type, traits_type > > getOStream()
TimeStepControlStrategyComposite loops over a vector of TimeStepControlStrategies.
void addStrategy(const Teuchos::RCP< TimeStepControlStrategy< Scalar > > &strategy)
Append strategy to the composite list.
ParameterList & sublist(const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
TimeStepControlStrategy class for TimeStepControl.
Teuchos::RCP< Teuchos::ParameterList > getTimeStepControlStrategyCompositePL()
Nonmember function to return ParameterList with default values.