Tempus Version of the Day
Time Integration
Loading...
Searching...
No Matches
Tempus_TimeStepControlStrategyComposite.hpp
Go to the documentation of this file.
1//@HEADER
2// *****************************************************************************
3// Tempus: Time Integration and Sensitivity Analysis Package
4//
5// Copyright 2017 NTESS and the Tempus contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8//@HEADER
9
10#ifndef Tempus_TimeStepControlStrategyComposite_hpp
11#define Tempus_TimeStepControlStrategyComposite_hpp
12
13#include "Tempus_config.hpp"
18#include "Tempus_SolutionHistory.hpp"
19
20namespace Tempus {
21
42template <class Scalar>
44 : virtual public TimeStepControlStrategy<Scalar> {
45 public:
48 {
49 this->setStrategyType("Composite");
50 this->setStepType("Variable");
51 this->isInitialized_ = false;
52 }
53
56
59 Teuchos::RCP<SolutionHistory<Scalar>> sh,
60 Status& integratorStatus) override
61 {
62 for (auto& s : strategies_) s->setNextTimeStep(tsc, sh, integratorStatus);
63 }
64
66
67 std::string description() const override
68 {
69 return "Tempus::TimeStepControlComposite";
70 }
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_) s->describe(*l_out, verbLevel);
93
94 *l_out << std::string(this->description().length() + 8, '-') << std::endl;
95 }
96 }
98
101 const Teuchos::RCP<TimeStepControlStrategy<Scalar>>& strategy)
102 {
103 if (Teuchos::nonnull(strategy)) {
104 if (this->size() == 0) this->setStepType(strategy->getStepType());
105
106 TEUCHOS_TEST_FOR_EXCEPTION(
107 this->getStepType() != strategy->getStepType(), std::logic_error,
108 "Error - Cannot mix 'Constant' and 'Variable' step types.\n"
109 "strategies in composite! Need at least one.\n");
110
111 strategies_.push_back(strategy);
112 }
113 }
114
116 virtual int size() const { return strategies_.size(); }
117
119 virtual std::vector<Teuchos::RCP<TimeStepControlStrategy<Scalar>>>
121 {
122 return strategies_;
123 }
124
126 void clearStrategies() { strategies_.clear(); }
127
128 virtual void initialize() const override
129 {
130 TEUCHOS_TEST_FOR_EXCEPTION(
131 strategies_.size() == 0, std::logic_error,
132 "Error - No strategies in composite! Need at least one.\n");
133
134 for (auto& s : strategies_) s->initialize();
135
136 auto strategy0 = strategies_[0];
137 for (auto& s : strategies_) {
138 TEUCHOS_TEST_FOR_EXCEPTION(s->isInitialized() == false, std::logic_error,
139 "Error - Composite strategy, "
140 << s->getName()
141 << " is not initialized!\n");
142
143 if (strategy0->getStepType() != s->getStepType()) {
144 std::ostringstream msg;
145 msg << "Error - All the Strategy Step Types must match.\n";
146 for (std::size_t i = 0; i < strategies_.size(); ++i) {
147 msg << " Strategy[" << i << "] = " << strategies_[i]->getStepType()
148 << " (" << strategies_[i]->getName() << ")\n";
149 }
150 TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error, msg.str());
151 }
152 }
153
154 this->isInitialized_ = true; // Only place where this is set to true!
155 }
156
158 virtual Teuchos::RCP<const Teuchos::ParameterList> getValidParameters()
159 const override
160 {
161 Teuchos::RCP<Teuchos::ParameterList> pl =
162 Teuchos::parameterList("Time Step Control Strategy");
163
164 pl->set<std::string>("Strategy Type", this->getStrategyType(), "Composite");
165
166 std::stringstream sList;
167 for (std::size_t i = 0; i < strategies_.size(); ++i) {
168 sList << strategies_[i]->getStrategyType();
169 if (i < strategies_.size() - 1) sList << ", ";
170 }
171 pl->set<std::string>("Strategy List", sList.str());
172
173 for (auto& s : strategies_) pl->set(s->getName(), *s->getValidParameters());
174
175 return pl;
176 }
177
178 private:
179 std::vector<Teuchos::RCP<TimeStepControlStrategy<Scalar>>> strategies_;
180};
181
182// Nonmember constructor - ParameterList
183// ------------------------------------------------------------------------
184template <class Scalar>
185Teuchos::RCP<TimeStepControlStrategyComposite<Scalar>>
187 Teuchos::RCP<Teuchos::ParameterList> const& pList,
188 std::string name = "Composite")
189{
190 using Teuchos::ParameterList;
191 using Teuchos::RCP;
192
193 std::vector<std::string> tscsList;
194
195 TEUCHOS_TEST_FOR_EXCEPTION(
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 =
216 (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, true)));
231
232 auto strategyType = pl->get<std::string>("Strategy Type", "Unknown");
233 if (strategyType == "Constant") {
234 tscsc->addStrategy(
235 createTimeStepControlStrategyConstant<Scalar>(pl, tscsName));
236 }
237 else if (strategyType == "Basic VS") {
238 tscsc->addStrategy(
239 createTimeStepControlStrategyBasicVS<Scalar>(pl, tscsName));
240 }
241 else if (strategyType == "Integral Controller") {
242 tscsc->addStrategy(
243 createTimeStepControlStrategyIntegralController<Scalar>(pl,
244 tscsName));
245 }
246 else if (strategyType == "Composite") {
247 tscsc->addStrategy(
248 createTimeStepControlStrategyComposite<Scalar>(pl, tscsName));
249 }
250 else {
251 RCP<Teuchos::FancyOStream> out =
252 Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
253 out->setOutputToRootOnly(0);
254 Teuchos::OSTab ostab(out, 1, "createTimeStepControlStrategyComposite()");
255 *out << "Warning -- Unknown strategy type!\n"
256 << "'Strategy Type' = '" << strategyType << "'\n"
257 << "Should call addStrategy() with this\n"
258 << "(app-specific?) strategy, and initialize().\n"
259 << std::endl;
260 }
261 }
262
263 tscsc->setName(name);
264
265 if (tscsc->size() == 0) {
266 RCP<Teuchos::FancyOStream> out =
267 Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
268 out->setOutputToRootOnly(0);
269 Teuchos::OSTab ostab(out, 1, "createTimeStepControlStrategyComposite()");
270 *out << "Warning -- Did not find a Tempus strategy to create!\n"
271 << "Should call addStrategy() with (app-specific?) strategy(ies),\n"
272 << "and initialize().\n"
273 << std::endl;
274 }
275 else {
276 tscsc->initialize();
277 }
278
279 return tscsc;
280}
281
283template <class Scalar>
284Teuchos::RCP<Teuchos::ParameterList> getTimeStepControlStrategyCompositePL()
285{
288 t->addStrategy(tscs);
289 return Teuchos::rcp_const_cast<Teuchos::ParameterList>(
290 t->getValidParameters());
291}
292
293} // namespace Tempus
294#endif // Tempus_TimeStepControlStrategy_hpp
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
TimeStepControlStrategyComposite loops over a vector of TimeStepControlStrategies.
std::vector< Teuchos::RCP< TimeStepControlStrategy< Scalar > > > strategies_
virtual std::vector< Teuchos::RCP< TimeStepControlStrategy< Scalar > > > getStrategies() const
Return composite list.
virtual Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
Return ParameterList with current values.
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const override
virtual void setNextTimeStep(const TimeStepControl< Scalar > &tsc, Teuchos::RCP< SolutionHistory< Scalar > > sh, Status &integratorStatus) override
Determine the time step size.
void addStrategy(const Teuchos::RCP< TimeStepControlStrategy< Scalar > > &strategy)
Append strategy to the composite list.
StepControlStrategy class for TimeStepControl.
TimeStepControlStrategy class for TimeStepControl.
bool isInitialized_
Bool if strategy is initialized.
TimeStepControl manages the time step size. There several mechanisms that effect the time step size a...
Status
Status for the Integrator, the Stepper and the SolutionState.
Teuchos::RCP< Teuchos::ParameterList > getTimeStepControlStrategyCompositePL()
Nonmember function to return ParameterList with default values.
Teuchos::RCP< TimeStepControlStrategyComposite< Scalar > > createTimeStepControlStrategyComposite(Teuchos::RCP< Teuchos::ParameterList > const &pList, std::string name="Composite")