Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Tempus_StepperDIRK_impl.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_StepperDIRK_impl_hpp
10 #define Tempus_StepperDIRK_impl_hpp
11 
12 #include "Tempus_config.hpp"
14 #include "Tempus_WrapperModelEvaluatorBasic.hpp"
15 #include "Teuchos_VerboseObjectParameterListHelpers.hpp"
16 #include "Thyra_VectorStdOps.hpp"
17 #include "NOX_Thyra.H"
18 
19 
20 namespace Tempus {
21 
22 // Forward Declaration for recursive includes (this Stepper <--> StepperFactory)
23 template<class Scalar> class StepperFactory;
24 
25 template<class Scalar>
27 {
28  this->setUseFSAL( this->getUseFSALDefault());
29  this->setICConsistency( this->getICConsistencyDefault());
30  this->setICConsistencyCheck( this->getICConsistencyCheckDefault());
31  this->setUseEmbedded( this->getUseEmbeddedDefault());
32  this->setZeroInitialGuess( false);
33 
34  this->setStageNumber(-1);
35 
36 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
37  stepperObserver_ = Teuchos::rcp(new StepperRKObserverComposite<Scalar>());
38 #endif
39  this->setAppAction(Teuchos::null);
40  this->setDefaultSolver();
41 }
42 
43 
44 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
45 template<class Scalar>
47  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> >& appModel,
48  const Teuchos::RCP<StepperRKObserver<Scalar> >& obs,
49  const Teuchos::RCP<Thyra::NonlinearSolverBase<Scalar> >& solver,
50  bool useFSAL,
51  std::string ICConsistency,
52  bool ICConsistencyCheck,
53  bool useEmbedded,
54  bool zeroInitialGuess)
55 {
56  this->setUseFSAL( useFSAL);
57  this->setICConsistency( ICConsistency);
58  this->setICConsistencyCheck( ICConsistencyCheck);
59  this->setUseEmbedded( useEmbedded);
60  this->setZeroInitialGuess( zeroInitialGuess);
61 
62  this->setStageNumber(-1);
63 
64  stepperObserver_ = Teuchos::rcp(new StepperRKObserverComposite<Scalar>());
65  this->setObserver(obs);
66  this->setAppAction(Teuchos::null);
67  this->setSolver(solver);
68 
69  if (appModel != Teuchos::null) {
70  this->setModel(appModel);
71  this->initialize();
72  }
73 }
74 #endif
75 template<class Scalar>
77  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> >& appModel,
78  const Teuchos::RCP<Thyra::NonlinearSolverBase<Scalar> >& solver,
79  bool useFSAL,
80  std::string ICConsistency,
81  bool ICConsistencyCheck,
82  bool useEmbedded,
83  bool zeroInitialGuess,
84  const Teuchos::RCP<StepperRKAppAction<Scalar> >& stepperRKAppAction)
85 {
86  this->setUseFSAL( useFSAL);
87  this->setICConsistency( ICConsistency);
88  this->setICConsistencyCheck( ICConsistencyCheck);
89  this->setUseEmbedded( useEmbedded);
90  this->setZeroInitialGuess( zeroInitialGuess);
91 
92  this->setStageNumber(-1);
93 
94 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
95  stepperObserver_ = Teuchos::rcp(new StepperRKObserverComposite<Scalar>());
96  this->setObserver(Teuchos::null);
97 #endif
98  this->setAppAction(stepperRKAppAction);
99  this->setSolver(solver);
100 
101  if (appModel != Teuchos::null) {
102  this->setModel(appModel);
103  this->initialize();
104  }
105 }
106 
107 
108 template<class Scalar>
110  Teuchos::RCP<Teuchos::ParameterList> pl) const
111 {
112  getValidParametersBasic(pl, this->getStepperType());
113  pl->set<bool>("Use Embedded", false,
114  "'Whether to use Embedded Stepper (if available) or not\n"
115  " 'true' - Stepper will compute embedded solution and is adaptive.\n"
116  " 'false' - Stepper is not embedded(adaptive).\n");
117  pl->set<std::string>("Description", this->getDescription());
118  pl->set<std::string>("Solver Name", "Default Solver",
119  "Name of ParameterList containing the solver specifications.");
120  pl->set<bool>("Zero Initial Guess", false);
121  pl->set<bool>("Reset Initial Guess", true);
122  Teuchos::RCP<Teuchos::ParameterList> solverPL = defaultSolverParameters();
123  pl->set("Default Solver", *solverPL);
124 }
125 
126 
127 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
128 template<class Scalar>
130  Teuchos::RCP<StepperObserver<Scalar> > obs)
131 {
132  if (this->stepperObserver_ == Teuchos::null)
133  this->stepperObserver_ =
134  Teuchos::rcp(new StepperRKObserverComposite<Scalar>());
135 
136  if (( obs == Teuchos::null ) and (this->stepperObserver_->getSize() >0 ) )
137  return;
138 
139  if (( obs == Teuchos::null ) and (this->stepperObserver_->getSize() == 0) )
140  obs = Teuchos::rcp(new StepperRKObserver<Scalar>());
141 
142  // Check that this casts to prevent a runtime error if it doesn't
143  if (Teuchos::rcp_dynamic_cast<StepperRKObserver<Scalar> > (obs) != Teuchos::null) {
144  this->stepperObserver_->addObserver(
145  Teuchos::rcp_dynamic_cast<StepperRKObserver<Scalar> > (obs, true) );
146  } else {
147  Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
148  Teuchos::OSTab ostab(out,0,"setObserver");
149  *out << "Tempus::StepperIMEX_RK::setObserver: Warning: An observer has been provided that";
150  *out << " does not support Tempus::StepperRKObserver. This observer WILL NOT be added.";
151  *out << " In the future, this will result in a runtime error!" << std::endl;
152  }
153 
154  this->isInitialized_ = false;
155 }
156 #endif
157 
158 
159 template<class Scalar>
161 {
162  // Initialize the stage vectors
163  const int numStages = this->tableau_->numStages();
164  this->stageX_ = this->wrapperModel_->getNominalValues().get_x()->clone_v();
165  stageXDot_.resize(numStages);
166  for (int i=0; i<numStages; ++i) {
167  stageXDot_[i] = Thyra::createMember(this->wrapperModel_->get_f_space());
168  assign(stageXDot_[i].ptr(), Teuchos::ScalarTraits<Scalar>::zero());
169  }
170  xTilde_ = Thyra::createMember(this->wrapperModel_->get_x_space());
171  assign(xTilde_.ptr(), Teuchos::ScalarTraits<Scalar>::zero());
172 
173  if (this->tableau_->isEmbedded() and this->getUseEmbedded()) {
174  ee_ = Thyra::createMember(this->wrapperModel_->get_f_space());
175  abs_u0 = Thyra::createMember(this->wrapperModel_->get_f_space());
176  abs_u = Thyra::createMember(this->wrapperModel_->get_f_space());
177  sc = Thyra::createMember(this->wrapperModel_->get_f_space());
178  }
179 
181 }
182 
183 
184 template<class Scalar>
186  const Teuchos::RCP<SolutionHistory<Scalar> >& solutionHistory)
187 {
188  using Teuchos::RCP;
189 
190  RCP<SolutionState<Scalar> > initialState = solutionHistory->getCurrentState();
191 
192  // Check if we need Stepper storage for xDot
193  if (initialState->getXDot() == Teuchos::null)
194  this->setStepperXDot(stageXDot_.back());
195 
197 }
198 
199 
200 template<class Scalar>
202  const Teuchos::RCP<SolutionHistory<Scalar> >& solutionHistory)
203 {
204  this->checkInitialized();
205 
206  using Teuchos::RCP;
207 
208  TEMPUS_FUNC_TIME_MONITOR("Tempus::StepperDIRK::takeStep()");
209  {
210  TEUCHOS_TEST_FOR_EXCEPTION(solutionHistory->getNumStates() < 2,
211  std::logic_error,
212  "Error - StepperDIRK<Scalar>::takeStep(...)\n"
213  "Need at least two SolutionStates for DIRK.\n"
214  " Number of States = " << solutionHistory->getNumStates() << "\n"
215  "Try setting in \"Solution History\" \"Storage Type\" = \"Undo\"\n"
216  " or \"Storage Type\" = \"Static\" and \"Storage Limit\" = \"2\"\n");
217 
218 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
219  this->stepperObserver_->observeBeginTakeStep(solutionHistory, *this);
220 #endif
221  RCP<StepperDIRK<Scalar> > thisStepper = Teuchos::rcpFromRef(*this);
222  this->stepperRKAppAction_->execute(solutionHistory, thisStepper,
224 
225  RCP<SolutionState<Scalar> > currentState=solutionHistory->getCurrentState();
226  RCP<SolutionState<Scalar> > workingState=solutionHistory->getWorkingState();
227  const Scalar dt = workingState->getTimeStep();
228  const Scalar time = currentState->getTime();
229 
230  const int numStages = this->tableau_->numStages();
231  Teuchos::SerialDenseMatrix<int,Scalar> A = this->tableau_->A();
232  Teuchos::SerialDenseVector<int,Scalar> b = this->tableau_->b();
233  Teuchos::SerialDenseVector<int,Scalar> c = this->tableau_->c();
234 
235  // Reset non-zero initial guess.
236  if ( this->getResetInitialGuess() && (!this->getZeroInitialGuess()) )
237  Thyra::assign(this->stageX_.ptr(), *(currentState->getX()));
238 
239  // Compute stage solutions
240  bool pass = true;
241  Thyra::SolveStatus<Scalar> sStatus;
242  for (int i=0; i < numStages; ++i) {
243  this->setStageNumber(i);
244 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
245  this->stepperObserver_->observeBeginStage(solutionHistory, *this);
246 
247  // ???: is it a good idea to leave this (no-op) here?
248  this->stepperObserver_
249  ->observeBeforeImplicitExplicitly(solutionHistory, *this);
250 #endif
251 
252  // Check if stageXDot_[i] is needed.
253  bool isNeeded = false;
254  for (int k=i+1; k<numStages; ++k) if (A(k,i) != 0.0) isNeeded = true;
255  if (b(i) != 0.0) isNeeded = true;
256  if (this->tableau_->isEmbedded() && this->getUseEmbedded() &&
257  this->tableau_->bstar()(i) != 0.0)
258  isNeeded = true;
259  if (isNeeded == false) {
260  assign(stageXDot_[i].ptr(), Teuchos::ScalarTraits<Scalar>::zero());
261  continue;
262  }
263 
264  Thyra::assign(xTilde_.ptr(), *(currentState->getX()));
265  for (int j=0; j < i; ++j) {
266  if (A(i,j) != Teuchos::ScalarTraits<Scalar>::zero()) {
267  Thyra::Vp_StV(xTilde_.ptr(), dt*A(i,j), *(stageXDot_[j]));
268  }
269  }
270 
271  this->stepperRKAppAction_->execute(solutionHistory, thisStepper,
273 
274  Scalar ts = time + c(i)*dt;
275  if (A(i,i) == Teuchos::ScalarTraits<Scalar>::zero()) {
276  // Explicit stage for the ImplicitODE_DAE
277  if (i == 0 && this->getUseFSAL() &&
278  workingState->getNConsecutiveFailures() == 0) {
279  // Reuse last evaluation for first step
280  RCP<Thyra::VectorBase<Scalar> > tmp = stageXDot_[0];
281  stageXDot_[0] = stageXDot_.back();
282  stageXDot_.back() = tmp;
283  } else {
284  // Calculate explicit stage
285  typedef Thyra::ModelEvaluatorBase MEB;
286  MEB::InArgs<Scalar> inArgs = this->wrapperModel_->getInArgs();
287  MEB::OutArgs<Scalar> outArgs = this->wrapperModel_->getOutArgs();
288  inArgs.set_x(xTilde_);
289  if (inArgs.supports(MEB::IN_ARG_t)) inArgs.set_t(ts);
290  if (inArgs.supports(MEB::IN_ARG_x_dot))
291  inArgs.set_x_dot(Teuchos::null);
292  outArgs.set_f(stageXDot_[i]);
293 
294 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
295  this->stepperObserver_->observeBeforeExplicit(solutionHistory, *this);
296 #endif
297  this->wrapperModel_->getAppModel()->evalModel(inArgs,outArgs);
298  }
299  } else {
300  // Implicit stage for the ImplicitODE_DAE
301  const Scalar alpha = 1.0/(dt*A(i,i));
302  const Scalar beta = 1.0;
303 
304  // Setup TimeDerivative
305  Teuchos::RCP<TimeDerivative<Scalar> > timeDer =
306  Teuchos::rcp(new StepperDIRKTimeDerivative<Scalar>(
307  alpha,xTilde_.getConst()));
308 
309  auto p = Teuchos::rcp(new ImplicitODEParameters<Scalar>(
310  timeDer, dt, alpha, beta, SOLVE_FOR_X, i));
311 
312 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
313  this->stepperObserver_->observeBeforeSolve(solutionHistory, *this);
314 #endif
315  this->stepperRKAppAction_->execute(solutionHistory, thisStepper,
317 
318  sStatus = this->solveImplicitODE(this->stageX_, stageXDot_[i], ts, p);
319 
320  if (sStatus.solveStatus != Thyra::SOLVE_STATUS_CONVERGED) pass=false;
321 
322 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
323  this->stepperObserver_->observeAfterSolve(solutionHistory, *this);
324 #endif
325  this->stepperRKAppAction_->execute(solutionHistory, thisStepper,
327 
328  timeDer->compute(this->stageX_, stageXDot_[i]);
329  }
330 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
331  this->stepperObserver_->observeEndStage(solutionHistory, *this);
332 #endif
333  this->stepperRKAppAction_->execute(solutionHistory, thisStepper,
335  this->stepperRKAppAction_->execute(solutionHistory, thisStepper,
337  }
338 
339  // Sum for solution: x_n = x_n-1 + Sum{ dt*b(i) * f(i) }
340  Thyra::assign((workingState->getX()).ptr(), *(currentState->getX()));
341  for (int i=0; i < numStages; ++i) {
342  if (b(i) != Teuchos::ScalarTraits<Scalar>::zero()) {
343  Thyra::Vp_StV((workingState->getX()).ptr(), dt*b(i), *(stageXDot_[i]));
344  }
345  }
346 
347  if (this->tableau_->isEmbedded() and this->getUseEmbedded()) {
348  const Scalar tolRel = workingState->getTolRel();
349  const Scalar tolAbs = workingState->getTolAbs();
350 
351  // just compute the error weight vector
352  // (all that is needed is the error, and not the embedded solution)
353  Teuchos::SerialDenseVector<int,Scalar> errWght = b ;
354  errWght -= this->tableau_->bstar();
355 
356  // compute local truncation error estimate: | u^{n+1} - \hat{u}^{n+1} |
357  // Sum for solution: ee_n = Sum{ (b(i) - bstar(i)) * dt*f(i) }
358  assign(ee_.ptr(), Teuchos::ScalarTraits<Scalar>::zero());
359  for (int i=0; i < numStages; ++i) {
360  if (errWght(i) != Teuchos::ScalarTraits<Scalar>::zero()) {
361  Thyra::Vp_StV(ee_.ptr(), dt*errWght(i), *(stageXDot_[i]));
362  }
363  }
364 
365  // compute: Atol + max(|u^n|, |u^{n+1}| ) * Rtol
366  Thyra::abs( *(currentState->getX()), abs_u0.ptr());
367  Thyra::abs( *(workingState->getX()), abs_u.ptr());
368  Thyra::pair_wise_max_update(tolRel, *abs_u0, abs_u.ptr());
369  Thyra::add_scalar(tolAbs, abs_u.ptr());
370 
371  // compute: || ee / sc ||
372  assign(sc.ptr(), Teuchos::ScalarTraits<Scalar>::zero());
373  Thyra::ele_wise_divide(Teuchos::as<Scalar>(1.0), *ee_, *abs_u, sc.ptr());
374  Scalar err = std::abs(Thyra::norm_inf(*sc));
375  workingState->setErrorRel(err);
376 
377  // test if step should be rejected
378  if (std::isinf(err) || std::isnan(err) || err > Teuchos::as<Scalar>(1.0))
379  pass = false;
380  }
381 
382  if (pass) workingState->setSolutionStatus(Status::PASSED);
383  else workingState->setSolutionStatus(Status::FAILED);
384 
385  workingState->setOrder(this->getOrder());
386  workingState->computeNorms(currentState);
387 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
388  this->stepperObserver_->observeEndTakeStep(solutionHistory, *this);
389 #endif
390  this->stepperRKAppAction_->execute(solutionHistory, thisStepper,
392  }
393  // reset the stage number
394  this->setStageNumber(-1);
395  return;
396 }
397 
398 /** \brief Provide a StepperState to the SolutionState.
399  * This Stepper does not have any special state data,
400  * so just provide the base class StepperState with the
401  * Stepper description. This can be checked to ensure
402  * that the input StepperState can be used by this Stepper.
403  */
404 template<class Scalar>
405 Teuchos::RCP<Tempus::StepperState<Scalar> >
408 {
409  Teuchos::RCP<Tempus::StepperState<Scalar> > stepperState =
410  rcp(new StepperState<Scalar>(this->getStepperType()));
411  return stepperState;
412 }
413 
414 
415 template<class Scalar>
417  Teuchos::FancyOStream &out,
418  const Teuchos::EVerbosityLevel verbLevel) const
419 {
420  out << std::endl;
421  Stepper<Scalar>::describe(out, verbLevel);
422  StepperImplicit<Scalar>::describe(out, verbLevel);
423 
424  out << "--- StepperDIRK ---\n";
425  out << " tableau_ = " << this->tableau_ << std::endl;
426  if (this->tableau_ != Teuchos::null) this->tableau_->describe(out, verbLevel);
427 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
428  out << " stepperObserver_ = " << stepperObserver_ << std::endl;
429 #endif
430  out << " stepperRKAppAction_= " << this->stepperRKAppAction_ << std::endl;
431  out << " xTilde_ = " << xTilde_ << std::endl;
432  out << " stageX_ = " << this->stageX_ << std::endl;
433  out << " stageXDot_.size() = " << stageXDot_.size() << std::endl;
434  const int numStages = stageXDot_.size();
435  for (int i=0; i<numStages; ++i)
436  out << " stageXDot_["<<i<<"] = " << stageXDot_[i] << std::endl;
437  out << " useEmbedded_ = "
438  << Teuchos::toString(useEmbedded_) << std::endl;
439  out << " ee_ = " << ee_ << std::endl;
440  out << " abs_u0 = " << abs_u0 << std::endl;
441  out << " abs_u = " << abs_u << std::endl;
442  out << " sc = " << sc << std::endl;
443  out << "-------------------" << std::endl;
444 }
445 
446 
447 template<class Scalar>
448 bool StepperDIRK<Scalar>::isValidSetup(Teuchos::FancyOStream & out) const
449 {
450  bool isValidSetup = true;
451 
452  if ( !Stepper<Scalar>::isValidSetup(out) ) isValidSetup = false;
453  if ( !StepperImplicit<Scalar>::isValidSetup(out) ) isValidSetup = false;
454 
455  if (this->tableau_ == Teuchos::null) {
456  isValidSetup = false;
457  out << "The tableau is not set!\n";
458  }
459 
460 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
461  if (stepperObserver_ == Teuchos::null) {
462  isValidSetup = false;
463  out << "The observer is not set!\n";
464  }
465 #endif
466  if (this->stepperRKAppAction_ == Teuchos::null) {
467  isValidSetup = false;
468  out << "The AppAction is not set!\n";
469  }
470 
471  return isValidSetup;
472 }
473 
474 
475 template<class Scalar>
476 Teuchos::RCP<const Teuchos::ParameterList>
478 {
479  Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
480  this->getValidParametersBasicDIRK(pl);
481 
482  return pl;
483 }
484 
485 
486 } // namespace Tempus
487 #endif // Tempus_StepperDIRK_impl_hpp
virtual void setInitialConditions(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Set the initial conditions and make them consistent.
virtual Teuchos::RCP< Tempus::StepperState< Scalar > > getDefaultStepperState()
Get a default (initial) StepperState.
const std::string toString(const Status status)
Convert Status to string.
virtual void setup(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &wrapperModel, const Teuchos::RCP< StepperRKObserver< Scalar > > &obs, const Teuchos::RCP< Thyra::NonlinearSolverBase< Scalar > > &solver, bool useFSAL, std::string ICConsistency, bool ICConsistencyCheck, bool useEmbedded, bool zeroInitialGuess)
Setup for constructor.
void getValidParametersBasicDIRK(Teuchos::RCP< Teuchos::ParameterList > pl) const
Teuchos::RCP< Teuchos::ParameterList > defaultSolverParameters()
Returns the default solver ParameterList for implicit Steppers.
virtual void initialize()
Initialize after construction and changing input parameters.
Thyra Base interface for time steppers.
virtual bool isValidSetup(Teuchos::FancyOStream &out) const
StepperState is a simple class to hold state information about the stepper.
Thyra Base interface for implicit time steppers.
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
Application Action for StepperRKBase.
virtual void setInitialConditions(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Set the initial conditions and make them consistent.
virtual void initialize()
Initialize after construction and changing input parameters.
Time-derivative interface for DIRK.
virtual void setupDefault()
Default setup for constructor.
StepperObserver class for Stepper class.
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
virtual void setObserver(Teuchos::RCP< StepperObserver< Scalar > > obs=Teuchos::null)
Set Observer.
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
StepperRKObserver class for StepperRK.
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
Solve for x and determine xDot from x.
void getValidParametersBasic(Teuchos::RCP< Teuchos::ParameterList > pl, std::string stepperType)
Provide basic parameters to Steppers.
virtual void takeStep(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Take the specified timestep, dt, and return true if successful.