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_RKButcherTableauBuilder.hpp"
13 #include "Tempus_config.hpp"
15 #include "Tempus_WrapperModelEvaluatorBasic.hpp"
16 #include "Teuchos_VerboseObjectParameterListHelpers.hpp"
17 #include "Thyra_VectorStdOps.hpp"
18 #include "NOX_Thyra.H"
19 
20 
21 namespace Tempus {
22 
23 // Forward Declaration for recursive includes (this Stepper <--> StepperFactory)
24 template<class Scalar> class StepperFactory;
25 
26 template<class Scalar>
28 {
29  this->setTableau();
30  this->setParameterList(Teuchos::null);
31  this->modelWarning();
32 }
33 
34 template<class Scalar>
36  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> >& appModel,
37  Teuchos::RCP<Teuchos::ParameterList> pList)
38 {
39  this->setTableau(pList);
40  this->setParameterList(pList);
41 
42  if (appModel == Teuchos::null) {
43  this->modelWarning();
44  }
45  else {
46  this->setModel(appModel);
47  this->initialize();
48  }
49 }
50 
51 template<class Scalar>
53  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> >& appModel,
54  std::string stepperType)
55 {
56  this->setTableau(stepperType);
57 
58  if (appModel == Teuchos::null) {
59  this->modelWarning();
60  }
61  else {
62  this->setModel(appModel);
63  this->initialize();
64  }
65 }
66 
67 template<class Scalar>
69  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> >& appModel,
70  std::string stepperType,
71  Teuchos::RCP<Teuchos::ParameterList> pList)
72 {
73  this->setTableau(stepperType);
74  this->setParameterList(pList);
75 
76  if (appModel == Teuchos::null) {
77  this->modelWarning();
78  }
79  else {
80  this->setModel(appModel);
81  this->initialize();
82  }
83 }
84 
85 
86 template<class Scalar>
87 void StepperDIRK<Scalar>::setTableau(std::string stepperType)
88 {
89  if (stepperType == "") {
90  this->setTableau();
91  } else {
92  Teuchos::RCP<const RKButcherTableau<Scalar> > DIRK_ButcherTableau =
93  createRKBT<Scalar>(stepperType, this->stepperPL_);
94  this->setTableau(DIRK_ButcherTableau);
95  }
96 }
97 
98 
99 template<class Scalar>
100 void StepperDIRK<Scalar>::setTableau(Teuchos::RCP<Teuchos::ParameterList> pList)
101 {
102  if (pList == Teuchos::null) {
103  // Create default parameters if null, otherwise keep current parameters.
104  if (this->stepperPL_ == Teuchos::null)
105  this->stepperPL_ = this->getDefaultParameters();
106  } else {
107  this->stepperPL_ = pList;
108  }
109 
110  std::string stepperType =
111  this->stepperPL_->template get<std::string>("Stepper Type",
112  "SDIRK 2 Stage 2nd order");
113  Teuchos::RCP<const RKButcherTableau<Scalar> > DIRK_ButcherTableau =
114  createRKBT<Scalar>(stepperType, this->stepperPL_);
115  this->setTableau(DIRK_ButcherTableau);
116 }
117 
118 
119 template<class Scalar>
121  Teuchos::RCP<const RKButcherTableau<Scalar> > DIRK_ButcherTableau)
122 {
123  DIRK_ButcherTableau_ = DIRK_ButcherTableau;
124 
125  TEUCHOS_TEST_FOR_EXCEPTION(DIRK_ButcherTableau_->isDIRK() != true,
126  std::logic_error,
127  "Error - StepperDIRK do not received a DIRK Butcher Tableau!\n" <<
128  " Tableau = " << DIRK_ButcherTableau_->description() << "\n");
129 }
130 
131 
132 template<class Scalar>
134  Teuchos::RCP<StepperObserver<Scalar> > obs)
135 {
136  if (obs == Teuchos::null) {
137  // Create default observer, otherwise keep current observer.
138  if (this->stepperObserver_ == Teuchos::null) {
139  stepperDIRKObserver_ =
140  Teuchos::rcp(new StepperDIRKObserver<Scalar>());
141  this->stepperObserver_ =
142  Teuchos::rcp_dynamic_cast<StepperObserver<Scalar> >
143  (stepperDIRKObserver_);
144  }
145  } else {
146  this->stepperObserver_ = obs;
147  stepperDIRKObserver_ =
148  Teuchos::rcp_dynamic_cast<StepperDIRKObserver<Scalar> >(this->stepperObserver_);
149  }
150 }
151 
152 
153 template<class Scalar>
155 {
156  TEUCHOS_TEST_FOR_EXCEPTION( this->wrapperModel_ == Teuchos::null,
157  std::logic_error,
158  "Error - Need to set the model, setModel(), before calling "
159  "StepperDIRK::initialize()\n");
160 
161  this->setTableau(this->stepperPL_);
162  this->setParameterList(this->stepperPL_);
163  this->setSolver();
164  this->setObserver();
165 
166  // Initialize the stage vectors
167  const int numStages = DIRK_ButcherTableau_->numStages();
168  stageX_ = this->wrapperModel_->getNominalValues().get_x()->clone_v();
169  stageXDot_.resize(numStages);
170  for (int i=0; i<numStages; ++i) {
171  stageXDot_[i] = Thyra::createMember(this->wrapperModel_->get_f_space());
172  assign(stageXDot_[i].ptr(), Teuchos::ScalarTraits<Scalar>::zero());
173  }
174  xTilde_ = Thyra::createMember(this->wrapperModel_->get_x_space());
175  assign(xTilde_.ptr(), Teuchos::ScalarTraits<Scalar>::zero());
176 
177  if (DIRK_ButcherTableau_->isEmbedded() and this->getEmbedded()) {
178  ee_ = Thyra::createMember(this->wrapperModel_->get_f_space());
179  abs_u0 = Thyra::createMember(this->wrapperModel_->get_f_space());
180  abs_u = Thyra::createMember(this->wrapperModel_->get_f_space());
181  sc = Thyra::createMember(this->wrapperModel_->get_f_space());
182  }
183 }
184 
185 
186 template<class Scalar>
188  const Teuchos::RCP<SolutionHistory<Scalar> >& solutionHistory)
189 {
190  using Teuchos::RCP;
191 
192  RCP<SolutionState<Scalar> > initialState = solutionHistory->getCurrentState();
193 
194  // Check if we need Stepper storage for xDot
195  if (initialState->getXDot() == Teuchos::null)
196  this->setStepperXDot(stageXDot_.back());
197 
199 }
200 
201 
202 template<class Scalar>
204  const Teuchos::RCP<SolutionHistory<Scalar> >& solutionHistory)
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  this->stepperObserver_->observeBeginTakeStep(solutionHistory, *this);
219  RCP<SolutionState<Scalar> > currentState=solutionHistory->getCurrentState();
220  RCP<SolutionState<Scalar> > workingState=solutionHistory->getWorkingState();
221  const Scalar dt = workingState->getTimeStep();
222  const Scalar time = currentState->getTime();
223 
224  const int numStages = DIRK_ButcherTableau_->numStages();
225  Teuchos::SerialDenseMatrix<int,Scalar> A = DIRK_ButcherTableau_->A();
226  Teuchos::SerialDenseVector<int,Scalar> b = DIRK_ButcherTableau_->b();
227  Teuchos::SerialDenseVector<int,Scalar> c = DIRK_ButcherTableau_->c();
228 
229  // Compute stage solutions
230  bool pass = true;
231  Thyra::SolveStatus<Scalar> sStatus;
232  for (int i=0; i < numStages; ++i) {
233  if (!Teuchos::is_null(stepperDIRKObserver_))
234  stepperDIRKObserver_->observeBeginStage(solutionHistory, *this);
235 
236  if ( i == 0 && this->getUseFSAL() &&
237  workingState->getNConsecutiveFailures() == 0 ) {
238 
239  RCP<Thyra::VectorBase<Scalar> > tmp = stageXDot_[0];
240  stageXDot_[0] = stageXDot_.back();
241  stageXDot_.back() = tmp;
242 
243  } else {
244 
245  Thyra::assign(xTilde_.ptr(), *(currentState->getX()));
246  for (int j=0; j < i; ++j) {
247  if (A(i,j) != Teuchos::ScalarTraits<Scalar>::zero()) {
248  Thyra::Vp_StV(xTilde_.ptr(), dt*A(i,j), *(stageXDot_[j]));
249  }
250  }
251 
252  Scalar ts = time + c(i)*dt;
253  if (A(i,i) == Teuchos::ScalarTraits<Scalar>::zero()) {
254  // Explicit stage for the ImplicitODE_DAE
255  bool isNeeded = false;
256  for (int k=i+1; k<numStages; ++k) if (A(k,i) != 0.0) isNeeded = true;
257  if (b(i) != 0.0) isNeeded = true;
258  if (isNeeded == false) {
259  // stageXDot_[i] is not needed.
260  assign(stageXDot_[i].ptr(), Teuchos::ScalarTraits<Scalar>::zero());
261  } else {
262  typedef Thyra::ModelEvaluatorBase MEB;
263  MEB::InArgs<Scalar> inArgs = this->wrapperModel_->getInArgs();
264  MEB::OutArgs<Scalar> outArgs = this->wrapperModel_->getOutArgs();
265  inArgs.set_x(xTilde_);
266  if (inArgs.supports(MEB::IN_ARG_t)) inArgs.set_t(ts);
267  if (inArgs.supports(MEB::IN_ARG_x_dot))
268  inArgs.set_x_dot(Teuchos::null);
269  outArgs.set_f(stageXDot_[i]);
270 
271  if (!Teuchos::is_null(stepperDIRKObserver_))
272  stepperDIRKObserver_->observeBeforeExplicit(solutionHistory,*this);
273  this->wrapperModel_->getAppModel()->evalModel(inArgs,outArgs);
274  }
275  } else {
276  // Implicit stage for the ImplicitODE_DAE
277  const Scalar alpha = 1.0/(dt*A(i,i));
278  const Scalar beta = 1.0;
279 
280  // Setup TimeDerivative
281  Teuchos::RCP<TimeDerivative<Scalar> > timeDer =
282  Teuchos::rcp(new StepperDIRKTimeDerivative<Scalar>(
283  alpha,xTilde_.getConst()));
284 
285  Teuchos::RCP<ImplicitODEParameters<Scalar> > p =
286  Teuchos::rcp(new ImplicitODEParameters<Scalar>(
287  timeDer, dt, alpha, beta));
288  p->stageNumber_ = i;
289 
290  if (!Teuchos::is_null(stepperDIRKObserver_))
291  stepperDIRKObserver_->observeBeforeSolve(solutionHistory, *this);
292 
293  sStatus = this->solveImplicitODE(stageX_, stageXDot_[i], ts, p);
294 
295  if (sStatus.solveStatus != Thyra::SOLVE_STATUS_CONVERGED) pass=false;
296 
297  if (!Teuchos::is_null(stepperDIRKObserver_))
298  stepperDIRKObserver_->observeAfterSolve(solutionHistory, *this);
299 
300  timeDer->compute(stageX_, stageXDot_[i]);
301  }
302  }
303 
304  if (!Teuchos::is_null(stepperDIRKObserver_))
305  stepperDIRKObserver_->observeEndStage(solutionHistory, *this);
306  }
307 
308  // Sum for solution: x_n = x_n-1 + Sum{ dt*b(i) * f(i) }
309  Thyra::assign((workingState->getX()).ptr(), *(currentState->getX()));
310  for (int i=0; i < numStages; ++i) {
311  if (b(i) != Teuchos::ScalarTraits<Scalar>::zero()) {
312  Thyra::Vp_StV((workingState->getX()).ptr(), dt*b(i), *(stageXDot_[i]));
313  }
314  }
315 
316  if (DIRK_ButcherTableau_->isEmbedded() and this->getEmbedded()) {
317  RCP<SolutionStateMetaData<Scalar> > metaData=workingState->getMetaData();
318  const Scalar tolAbs = metaData->getTolRel();
319  const Scalar tolRel = metaData->getTolAbs();
320 
321  // just compute the error weight vector
322  // (all that is needed is the error, and not the embedded solution)
323  Teuchos::SerialDenseVector<int,Scalar> errWght = b ;
324  errWght -= DIRK_ButcherTableau_->bstar();
325 
326  // compute local truncation error estimate: | u^{n+1} - \hat{u}^{n+1} |
327  // Sum for solution: ee_n = Sum{ (b(i) - bstar(i)) * dt*f(i) }
328  assign(ee_.ptr(), Teuchos::ScalarTraits<Scalar>::zero());
329  for (int i=0; i < numStages; ++i) {
330  if (errWght(i) != Teuchos::ScalarTraits<Scalar>::zero()) {
331  Thyra::Vp_StV(ee_.ptr(), dt*errWght(i), *(stageXDot_[i]));
332  }
333  }
334 
335  // compute: Atol + max(|u^n|, |u^{n+1}| ) * Rtol
336  Thyra::abs( *(currentState->getX()), abs_u0.ptr());
337  Thyra::abs( *(workingState->getX()), abs_u.ptr());
338  Thyra::pair_wise_max_update(tolRel, *abs_u0, abs_u.ptr());
339  Thyra::add_scalar(tolAbs, abs_u.ptr());
340 
341  // compute: || ee / sc ||
342  assign(sc.ptr(), Teuchos::ScalarTraits<Scalar>::zero());
343  Thyra::ele_wise_divide(Teuchos::as<Scalar>(1.0), *ee_, *abs_u, sc.ptr());
344  Scalar err = std::abs(Thyra::norm_inf(*sc));
345  metaData->setErrorRel(err);
346 
347  // test if step should be rejected
348  if (std::isinf(err) || std::isnan(err) || err > Teuchos::as<Scalar>(1.0))
349  pass = false;
350  }
351 
352  if (pass) workingState->setSolutionStatus(Status::PASSED);
353  else workingState->setSolutionStatus(Status::FAILED);
354 
355  workingState->setOrder(this->getOrder());
356  this->stepperObserver_->observeEndTakeStep(solutionHistory, *this);
357  }
358  return;
359 }
360 
361 /** \brief Provide a StepperState to the SolutionState.
362  * This Stepper does not have any special state data,
363  * so just provide the base class StepperState with the
364  * Stepper description. This can be checked to ensure
365  * that the input StepperState can be used by this Stepper.
366  */
367 template<class Scalar>
368 Teuchos::RCP<Tempus::StepperState<Scalar> >
371 {
372  Teuchos::RCP<Tempus::StepperState<Scalar> > stepperState =
373  rcp(new StepperState<Scalar>(description()));
374  return stepperState;
375 }
376 
377 
378 template<class Scalar>
380 {
381  return(DIRK_ButcherTableau_->description());
382 }
383 
384 
385 template<class Scalar>
387  Teuchos::FancyOStream &out,
388  const Teuchos::EVerbosityLevel /* verbLevel */) const
389 {
390  out << description() << "::describe:" << std::endl
391  << "wrapperModel_ = " << this->wrapperModel_->description() << std::endl;
392 }
393 
394 
395 template <class Scalar>
397  const Teuchos::RCP<Teuchos::ParameterList> & pList)
398 {
399  if (pList == Teuchos::null) {
400  // Create default parameters if null, otherwise keep current parameters.
401  if (this->stepperPL_ == Teuchos::null) this->stepperPL_ = this->getDefaultParameters();
402  } else {
403  this->stepperPL_ = pList;
404  }
405  // Can not validate because of optional Parameters.
406  //stepperPL_->validateParametersAndSetDefaults(*this->getValidParameters());
407 }
408 
409 
410 template<class Scalar>
411 Teuchos::RCP<const Teuchos::ParameterList>
413 {
414  Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
415  if (DIRK_ButcherTableau_ == Teuchos::null) {
416  auto DIRK_ButcherTableau =
417  createRKBT<Scalar>("SDIRK 2 Stage 2nd order", Teuchos::null);
418  pl->setParameters(*(DIRK_ButcherTableau->getValidParameters()));
419  } else {
420  pl->setParameters(*(DIRK_ButcherTableau_->getValidParameters()));
421  }
422 
423  this->getValidParametersBasic(pl);
424  pl->set<bool>("Initial Condition Consistency Check", false);
425  pl->set<bool>("Zero Initial Guess", false);
426  return pl;
427 }
428 
429 template <class Scalar>
430 Teuchos::RCP<Teuchos::ParameterList>
432 {
433  using Teuchos::RCP;
434  using Teuchos::ParameterList;
435  using Teuchos::rcp_const_cast;
436 
437  RCP<ParameterList> pl =
438  rcp_const_cast<ParameterList>(this->getValidParameters());
439 
440  pl->set<std::string>("Solver Name", "Default Solver");
441  RCP<ParameterList> solverPL = this->defaultSolverParameters();
442  pl->set("Default Solver", *solverPL);
443 
444  return pl;
445 }
446 
447 template <class Scalar>
448 Teuchos::RCP<Teuchos::ParameterList>
450 {
451  return(this->stepperPL_);
452 }
453 
454 
455 template <class Scalar>
456 Teuchos::RCP<Teuchos::ParameterList>
458 {
459  Teuchos::RCP<Teuchos::ParameterList> temp_plist = this->stepperPL_;
460  this->stepperPL_ = Teuchos::null;
461  return(temp_plist);
462 }
463 
464 
465 } // namespace Tempus
466 #endif // Tempus_StepperDIRK_impl_hpp
virtual void setInitialConditions(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Set the initial conditions and make them consistent.
virtual void setTableau(std::string stepperType)
StepperDIRKObserver class for StepperDIRK.
virtual Teuchos::RCP< Tempus::StepperState< Scalar > > getDefaultStepperState()
Get a default (initial) StepperState.
Teuchos::RCP< Teuchos::ParameterList > unsetParameterList()
StepperDIRK()
Default constructor.
StepperState is a simple class to hold state information about the stepper.
virtual void setInitialConditions(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Set the initial conditions and make them consistent.
virtual void initialize()
Initialize during construction and after changing input parameters.
Time-derivative interface for DIRK.
virtual std::string description() const
StepperObserver class for Stepper class.
Teuchos::RCP< SolutionHistory< Scalar > > solutionHistory(Teuchos::RCP< Teuchos::ParameterList > pList=Teuchos::null)
Nonmember constructor.
Teuchos::RCP< Teuchos::ParameterList > getNonconstParameterList()
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
void setParameterList(const Teuchos::RCP< Teuchos::ParameterList > &pl)
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
Teuchos::RCP< Teuchos::ParameterList > getDefaultParameters() const
virtual void takeStep(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Take the specified timestep, dt, and return true if successful.