Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 "Thyra_VectorStdOps.hpp"
13 
14 #include "Tempus_WrapperModelEvaluatorBasic.hpp"
15 
16 namespace Tempus {
17 
18 template <class Scalar>
20 {
21  this->setUseEmbedded(false);
22  this->setZeroInitialGuess(false);
23  this->setStageNumber(-1);
24 
25  this->setAppAction(Teuchos::null);
26  this->setDefaultSolver();
27 }
28 
29 template <class Scalar>
31  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> >& appModel,
33  bool useFSAL, std::string ICConsistency, bool ICConsistencyCheck,
34  bool useEmbedded, bool zeroInitialGuess,
35  const Teuchos::RCP<StepperRKAppAction<Scalar> >& stepperRKAppAction)
36 {
37  this->setUseFSAL(useFSAL);
38  this->setICConsistency(ICConsistency);
39  this->setICConsistencyCheck(ICConsistencyCheck);
40  this->setUseEmbedded(useEmbedded);
41  this->setZeroInitialGuess(zeroInitialGuess);
42 
43  this->setStageNumber(-1);
44  this->setErrorNorm();
45  this->setAppAction(stepperRKAppAction);
46  this->setSolver(solver);
47 
48  if (appModel != Teuchos::null) {
49  this->setModel(appModel);
50  this->initialize();
51  }
52 }
53 
54 template <class Scalar>
57 {
58  auto pl = this->getValidParametersBasicImplicit();
59  pl->template set<bool>(
60  "Use Embedded", this->getUseEmbedded(),
61  "'Whether to use Embedded Stepper (if available) or not\n"
62  " 'true' - Stepper will compute embedded solution and is adaptive.\n"
63  " 'false' - Stepper is not embedded(adaptive).\n");
64  pl->template set<std::string>("Description", this->getDescription());
65  pl->template set<bool>("Reset Initial Guess", this->getResetInitialGuess());
66 
67  return pl;
68 }
69 
70 template <class Scalar>
72 {
73  TEUCHOS_TEST_FOR_EXCEPTION(this->tableau_ == Teuchos::null, std::logic_error,
74  "Error - Need to set the tableau, before calling "
75  "StepperDIRK::initialize()\n");
76 
78  this->wrapperModel_ == Teuchos::null, std::logic_error,
79  "Error - Need to set the model, setModel(), before calling "
80  "StepperDIRK::initialize()\n");
81 
83 }
84 
85 template <class Scalar>
87  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> >& appModel)
88 {
90 
91  // Set the stage vectors
92  const int numStages = this->tableau_->numStages();
93  stageXDot_.resize(numStages);
94  for (int i = 0; i < numStages; ++i) {
95  stageXDot_[i] = Thyra::createMember(this->wrapperModel_->get_f_space());
96  assign(stageXDot_[i].ptr(), Teuchos::ScalarTraits<Scalar>::zero());
97  }
98  xTilde_ = Thyra::createMember(this->wrapperModel_->get_x_space());
99  assign(xTilde_.ptr(), Teuchos::ScalarTraits<Scalar>::zero());
100 
101  this->setEmbeddedMemory();
102  this->setErrorNorm();
103 
104  this->isInitialized_ = false;
105 }
106 
107 template <class Scalar>
109 {
110  if (this->getModel() == Teuchos::null)
111  return; // Embedded memory will be set when setModel() is called.
112 
113  if (this->tableau_->isEmbedded() && this->getUseEmbedded()) {
114  this->ee_ = Thyra::createMember(this->wrapperModel_->get_f_space());
115  this->abs_u0 = Thyra::createMember(this->wrapperModel_->get_f_space());
116  this->abs_u = Thyra::createMember(this->wrapperModel_->get_f_space());
117  this->sc = Thyra::createMember(this->wrapperModel_->get_f_space());
118  }
119  else {
120  this->ee_ = Teuchos::null;
121  this->abs_u0 = Teuchos::null;
122  this->abs_u = Teuchos::null;
123  this->sc = Teuchos::null;
124  }
125 }
126 
127 template <class Scalar>
129  const Teuchos::RCP<SolutionHistory<Scalar> >& solutionHistory)
130 {
131  this->setStepperXDot(stageXDot_.back());
133 }
134 
135 template <class Scalar>
137  const Teuchos::RCP<SolutionHistory<Scalar> >& solutionHistory)
138 {
139  this->checkInitialized();
140 
141  using Teuchos::RCP;
142 
143  TEMPUS_FUNC_TIME_MONITOR("Tempus::StepperDIRK::takeStep()");
144  {
146  solutionHistory->getNumStates() < 2, std::logic_error,
147  "Error - StepperDIRK<Scalar>::takeStep(...)\n"
148  << "Need at least two SolutionStates for DIRK.\n"
149  << " Number of States = " << solutionHistory->getNumStates()
150  << "\nTry setting in \"Solution History\" "
151  << "\"Storage Type\" = \"Undo\"\n"
152  << " or \"Storage Type\" = \"Static\" and "
153  << "\"Storage Limit\" = \"2\"\n");
154 
155  RCP<SolutionState<Scalar> > currentState =
156  solutionHistory->getCurrentState();
157  RCP<SolutionState<Scalar> > workingState =
158  solutionHistory->getWorkingState();
159  const Scalar dt = workingState->getTimeStep();
160  const Scalar time = currentState->getTime();
161 
162  const int numStages = this->tableau_->numStages();
163  Teuchos::SerialDenseMatrix<int, Scalar> A = this->tableau_->A();
164  Teuchos::SerialDenseVector<int, Scalar> b = this->tableau_->b();
165  Teuchos::SerialDenseVector<int, Scalar> c = this->tableau_->c();
166 
167  // Reset non-zero initial guess.
168  if (this->getResetInitialGuess() && (!this->getZeroInitialGuess()))
169  Thyra::assign(workingState->getX().ptr(), *(currentState->getX()));
170 
171  RCP<StepperDIRK<Scalar> > thisStepper = Teuchos::rcpFromRef(*this);
172  this->stepperRKAppAction_->execute(
173  solutionHistory, thisStepper,
175 
176  // Compute stage solutions
177  bool pass = true;
179  for (int i = 0; i < numStages; ++i) {
180  this->setStageNumber(i);
181 
182  Thyra::assign(xTilde_.ptr(), *(currentState->getX()));
183  for (int j = 0; j < i; ++j) {
184  if (A(i, j) != Teuchos::ScalarTraits<Scalar>::zero()) {
185  Thyra::Vp_StV(xTilde_.ptr(), dt * A(i, j), *(stageXDot_[j]));
186  }
187  }
188  this->setStepperXDot(stageXDot_[i]);
189 
190  this->stepperRKAppAction_->execute(
191  solutionHistory, thisStepper,
193 
194  Scalar ts = time + c(i) * dt;
195 
196  // Check if stageXDot_[i] is needed.
197  bool isNeeded = false;
198  for (int k = i + 1; k < numStages; ++k)
199  if (A(k, i) != 0.0) isNeeded = true;
200  if (b(i) != 0.0) isNeeded = true;
201  if (this->tableau_->isEmbedded() && this->getUseEmbedded() &&
202  this->tableau_->bstar()(i) != 0.0)
203  isNeeded = true;
204  if (isNeeded == false) {
205  assign(stageXDot_[i].ptr(), Teuchos::ScalarTraits<Scalar>::zero());
206  }
207  else if (A(i, i) == Teuchos::ScalarTraits<Scalar>::zero()) {
208  // Explicit stage for the ImplicitODE_DAE
209  if (i == 0 && this->getUseFSAL() &&
210  workingState->getNConsecutiveFailures() == 0) {
211  // Reuse last evaluation for first step
212  RCP<Thyra::VectorBase<Scalar> > tmp = stageXDot_[0];
213  stageXDot_[0] = stageXDot_.back();
214  stageXDot_.back() = tmp;
215  this->setStepperXDot(stageXDot_[0]);
216  }
217  else {
218  // Calculate explicit stage
219  typedef Thyra::ModelEvaluatorBase MEB;
220  MEB::InArgs<Scalar> inArgs = this->wrapperModel_->createInArgs();
221  MEB::OutArgs<Scalar> outArgs = this->wrapperModel_->createOutArgs();
222  inArgs.set_x(xTilde_);
223  if (inArgs.supports(MEB::IN_ARG_t)) inArgs.set_t(ts);
224  if (inArgs.supports(MEB::IN_ARG_x_dot))
225  inArgs.set_x_dot(Teuchos::null);
226  outArgs.set_f(stageXDot_[i]);
227 
228  this->wrapperModel_->getAppModel()->evalModel(inArgs, outArgs);
229  }
230  }
231  else {
232  // Implicit stage for the ImplicitODE_DAE
233  const Scalar alpha = 1.0 / (dt * A(i, i));
234  const Scalar beta = 1.0;
235 
236  // Setup TimeDerivative
238  new StepperDIRKTimeDerivative<Scalar>(alpha, xTilde_.getConst()));
239 
241  timeDer, dt, alpha, beta, SOLVE_FOR_X, i));
242 
243  this->stepperRKAppAction_->execute(
244  solutionHistory, thisStepper,
246 
247  sStatus =
248  this->solveImplicitODE(workingState->getX(), stageXDot_[i], ts, p);
249 
250  if (sStatus.solveStatus != Thyra::SOLVE_STATUS_CONVERGED) pass = false;
251 
252  this->stepperRKAppAction_->execute(
253  solutionHistory, thisStepper,
255 
256  timeDer->compute(workingState->getX(), stageXDot_[i]);
257  }
258  this->stepperRKAppAction_->execute(
259  solutionHistory, thisStepper,
261  this->stepperRKAppAction_->execute(
262  solutionHistory, thisStepper,
264  }
265  // reset the stage number
266  this->setStageNumber(-1);
267 
268  // Sum for solution: x_n = x_n-1 + Sum{ dt*b(i) * f(i) }
269  Thyra::assign((workingState->getX()).ptr(), *(currentState->getX()));
270  for (int i = 0; i < numStages; ++i) {
271  if (b(i) != Teuchos::ScalarTraits<Scalar>::zero()) {
272  Thyra::Vp_StV((workingState->getX()).ptr(), dt * b(i),
273  *(stageXDot_[i]));
274  }
275  }
276 
277  if (this->tableau_->isEmbedded() && this->getUseEmbedded()) {
278  const Scalar tolRel = workingState->getTolRel();
279  const Scalar tolAbs = workingState->getTolAbs();
280 
281  // update the tolerance
282  this->stepperErrorNormCalculator_->setRelativeTolerance(tolRel);
283  this->stepperErrorNormCalculator_->setAbsoluteTolerance(tolAbs);
284 
285  // just compute the error weight vector
286  // (all that is needed is the error, and not the embedded solution)
288  errWght -= this->tableau_->bstar();
289 
290  // compute local truncation error estimate: | u^{n+1} - \hat{u}^{n+1} |
291  // Sum for solution: ee_n = Sum{ (b(i) - bstar(i)) * dt*f(i) }
292  assign(this->ee_.ptr(), Teuchos::ScalarTraits<Scalar>::zero());
293  for (int i = 0; i < numStages; ++i) {
294  if (errWght(i) != Teuchos::ScalarTraits<Scalar>::zero()) {
295  Thyra::Vp_StV(this->ee_.ptr(), dt * errWght(i), *(stageXDot_[i]));
296  }
297  }
298 
299  Scalar err = this->stepperErrorNormCalculator_->computeWRMSNorm(
300  currentState->getX(), workingState->getX(), this->ee_);
301  workingState->setErrorRel(err);
302 
303  // test if step should be rejected
304  if (std::isinf(err) || std::isnan(err) || err > Teuchos::as<Scalar>(1.0))
305  pass = false;
306  }
307 
308  if (pass)
309  workingState->setSolutionStatus(Status::PASSED);
310  else
311  workingState->setSolutionStatus(Status::FAILED);
312 
313  workingState->setOrder(this->getOrder());
314  workingState->computeNorms(currentState);
315  this->stepperRKAppAction_->execute(
316  solutionHistory, thisStepper,
318  }
319  return;
320 }
321 
328 template <class Scalar>
331 {
333  rcp(new StepperState<Scalar>(this->getStepperType()));
334  return stepperState;
335 }
336 
337 template <class Scalar>
339  Teuchos::FancyOStream& out, const Teuchos::EVerbosityLevel verbLevel) const
340 {
341  out.setOutputToRootOnly(0);
342  out << std::endl;
343  Stepper<Scalar>::describe(out, verbLevel);
344  StepperImplicit<Scalar>::describe(out, verbLevel);
345 
346  out << "--- StepperDIRK ---\n";
347  out << " tableau_ = " << this->tableau_ << std::endl;
348  if (this->tableau_ != Teuchos::null) this->tableau_->describe(out, verbLevel);
349  out << " stepperRKAppAction_ = " << this->stepperRKAppAction_ << std::endl;
350  out << " xTilde_ = " << xTilde_ << std::endl;
351  out << " stageXDot_.size() = " << stageXDot_.size() << std::endl;
352  const int numStages = stageXDot_.size();
353  for (int i = 0; i < numStages; ++i)
354  out << " stageXDot_[" << i << "] = " << stageXDot_[i] << std::endl;
355  out << " useEmbedded_ = " << Teuchos::toString(this->useEmbedded_)
356  << std::endl;
357  out << " ee_ = " << this->ee_ << std::endl;
358  out << " abs_u0 = " << this->abs_u0 << std::endl;
359  out << " abs_u = " << this->abs_u << std::endl;
360  out << " sc = " << this->sc << std::endl;
361  out << "-------------------" << std::endl;
362 }
363 
364 template <class Scalar>
366 {
367  out.setOutputToRootOnly(0);
368  bool isValidSetup = true;
369 
370  if (!Stepper<Scalar>::isValidSetup(out)) isValidSetup = false;
371  if (!StepperImplicit<Scalar>::isValidSetup(out)) isValidSetup = false;
372 
373  if (this->tableau_ == Teuchos::null) {
374  isValidSetup = false;
375  out << "The tableau is not set!\n";
376  }
377 
378  if (this->stepperRKAppAction_ == Teuchos::null) {
379  isValidSetup = false;
380  out << "The AppAction is not set!\n";
381  }
382 
383  return isValidSetup;
384 }
385 
386 template <class Scalar>
389 {
390  return this->getValidParametersBasicDIRK();
391 }
392 
393 } // namespace Tempus
394 #endif // Tempus_StepperDIRK_impl_hpp
Teuchos::RCP< Teuchos::ParameterList > getValidParametersBasicDIRK() const
virtual void setModel(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &appModel) override
Set the model.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
virtual void initialize()
Initialize after construction and changing input parameters.
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const override
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const override
Thyra Base interface for time steppers.
virtual void setModel(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &appModel) override
Set the model.
virtual void setInitialConditions(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory) override
Set the initial conditions and make them consistent.
StepperState is a simple class to hold state information about the stepper.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Thyra Base interface for implicit time steppers.
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
Application Action for StepperRKBase.
Time-derivative interface for DIRK.
virtual void setupDefault()
Default setup for constructor.
virtual Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
basic_FancyOStream & setOutputToRootOnly(const int rootRank)
virtual bool isValidSetup(Teuchos::FancyOStream &out) const override
ESolveStatus solveStatus
virtual void initialize() override
Initialize after construction and changing input parameters.
virtual void setInitialConditions(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory) override
Set the initial conditions and make them consistent.
virtual void setEmbeddedMemory() override
virtual void setup(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &wrapperModel, const Teuchos::RCP< Thyra::NonlinearSolverBase< Scalar > > &solver, bool useFSAL, std::string ICConsistency, bool ICConsistencyCheck, bool useEmbedded, bool zeroInitialGuess, const Teuchos::RCP< StepperRKAppAction< Scalar > > &stepperRKAppAction)
Setup for constructor.
virtual Teuchos::RCP< Tempus::StepperState< Scalar > > getDefaultStepperState() override
Get a default (initial) StepperState.
Solve for x and determine xDot from x.
virtual void takeStep(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory) override
Take the specified timestep, dt, and return true if successful.
std::string toString(const T &t)