10 #ifndef Tempus_StepperBackwardEuler_impl_hpp
11 #define Tempus_StepperBackwardEuler_impl_hpp
14 #include "Tempus_WrapperModelEvaluatorBasic.hpp"
15 #include "Tempus_StepperFactory.hpp"
19 template <
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();
34 template <
class Scalar>
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);
60 template <
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;
82 template <
class Scalar>
86 predictorStepper_ = predictorStepper;
87 if (predictorStepper_ == Teuchos::null)
return;
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;
103 template <
class Scalar>
107 if (appAction == Teuchos::null) {
109 stepperBEAppAction_ =
113 stepperBEAppAction_ = appAction;
115 this->isInitialized_ =
false;
118 template <
class Scalar>
124 if (predictorStepper_ != Teuchos::null) {
126 if (predictorStepper_->getModel() == Teuchos::null) {
127 predictorStepper_->setModel(appModel);
128 predictorStepper_->initialize();
132 this->isInitialized_ =
false;
135 template <
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());
152 template <
class Scalar>
156 this->checkInitialized();
160 TEMPUS_FUNC_TIME_MONITOR(
"Tempus::StepperBackwardEuler::takeStep()");
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();
197 const Scalar alpha = Scalar(1.0) / dt;
198 const Scalar beta = Scalar(1.0);
202 stepperBEAppAction_->execute(
203 solutionHistory, thisStepper,
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,
225 template <
class Scalar>
229 if (predictorStepper_ == Teuchos::null)
return;
230 predictorStepper_->takeStep(solutionHistory);
232 if (solutionHistory->getWorkingState()->getSolutionStatus() ==
235 Teuchos::OSTab ostab(out, 1,
"StepperBackwardEuler::computePredictor");
236 *out <<
"Warning - predictorStepper has failed." << std::endl;
240 solutionHistory->getWorkingState()->setSolutionStatus(
Status::WORKING);
250 template <
class Scalar>
259 template <
class Scalar>
268 out <<
"--- StepperBackwardEuler ---\n";
269 out <<
" predictorStepper_ = " << predictorStepper_
271 if (predictorStepper_ != Teuchos::null) {
272 out <<
" predictorStepper_->isInitialized() = "
274 out <<
" predictor stepper type = "
275 << predictorStepper_->description() << std::endl;
277 out <<
" stepperBEAppAction_ = " << stepperBEAppAction_
279 out <<
"----------------------------" << std::endl;
282 template <
class Scalar>
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";
307 template <
class Scalar>
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());
319 template <
class Scalar>
325 template <
class Scalar>
330 const int param_index)
const
333 MEB::OutArgs<Scalar> outArgs = this->wrapperModel_->createOutArgs();
334 outArgs.set_f(Teuchos::rcpFromRef(residual));
335 computeStepResidDerivImpl(outArgs, x, t, p, param_index);
338 template <
class Scalar>
343 const int param_index,
const int deriv_index)
const
346 MEB::OutArgs<Scalar> outArgs = this->wrapperModel_->createOutArgs();
348 outArgs.set_W_op(Teuchos::rcpFromRef(jacobian));
349 computeStepResidDerivImpl(outArgs, x, t, p, param_index, deriv_index);
352 template <
class Scalar>
357 const int param_index)
const
360 MEB::OutArgs<Scalar> outArgs = this->wrapperModel_->createOutArgs();
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);
368 template <
class Scalar>
373 const int param_index)
const
376 MEB::OutArgs<Scalar> outArgs = this->wrapperModel_->createOutArgs();
378 outArgs.set_W(Teuchos::rcpFromRef(jacobian_solver));
379 computeStepResidDerivImpl(outArgs, x, t, p, param_index, 0);
382 template <
class Scalar>
387 const int param_index,
const int deriv_index)
const
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();
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));
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);
430 template <
class Scalar>
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();
454 #endif // Tempus_StepperBackwardEuler_impl_hpp
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.
StepperBackwardEuler()
Default constructor.
T & get(const std::string &name, T def_value)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
virtual void computePredictor(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Compute predictor given the supplied stepper.
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.
Default modifier for StepperBackwardEuler.
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.
Application Action for StepperBackwardEuler.
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const override
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 setModel(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &appModel) override
Set the model.
Thyra Base interface for time steppers.
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
virtual void takeStep(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory) override
Take the specified timestep, dt, and return true if successful.
virtual bool isValidSetup(Teuchos::FancyOStream &out) const override
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
Return a valid ParameterList with current settings.
void setPredictor(std::string predictorType="None")
Set the predictor.
virtual Teuchos::RCP< Tempus::StepperState< Scalar > > getDefaultStepperState() override
Get a default (initial) StepperState.
Backward Euler time stepper.
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
basic_FancyOStream & setOutputToRootOnly(const int rootRank)
virtual void setAppAction(Teuchos::RCP< StepperBackwardEulerAppAction< Scalar > > appAction)
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 describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const override
virtual void setInitialConditions(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory) override
Set the initial conditions and make them consistent.
Teuchos::RCP< StepperBackwardEuler< Scalar > > createStepperBackwardEuler(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &model, Teuchos::RCP< Teuchos::ParameterList > pl)
Nonmember constructor - ModelEvaluator and ParameterList.
virtual void setModel(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &appModel) override
Set the model.
Time-derivative interface for Backward Euler.
#define TEUCHOS_ASSERT(assertion_test)
std::string toString(const T &t)
virtual int stencilLength() const override
Return the number of solution vectors in the time step stencil.