Rythmos - Transient Integration for Differential Equations  Version of the Day
 All Classes Functions Variables Typedefs Pages
Rythmos_ImplicitBDFStepperStepControl_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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 // USA
24 // Questions? Contact Todd S. Coffey (tscoffe@sandia.gov)
25 //
26 // ***********************************************************************
27 //@HEADER
28 
29 #ifndef Rythmos_IMPLICITBDF_STEPPER_STEP_CONTROL_DEF_H
30 #define Rythmos_IMPLICITBDF_STEPPER_STEP_CONTROL_DEF_H
31 
32 #include "Rythmos_ImplicitBDFStepperStepControl_decl.hpp"
33 #include "Rythmos_ImplicitBDFStepper.hpp"
34 #include "Rythmos_ImplicitBDFStepperErrWtVecCalc.hpp"
35 
36 namespace Rythmos {
37 
38 template<class Scalar>
39 void ImplicitBDFStepperStepControl<Scalar>::setStepControlState_(StepControlStrategyState newState)
40 {
41  if (stepControlState_ == UNINITIALIZED) {
42  TEUCHOS_TEST_FOR_EXCEPT(newState != BEFORE_FIRST_STEP);
43  } else if (stepControlState_ == BEFORE_FIRST_STEP) {
44  TEUCHOS_TEST_FOR_EXCEPT(newState != MID_STEP);
45  } else if (stepControlState_ == MID_STEP) {
46  TEUCHOS_TEST_FOR_EXCEPT(newState != AFTER_CORRECTION);
47  } else if (stepControlState_ == AFTER_CORRECTION) {
48  TEUCHOS_TEST_FOR_EXCEPT(newState != READY_FOR_NEXT_STEP);
49  checkReduceOrderCalled_ = false;
50  } else if (stepControlState_ == READY_FOR_NEXT_STEP) {
51  TEUCHOS_TEST_FOR_EXCEPT(newState != MID_STEP);
52  }
53  stepControlState_ = newState;
54 }
55 
56 template<class Scalar>
57 StepControlStrategyState ImplicitBDFStepperStepControl<Scalar>::getCurrentState()
58 {
59  return(stepControlState_);
60 }
61 
62 template<class Scalar>
63 void ImplicitBDFStepperStepControl<Scalar>::updateCoeffs_()
64 {
65  TEUCHOS_TEST_FOR_EXCEPT(!((stepControlState_ == BEFORE_FIRST_STEP) || (stepControlState_ == READY_FOR_NEXT_STEP)));
66  using Teuchos::as;
67  typedef Teuchos::ScalarTraits<Scalar> ST;
68 
69  // If the number of steps taken with constant order and constant stepsize is
70  // more than the current order + 1 then we don't bother to update the
71  // coefficients because we've reached a constant step-size formula. When
72  // this is is not true, then we update the coefficients for the variable
73  // step-sizes.
74  if ((hh_ != usedStep_) || (currentOrder_ != usedOrder_)) {
75  nscsco_ = 0;
76  }
77  nscsco_ = std::min(nscsco_+1,usedOrder_+2);
78  if (currentOrder_+1 >= nscsco_) {
79  alpha_[0] = ST::one();
80  Scalar temp1 = hh_;
81  sigma_[0] = ST::one();
82  gamma_[0] = ST::zero();
83  for (int i=1;i<=currentOrder_;++i) {
84  Scalar temp2 = psi_[i-1];
85  psi_[i-1] = temp1;
86  temp1 = temp2 + hh_;
87  alpha_[i] = hh_/temp1;
88  sigma_[i] = Scalar(i+1)*sigma_[i-1]*alpha_[i];
89  gamma_[i] = gamma_[i-1]+alpha_[i-1]/hh_;
90  }
91  psi_[currentOrder_] = temp1;
92  alpha_s_ = ST::zero();
93  alpha_0_ = ST::zero();
94  for (int i=0;i<currentOrder_;++i) {
95  alpha_s_ = alpha_s_ - Scalar(ST::one()/(i+ST::one()));
96  alpha_0_ = alpha_0_ - alpha_[i];
97  }
98  cj_ = -alpha_s_/hh_;
99  ck_ = std::abs(alpha_[currentOrder_]+alpha_s_-alpha_0_);
100  ck_ = std::max(ck_,alpha_[currentOrder_]);
101  }
102  RCP<Teuchos::FancyOStream> out = this->getOStream();
103  Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel();
104  Teuchos::OSTab ostab(out,1,"updateCoeffs_");
105  if ( as<int>(verbLevel) >= as<int>(Teuchos::VERB_HIGH) ) {
106  for (int i=0;i<=maxOrder_;++i) {
107  *out << "alpha_[" << i << "] = " << alpha_[i] << std::endl;
108  *out << "sigma_[" << i << "] = " << sigma_[i] << std::endl;
109  *out << "gamma_[" << i << "] = " << gamma_[i] << std::endl;
110  *out << "psi_[" << i << "] = " << psi_[i] << std::endl;
111  *out << "alpha_s_ = " << alpha_s_ << std::endl;
112  *out << "alpha_0_ = " << alpha_0_ << std::endl;
113  *out << "cj_ = " << cj_ << std::endl;
114  *out << "ck_ = " << ck_ << std::endl;
115  }
116  }
117 }
118 
119 template<class Scalar>
120 ImplicitBDFStepperStepControl<Scalar>::ImplicitBDFStepperStepControl()
121 {
122  defaultInitializeAllData_();
123 }
124 
125 template<class Scalar>
126 void ImplicitBDFStepperStepControl<Scalar>::initialize(const StepperBase<Scalar>& stepper)
127 {
128  using Teuchos::as;
129  typedef Teuchos::ScalarTraits<Scalar> ST;
130  using Thyra::createMember;
131 
132  RCP<Teuchos::FancyOStream> out = this->getOStream();
133  Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel();
134  const bool doTrace = (as<int>(verbLevel) >= as<int>(Teuchos::VERB_HIGH));
135  Teuchos::OSTab ostab(out,1,"initialize");
136 
137  if (doTrace) {
138  *out
139  << "\nEntering " << this->Teuchos::Describable::description()
140  << "::initialize()...\n";
141  }
142 
143  // Set initial time:
144  TimeRange<Scalar> stepperRange = stepper.getTimeRange();
145  TEUCHOS_TEST_FOR_EXCEPTION(
146  !stepperRange.isValid(),
147  std::logic_error,
148  "Error, Stepper does not have valid time range for initialization of ImplicitBDFStepperStepControl!\n"
149  );
150  time_ = stepperRange.upper();
151 
152  if (parameterList_ == Teuchos::null) {
153  RCP<Teuchos::ParameterList> emptyParameterList = Teuchos::rcp(new Teuchos::ParameterList);
154  this->setParameterList(emptyParameterList);
155  }
156 
157  if (is_null(errWtVecCalc_)) {
158  RCP<ImplicitBDFStepperErrWtVecCalc<Scalar> > IBDFErrWtVecCalc = rcp(new ImplicitBDFStepperErrWtVecCalc<Scalar>());
159  errWtVecCalc_ = IBDFErrWtVecCalc;
160  }
161 
162  // 08/22/07 initialize can be called from the stepper when setInitialCondition is called.
163  checkReduceOrderCalled_ = false;
164  stepControlState_ = UNINITIALIZED;
165 
166  currentOrder_=1; // Current order of integration
167  oldOrder_=1; // previous order of integration
168  usedOrder_=1; // order used in current step (used after currentOrder_ is updated)
169  alpha_s_=Scalar(-ST::one()); // $\alpha_s$ fixed-leading coefficient of this BDF method
170  alpha_.clear(); // $\alpha_j(n)=h_n/\psi_j(n)$ coefficient used in local error test
171  // note: $h_n$ = current step size, n = current time step
172  gamma_.clear(); // calculate time derivative of history array for predictor
173  psi_.clear(); // $\psi_j(n) = t_n-t_{n-j}$ intermediary variable used to
174  sigma_.clear(); // $\sigma_j(n) = \frac{h_n^j(j-1)!}{\psi_1(n)*\cdots *\psi_j(n)}$
175  Scalar zero = ST::zero();
176  for (int i=0 ; i<=maxOrder_ ; ++i) {
177  alpha_.push_back(zero);
178  gamma_.push_back(zero);
179  psi_.push_back(zero);
180  sigma_.push_back(zero);
181  }
182  alpha_0_=zero; // $-\sum_{j=1}^k \alpha_j(n)$ coefficient used in local error test
183  cj_=zero; // $-\alpha_s/h_n$ coefficient used in local error test
184  ck_=zero; // local error coefficient gamma_[0] = 0; // $\gamma_j(n)=\sum_{l=1}^{j-1}1/\psi_l(n)$ coefficient used to
185  hh_=zero;
186  numberOfSteps_=0; // number of total time integration steps taken
187  nef_=0;
188  usedStep_=zero;
189  nscsco_=0;
190  Ek_=zero;
191  Ekm1_=zero;
192  Ekm2_=zero;
193  Ekp1_=zero;
194  Est_=zero;
195  Tk_=zero;
196  Tkm1_=zero;
197  Tkm2_=zero;
198  Tkp1_=zero;
199  newOrder_=currentOrder_;
200  initialPhase_=true;
201 
202  if ( as<int>(verbLevel) >= as<int>(Teuchos::VERB_HIGH) ) {
203  *out << "currentOrder_ = " << currentOrder_ << std::endl;
204  *out << "oldOrder_ = " << oldOrder_ << std::endl;
205  *out << "usedOrder_ = " << usedOrder_ << std::endl;
206  *out << "alpha_s_ = " << alpha_s_ << std::endl;
207  for (int i=0 ; i<=maxOrder_ ; ++i) {
208  *out << "alpha_[" << i << "] = " << alpha_[i] << std::endl;
209  *out << "gamma_[" << i << "] = " << gamma_[i] << std::endl;
210  *out << "psi_[" << i << "] = " << psi_[i] << std::endl;
211  *out << "sigma_[" << i << "] = " << sigma_[i] << std::endl;
212  }
213  *out << "alpha_0_ = " << alpha_0_ << std::endl;
214  *out << "cj_ = " << cj_ << std::endl;
215  *out << "ck_ = " << ck_ << std::endl;
216  *out << "numberOfSteps_ = " << numberOfSteps_ << std::endl;
217  *out << "nef_ = " << nef_ << std::endl;
218  *out << "usedStep_ = " << usedStep_ << std::endl;
219  *out << "nscsco_ = " << nscsco_ << std::endl;
220  *out << "Ek_ = " << Ek_ << std::endl;
221  *out << "Ekm1_ = " << Ekm1_ << std::endl;
222  *out << "Ekm2_ = " << Ekm2_ << std::endl;
223  *out << "Ekp1_ = " << Ekp1_ << std::endl;
224  *out << "Est_ = " << Est_ << std::endl;
225  *out << "Tk_ = " << Tk_ << std::endl;
226  *out << "Tkm1_ = " << Tkm1_ << std::endl;
227  *out << "Tkm2_ = " << Tkm2_ << std::endl;
228  *out << "Tkp1_ = " << Tkp1_ << std::endl;
229  *out << "newOrder_ = " << newOrder_ << std::endl;
230  *out << "initialPhase_ = " << initialPhase_ << std::endl;
231  }
232 
233 
234  if (is_null(delta_)) {
235  delta_ = createMember(stepper.get_x_space());
236  }
237  if (is_null(errWtVec_)) {
238  errWtVec_ = createMember(stepper.get_x_space());
239  }
240  V_S(delta_.ptr(),zero);
241 
242  setStepControlState_(BEFORE_FIRST_STEP);
243 
244  if (doTrace) {
245  *out
246  << "\nLeaving " << this->Teuchos::Describable::description()
247  << "::initialize()...\n";
248  }
249 }
250 
251 template<class Scalar>
252 void ImplicitBDFStepperStepControl<Scalar>::getFirstTimeStep_(const StepperBase<Scalar>& stepper)
253 {
254 
255  TEUCHOS_TEST_FOR_EXCEPT(!(stepControlState_ == BEFORE_FIRST_STEP));
256 
257  using Teuchos::as;
258  typedef Teuchos::ScalarTraits<Scalar> ST;
259  Scalar zero = ST::zero();
260 
261  RCP<Teuchos::FancyOStream> out = this->getOStream();
262  Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel();
263  const bool doTrace = (as<int>(verbLevel) >= as<int>(Teuchos::VERB_HIGH));
264  Teuchos::OSTab ostab(out,1,"getFirstTimeStep_");
265 
266  const ImplicitBDFStepper<Scalar>& implicitBDFStepper
267  = Teuchos::dyn_cast<const ImplicitBDFStepper<Scalar> >(stepper);
268  const Thyra::VectorBase<Scalar>& xHistory0
269  = implicitBDFStepper.getxHistory(0);
270  errWtVecCalc_->errWtVecSet(&*errWtVec_,xHistory0,relErrTol_,absErrTol_);
271 
272  // Choose initial step-size
273  Scalar time_to_stop = stopTime_ - time_;
274  Scalar currentTimeStep = ST::nan();
275  if (stepSizeType_ == STEP_TYPE_FIXED) {
276  currentTimeStep = hh_;
277  //currentTimeStep = 0.1 * time_to_stop;
278  //currentTimeStep = std::min(hh_, currentTimeStep);
279  } else {
280  // compute an initial step-size based on rate of change
281  // in the solution initially
282  const Thyra::VectorBase<Scalar>& xHistory1
283  = implicitBDFStepper.getxHistory(1);
284  Scalar ypnorm = wRMSNorm_(*errWtVec_,xHistory1);
285  if (ypnorm > zero) { // time-dependent DAE
286  currentTimeStep = std::min( h0_max_factor_*std::abs(time_to_stop),
287  std::sqrt(2.0)/(h0_safety_*ypnorm) );
288  } else { // non-time-dependent DAE
289  currentTimeStep = h0_max_factor_*std::abs(time_to_stop);
290  }
291  // choose std::min of user specified value and our value:
292  if (hh_ > zero) {
293  currentTimeStep = std::min(hh_, currentTimeStep);
294  }
295  // check for maximum step-size:
296 #ifdef HAVE_RYTHMOS_DEBUG
297  TEUCHOS_TEST_FOR_EXCEPT(ST::isnaninf(currentTimeStep));
298 #endif // HAVE_RYTHMOS_DEBUG
299  Scalar rh = std::abs(currentTimeStep)*h_max_inv_;
300  if (rh>1.0) {
301  currentTimeStep = currentTimeStep/rh;
302  }
303  }
304  hh_ = currentTimeStep;
305 
306  // Coefficient initialization
307  psi_[0] = hh_;
308  cj_ = 1/psi_[0];
309 
310  if (doTrace) {
311  *out << "\nhh_ = " << hh_ << std::endl;
312  *out << "psi_[0] = " << psi_[0] << std::endl;
313  *out << "cj_ = " << cj_ << std::endl;
314  }
315 
316 }
317 
318 template<class Scalar>
319 void ImplicitBDFStepperStepControl<Scalar>::setRequestedStepSize(
320  const StepperBase<Scalar>& stepper
321  ,const Scalar& stepSize
322  ,const StepSizeType& stepSizeType
323  )
324 {
325  typedef Teuchos::ScalarTraits<Scalar> ST;
326  TEUCHOS_TEST_FOR_EXCEPT(!((stepControlState_ == UNINITIALIZED) || (stepControlState_ == BEFORE_FIRST_STEP) || (stepControlState_ == READY_FOR_NEXT_STEP) || (stepControlState_ == MID_STEP)));
327  TEUCHOS_TEST_FOR_EXCEPTION(
328  ((stepSizeType == STEP_TYPE_FIXED) && (stepSize == ST::zero())),
329  std::logic_error,
330  "Error, step size type == STEP_TYPE_FIXED, but requested step size == 0!\n"
331  );
332  bool didInitialization = false;
333  if (stepControlState_ == UNINITIALIZED) {
334  initialize(stepper);
335  didInitialization = true;
336  }
337 
338  // errWtVecSet_ is called during initialize
339  if (!didInitialization) {
340  const ImplicitBDFStepper<Scalar>& implicitBDFStepper = Teuchos::dyn_cast<const ImplicitBDFStepper<Scalar> >(stepper);
341  const Thyra::VectorBase<Scalar>& xHistory = implicitBDFStepper.getxHistory(0);
342  errWtVecCalc_->errWtVecSet(&*errWtVec_,xHistory,relErrTol_,absErrTol_);
343  }
344 
345  stepSizeType_ = stepSizeType;
346  if (stepSizeType_ == STEP_TYPE_FIXED) {
347  hh_ = stepSize;
348  if (numberOfSteps_ == 0) {
349  psi_[0] = hh_;
350  cj_ = 1/psi_[0];
351  }
352  } else { // STEP_TYPE_VARIABLE
353  if (stepSize != ST::zero()) {
354  maxTimeStep_ = stepSize;
355  h_max_inv_ = Scalar(ST::one()/maxTimeStep_);
356  }
357  }
358 }
359 
360 template<class Scalar>
361 void ImplicitBDFStepperStepControl<Scalar>::nextStepSize(const StepperBase<Scalar>& stepper, Scalar* stepSize, StepSizeType* stepSizeType, int* order)
362 {
363  TEUCHOS_TEST_FOR_EXCEPT(!((stepControlState_ == BEFORE_FIRST_STEP) ||
364  (stepControlState_ == MID_STEP) ||
365  (stepControlState_ == READY_FOR_NEXT_STEP) )
366  );
367  if (stepControlState_ == BEFORE_FIRST_STEP) {
368  getFirstTimeStep_(stepper);
369  }
370  if (stepControlState_ != MID_STEP) {
371  this->updateCoeffs_();
372  }
373  if (stepSizeType_ == STEP_TYPE_VARIABLE) {
374  if (hh_ > maxTimeStep_) {
375  hh_ = maxTimeStep_;
376  }
377  }
378  *stepSize = hh_;
379  *stepSizeType = stepSizeType_;
380  *order = currentOrder_;
381  if (stepControlState_ != MID_STEP) {
382  setStepControlState_(MID_STEP);
383  }
384 }
385 
386 template<class Scalar>
387 void ImplicitBDFStepperStepControl<Scalar>::setCorrection(
388  const StepperBase<Scalar>& stepper
389  ,const RCP<const Thyra::VectorBase<Scalar> >& soln
390  ,const RCP<const Thyra::VectorBase<Scalar> >& ee
391  ,int solveStatus)
392 {
393  TEUCHOS_TEST_FOR_EXCEPT(stepControlState_ != MID_STEP);
394  TEUCHOS_TEST_FOR_EXCEPTION(is_null(ee), std::logic_error, "Error, ee == Teuchos::null!\n");
395  ee_ = ee;
396  newtonConvergenceStatus_ = solveStatus;
397  setStepControlState_(AFTER_CORRECTION);
398 }
399 
400 template<class Scalar>
401 void ImplicitBDFStepperStepControl<Scalar>::completeStep(const StepperBase<Scalar>& stepper)
402 {
403  TEUCHOS_TEST_FOR_EXCEPT(stepControlState_ != AFTER_CORRECTION);
404  using Teuchos::as;
405  typedef Teuchos::ScalarTraits<Scalar> ST;
406 
407  numberOfSteps_ ++;
408  nef_ = 0;
409  time_ += hh_;
410  RCP<Teuchos::FancyOStream> out = this->getOStream();
411  Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel();
412  Teuchos::OSTab ostab(out,1,"completeStep_");
413 
414  if ( as<int>(verbLevel) >= as<int>(Teuchos::VERB_HIGH) ) {
415  *out << "numberOfSteps_ = " << numberOfSteps_ << std::endl;
416  *out << "nef_ = " << nef_ << std::endl;
417  *out << "time_ = " << time_ << std::endl;
418  }
419 
420  // Only update the time step if we are NOT running constant stepsize.
421  bool adjustStep = (stepSizeType_ == STEP_TYPE_VARIABLE);
422 
423  Scalar newTimeStep = hh_;
424  Scalar rr = ST::one(); // step size ratio = new step / old step
425  // 03/11/04 tscoffe: Here is the block for choosing order & step-size when
426  // the local error test PASSES (and Newton succeeded).
427  int orderDiff = currentOrder_ - usedOrder_;
428  usedOrder_ = currentOrder_;
429  usedStep_ = hh_;
430  if ((newOrder_ == currentOrder_-1) || (currentOrder_ == maxOrder_)) {
431  // If we reduced our order or reached std::max order then move to the next phase
432  // of integration where we don't automatically double the step-size and
433  // increase the order.
434  initialPhase_ = false;
435  }
436  if ( as<int>(verbLevel) >= as<int>(Teuchos::VERB_HIGH) ) {
437  *out << "initialPhase_ = " << initialPhase_ << std::endl;
438  }
439  if (initialPhase_) {
440  currentOrder_++;
441  newTimeStep = h_phase0_incr_ * hh_;
442  if ( as<int>(verbLevel) >= as<int>(Teuchos::VERB_HIGH) ) {
443  *out << "currentOrder_ = " << currentOrder_ << std::endl;
444  *out << "newTimeStep = " << newTimeStep << std::endl;
445  }
446  } else { // not in the initial phase of integration
447  BDFactionFlag action = ACTION_UNSET;
448  if (newOrder_ == currentOrder_-1) {
449  action = ACTION_LOWER;
450  } else if (newOrder_ == maxOrder_) {
451  action = ACTION_MAINTAIN;
452  } else if ((currentOrder_+1>=nscsco_) || (orderDiff == 1)) {
453  // If we just raised the order last time then we won't raise it again
454  // until we've taken currentOrder_+1 steps at order currentOrder_.
455  action = ACTION_MAINTAIN;
456  } else { // consider changing the order
457  const ImplicitBDFStepper<Scalar>& implicitBDFStepper = Teuchos::dyn_cast<const ImplicitBDFStepper<Scalar> >(stepper);
458  const Thyra::VectorBase<Scalar>& xHistory = implicitBDFStepper.getxHistory(currentOrder_+1);
459  V_StVpStV(delta_.ptr(),ST::one(),*ee_,Scalar(-ST::one()),xHistory);
460  Tkp1_ = wRMSNorm_(*errWtVec_,*delta_);
461  Ekp1_ = Tkp1_/(currentOrder_+2);
462  if ( as<int>(verbLevel) >= as<int>(Teuchos::VERB_HIGH) ) {
463  *out << "delta_ = " << std::endl;
464  delta_->describe(*out,verbLevel);
465  *out << "Tkp1_ = ||delta_||_WRMS = " << Tkp1_ << std::endl;
466  *out << "Ekp1_ = " << Ekp1_ << std::endl;
467  }
468  if (currentOrder_ == 1) {
469  if (Tkp1_ >= Tkp1_Tk_safety_ * Tk_) {
470  action = ACTION_MAINTAIN;
471  } else {
472  action = ACTION_RAISE;
473  }
474  } else {
475  if (Tkm1_ <= std::min(Tk_,Tkp1_)) {
476  action = ACTION_LOWER;
477  } else if (Tkp1_ >= Tk_) {
478  action = ACTION_MAINTAIN;
479  } else {
480  action = ACTION_RAISE;
481  }
482  }
483  }
484  if (currentOrder_ < minOrder_) {
485  action = ACTION_RAISE;
486  } else if ( (currentOrder_ == minOrder_) && (action == ACTION_LOWER) ) {
487  action = ACTION_MAINTAIN;
488  }
489  if (action == ACTION_RAISE) {
490  currentOrder_++;
491  Est_ = Ekp1_;
492  } else if (action == ACTION_LOWER) {
493  currentOrder_--;
494  Est_ = Ekm1_;
495  }
496  newTimeStep = hh_;
497  rr = pow(r_safety_*Est_+r_fudge_,-1.0/(currentOrder_+1.0));
498  if (rr >= r_hincr_test_) {
499  rr = r_hincr_;
500  newTimeStep = rr*hh_;
501  } else if (rr <= 1) {
502  rr = std::max(r_min_,std::min(r_max_,rr));
503  newTimeStep = rr*hh_;
504  }
505  if ( as<int>(verbLevel) >= as<int>(Teuchos::VERB_HIGH) ) {
506  *out << "Est_ = " << Est_ << std::endl;
507  *out << "rr = " << rr << std::endl;
508  *out << "newTimeStep = " << newTimeStep << std::endl;
509  }
510  }
511 
512  if (time_ < stopTime_) {
513  // If the step needs to be adjusted:
514  if (adjustStep) {
515  newTimeStep = std::max(newTimeStep, minTimeStep_);
516  newTimeStep = std::min(newTimeStep, maxTimeStep_);
517 
518  Scalar nextTimePt = time_ + newTimeStep;
519 
520  if (nextTimePt > stopTime_) {
521  nextTimePt = stopTime_;
522  newTimeStep = stopTime_ - time_;
523  }
524 
525  hh_ = newTimeStep;
526 
527  } else { // if time step is constant for this step:
528  Scalar nextTimePt = time_ + hh_;
529 
530  if (nextTimePt > stopTime_) {
531  nextTimePt = stopTime_;
532  hh_ = stopTime_ - time_;
533  }
534  }
535  }
536  if ( as<int>(verbLevel) >= as<int>(Teuchos::VERB_HIGH) ) {
537  *out << "hh_ = " << hh_ << std::endl;
538  *out << "currentOrder_ = " << currentOrder_ << std::endl;
539  }
540  setStepControlState_(READY_FOR_NEXT_STEP);
541 }
542 
543 template<class Scalar>
544 AttemptedStepStatusFlag ImplicitBDFStepperStepControl<Scalar>::rejectStep(const StepperBase<Scalar>& stepper)
545 {
546  TEUCHOS_TEST_FOR_EXCEPT(stepControlState_ != AFTER_CORRECTION);
547 
548  using Teuchos::as;
549 
550  // This routine puts its output in newTimeStep and newOrder
551 
552  // This routine changes the following variables:
553  // initialPhase, nef, psi, newTimeStep,
554  // newOrder, currentOrder_, currentTimeStep, dsDae.xHistory,
555  // dsDae.qHistory, nextTimePt,
556  // currentTimeStepSum, nextTimePt
557 
558  // This routine reads but does not change the following variables:
559  // r_factor, r_safety, Est_, r_fudge_, r_min_, r_max_,
560  // minTimeStep_, maxTimeStep_, time, stopTime_
561 
562  // Only update the time step if we are NOT running constant stepsize.
563  bool adjustStep = (stepSizeType_ == STEP_TYPE_VARIABLE);
564 
565  Scalar newTimeStep = hh_;
566  Scalar rr = 1.0; // step size ratio = new step / old step
567  nef_++;
568  RCP<Teuchos::FancyOStream> out = this->getOStream();
569  Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel();
570  Teuchos::OSTab ostab(out,1,"rejectStep_");
571  if ( as<int>(verbLevel) >= as<int>(Teuchos::VERB_HIGH) ) {
572  *out << "adjustStep = " << adjustStep << std::endl;
573  *out << "nef_ = " << nef_ << std::endl;
574  }
575  if (nef_ >= max_LET_fail_) {
576  TEUCHOS_TEST_FOR_EXCEPTION(nef_ >= max_LET_fail_, std::logic_error, "Error, maximum number of local error test failures.\n");
577  }
578  initialPhase_ = false;
579  if (adjustStep) {
580  if ( as<int>(verbLevel) >= as<int>(Teuchos::VERB_HIGH) ) {
581  *out << "initialPhase_ = " << initialPhase_ << std::endl;
582  }
583  for (int i=1;i<=currentOrder_;++i) {
584  psi_[i-1] = psi_[i] - hh_;
585  }
586 
587  if ((newtonConvergenceStatus_ < 0)) {
589  // rely on the error estimate - it may be full of Nan's.
590  rr = r_min_;
591  newTimeStep = rr * hh_;
592 
593  if (nef_ > 2) {
594  newOrder_ = 1;//consistent with block below.
595  }
596  } else {
597  // 03/11/04 tscoffe: Here is the block for choosing order &
598  // step-size when the local error test FAILS (but Newton
599  // succeeded).
600  if (nef_ == 1) { // first local error test failure
601  rr = r_factor_*pow(r_safety_*Est_+r_fudge_,-1.0/(newOrder_+1.0));
602  rr = std::max(r_min_,std::min(r_max_,rr));
603  newTimeStep = rr * hh_;
604  } else if (nef_ == 2) { // second failure
605  rr = r_min_;
606  newTimeStep = rr * hh_;
607  } else if (nef_ > 2) { // third and later failures
608  newOrder_ = 1;
609  rr = r_min_;
610  newTimeStep = rr * hh_;
611  }
612  }
613  if (newOrder_ >= minOrder_) {
614  currentOrder_ = newOrder_;
615  }
616  if ( as<int>(verbLevel) >= as<int>(Teuchos::VERB_HIGH) ) {
617  *out << "newTimeStep = " << newTimeStep << std::endl;
618  *out << "rr = " << rr << std::endl;
619  *out << "newOrder_ = " << newOrder_ << std::endl;
620  *out << "currentOrder_ = " << currentOrder_ << std::endl;
621  }
622  if (numberOfSteps_ == 0) { // still first step
623  psi_[0] = newTimeStep;
624  if ( as<int>(verbLevel) >= as<int>(Teuchos::VERB_HIGH) ) {
625  *out << "numberOfSteps_ == 0:" << std::endl;
626  *out << "psi_[0] = " << psi_[0] << std::endl;
627  }
628  }
629  } else if (!adjustStep) {
630  if ( as<int>(verbLevel) != as<int>(Teuchos::VERB_NONE) ) {
631  *out << "Rythmos_ImplicitBDFStepperStepControl::rejectStep(...): "
632  << "Warning: Local error test failed with constant step-size."
633  << std::endl;
634  }
635  }
636 
637  AttemptedStepStatusFlag return_status = PREDICT_AGAIN;
638 
639  // If the step needs to be adjusted:
640  if (adjustStep) {
641  newTimeStep = std::max(newTimeStep, minTimeStep_);
642  newTimeStep = std::min(newTimeStep, maxTimeStep_);
643 
644  Scalar nextTimePt = time_ + newTimeStep;
645 
646  if (nextTimePt > stopTime_) {
647  nextTimePt = stopTime_;
648  newTimeStep = stopTime_ - time_;
649  }
650 
651  hh_ = newTimeStep;
652 
653  } else { // if time step is constant for this step:
654  Scalar nextTimePt = time_ + hh_;
655 
656  if (nextTimePt > stopTime_) {
657  nextTimePt = stopTime_;
658  hh_ = stopTime_ - time_;
659  }
660  return_status = CONTINUE_ANYWAY;
661  }
662  if ( as<int>(verbLevel) >= as<int>(Teuchos::VERB_HIGH) ) {
663  *out << "hh_ = " << hh_ << std::endl;
664  }
665 
666  if (return_status == PREDICT_AGAIN) {
667  setStepControlState_(READY_FOR_NEXT_STEP);
668  } else if (return_status == CONTINUE_ANYWAY) {
669  // do nothing, as we'll call completeStep which must be in AFTER_CORRECTION state.
670  }
671 
672  return(return_status);
673 }
674 
675 template<class Scalar>
676 Scalar ImplicitBDFStepperStepControl<Scalar>::checkReduceOrder_(const StepperBase<Scalar>& stepper)
677 {
678  TEUCHOS_TEST_FOR_EXCEPT(stepControlState_ != AFTER_CORRECTION);
679  TEUCHOS_TEST_FOR_EXCEPT(checkReduceOrderCalled_ == true);
680 
681  using Teuchos::as;
682 
683  const ImplicitBDFStepper<Scalar>& implicitBDFStepper = Teuchos::dyn_cast<const ImplicitBDFStepper<Scalar> >(stepper);
684 
685  // This routine puts its output in newOrder_
686 
687  RCP<Teuchos::FancyOStream> out = this->getOStream();
688  Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel();
689  Teuchos::OSTab ostab(out,1,"checkReduceOrder_");
690  if ( as<int>(verbLevel) >= as<int>(Teuchos::VERB_HIGH) ) {
691  *out << "sigma_[" << currentOrder_ << "] = " << sigma_[currentOrder_] << std::endl;
692  *out << "ee_ = " << std::endl;
693  ee_->describe(*out,verbLevel);
694  *out << "errWtVec_ = " << std::endl;
695  errWtVec_->describe(*out,verbLevel);
696  }
697 
698  Scalar enorm = wRMSNorm_(*errWtVec_,*ee_);
699  Ek_ = sigma_[currentOrder_]*enorm;
700  Tk_ = Scalar(currentOrder_+1)*Ek_;
701  Est_ = Ek_;
702  newOrder_ = currentOrder_;
703  if ( as<int>(verbLevel) >= as<int>(Teuchos::VERB_HIGH) ) {
704  *out << "currentOrder_ = " << currentOrder_ << std::endl;
705  *out << "Ek_ = " << Ek_ << std::endl;
706  *out << "Tk_ = " << Tk_ << std::endl;
707  *out << "enorm = " << enorm << std::endl;
708  }
709  if (currentOrder_>1) {
710  const Thyra::VectorBase<Scalar>& xHistoryCur = implicitBDFStepper.getxHistory(currentOrder_);
711  V_VpV(delta_.ptr(),xHistoryCur,*ee_);
712  Ekm1_ = sigma_[currentOrder_-1]*wRMSNorm_(*errWtVec_,*delta_);
713  Tkm1_ = currentOrder_*Ekm1_;
714  if ( as<int>(verbLevel) >= as<int>(Teuchos::VERB_HIGH) ) {
715  *out << "Ekm1_ = " << Ekm1_ << std::endl;
716  *out << "Tkm1_ = " << Tkm1_ << std::endl;
717  }
718  if (currentOrder_>2) {
719  const Thyra::VectorBase<Scalar>& xHistoryPrev = implicitBDFStepper.getxHistory(currentOrder_-1);
720  Vp_V(delta_.ptr(),xHistoryPrev);
721  Ekm2_ = sigma_[currentOrder_-2]*wRMSNorm_(*errWtVec_,*delta_);
722  Tkm2_ = (currentOrder_-1)*Ekm2_;
723  if ( as<int>(verbLevel) >= as<int>(Teuchos::VERB_HIGH) ) {
724  *out << "Ekm2_ = " << Ekm2_ << std::endl;
725  *out << "Tkm2_ = " << Tkm2_ << std::endl;
726  }
727  if (std::max(Tkm1_,Tkm2_)<=Tk_) {
728  newOrder_--;
729  Est_ = Ekm1_;
730  }
731  }
732  else if (Tkm1_ <= Tkm1_Tk_safety_ * Tk_) {
733  newOrder_--;
734  Est_ = Ekm1_;
735  }
736  }
737  if ( as<int>(verbLevel) >= as<int>(Teuchos::VERB_HIGH) ) {
738  *out << "Est_ = " << Est_ << std::endl;
739  *out << "newOrder_= " << newOrder_ << std::endl;
740  }
741  checkReduceOrderCalled_ = true;
742  return(enorm);
743 }
744 
745 template<class Scalar>
746 bool ImplicitBDFStepperStepControl<Scalar>::acceptStep(const StepperBase<Scalar>& stepper, Scalar* LETValue)
747 {
748  TEUCHOS_TEST_FOR_EXCEPT(stepControlState_ != AFTER_CORRECTION);
749  typedef Teuchos::ScalarTraits<Scalar> ST;
750  RCP<Teuchos::FancyOStream> out = this->getOStream();
751  Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel();
752  Teuchos::OSTab ostab(out,1,"acceptStep");
753 
754  bool return_status = false;
755  Scalar enorm = checkReduceOrder_(stepper);
756  Scalar LET = ck_*enorm;
757 
758  if (failStepIfNonlinearSolveFails_ && (newtonConvergenceStatus_ < 0) )
759  return false;
760 
761  if (LETValue) {
762  *LETValue = LET;
763  }
764  if (LET < ST::one()) {
765  return_status = true;
766  }
767  if ( Teuchos::as<int>(verbLevel) >= Teuchos::as<int>(Teuchos::VERB_HIGH) ) {
768  *out << "ck_ = " << ck_ << std::endl;
769  *out << "enorm = " << enorm << std::endl;
770  *out << "Local Truncation Error Check: (ck*enorm) < 1: (" << LET << ") <?= 1" << std::endl;
771  }
772  return(return_status);
773 }
774 
775 template<class Scalar>
776 void ImplicitBDFStepperStepControl<Scalar>::describe(
777  Teuchos::FancyOStream &out,
778  const Teuchos::EVerbosityLevel verbLevel
779  ) const
780 {
781 
782  using Teuchos::as;
783 
784  if ( (as<int>(verbLevel) == as<int>(Teuchos::VERB_DEFAULT) ) ||
785  (as<int>(verbLevel) >= as<int>(Teuchos::VERB_LOW) )
786  ) {
787  out << this->description() << "::describe" << std::endl;
788  }
789  else if (as<int>(verbLevel) >= as<int>(Teuchos::VERB_LOW)) {
790  out << "time_ = " << time_ << std::endl;
791  out << "hh_ = " << hh_ << std::endl;
792  out << "currentOrder_ = " << currentOrder_ << std::endl;
793  }
794  else if (as<int>(verbLevel) >= as<int>(Teuchos::VERB_MEDIUM)) {
795  }
796  else if (as<int>(verbLevel) >= as<int>(Teuchos::VERB_HIGH)) {
797  out << "ee_ = ";
798  if (ee_ == Teuchos::null) {
799  out << "Teuchos::null" << std::endl;
800  } else {
801  ee_->describe(out,verbLevel);
802  }
803  out << "delta_ = ";
804  if (delta_ == Teuchos::null) {
805  out << "Teuchos::null" << std::endl;
806  } else {
807  delta_->describe(out,verbLevel);
808  }
809  out << "errWtVec_ = ";
810  if (errWtVec_ == Teuchos::null) {
811  out << "Teuchos::null" << std::endl;
812  } else {
813  errWtVec_->describe(out,verbLevel);
814  }
815  }
816 }
817 
818 template<class Scalar>
819 void ImplicitBDFStepperStepControl<Scalar>::setParameterList(
820  RCP<Teuchos::ParameterList> const& paramList
821  )
822 {
823 
824  using Teuchos::as;
825  typedef Teuchos::ScalarTraits<Scalar> ST;
826 
827  TEUCHOS_TEST_FOR_EXCEPT(paramList == Teuchos::null);
828  paramList->validateParameters(*this->getValidParameters(),0);
829  parameterList_ = paramList;
830  Teuchos::readVerboseObjectSublist(&*parameterList_,this);
831 
832  minOrder_ = parameterList_->get("minOrder",int(1)); // minimum order
833  TEUCHOS_TEST_FOR_EXCEPTION(
834  !((1 <= minOrder_) && (minOrder_ <= 5)), std::logic_error,
835  "Error, minOrder_ = " << minOrder_ << " is not in range [1,5]!\n"
836  );
837  maxOrder_ = parameterList_->get("maxOrder",int(5)); // maximum order
838  TEUCHOS_TEST_FOR_EXCEPTION(
839  !((1 <= maxOrder_) && (maxOrder_ <= 5)), std::logic_error,
840  "Error, maxOrder_ = " << maxOrder_ << " is not in range [1,5]!\n"
841  );
842 
843  relErrTol_ = parameterList_->get( "relErrTol", Scalar(1.0e-4) );
844  absErrTol_ = parameterList_->get( "absErrTol", Scalar(1.0e-6) );
845  bool constantStepSize = parameterList_->get( "constantStepSize", false );
846  stopTime_ = parameterList_->get( "stopTime", Scalar(1.0) );
847 
848  if (constantStepSize == true) {
849  stepSizeType_ = STEP_TYPE_FIXED;
850  } else {
851  stepSizeType_ = STEP_TYPE_VARIABLE;
852  }
853 
854  failStepIfNonlinearSolveFails_ =
855  parameterList_->get( "failStepIfNonlinearSolveFails", false );
856 
857  RCP<Teuchos::FancyOStream> out = this->getOStream();
858  Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel();
859  Teuchos::OSTab ostab(out,1,"setParameterList");
860  out->precision(15);
861 
862  setDefaultMagicNumbers_(parameterList_->sublist("magicNumbers"));
863 
864  if ( as<int>(verbLevel) >= as<int>(Teuchos::VERB_HIGH) ) {
865  *out << "minOrder_ = " << minOrder_ << std::endl;
866  *out << "maxOrder_ = " << maxOrder_ << std::endl;
867  *out << "relErrTol = " << relErrTol_ << std::endl;
868  *out << "absErrTol = " << absErrTol_ << std::endl;
869  *out << "stepSizeType = " << stepSizeType_ << std::endl;
870  *out << "stopTime_ = " << stopTime_ << std::endl;
871  *out << "failStepIfNonlinearSolveFails_ = "
872  << failStepIfNonlinearSolveFails_ << std::endl;
873  }
874 
875 }
876 
877 template<class Scalar>
878 void ImplicitBDFStepperStepControl<Scalar>::setDefaultMagicNumbers_(
879  Teuchos::ParameterList &magicNumberList)
880 {
881 
882  using Teuchos::as;
883 
884  // Magic Number Defaults:
885  h0_safety_ = magicNumberList.get( "h0_safety", Scalar(2.0) );
886  h0_max_factor_ = magicNumberList.get( "h0_max_factor", Scalar(0.001) );
887  h_phase0_incr_ = magicNumberList.get( "h_phase0_incr", Scalar(2.0) );
888  h_max_inv_ = magicNumberList.get( "h_max_inv", Scalar(0.0) );
889  Tkm1_Tk_safety_ = magicNumberList.get( "Tkm1_Tk_safety", Scalar(2.0) );
890  Tkp1_Tk_safety_ = magicNumberList.get( "Tkp1_Tk_safety", Scalar(0.5) );
891  r_factor_ = magicNumberList.get( "r_factor", Scalar(0.9) );
892  r_safety_ = magicNumberList.get( "r_safety", Scalar(2.0) );
893  r_fudge_ = magicNumberList.get( "r_fudge", Scalar(0.0001) );
894  r_min_ = magicNumberList.get( "r_min", Scalar(0.125) );
895  r_max_ = magicNumberList.get( "r_max", Scalar(0.9) );
896  r_hincr_test_ = magicNumberList.get( "r_hincr_test", Scalar(2.0) );
897  r_hincr_ = magicNumberList.get( "r_hincr", Scalar(2.0) );
898  max_LET_fail_ = magicNumberList.get( "max_LET_fail", int(15) );
899  minTimeStep_ = magicNumberList.get( "minTimeStep", Scalar(0.0) );
900  maxTimeStep_ = magicNumberList.get( "maxTimeStep", Scalar(10.0) );
901 
902  RCP<Teuchos::FancyOStream> out = this->getOStream();
903  Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel();
904  Teuchos::OSTab ostab(out,1,"setDefaultMagicNumbers_");
905  if ( as<int>(verbLevel) >= as<int>(Teuchos::VERB_HIGH) ) {
906  *out << "h0_safety_ = " << h0_safety_ << std::endl;
907  *out << "h0_max_factor_ = " << h0_max_factor_ << std::endl;
908  *out << "h_phase0_incr_ = " << h_phase0_incr_ << std::endl;
909  *out << "h_max_inv_ = " << h_max_inv_ << std::endl;
910  *out << "Tkm1_Tk_safety_ = " << Tkm1_Tk_safety_ << std::endl;
911  *out << "Tkp1_Tk_safety_ = " << Tkp1_Tk_safety_ << std::endl;
912  *out << "r_factor_ = " << r_factor_ << std::endl;
913  *out << "r_safety_ = " << r_safety_ << std::endl;
914  *out << "r_fudge_ = " << r_fudge_ << std::endl;
915  *out << "r_min_ = " << r_min_ << std::endl;
916  *out << "r_max_ = " << r_max_ << std::endl;
917  *out << "r_hincr_test_ = " << r_hincr_test_ << std::endl;
918  *out << "r_hincr_ = " << r_hincr_ << std::endl;
919  *out << "max_LET_fail_ = " << max_LET_fail_ << std::endl;
920  *out << "minTimeStep_ = " << minTimeStep_ << std::endl;
921  *out << "maxTimeStep_ = " << maxTimeStep_ << std::endl;
922  }
923 
924 }
925 
926 template<class Scalar>
927 RCP<const Teuchos::ParameterList>
928 ImplicitBDFStepperStepControl<Scalar>::getValidParameters() const
929 {
930 
931  static RCP<Teuchos::ParameterList> validPL;
932 
933  if (is_null(validPL)) {
934 
935  RCP<Teuchos::ParameterList>
936  pl = Teuchos::parameterList();
937 
938  pl->set<int> ( "minOrder", 1,
939  "lower limit of order selection, guaranteed"
940  );
941  pl->set<int> ( "maxOrder", 5,
942  "upper limit of order selection, does not guarantee this order"
943  );
944  pl->set<Scalar>( "relErrTol", Scalar(1.0e-4) );
945  pl->set<Scalar>( "absErrTol", Scalar(1.0e-6) );
946  pl->set<bool> ( "constantStepSize", false );
947  pl->set<Scalar>( "stopTime", Scalar(10.0) );
948  pl->set<bool>("failStepIfNonlinearSolveFails", false,
949  "Power user command. Will force the function acceptStep() to return false ieven if the LET is acceptable. Used to run with loose tolerances but enforce a correct nonlinear solution to the step.");
950 
951  Teuchos::ParameterList
952  &magicNumberList = pl->sublist("magicNumbers",
953  false,
954  "These are knobs in the algorithm that have been set to reasonable values using lots of testing and heuristics and some theory."
955  );
956  magicNumberList.set<Scalar>( "h0_safety", Scalar(2.0) );
957  magicNumberList.set<Scalar>( "h0_max_factor", Scalar(0.001) );
958  magicNumberList.set<Scalar>( "h_phase0_incr", Scalar(2.0),
959  "initial ramp-up in variable mode (stepSize multiplier) "
960  );
961  magicNumberList.set<Scalar>( "h_max_inv", Scalar(0.0) );
962  magicNumberList.set<Scalar>( "Tkm1_Tk_safety", Scalar(2.0) );
963  magicNumberList.set<Scalar>( "Tkp1_Tk_safety", Scalar(0.5) );
964  magicNumberList.set<Scalar>( "r_factor", Scalar(0.9),
965  "used in rejectStep: time step ratio multiplier"
966  );
967  magicNumberList.set<Scalar>( "r_safety", Scalar(2.0),
968  "local error multiplier as part of time step ratio calculation"
969  );
970  magicNumberList.set<Scalar>( "r_fudge", Scalar(0.0001),
971  "local error addition as part of time step ratio calculation"
972  );
973  magicNumberList.set<Scalar>( "r_min", Scalar(0.125),
974  "used in rejectStep: how much to cut step and lower bound for time step ratio"
975  );
976  magicNumberList.set<Scalar>( "r_max", Scalar(0.9),
977  "upper bound for time step ratio"
978  );
979  magicNumberList.set<Scalar>( "r_hincr_test", Scalar(2.0),
980  "used in completeStep: if time step ratio > this then set time step ratio to r_hincr"
981  );
982  magicNumberList.set<Scalar>( "r_hincr", Scalar(2.0),
983  "used in completeStep: limit on time step ratio increases, not checked by r_max"
984  );
985  magicNumberList.set<int> ( "max_LET_fail", int(15),
986  "Max number of rejected steps"
987  );
988  magicNumberList.set<Scalar>( "minTimeStep", Scalar(0.0),
989  "bound on smallest time step in variable mode."
990  );
991  magicNumberList.set<Scalar>( "maxTimeStep", Scalar(10.0),
992  "bound on largest time step in variable mode."
993  );
994 
995  Teuchos::setupVerboseObjectSublist(&*pl);
996 
997  validPL = pl;
998 
999  }
1000 
1001  return (validPL);
1002 
1003 }
1004 
1005 template<class Scalar>
1006 RCP<Teuchos::ParameterList>
1007 ImplicitBDFStepperStepControl<Scalar>::unsetParameterList()
1008 {
1009  RCP<Teuchos::ParameterList> temp_param_list = parameterList_;
1010  parameterList_ = Teuchos::null;
1011  return(temp_param_list);
1012 }
1013 
1014 template<class Scalar>
1015 RCP<Teuchos::ParameterList>
1016 ImplicitBDFStepperStepControl<Scalar>::getNonconstParameterList()
1017 {
1018  return(parameterList_);
1019 }
1020 
1021 template<class Scalar>
1022 void ImplicitBDFStepperStepControl<Scalar>::setStepControlData(const StepperBase<Scalar>& stepper)
1023 {
1024  if (stepControlState_ == UNINITIALIZED) {
1025  initialize(stepper);
1026  }
1027  const ImplicitBDFStepper<Scalar>& bdfstepper = Teuchos::dyn_cast<const ImplicitBDFStepper<Scalar> >(stepper);
1028  int desiredOrder = bdfstepper.getOrder();
1029  TEUCHOS_TEST_FOR_EXCEPT(!((1 <= desiredOrder) && (desiredOrder <= maxOrder_)));
1030  if (stepControlState_ == BEFORE_FIRST_STEP) {
1031  TEUCHOS_TEST_FOR_EXCEPTION(
1032  desiredOrder > 1,
1033  std::logic_error,
1034  "Error, this ImplicitBDF stepper has not taken a step yet, so it cannot take a step of order " << desiredOrder << " > 1!\n"
1035  );
1036  }
1037  TEUCHOS_TEST_FOR_EXCEPT(!(desiredOrder <= currentOrder_+1));
1038  currentOrder_ = desiredOrder;
1039 
1040  using Teuchos::as;
1041  RCP<Teuchos::FancyOStream> out = this->getOStream();
1042  Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel();
1043  Teuchos::OSTab ostab(out,1,"setStepControlData");
1044 
1045  if ( as<int>(verbLevel) >= as<int>(Teuchos::VERB_EXTREME) ) {
1046  *out << "currentOrder_ = " << currentOrder_ << std::endl;
1047  }
1048 }
1049 
1050 template<class Scalar>
1051 bool ImplicitBDFStepperStepControl<Scalar>::supportsCloning() const
1052 {
1053  return true;
1054 }
1055 
1056 
1057 template<class Scalar>
1058 RCP<StepControlStrategyBase<Scalar> >
1059 ImplicitBDFStepperStepControl<Scalar>::cloneStepControlStrategyAlgorithm() const
1060 {
1061 
1062  RCP<ImplicitBDFStepperStepControl<Scalar> > stepControl = rcp(new ImplicitBDFStepperStepControl<Scalar>());
1063 
1064  if (!is_null(parameterList_)) {
1065  stepControl->setParameterList(parameterList_);
1066  }
1067 
1068  return stepControl;
1069 }
1070 
1071 template<class Scalar>
1072 void ImplicitBDFStepperStepControl<Scalar>::setErrWtVecCalc(const RCP<ErrWtVecCalcBase<Scalar> >& errWtVecCalc)
1073 {
1074  TEUCHOS_TEST_FOR_EXCEPT(is_null(errWtVecCalc));
1075  errWtVecCalc_ = errWtVecCalc;
1076 }
1077 
1078 template<class Scalar>
1079 RCP<const ErrWtVecCalcBase<Scalar> > ImplicitBDFStepperStepControl<Scalar>::getErrWtVecCalc() const
1080 {
1081  return(errWtVecCalc_);
1082 }
1083 
1084 template<class Scalar>
1085 Scalar ImplicitBDFStepperStepControl<Scalar>::wRMSNorm_(
1086  const Thyra::VectorBase<Scalar>& weight,
1087  const Thyra::VectorBase<Scalar>& vector) const
1088 {
1089  return(norm_2(weight,vector));
1090 }
1091 
1092 template<class Scalar>
1093 void ImplicitBDFStepperStepControl<Scalar>::defaultInitializeAllData_()
1094 {
1095  typedef Teuchos::ScalarTraits<Scalar> ST;
1096  Scalar zero = ST::zero();
1097  Scalar mone = Scalar(-ST::one());
1098 
1099  stepControlState_ = UNINITIALIZED;
1100  hh_ = zero;
1101  numberOfSteps_ = 0;
1102  stepSizeType_ = STEP_TYPE_VARIABLE;
1103 
1104  minOrder_ = -1;
1105  maxOrder_ = -1;
1106  nef_ = 0;
1107  midStep_ = false;
1108  checkReduceOrderCalled_ = false;
1109  time_ = -std::numeric_limits<Scalar>::max();
1110  relErrTol_ = mone;
1111  absErrTol_ = mone;
1112  usedStep_ = mone;
1113  currentOrder_ = 1;
1114  usedOrder_ = -1;
1115  nscsco_ = -1;
1116  alpha_s_ = mone;
1117  alpha_0_ = mone;
1118  cj_ = mone;
1119  ck_ = mone;
1120  ck_enorm_ = mone;
1121  constantStepSize_ = false;
1122  Ek_ = mone;
1123  Ekm1_ = mone;
1124  Ekm2_ = mone;
1125  Ekp1_ = mone;
1126  Est_ = mone;
1127  Tk_ = mone;
1128  Tkm1_ = mone;
1129  Tkm2_ = mone;
1130  Tkp1_ = mone;
1131  newOrder_ = -1;
1132  oldOrder_ = -1;
1133  initialPhase_ = false;
1134  stopTime_ = mone;
1135  h0_safety_ = mone;
1136  h0_max_factor_ = mone;
1137  h_phase0_incr_ = mone;
1138  h_max_inv_ = mone;
1139  Tkm1_Tk_safety_ = mone;
1140  Tkp1_Tk_safety_ = mone;
1141  r_factor_ = mone;
1142  r_safety_ = mone;
1143  r_fudge_ = mone;
1144  r_min_ = mone;
1145  r_max_ = mone;
1146  r_hincr_test_ = mone;
1147  r_hincr_ = mone;
1148  max_LET_fail_ = -1;
1149  minTimeStep_ = mone;
1150  maxTimeStep_ = mone;
1151  newtonConvergenceStatus_ = -1;
1152 }
1153 
1154 template<class Scalar>
1155 int ImplicitBDFStepperStepControl<Scalar>::getMinOrder() const
1156 {
1157  TEUCHOS_TEST_FOR_EXCEPTION(
1158  stepControlState_ == UNINITIALIZED, std::logic_error,
1159  "Error, attempting to call getMinOrder before intiialization!\n"
1160  );
1161  return(minOrder_);
1162 }
1163 
1164 template<class Scalar>
1165 int ImplicitBDFStepperStepControl<Scalar>::getMaxOrder() const
1166 {
1167  TEUCHOS_TEST_FOR_EXCEPTION(
1168  stepControlState_ == UNINITIALIZED, std::logic_error,
1169  "Error, attempting to call getMaxOrder before initialization!\n"
1170  );
1171  return(maxOrder_);
1172 }
1173 
1174 //
1175 // Explicit Instantiation macro
1176 //
1177 // Must be expanded from within the Rythmos namespace!
1178 //
1179 
1180 #define RYTHMOS_IMPLICITBDF_STEPPER_STEPCONTROL_INSTANT(SCALAR) \
1181  template class ImplicitBDFStepperStepControl< SCALAR >;
1182 
1183 
1184 } // namespace Rythmos
1185 #endif // Rythmos_IMPLICITBDF_STEPPER_STEP_CONTROL_DEF_H
1186