9 #ifndef Tempus_StepperBackwardEuler_impl_hpp
10 #define Tempus_StepperBackwardEuler_impl_hpp
13 #include "Tempus_WrapperModelEvaluatorBasic.hpp"
14 #include "Tempus_StepperFactory.hpp"
20 template<
class Scalar>
23 this->setStepperName(
"Backward Euler");
24 this->setStepperType(
"Backward Euler");
25 this->setUseFSAL(
false);
26 this->setICConsistency(
"None");
27 this->setICConsistencyCheck(
false);
28 this->setZeroInitialGuess(
false);
30 this->setAppAction(Teuchos::null);
31 this->setDefaultSolver();
36 template<
class Scalar>
42 std::string ICConsistency,
43 bool ICConsistencyCheck,
44 bool zeroInitialGuess,
47 this->setStepperName(
"Backward Euler");
48 this->setStepperType(
"Backward Euler");
49 this->setUseFSAL( useFSAL);
50 this->setICConsistency( ICConsistency);
51 this->setICConsistencyCheck( ICConsistencyCheck);
52 this->setZeroInitialGuess( zeroInitialGuess);
54 this->setAppAction(stepperBEAppAction);
55 this->setSolver(solver);
56 this->setPredictor(predictorStepper);
58 if (appModel != Teuchos::null) {
59 this->setModel(appModel);
65 template<
class Scalar>
68 if (predictorType ==
"None") {
69 predictorStepper_ = Teuchos::null;
74 if (this->wrapperModel_ != Teuchos::null &&
75 this->wrapperModel_->getAppModel() != Teuchos::null) {
76 predictorStepper_ = sf->createStepper(predictorType,
77 this->wrapperModel_->getAppModel());
79 predictorStepper_ = sf->createStepper(predictorType);
82 this->isInitialized_ =
false;
87 template<
class Scalar>
91 predictorStepper_ = predictorStepper;
92 if (predictorStepper_ == Teuchos::null)
return;
95 predictorStepper_->getModel() == Teuchos::null &&
96 this->wrapperModel_->getAppModel() == Teuchos::null, std::logic_error,
97 "Error - Need to set the model, setModel(), before calling "
98 "StepperBackwardEuler::setPredictor()\n");
100 if (predictorStepper_->getModel() == Teuchos::null)
101 predictorStepper_->setModel(this->wrapperModel_->getAppModel());
102 predictorStepper_->initialize();
104 this->isInitialized_ =
false;
108 template<
class Scalar>
112 if (appAction == Teuchos::null) {
114 stepperBEAppAction_ =
117 stepperBEAppAction_ = appAction;
119 this->isInitialized_ =
false;
123 template<
class Scalar>
129 RCP<SolutionState<Scalar> > initialState = solutionHistory->getCurrentState();
132 if (initialState->getXDot() == Teuchos::null)
133 this->setStepperXDot(initialState->getX()->clone_v());
135 this->setStepperXDot(initialState->getXDot());
141 template<
class Scalar>
145 this->checkInitialized();
149 TEMPUS_FUNC_TIME_MONITOR(
"Tempus::StepperBackwardEuler::takeStep()");
153 "Error - StepperBackwardEuler<Scalar>::takeStep(...)\n"
154 "Need at least two SolutionStates for Backward Euler.\n"
155 " Number of States = " << solutionHistory->getNumStates() <<
"\n"
156 "Try setting in \"Solution History\" \"Storage Type\" = \"Undo\"\n"
157 " or \"Storage Type\" = \"Static\" and \"Storage Limit\" = \"2\"\n");
159 RCP<StepperBackwardEuler<Scalar> > thisStepper = Teuchos::rcpFromRef(*
this);
160 stepperBEAppAction_->execute(solutionHistory, thisStepper,
163 RCP<SolutionState<Scalar> > workingState=solutionHistory->getWorkingState();
164 RCP<SolutionState<Scalar> > currentState=solutionHistory->getCurrentState();
166 RCP<const Thyra::VectorBase<Scalar> > xOld = currentState->getX();
167 RCP<Thyra::VectorBase<Scalar> > x = workingState->getX();
168 if (workingState->getXDot() != Teuchos::null)
169 this->setStepperXDot(workingState->getXDot());
170 RCP<Thyra::VectorBase<Scalar> > xDot = this->getStepperXDot();
172 computePredictor(solutionHistory);
176 const Scalar time = workingState->getTime();
177 const Scalar dt = workingState->getTimeStep();
182 Scalar(1.0)/dt,xOld));
184 const Scalar alpha = Scalar(1.0)/dt;
185 const Scalar beta = Scalar(1.0);
187 timeDer, dt, alpha, beta));
189 stepperBEAppAction_->execute(solutionHistory, thisStepper,
193 this->solveImplicitODE(x, xDot, time, p);
195 stepperBEAppAction_->execute(solutionHistory, thisStepper,
198 if (workingState->getXDot() != Teuchos::null)
199 timeDer->compute(x, xDot);
201 workingState->setSolutionStatus(sStatus);
202 workingState->setOrder(this->getOrder());
203 workingState->computeNorms(currentState);
204 stepperBEAppAction_->execute(solutionHistory, thisStepper,
210 template<
class Scalar>
214 if (predictorStepper_ == Teuchos::null)
return;
215 predictorStepper_->takeStep(solutionHistory);
217 if (solutionHistory->getWorkingState()->getSolutionStatus()==
Status::FAILED) {
219 Teuchos::OSTab ostab(out,1,
"StepperBackwardEuler::computePredictor");
220 *out <<
"Warning - predictorStepper has failed." << std::endl;
223 solutionHistory->getWorkingState()->setSolutionStatus(
Status::WORKING);
234 template<
class Scalar>
245 template<
class Scalar>
254 out <<
"--- StepperBackwardEuler ---\n";
255 out <<
" predictorStepper_ = "
256 << predictorStepper_ << std::endl;
257 if (predictorStepper_ != Teuchos::null) {
258 out <<
" predictorStepper_->isInitialized() = "
260 out <<
" predictor stepper type = "
261 << predictorStepper_->description() << std::endl;
263 out <<
" stepperBEAppAction_ = "
264 << stepperBEAppAction_ << std::endl;
265 out <<
"----------------------------" << std::endl;
269 template<
class Scalar>
272 bool isValidSetup =
true;
277 if (predictorStepper_ != Teuchos::null) {
278 if ( !predictorStepper_->isInitialized() ) {
279 isValidSetup =
false;
280 out <<
"The predictor stepper is not initialized!\n";
284 if (stepperBEAppAction_ == Teuchos::null) {
285 isValidSetup =
false;
286 out <<
"The Backward Euler AppAction is not set!\n";
293 template<
class Scalar>
297 auto pl = this->getValidParametersBasicImplicit();
298 if (predictorStepper_ == Teuchos::null)
299 pl->set(
"Predictor Stepper Type",
"None");
301 pl->set(
"Predictor Stepper Type", predictorStepper_->getStepperType());
306 template <
class Scalar>
314 template <
class Scalar>
321 const int param_index)
const
324 MEB::OutArgs<Scalar> outArgs = this->wrapperModel_->getOutArgs();
325 outArgs.set_f(Teuchos::rcpFromRef(residual));
326 computeStepResidDerivImpl(outArgs, x, t, p, param_index);
329 template <
class Scalar>
336 const int param_index,
337 const int deriv_index)
const
340 MEB::OutArgs<Scalar> outArgs = this->wrapperModel_->getOutArgs();
342 outArgs.set_W_op(Teuchos::rcpFromRef(jacobian));
343 computeStepResidDerivImpl(outArgs, x, t, p, param_index, deriv_index);
346 template <
class Scalar>
353 const int param_index)
const
356 MEB::OutArgs<Scalar> outArgs = this->wrapperModel_->getOutArgs();
357 TEUCHOS_ASSERT(outArgs.supports(MEB::OUT_ARG_DfDp, param_index).supports(MEB::DERIV_LINEAR_OP));
358 outArgs.set_DfDp(param_index,
359 MEB::Derivative<Scalar>(Teuchos::rcpFromRef(deriv)));
360 computeStepResidDerivImpl(outArgs, x, t, p, param_index);
363 template <
class Scalar>
370 const int param_index)
const
373 MEB::OutArgs<Scalar> outArgs = this->wrapperModel_->getOutArgs();
375 outArgs.set_W(Teuchos::rcpFromRef(jacobian_solver));
376 computeStepResidDerivImpl(outArgs, x, t, p, param_index, 0);
379 template <
class Scalar>
386 const int param_index,
387 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_->getInArgs();
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);
431 template<
class Scalar>
439 stepper->setStepperImplicitValues(pl);
441 if (model != Teuchos::null) {
442 stepper->setModel(model);
444 if (pl != Teuchos::null) {
445 std::string predictorName =
446 pl->
get<std::string>(
"Predictor Stepper Type",
"None");
447 stepper->setPredictor(predictorName);
449 stepper->initialize();
457 #endif // Tempus_StepperBackwardEuler_impl_hpp
virtual void setInitialConditions(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Set the initial conditions and make them consistent.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
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.
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
Compute time step residual.
T & get(const std::string &name, T def_value)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
virtual void takeStep(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Take the specified timestep, dt, and return true if successful.
virtual void computePredictor(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Compute predictor given the supplied stepper.
virtual int stencilLength() const
Return the number of solution vectors in the time step stencil.
Default modifier for StepperBackwardEuler.
virtual Teuchos::RCP< Tempus::StepperState< Scalar > > getDefaultStepperState()
Get a default (initial) StepperState.
Application Action for StepperBackwardEuler.
Thyra Base interface for time steppers.
StepperState is a simple class to hold state information about the stepper.
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
Compute time step Jacobian solver.
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 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
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
Compute time step derivative w.r.t. model parameters.
void setPredictor(std::string predictorType="None")
Set the predictor.
Backward Euler time stepper.
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
virtual void setAppAction(Teuchos::RCP< StepperBackwardEulerAppAction< Scalar > > appAction)
virtual bool isValidSetup(Teuchos::FancyOStream &out) const
virtual void setInitialConditions(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Set the initial conditions and make them consistent.
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.
Time-derivative interface for Backward Euler.
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
#define TEUCHOS_ASSERT(assertion_test)
std::string toString(const T &t)