Rythmos - Transient Integration for Differential Equations  Version of the Day
 All Classes Functions Variables Typedefs Pages
Rythmos_RampingIntegrationControlStrategy_def.hpp
1 //@HEADER
2 // ***********************************************************************
3 //
4 // Rythmos Package
5 // Copyright (2006) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // This library is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as
12 // published by the Free Software Foundation; either version 2.1 of the
13 // License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
23 // USA
24 // Questions? Contact Todd S. Coffey (tscoffe@sandia.gov)
25 //
26 // ***********************************************************************
27 //@HEADER
28 
29 
30 #ifndef RYTHMOS_RAMPING_INTEGRATION_CONTROL_STRATEGY_DEF_HPP
31 #define RYTHMOS_RAMPING_INTEGRATION_CONTROL_STRATEGY_DEF_HPP
32 
33 #include "Rythmos_RampingIntegrationControlStrategy_decl.hpp"
34 #include "Teuchos_VerboseObjectParameterListHelpers.hpp"
35 
36 
37 namespace Rythmos {
38 
39 
40 template<class Scalar>
41 RCP<RampingIntegrationControlStrategy<Scalar> >
42 rampingIntegrationControlStrategy()
43 {
44  RCP<RampingIntegrationControlStrategy<Scalar> >
45  integrationControl =
47  return integrationControl;
48 }
49 
50 
51 template<class Scalar>
52 RCP<RampingIntegrationControlStrategy<Scalar> >
53 rampingIntegrationControlStrategy( const RCP<ParameterList> &paramList )
54 {
55  RCP<RampingIntegrationControlStrategy<Scalar> >
56  integrationControl =
58  integrationControl->setParameterList(paramList);
59  return integrationControl;
60 }
61 
62 
63 //
64 // Implementation
65 //
66 
67 
68 // Static members
69 
70 
71 template<class Scalar>
72 const std::string
74 = "Take Variable Steps";
75 
76 template<class Scalar>
77 const bool
79 = true;
80 
81 
82 template<class Scalar>
83 const std::string
85 = "Number of Initial Constant Steps";
86 
87 template<class Scalar>
88 const int
90 = 0;
91 
92 
93 template<class Scalar>
94 const std::string
96 = "Number of Ramping Steps";
97 
98 template<class Scalar>
99 const int
101 = 6;
102 
103 
104 template<class Scalar>
105 const std::string
107 = "Initial dt";
108 
109 template<class Scalar>
110 const double
112 = -1.0;
113 
114 
115 template<class Scalar>
116 const std::string
118 = "Min dt";
119 
120 template<class Scalar>
121 const double
123 = std::numeric_limits<Scalar>::min();
124 
125 
126 template<class Scalar>
127 const std::string
129 = "Max dt";
130 
131 template<class Scalar>
132 const double
134 = std::numeric_limits<Scalar>::max();
135 
136 
137 template<class Scalar>
138 const std::string
140 = "Ramping Factor";
141 
142 template<class Scalar>
143 const double
145 = 1.0;
146 
147 
148 template<class Scalar>
149 const std::string
151 = "Maximum Number of Step Failures";
152 
153 template<class Scalar>
154 const int
156 = 100;
157 
158 
159 
160 // Constructors/Initializers
161 
162 
163 template<class Scalar>
165  : take_variable_steps_(take_variable_steps_default_),
166  num_constant_steps_(num_constant_steps_default_),
167  num_ramping_steps_(num_ramping_steps_default_),
168  initial_dt_(initial_dt_default_),
169  min_dt_(min_dt_default_),
170  max_dt_(max_dt_default_),
171  ramping_factor_(ramping_factor_default_),
172  num_step_failures_(0),
173  max_step_failures_(max_step_failures_default_),
174  current_dt_(-1.0)
175 {}
176 
177 
178 // Overridden from ParameterListAcceptor
179 
180 
181 template<class Scalar>
183  RCP<ParameterList> const& paramList
184  )
185 {
186  using Teuchos::as;
187  using Teuchos::get;
188  // typedef Teuchos::ScalarTraits<Scalar> ST; // unused
189  TEUCHOS_TEST_FOR_EXCEPT(is_null(paramList));
190  paramList->validateParametersAndSetDefaults(*getValidParameters());
191  this->setMyParamList(paramList);
192 
193  take_variable_steps_ = paramList->get<bool> (take_variable_steps_name_,
194  take_variable_steps_default_);
195  num_constant_steps_ = paramList->get<int> (num_constant_steps_name_,
196  num_constant_steps_default_);
197  num_ramping_steps_ = paramList->get<int> (num_ramping_steps_name_,
198  num_ramping_steps_default_);
199  initial_dt_ = paramList->get<double>(initial_dt_name_,
200  initial_dt_default_);
201  min_dt_ = paramList->get<double>(min_dt_name_, min_dt_default_);
202  max_dt_ = paramList->get<double>(max_dt_name_, max_dt_default_);
203  ramping_factor_ = paramList->get<double>(ramping_factor_name_,
204  ramping_factor_default_);
205  max_step_failures_ = paramList->get<int> (max_step_failures_name_,
206  max_step_failures_default_);
207 
208  Teuchos::readVerboseObjectSublist(&*paramList,this);
209 }
210 
211 
212 template<class Scalar>
213 RCP<const ParameterList>
215 {
216  static RCP<const ParameterList> validPL;
217  if (is_null(validPL) ) {
218  RCP<ParameterList> pl = Teuchos::parameterList();
219 
220  pl->set( take_variable_steps_name_, take_variable_steps_default_,
221  "Take variable time steps after '" + num_constant_steps_name_ +
222  "' plus '" + num_ramping_steps_name_ + "' steps. Variable time "
223  "stepping allows the Stepper to adjust the time step through a "
224  "StepControlStrategy after fixed time steps during initial constant "
225  "steps and ramping steps. If false, fixed-time steps are taken "
226  "after ramping. Fixed time stepping requires the Stepper "
227  "to take the time step set by this IntegrationControlStrategy.");
228  pl->set(num_constant_steps_name_, num_constant_steps_default_,
229  "Number of initial constant steps to take before starting the ramping.");
230  pl->set(num_ramping_steps_name_, num_ramping_steps_default_,
231  "Number of ramping steps to take before handing control to "
232  "variable stepper if '" + take_variable_steps_name_ +
233  "' is set to true. Otherwise take fixed-time steps.");
234  pl->set(initial_dt_name_, initial_dt_default_,
235  "Initial time step.");
236  pl->set(min_dt_name_, min_dt_default_,
237  "Minimum time step.");
238  pl->set(max_dt_name_, max_dt_default_,
239  "Maximum time step.");
240  pl->set(ramping_factor_name_, ramping_factor_default_,
241  "Time step growth factor used during ramping phase. dt_{n+1} = "
242  "(ramping factor) * dt_n");
243  pl->set(max_step_failures_name_, max_step_failures_default_,
244  "The maximum number of step failures before exiting with error.");
245  Teuchos::setupVerboseObjectSublist(&*pl);
246  validPL = pl;
247  }
248  return validPL;
249 }
250 
251 
252 // Overridden from IntegrationControlStrategyBase
253 
254 
255 template<class Scalar>
257 {
258  return true;
259 }
260 
261 
262 template<class Scalar>
263 RCP<IntegrationControlStrategyBase<Scalar> >
265 {
266  RCP<RampingIntegrationControlStrategy<Scalar> >
267  integrCtrlStry = rampingIntegrationControlStrategy<Scalar>();
268  const RCP<const ParameterList> paramList = this->getParameterList();
269  if (!is_null(paramList))
270  integrCtrlStry->setParameterList(Teuchos::parameterList(*paramList));
271 
272  integrCtrlStry->take_variable_steps_ = this->take_variable_steps_;
273  integrCtrlStry->num_constant_steps_ = this->num_constant_steps_;
274  integrCtrlStry->num_ramping_steps_ = this->num_ramping_steps_;
275  integrCtrlStry->initial_dt_ = this->initial_dt_;
276  integrCtrlStry->min_dt_ = this->min_dt_;
277  integrCtrlStry->max_dt_ = this->max_dt_;
278  integrCtrlStry->ramping_factor_ = this->ramping_factor_;
279  integrCtrlStry->current_dt_ = this->current_dt_;
280 
281  return integrCtrlStry;
282 }
283 
284 
285 template<class Scalar>
286 void
288  const TimeRange<Scalar> &integrationTimeDomain
289  )
290 {
291  typedef Teuchos::ScalarTraits<Scalar> ST;
292 #ifdef HAVE_RYTHMOS_DEBUG
293  TEUCHOS_ASSERT(integrationTimeDomain.length() > ST::zero());
294 #endif
295  integrationTimeDomain_ = integrationTimeDomain;
296  if (max_dt_ < ST::zero()) {
297  max_dt_ = integrationTimeDomain_.length();
298  }
299 
300  current_dt_ = initial_dt_;
301 }
302 
303 
304 template<class Scalar>
307  const StepperBase<Scalar> &/* stepper */,
308  const StepControlInfo<Scalar> &/* stepCtrlInfoLast */,
309  const int timeStepIter
310  )
311 {
312 
313 #ifdef HAVE_RYTHMOS_DEBUG
314  typedef Teuchos::ScalarTraits<Scalar> ST;
315  TEUCHOS_ASSERT(integrationTimeDomain_.length() > ST::zero());
316 #endif
317 
318  StepControlInfo<Scalar> trialStepCtrlInfo;
319 
320  if (timeStepIter < num_constant_steps_)
321  current_dt_ = initial_dt_;
322  else if (timeStepIter < num_constant_steps_ + num_ramping_steps_)
323  current_dt_ *= ramping_factor_;
324 
325  current_dt_ = std::min(max_dt_, current_dt_);
326  current_dt_ = std::max(min_dt_, current_dt_);
327 
328  num_step_failures_ = std::min(num_step_failures_-1,0);
329 
330  trialStepCtrlInfo.stepSize = current_dt_;
331  if (take_variable_steps_) {
332  if (timeStepIter < num_constant_steps_ + num_ramping_steps_)
333  trialStepCtrlInfo.stepType = STEP_TYPE_FIXED;
334  else
335  trialStepCtrlInfo.stepType = STEP_TYPE_VARIABLE;
336  } else {
337  trialStepCtrlInfo.stepType = STEP_TYPE_FIXED;
338  }
339 
340  return trialStepCtrlInfo;
341 }
342 
343 
344 template<class Scalar>
346  const StepperBase<Scalar> &/* stepper */,
347  const StepControlInfo<Scalar> &/* stepCtrlInfoLast */,
348  const int /* timeStepIter */,
349  const StepControlInfo<Scalar> &/* stepCtrlInfo */
350  )
351 {
352  if (current_dt_ == min_dt_) return false;
353  num_step_failures_++;
354  if (num_step_failures_ > max_step_failures_) return false;
355  current_dt_ = std::max(min_dt_, current_dt_/ramping_factor_);
356  return true;
357 }
358 
359 
360 //
361 // Explicit Instantiation macro
362 //
363 // Must be expanded from within the Rythmos namespace!
364 //
365 
366 #define RYTHMOS_RAMPING_INTEGRATION_CONTROL_STRATEGY_INSTANT(SCALAR) \
367  \
368  template class RampingIntegrationControlStrategy< SCALAR >; \
369  \
370  template RCP<RampingIntegrationControlStrategy< SCALAR > > \
371  rampingIntegrationControlStrategy(); \
372  \
373  template RCP<RampingIntegrationControlStrategy< SCALAR > > \
374  rampingIntegrationControlStrategy( const RCP<ParameterList> &paramList );
375 
376 
377 } // namespace Rythmos
378 
379 
380 #endif // RYTHMOS_RAMPING_INTEGRATION_CONTROL_STRATEGY_DEF_HPP
void resetIntegrationControlStrategy(const TimeRange< Scalar > &integrationTimeDomain)
Base class for defining stepper functionality.
Simple struct to aggregate integration/stepper control information.
bool handlesFailedTimeSteps() const
Overridden from IntegrationControlStrategyBase.
bool resetForFailedTimeStep(const StepperBase< Scalar > &stepper, const StepControlInfo< Scalar > &stepCtrlInfoLast, const int timeStepIter, const StepControlInfo< Scalar > &stepCtrlInfo)
Controls inital ramping at a fixed or incrementing time step size.
RCP< IntegrationControlStrategyBase< Scalar > > cloneIntegrationControlStrategy() const
StepControlInfo< Scalar > getNextStepControlInfo(const StepperBase< Scalar > &stepper, const StepControlInfo< Scalar > &stepCtrlInfoLast, const int timeStepIter)