9 #ifndef Tempus_StepperBackwardEuler_impl_hpp
10 #define Tempus_StepperBackwardEuler_impl_hpp
12 #include "Tempus_config.hpp"
15 #include "Tempus_WrapperModelEvaluatorBasic.hpp"
16 #include "Teuchos_VerboseObjectParameterListHelpers.hpp"
17 #include "NOX_Thyra.H"
26 template<
class Scalar>
29 this->setStepperType(
"Backward Euler");
30 this->setUseFSAL( this->getUseFSALDefault());
31 this->setICConsistency( this->getICConsistencyDefault());
32 this->setICConsistencyCheck( this->getICConsistencyCheckDefault());
33 this->setZeroInitialGuess(
false);
35 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
38 this->setAppAction(Teuchos::null);
39 this->setDefaultSolver();
40 this->setPredictor(
"None");
44 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
45 template<
class Scalar>
47 const Teuchos::RCP<
const Thyra::ModelEvaluator<Scalar> >& appModel,
49 const Teuchos::RCP<Thyra::NonlinearSolverBase<Scalar> >& solver,
52 std::string ICConsistency,
53 bool ICConsistencyCheck,
54 bool zeroInitialGuess)
57 this->setStepperType(
"Backward Euler");
58 this->setUseFSAL( useFSAL);
59 this->setICConsistency( ICConsistency);
60 this->setICConsistencyCheck( ICConsistencyCheck);
61 this->setZeroInitialGuess( zeroInitialGuess);
63 this->setObserver(obs);
64 this->setAppAction(Teuchos::null);
65 this->setSolver(solver);
66 this->setPredictor(predictorStepper);
68 if (appModel != Teuchos::null) {
69 this->setModel(appModel);
76 template<
class Scalar>
78 const Teuchos::RCP<
const Thyra::ModelEvaluator<Scalar> >& appModel,
79 const Teuchos::RCP<Thyra::NonlinearSolverBase<Scalar> >& solver,
82 std::string ICConsistency,
83 bool ICConsistencyCheck,
84 bool zeroInitialGuess,
87 this->setStepperType(
"Backward Euler");
88 this->setUseFSAL( useFSAL);
89 this->setICConsistency( ICConsistency);
90 this->setICConsistencyCheck( ICConsistencyCheck);
91 this->setZeroInitialGuess( zeroInitialGuess);
93 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
96 this->setAppAction(stepperBEAppAction);
97 this->setSolver(solver);
98 this->setPredictor(predictorStepper);
100 if (appModel != Teuchos::null) {
101 this->setModel(appModel);
108 template<
class Scalar>
111 if (predictorType ==
"None") {
112 predictorStepper_ = Teuchos::null;
116 TEUCHOS_TEST_FOR_EXCEPTION(
117 this->wrapperModel_->getAppModel() == Teuchos::null, std::logic_error,
118 "Error - Need to set the model, setModel(), before calling "
119 "StepperBackwardEuler::setPredictor()\n");
124 sf->createStepper(predictorType, this->wrapperModel_->getAppModel());
126 this->isInitialized_ =
false;
131 template<
class Scalar>
135 predictorStepper_ = predictorStepper;
136 if (predictorStepper_ == Teuchos::null)
return;
138 TEUCHOS_TEST_FOR_EXCEPTION(
139 predictorStepper_->getModel() == Teuchos::null &&
140 this->wrapperModel_->getAppModel() == Teuchos::null, std::logic_error,
141 "Error - Need to set the model, setModel(), before calling "
142 "StepperBackwardEuler::setPredictor()\n");
144 if (predictorStepper_->getModel() == Teuchos::null)
145 predictorStepper_->setModel(this->wrapperModel_->getAppModel());
146 predictorStepper_->initialize();
148 this->isInitialized_ =
false;
152 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
153 template<
class Scalar>
157 if (obs == Teuchos::null) {
159 if (this->stepperObserver_ == Teuchos::null) {
162 this->stepperObserver_ =
166 this->stepperObserver_ = obs;
169 (this->stepperObserver_,
true);
172 this->isInitialized_ =
false;
177 template<
class Scalar>
181 if (appAction == Teuchos::null) {
183 stepperBEAppAction_ =
186 stepperBEAppAction_ = appAction;
188 this->isInitialized_ =
false;
192 template<
class Scalar>
198 RCP<SolutionState<Scalar> > initialState = solutionHistory->getCurrentState();
201 if (initialState->getXDot() == Teuchos::null)
202 this->setStepperXDot(initialState->getX()->clone_v());
206 if (this->getUseFSAL()) {
207 RCP<Teuchos::FancyOStream> out = this->getOStream();
208 Teuchos::OSTab ostab(out,1,
"StepperBackwardEuler::setInitialConditions()");
209 *out <<
"\nWarning -- The First-Step-As-Last (FSAL) principle is not "
210 <<
"needed with Backward Euler. The default is to set useFSAL=false, "
211 <<
"however useFSAL=true will also work but have no affect "
212 <<
"(i.e., no-op).\n" << std::endl;
217 template<
class Scalar>
221 this->checkInitialized();
225 TEMPUS_FUNC_TIME_MONITOR(
"Tempus::StepperBackwardEuler::takeStep()");
227 TEUCHOS_TEST_FOR_EXCEPTION(solutionHistory->getNumStates() < 2,
229 "Error - StepperBackwardEuler<Scalar>::takeStep(...)\n"
230 "Need at least two SolutionStates for Backward Euler.\n"
231 " Number of States = " << solutionHistory->getNumStates() <<
"\n"
232 "Try setting in \"Solution History\" \"Storage Type\" = \"Undo\"\n"
233 " or \"Storage Type\" = \"Static\" and \"Storage Limit\" = \"2\"\n");
235 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
236 this->stepperObserver_->observeBeginTakeStep(solutionHistory, *
this);
238 RCP<StepperBackwardEuler<Scalar> > thisStepper = Teuchos::rcpFromRef(*
this);
239 stepperBEAppAction_->execute(solutionHistory, thisStepper,
242 RCP<SolutionState<Scalar> > workingState=solutionHistory->getWorkingState();
243 RCP<SolutionState<Scalar> > currentState=solutionHistory->getCurrentState();
245 RCP<const Thyra::VectorBase<Scalar> > xOld = currentState->getX();
246 RCP<Thyra::VectorBase<Scalar> > x = workingState->getX();
248 RCP<Thyra::VectorBase<Scalar> > xDot = this->getStepperXDot(workingState);
250 computePredictor(solutionHistory);
254 const Scalar time = workingState->getTime();
255 const Scalar dt = workingState->getTimeStep();
258 Teuchos::RCP<TimeDerivative<Scalar> > timeDer =
260 Scalar(1.0)/dt,xOld));
262 const Scalar alpha = Scalar(1.0)/dt;
263 const Scalar beta = Scalar(1.0);
265 timeDer, dt, alpha, beta));
267 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
268 if (!Teuchos::is_null(stepperBEObserver_))
269 stepperBEObserver_->observeBeforeSolve(solutionHistory, *
this);
271 stepperBEAppAction_->execute(solutionHistory, thisStepper,
274 const Thyra::SolveStatus<Scalar> sStatus =
275 this->solveImplicitODE(x, xDot, time, p);
277 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
278 if (!Teuchos::is_null(stepperBEObserver_))
279 stepperBEObserver_->observeAfterSolve(solutionHistory, *
this);
281 stepperBEAppAction_->execute(solutionHistory, thisStepper,
284 if (workingState->getXDot() != Teuchos::null)
285 timeDer->compute(x, xDot);
287 workingState->setSolutionStatus(sStatus);
288 workingState->setOrder(this->getOrder());
289 workingState->computeNorms(currentState);
290 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
291 this->stepperObserver_->observeEndTakeStep(solutionHistory, *
this);
293 stepperBEAppAction_->execute(solutionHistory, thisStepper,
299 template<
class Scalar>
303 if (predictorStepper_ == Teuchos::null)
return;
304 predictorStepper_->takeStep(solutionHistory);
306 if (solutionHistory->getWorkingState()->getSolutionStatus()==
Status::FAILED) {
307 Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
308 Teuchos::OSTab ostab(out,1,
"StepperBackwardEuler::computePredictor");
309 *out <<
"Warning - predictorStepper has failed." << std::endl;
312 solutionHistory->getWorkingState()->setSolutionStatus(
Status::WORKING);
323 template<
class Scalar>
324 Teuchos::RCP<Tempus::StepperState<Scalar> >
328 Teuchos::RCP<Tempus::StepperState<Scalar> > stepperState =
334 template<
class Scalar>
336 Teuchos::FancyOStream &out,
337 const Teuchos::EVerbosityLevel verbLevel)
const
343 out <<
"--- StepperBackwardEuler ---\n";
344 if (predictorStepper_ != Teuchos::null) {
345 out <<
" predictor stepper type = "
346 << predictorStepper_->description() << std::endl;
348 out <<
" predictorStepper_ = "
349 << predictorStepper_ << std::endl;
350 out <<
" predictorStepper_->isInitialized() = "
352 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
353 out <<
" stepperBEObserver_ = "
354 << stepperBEObserver_ << std::endl;
356 out <<
" stepperBEAppAction_ = "
357 << stepperBEAppAction_ << std::endl;
358 out <<
"----------------------------" << std::endl;
362 template<
class Scalar>
365 bool isValidSetup =
true;
370 if (predictorStepper_ != Teuchos::null) {
371 if ( !predictorStepper_->isInitialized() ) {
372 isValidSetup =
false;
373 out <<
"The predictor stepper is not initialized!\n";
377 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
378 if (stepperBEObserver_ == Teuchos::null) {
379 isValidSetup =
false;
380 out <<
"The Backward Euler observer is not set!\n";
383 if (stepperBEAppAction_ == Teuchos::null) {
384 isValidSetup =
false;
385 out <<
"The Backward Euler AppAction is not set!\n";
392 template<
class Scalar>
393 Teuchos::RCP<const Teuchos::ParameterList>
396 Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
398 pl->set<std::string>(
"Solver Name",
"Default Solver");
399 pl->set<
bool> (
"Zero Initial Guess",
false);
400 pl->set<std::string>(
"Predictor Stepper Type",
"None");
402 pl->set(
"Default Solver", *solverPL);
408 template <
class Scalar>
416 template <
class Scalar>
419 Thyra::VectorBase<Scalar>& residual,
420 const Teuchos::Array< Teuchos::RCP<
const Thyra::VectorBase<Scalar> > >& x,
421 const Teuchos::Array<Scalar>& t,
422 const Thyra::VectorBase<Scalar>& p,
423 const int param_index)
const
425 typedef Thyra::ModelEvaluatorBase MEB;
426 MEB::OutArgs<Scalar> outArgs = this->wrapperModel_->getOutArgs();
427 outArgs.set_f(Teuchos::rcpFromRef(residual));
428 computeStepResidDerivImpl(outArgs, x, t, p, param_index);
431 template <
class Scalar>
434 Thyra::LinearOpBase<Scalar>& jacobian,
435 const Teuchos::Array< Teuchos::RCP<
const Thyra::VectorBase<Scalar> > >& x,
436 const Teuchos::Array<Scalar>& t,
437 const Thyra::VectorBase<Scalar>& p,
438 const int param_index,
439 const int deriv_index)
const
441 typedef Thyra::ModelEvaluatorBase MEB;
442 MEB::OutArgs<Scalar> outArgs = this->wrapperModel_->getOutArgs();
443 TEUCHOS_ASSERT(outArgs.supports(MEB::OUT_ARG_W_op));
444 outArgs.set_W_op(Teuchos::rcpFromRef(jacobian));
445 computeStepResidDerivImpl(outArgs, x, t, p, param_index, deriv_index);
448 template <
class Scalar>
451 Thyra::LinearOpBase<Scalar>& deriv,
452 const Teuchos::Array< Teuchos::RCP<
const Thyra::VectorBase<Scalar> > >& x,
453 const Teuchos::Array<Scalar>& t,
454 const Thyra::VectorBase<Scalar>& p,
455 const int param_index)
const
457 typedef Thyra::ModelEvaluatorBase MEB;
458 MEB::OutArgs<Scalar> outArgs = this->wrapperModel_->getOutArgs();
459 TEUCHOS_ASSERT(outArgs.supports(MEB::OUT_ARG_DfDp, param_index).supports(MEB::DERIV_LINEAR_OP));
460 outArgs.set_DfDp(param_index,
461 MEB::Derivative<Scalar>(Teuchos::rcpFromRef(deriv)));
462 computeStepResidDerivImpl(outArgs, x, t, p, param_index);
465 template <
class Scalar>
468 Thyra::LinearOpWithSolveBase<Scalar>& jacobian_solver,
469 const Teuchos::Array< Teuchos::RCP<
const Thyra::VectorBase<Scalar> > >& x,
470 const Teuchos::Array<Scalar>& t,
471 const Thyra::VectorBase<Scalar>& p,
472 const int param_index)
const
474 typedef Thyra::ModelEvaluatorBase MEB;
475 MEB::OutArgs<Scalar> outArgs = this->wrapperModel_->getOutArgs();
476 TEUCHOS_ASSERT(outArgs.supports(MEB::OUT_ARG_W));
477 outArgs.set_W(Teuchos::rcpFromRef(jacobian_solver));
478 computeStepResidDerivImpl(outArgs, x, t, p, param_index, 0);
481 template <
class Scalar>
484 const Thyra::ModelEvaluatorBase::OutArgs<Scalar>& outArgs,
485 const Teuchos::Array< Teuchos::RCP<
const Thyra::VectorBase<Scalar> > >& x,
486 const Teuchos::Array<Scalar>& t,
487 const Thyra::VectorBase<Scalar>& p,
488 const int param_index,
489 const int deriv_index)
const
492 typedef Thyra::ModelEvaluatorBase MEB;
494 TEUCHOS_ASSERT(x.size() == 2);
495 TEUCHOS_ASSERT(t.size() == 2);
496 RCP<const Thyra::VectorBase<Scalar> > xn = x[0];
497 RCP<const Thyra::VectorBase<Scalar> > xo = x[1];
498 const Scalar tn = t[0];
499 const Scalar to = t[1];
500 const Scalar dt = tn-to;
503 RCP<Thyra::VectorBase<Scalar> > x_dot = xn->clone_v();
504 Teuchos::RCP<TimeDerivative<Scalar> > timeDer =
506 timeDer->compute(xn, x_dot);
509 MEB::InArgs<Scalar> inArgs = this->wrapperModel_->getInArgs();
511 if (inArgs.supports(MEB::IN_ARG_x_dot )) inArgs.set_x_dot (x_dot);
512 if (inArgs.supports(MEB::IN_ARG_t )) inArgs.set_t (tn);
513 if (inArgs.supports(MEB::IN_ARG_step_size )) inArgs.set_step_size(dt);
514 inArgs.set_p(param_index, Teuchos::rcpFromRef(p));
515 TEUCHOS_ASSERT(inArgs.supports(MEB::IN_ARG_alpha));
516 TEUCHOS_ASSERT(inArgs.supports(MEB::IN_ARG_beta));
517 if (deriv_index == 0) {
519 inArgs.set_alpha(Scalar(1.0)/dt);
520 inArgs.set_beta(Scalar(1.0));
522 else if (deriv_index == 1) {
524 inArgs.set_alpha(Scalar(-1.0)/dt);
525 inArgs.set_beta(Scalar(0.0));
527 this->wrapperModel_->getAppModel()->evalModel(inArgs, outArgs);
531 #endif // Tempus_StepperBackwardEuler_impl_hpp
StepperBackwardEulerObserver class for StepperBackwardEuler.
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.
const std::string toString(const Status status)
Convert Status to string.
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.
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.
Teuchos::RCP< Teuchos::ParameterList > defaultSolverParameters()
Returns the default solver ParameterList for implicit Steppers.
Application Action for StepperBackwardEuler.
virtual void setObserver(Teuchos::RCP< StepperObserver< Scalar > > obs=Teuchos::null)
Set Observer.
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.
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.
StepperObserver class for Stepper class.
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
Time-derivative interface for Backward Euler.
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
void getValidParametersBasic(Teuchos::RCP< Teuchos::ParameterList > pl, std::string stepperType)
Provide basic parameters to Steppers.