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 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 
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 
156  {
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>
188  std::string name = "Composite")
189 {
190  using Teuchos::RCP;
192 
193  std::vector<std::string> tscsList;
194 
196  pList->get<std::string>("Strategy Type") != "Composite", std::logic_error,
197  "Error - Strategy Type != 'Composite'. (='"
198  +pList->get<std::string>("Strategy Type")+"')\n");
199 
200  // string tokenizer
201  tscsList.clear();
202  std::string str = pList->get<std::string>("Strategy List");
203  std::string delimiters(",");
204  const char* WhiteSpace = " \t\v\r\n";
205  // Skip delimiters at the beginning
206  std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
207  // Find the first delimiter
208  std::string::size_type pos = str.find_first_of(delimiters, lastPos);
209  while ((pos != std::string::npos) || (lastPos != std::string::npos)) {
210  // Found a token, add it to the vector
211  std::string token = str.substr(lastPos,pos-lastPos);
212 
213  std::size_t start = token.find_first_not_of(WhiteSpace);
214  std::size_t end = token.find_last_not_of(WhiteSpace);
215  token = (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  } else if (strategyType == "Basic VS") {
236  tscsc->addStrategy(
237  createTimeStepControlStrategyBasicVS<Scalar>(pl, tscsName));
238  } else if (strategyType == "Integral Controller") {
239  tscsc->addStrategy(
240  createTimeStepControlStrategyIntegralController<Scalar>(pl, tscsName));
241  } else if (strategyType == "Composite") {
242  tscsc->addStrategy(
243  createTimeStepControlStrategyComposite<Scalar>(pl, tscsName));
244  } else {
245  RCP<Teuchos::FancyOStream> out =
246  Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
247  out->setOutputToRootOnly(0);
248  Teuchos::OSTab ostab(out,1, "createTimeStepControlStrategyComposite()");
249  *out << "Warning -- Unknown strategy type!\n"
250  << "'Strategy Type' = '" << strategyType << "'\n"
251  << "Should call addStrategy() with this\n"
252  << "(app-specific?) strategy, and initialize().\n" << std::endl;
253  }
254 
255  }
256 
257  tscsc->setName(name);
258 
259  if (tscsc->size() == 0) {
260  RCP<Teuchos::FancyOStream> out =
261  Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
262  out->setOutputToRootOnly(0);
263  Teuchos::OSTab ostab(out,1, "createTimeStepControlStrategyComposite()");
264  *out << "Warning -- Did not find a Tempus strategy to create!\n"
265  << "Should call addStrategy() with (app-specific?) strategy(ies),\n"
266  << "and initialize().\n" << std::endl;
267  } else {
268  tscsc->initialize();
269  }
270 
271  return tscsc;
272 
273 }
274 
275 
277 template<class Scalar>
279 {
282  t->addStrategy(tscs);
283  return Teuchos::rcp_const_cast<Teuchos::ParameterList> (t->getValidParameters());
284 }
285 
286 
287 } // namespace Tempus
288 #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)
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")
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.