10#ifndef Tempus_StepperBackwardEuler_impl_hpp
11#define Tempus_StepperBackwardEuler_impl_hpp
14#include "Tempus_WrapperModelEvaluatorBasic.hpp"
15#include "Tempus_StepperFactory.hpp"
19template <
class Scalar>
22 this->setStepperName(
"Backward Euler");
23 this->setStepperType(
"Backward Euler");
24 this->setUseFSAL(
false);
25 this->setICConsistency(
"None");
26 this->setICConsistencyCheck(
false);
27 this->setZeroInitialGuess(
false);
29 this->setAppAction(Teuchos::null);
30 this->setDefaultSolver();
34template <
class Scalar>
37 const Teuchos::RCP<Thyra::NonlinearSolverBase<Scalar> >& solver,
39 std::string ICConsistency,
bool ICConsistencyCheck,
bool zeroInitialGuess,
43 this->setStepperName(
"Backward Euler");
44 this->setStepperType(
"Backward Euler");
45 this->setUseFSAL(useFSAL);
46 this->setICConsistency(ICConsistency);
47 this->setICConsistencyCheck(ICConsistencyCheck);
48 this->setZeroInitialGuess(zeroInitialGuess);
50 this->setAppAction(stepperBEAppAction);
51 this->setSolver(solver);
52 this->setPredictor(predictorStepper);
54 if (appModel != Teuchos::null) {
55 this->setModel(appModel);
60template <
class Scalar>
63 if (predictorType ==
"None") {
64 predictorStepper_ = Teuchos::null;
69 if (this->wrapperModel_ != Teuchos::null &&
70 this->wrapperModel_->getAppModel() != Teuchos::null) {
72 sf->createStepper(predictorType, this->wrapperModel_->getAppModel());
75 predictorStepper_ = sf->createStepper(predictorType);
78 this->isInitialized_ =
false;
82template <
class Scalar>
86 predictorStepper_ = predictorStepper;
87 if (predictorStepper_ == Teuchos::null)
return;
89 TEUCHOS_TEST_FOR_EXCEPTION(
90 predictorStepper_->getModel() == Teuchos::null &&
91 this->wrapperModel_->getAppModel() == Teuchos::null,
93 "Error - Need to set the model, setModel(), before calling "
94 "StepperBackwardEuler::setPredictor()\n");
96 if (predictorStepper_->getModel() == Teuchos::null)
97 predictorStepper_->setModel(this->wrapperModel_->getAppModel());
98 predictorStepper_->initialize();
100 this->isInitialized_ =
false;
103template <
class Scalar>
107 if (appAction == Teuchos::null) {
109 stepperBEAppAction_ =
113 stepperBEAppAction_ = appAction;
115 this->isInitialized_ =
false;
118template <
class Scalar>
124 if (predictorStepper_ != Teuchos::null) {
126 if (predictorStepper_->getModel() == Teuchos::null) {
127 predictorStepper_->setModel(appModel);
128 predictorStepper_->initialize();
132 this->isInitialized_ =
false;
135template <
class Scalar>
141 RCP<SolutionState<Scalar> > initialState = solutionHistory->getCurrentState();
144 if (initialState->getXDot() == Teuchos::null)
145 this->setStepperXDot(initialState->getX()->clone_v());
147 this->setStepperXDot(initialState->getXDot());
152template <
class Scalar>
156 this->checkInitialized();
160 TEMPUS_FUNC_TIME_MONITOR(
"Tempus::StepperBackwardEuler::takeStep()");
162 TEUCHOS_TEST_FOR_EXCEPTION(
163 solutionHistory->getNumStates() < 2, std::logic_error,
164 "Error - StepperBackwardEuler<Scalar>::takeStep(...)\n"
165 <<
"Need at least two SolutionStates for Backward Euler.\n"
166 <<
" Number of States = " << solutionHistory->getNumStates()
167 <<
"\n Try setting in \"Solution History\" \"Storage Type\" = "
168 <<
"\"Undo\"\n or \"Storage Type\" = \"Static\" and \"Storage Limit\" = "
171 RCP<StepperBackwardEuler<Scalar> > thisStepper = Teuchos::rcpFromRef(*
this);
172 stepperBEAppAction_->execute(
173 solutionHistory, thisStepper,
176 RCP<SolutionState<Scalar> > workingState =
177 solutionHistory->getWorkingState();
178 RCP<SolutionState<Scalar> > currentState =
179 solutionHistory->getCurrentState();
181 RCP<const Thyra::VectorBase<Scalar> > xOld = currentState->getX();
182 RCP<Thyra::VectorBase<Scalar> > x = workingState->getX();
183 if (workingState->getXDot() != Teuchos::null)
184 this->setStepperXDot(workingState->getXDot());
185 RCP<Thyra::VectorBase<Scalar> > xDot = this->getStepperXDot();
187 computePredictor(solutionHistory);
190 const Scalar time = workingState->getTime();
191 const Scalar dt = workingState->getTimeStep();
194 Teuchos::RCP<TimeDerivative<Scalar> > timeDer = Teuchos::rcp(
197 const Scalar alpha = Scalar(1.0) / dt;
198 const Scalar beta = Scalar(1.0);
199 auto p = Teuchos::rcp(
202 stepperBEAppAction_->execute(
203 solutionHistory, thisStepper,
206 const Thyra::SolveStatus<Scalar> sStatus =
207 this->solveImplicitODE(x, xDot, time, p);
209 stepperBEAppAction_->execute(
210 solutionHistory, thisStepper,
213 if (workingState->getXDot() != Teuchos::null) timeDer->compute(x, xDot);
215 workingState->setSolutionStatus(sStatus);
216 workingState->setOrder(this->getOrder());
217 workingState->computeNorms(currentState);
218 stepperBEAppAction_->execute(
219 solutionHistory, thisStepper,
225template <
class Scalar>
229 if (predictorStepper_ == Teuchos::null)
return;
230 predictorStepper_->takeStep(solutionHistory);
232 if (solutionHistory->getWorkingState()->getSolutionStatus() ==
234 Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
235 Teuchos::OSTab ostab(out, 1,
"StepperBackwardEuler::computePredictor");
236 *out <<
"Warning - predictorStepper has failed." << std::endl;
240 solutionHistory->getWorkingState()->setSolutionStatus(
Status::WORKING);
250template <
class Scalar>
251Teuchos::RCP<Tempus::StepperState<Scalar> >
254 Teuchos::RCP<Tempus::StepperState<Scalar> > stepperState =
259template <
class Scalar>
261 Teuchos::FancyOStream& out,
const Teuchos::EVerbosityLevel verbLevel)
const
263 out.setOutputToRootOnly(0);
268 out <<
"--- StepperBackwardEuler ---\n";
269 out <<
" predictorStepper_ = " << predictorStepper_
271 if (predictorStepper_ != Teuchos::null) {
272 out <<
" predictorStepper_->isInitialized() = "
273 << Teuchos::toString(predictorStepper_->isInitialized()) << std::endl;
274 out <<
" predictor stepper type = "
275 << predictorStepper_->description() << std::endl;
277 out <<
" stepperBEAppAction_ = " << stepperBEAppAction_
279 out <<
"----------------------------" << std::endl;
282template <
class Scalar>
284 Teuchos::FancyOStream& out)
const
286 out.setOutputToRootOnly(0);
287 bool isValidSetup =
true;
292 if (predictorStepper_ != Teuchos::null) {
293 if (!predictorStepper_->isInitialized()) {
294 isValidSetup =
false;
295 out <<
"The predictor stepper is not initialized!\n";
299 if (stepperBEAppAction_ == Teuchos::null) {
300 isValidSetup =
false;
301 out <<
"The Backward Euler AppAction is not set!\n";
307template <
class Scalar>
308Teuchos::RCP<const Teuchos::ParameterList>
311 auto pl = this->getValidParametersBasicImplicit();
312 if (predictorStepper_ == Teuchos::null)
313 pl->set(
"Predictor Stepper Type",
"None");
315 pl->set(
"Predictor Stepper Type", predictorStepper_->getStepperType());
319template <
class Scalar>
325template <
class Scalar>
330 const int param_index)
const
332 typedef Thyra::ModelEvaluatorBase MEB;
333 MEB::OutArgs<Scalar> outArgs = this->wrapperModel_->createOutArgs();
334 outArgs.set_f(Teuchos::rcpFromRef(residual));
335 computeStepResidDerivImpl(outArgs, x, t, p, param_index);
338template <
class Scalar>
343 const int param_index,
const int deriv_index)
const
345 typedef Thyra::ModelEvaluatorBase MEB;
346 MEB::OutArgs<Scalar> outArgs = this->wrapperModel_->createOutArgs();
347 TEUCHOS_ASSERT(outArgs.supports(MEB::OUT_ARG_W_op));
348 outArgs.set_W_op(Teuchos::rcpFromRef(jacobian));
349 computeStepResidDerivImpl(outArgs, x, t, p, param_index, deriv_index);
352template <
class Scalar>
357 const int param_index)
const
359 typedef Thyra::ModelEvaluatorBase MEB;
360 MEB::OutArgs<Scalar> outArgs = this->wrapperModel_->createOutArgs();
361 TEUCHOS_ASSERT(outArgs.supports(MEB::OUT_ARG_DfDp, param_index)
362 .supports(MEB::DERIV_LINEAR_OP));
363 outArgs.set_DfDp(param_index,
364 MEB::Derivative<Scalar>(Teuchos::rcpFromRef(deriv)));
365 computeStepResidDerivImpl(outArgs, x, t, p, param_index);
368template <
class Scalar>
373 const int param_index)
const
375 typedef Thyra::ModelEvaluatorBase MEB;
376 MEB::OutArgs<Scalar> outArgs = this->wrapperModel_->createOutArgs();
377 TEUCHOS_ASSERT(outArgs.supports(MEB::OUT_ARG_W));
378 outArgs.set_W(Teuchos::rcpFromRef(jacobian_solver));
379 computeStepResidDerivImpl(outArgs, x, t, p, param_index, 0);
382template <
class Scalar>
384 const Thyra::ModelEvaluatorBase::OutArgs<Scalar>& outArgs,
387 const int param_index,
const int deriv_index)
const
390 typedef Thyra::ModelEvaluatorBase MEB;
392 TEUCHOS_ASSERT(x.size() == 2);
393 TEUCHOS_ASSERT(t.size() == 2);
394 RCP<const Thyra::VectorBase<Scalar> > xn = x[0];
395 RCP<const Thyra::VectorBase<Scalar> > xo = x[1];
396 const Scalar tn = t[0];
397 const Scalar to = t[1];
398 const Scalar dt = tn - to;
401 RCP<Thyra::VectorBase<Scalar> > x_dot = xn->clone_v();
402 Teuchos::RCP<TimeDerivative<Scalar> > timeDer = Teuchos::rcp(
404 timeDer->compute(xn, x_dot);
407 MEB::InArgs<Scalar> inArgs = this->wrapperModel_->createInArgs();
409 if (inArgs.supports(MEB::IN_ARG_x_dot)) inArgs.set_x_dot(x_dot);
410 if (inArgs.supports(MEB::IN_ARG_t)) inArgs.set_t(tn);
411 if (inArgs.supports(MEB::IN_ARG_step_size)) inArgs.set_step_size(dt);
412 inArgs.set_p(param_index, Teuchos::rcpFromRef(p));
413 TEUCHOS_ASSERT(inArgs.supports(MEB::IN_ARG_alpha));
414 TEUCHOS_ASSERT(inArgs.supports(MEB::IN_ARG_beta));
415 if (deriv_index == 0) {
417 inArgs.set_alpha(Scalar(1.0) / dt);
418 inArgs.set_beta(Scalar(1.0));
420 else if (deriv_index == 1) {
422 inArgs.set_alpha(Scalar(-1.0) / dt);
423 inArgs.set_beta(Scalar(0.0));
425 this->wrapperModel_->getAppModel()->evalModel(inArgs, outArgs);
430template <
class Scalar>
433 Teuchos::RCP<Teuchos::ParameterList> pl)
437 stepper->setStepperImplicitValues(pl);
439 if (pl != Teuchos::null) {
440 std::string predictorName =
441 pl->get<std::string>(
"Predictor Stepper Type",
"None");
442 stepper->setPredictor(predictorName);
445 if (model != Teuchos::null) {
446 stepper->setModel(model);
447 stepper->initialize();
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
Application Action for StepperBackwardEuler.
Default modifier for StepperBackwardEuler.
Time-derivative interface for Backward Euler.
Backward Euler time stepper.
virtual Teuchos::RCP< Tempus::StepperState< Scalar > > getDefaultStepperState() override
Get a default (initial) StepperState.
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const override
virtual void setAppAction(Teuchos::RCP< StepperBackwardEulerAppAction< Scalar > > appAction)
virtual int stencilLength() const override
Return the number of solution vectors in the time step stencil.
virtual void computeStepResidual(Thyra::VectorBase< Scalar > &residual, const Teuchos::Array< Teuchos::RCP< const Thyra::VectorBase< Scalar > > > &x, const Teuchos::Array< Scalar > &t, const Thyra::VectorBase< Scalar > &p, const int param_index) const override
Compute time step residual.
virtual void setModel(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &appModel) override
Set the model.
virtual void computePredictor(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Compute predictor given the supplied stepper.
void setPredictor(std::string predictorType="None")
Set the predictor.
virtual void computeStepSolver(Thyra::LinearOpWithSolveBase< Scalar > &jacobian_solver, const Teuchos::Array< Teuchos::RCP< const Thyra::VectorBase< Scalar > > > &x, const Teuchos::Array< Scalar > &t, const Thyra::VectorBase< Scalar > &p, const int param_index) const override
Compute time step Jacobian solver.
virtual void computeStepJacobian(Thyra::LinearOpBase< Scalar > &jacobian, const Teuchos::Array< Teuchos::RCP< const Thyra::VectorBase< Scalar > > > &x, const Teuchos::Array< Scalar > &t, const Thyra::VectorBase< Scalar > &p, const int param_index, const int deriv_index) const override
Compute time step Jacobian.
virtual void computeStepParamDeriv(Thyra::LinearOpBase< Scalar > &deriv, const Teuchos::Array< Teuchos::RCP< const Thyra::VectorBase< Scalar > > > &x, const Teuchos::Array< Scalar > &t, const Thyra::VectorBase< Scalar > &p, const int param_index) const override
Compute time step derivative w.r.t. model parameters.
StepperBackwardEuler()
Default constructor.
virtual bool isValidSetup(Teuchos::FancyOStream &out) const override
virtual void setInitialConditions(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory) override
Set the initial conditions and make them consistent.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
Return a valid ParameterList with current settings.
void computeStepResidDerivImpl(const Thyra::ModelEvaluatorBase::OutArgs< Scalar > &outArgs, const Teuchos::Array< Teuchos::RCP< const Thyra::VectorBase< Scalar > > > &x, const Teuchos::Array< Scalar > &t, const Thyra::VectorBase< Scalar > &p, const int param_index, const int deriv_index=0) const
Implementation of computeStep*() methods.
virtual void takeStep(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory) override
Take the specified timestep, dt, and return true if successful.
Thyra Base interface for implicit time steppers.
virtual void setInitialConditions(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory) override
Set the initial conditions and make them consistent.
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const override
virtual void setModel(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &appModel) override
Set the model.
StepperState is a simple class to hold state information about the stepper.
Thyra Base interface for time steppers.
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
Teuchos::RCP< StepperBackwardEuler< Scalar > > createStepperBackwardEuler(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &model, Teuchos::RCP< Teuchos::ParameterList > pl)
Nonmember constructor - ModelEvaluator and ParameterList.