10 #ifndef Tempus_StepperBDF2_impl_hpp
11 #define Tempus_StepperBDF2_impl_hpp
13 #include "Tempus_WrapperModelEvaluatorBasic.hpp"
15 #include "Tempus_StepperFactory.hpp"
19 template <
class Scalar>
22 this->setStepperName(
"BDF2");
23 this->setStepperType(
"BDF2");
24 this->setUseFSAL(
false);
25 this->setICConsistency(
"None");
26 this->setICConsistencyCheck(
false);
27 this->setZeroInitialGuess(
false);
29 this->setAppAction(Teuchos::null);
30 this->setDefaultSolver();
31 this->setStartUpStepper(
"DIRK 1 Stage Theta Method");
34 template <
class Scalar>
39 std::string ICConsistency,
bool ICConsistencyCheck,
bool zeroInitialGuess,
42 this->setStepperName(
"BDF2");
43 this->setStepperType(
"BDF2");
44 this->setUseFSAL(useFSAL);
45 this->setICConsistency(ICConsistency);
46 this->setICConsistencyCheck(ICConsistencyCheck);
47 this->setZeroInitialGuess(zeroInitialGuess);
49 this->setAppAction(stepperBDF2AppAction);
50 this->setSolver(solver);
51 this->setStartUpStepper(startUpStepper);
53 if (appModel != Teuchos::null) {
54 this->setModel(appModel);
59 template <
class Scalar>
65 if (startUpStepper_->getModel() == Teuchos::null) {
66 startUpStepper_->setModel(appModel);
67 startUpStepper_->initialize();
70 this->isInitialized_ =
false;
73 template <
class Scalar>
77 if (this->wrapperModel_ != Teuchos::null &&
78 this->wrapperModel_->getAppModel() != Teuchos::null) {
79 startUpStepper_ = sf->createStepper(startupStepperType,
80 this->wrapperModel_->getAppModel());
83 startUpStepper_ = sf->createStepper(startupStepperType);
86 this->isInitialized_ =
false;
89 template <
class Scalar>
93 startUpStepper_ = startUpStepper;
95 if (this->wrapperModel_ != Teuchos::null) {
97 this->wrapperModel_->getAppModel() == Teuchos::null, std::logic_error,
98 "Error - Can not set the startUpStepper to Teuchos::null.\n");
100 if (startUpStepper->getModel() == Teuchos::null &&
101 this->wrapperModel_->getAppModel() != Teuchos::null) {
102 startUpStepper_->setModel(this->wrapperModel_->getAppModel());
103 startUpStepper_->initialize();
107 this->isInitialized_ =
false;
110 template <
class Scalar>
114 if (appAction == Teuchos::null) {
116 stepperBDF2AppAction_ =
120 stepperBDF2AppAction_ = appAction;
124 template <
class Scalar>
130 template <
class Scalar>
136 RCP<SolutionState<Scalar> > initialState = solutionHistory->getCurrentState();
139 if (initialState->getXDot() == Teuchos::null)
140 this->setStepperXDot(initialState->getX()->clone_v());
142 this->setStepperXDot(initialState->getXDot());
147 template <
class Scalar>
151 this->checkInitialized();
155 TEMPUS_FUNC_TIME_MONITOR(
"Tempus::StepperBDF2::takeStep()");
157 int numStates = solutionHistory->getNumStates();
159 RCP<Thyra::VectorBase<Scalar> > xOld;
160 RCP<Thyra::VectorBase<Scalar> > xOldOld;
165 computeStartUp(solutionHistory);
169 (numStates < 3), std::logic_error,
170 "Error in Tempus::StepperBDF2::takeStep(): numStates after \n"
171 <<
"startup stepper must be at least 3, whereas numStates = "
172 << numStates <<
"!\n"
173 <<
"If running with Storage Type = Static, "
174 <<
"make sure Storage Limit > 2.\n");
179 RCP<StepperBDF2<Scalar> > thisStepper = Teuchos::rcpFromRef(*
this);
180 stepperBDF2AppAction_->execute(
181 solutionHistory, thisStepper,
184 RCP<SolutionState<Scalar> > workingState =
185 solutionHistory->getWorkingState();
186 RCP<SolutionState<Scalar> > currentState =
187 solutionHistory->getCurrentState();
189 RCP<Thyra::VectorBase<Scalar> > x = workingState->getX();
190 if (workingState->getXDot() != Teuchos::null)
191 this->setStepperXDot(workingState->getXDot());
192 RCP<Thyra::VectorBase<Scalar> > xDot = this->getStepperXDot();
195 const Scalar time = workingState->getTime();
196 const Scalar dt = workingState->getTimeStep();
197 const Scalar dtOld = currentState->getTimeStep();
199 xOld = solutionHistory->getStateTimeIndexNM1()->getX();
200 xOldOld = solutionHistory->getStateTimeIndexNM2()->getX();
201 order_ = Scalar(2.0);
207 const Scalar alpha = getAlpha(dt, dtOld);
208 const Scalar beta = getBeta(dt);
212 stepperBDF2AppAction_->execute(
213 solutionHistory, thisStepper,
217 this->solveImplicitODE(x, xDot, time, p);
219 stepperBDF2AppAction_->execute(
220 solutionHistory, thisStepper,
223 if (workingState->getXDot() != Teuchos::null) timeDer->compute(x, xDot);
225 workingState->setSolutionStatus(sStatus);
226 workingState->setOrder(getOrder());
227 workingState->computeNorms(currentState);
228 stepperBDF2AppAction_->execute(
229 solutionHistory, thisStepper,
235 template <
class Scalar>
239 auto out = Teuchos::fancyOStream(this->getOStream());
240 out->setOutputToRootOnly(0);
242 *out <<
"Warning -- Taking a startup step for BDF2 using '"
243 << startUpStepper_->getStepperType() <<
"'!" << std::endl;
246 startUpStepper_->takeStep(solutionHistory);
248 order_ = startUpStepper_->getOrder();
257 template <
class Scalar>
266 template <
class Scalar>
270 auto l_out = Teuchos::fancyOStream(out.
getOStream());
272 l_out->setOutputToRootOnly(0);
277 *l_out <<
"--- StepperBDF2 ---\n";
278 if (startUpStepper_ != Teuchos::null) {
279 *l_out <<
" startup stepper type = "
280 << startUpStepper_->description() << std::endl;
282 *l_out <<
" startUpStepper_ = " << startUpStepper_
284 *l_out <<
" startUpStepper_->isInitialized() = "
286 *l_out <<
" stepperBDF2AppAction_ = " << stepperBDF2AppAction_
288 *l_out <<
"----------------------------" << std::endl;
289 *l_out <<
" order_ = " << order_ << std::endl;
290 *l_out <<
"-------------------" << std::endl;
293 template <
class Scalar>
296 bool isValidSetup =
true;
297 auto l_out = Teuchos::fancyOStream(out.
getOStream());
298 l_out->setOutputToRootOnly(0);
303 if (!this->startUpStepper_->isInitialized()) {
304 isValidSetup =
false;
305 *l_out <<
"The startup stepper is not initialized!\n";
307 if (stepperBDF2AppAction_ == Teuchos::null) {
308 isValidSetup =
false;
309 *l_out <<
"The BDF2 AppAction is not set!\n";
314 template <
class Scalar>
318 auto pl = this->getValidParametersBasicImplicit();
319 pl->set(
"Start Up Stepper Type", startUpStepper_->getStepperType());
325 template <
class Scalar>
331 stepper->setStepperImplicitValues(pl);
333 std::string startUpStepperName =
"DIRK 1 Stage Theta Method";
334 if (pl != Teuchos::null)
336 pl->
get<std::string>(
"Start Up Stepper Type", startUpStepperName);
337 stepper->setStartUpStepper(startUpStepperName);
339 if (model != Teuchos::null) {
340 stepper->setModel(model);
341 stepper->initialize();
348 #endif // Tempus_StepperBDF2_impl_hpp
BDF2 (Backward-Difference-Formula-2) time stepper.
virtual void setInitialConditions(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Set the initial conditions and make them consistent.
Time-derivative interface for BDF2.
virtual Teuchos::RCP< Tempus::StepperState< Scalar > > getDefaultStepperState()
Get a default (initial) StepperState.
virtual void setModel(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &appModel)
Set the model.
T & get(const std::string &name, T def_value)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
Default modifier for StepperBDF2.
virtual void initialize()
Initialize after construction and changing input parameters.
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const override
virtual void computeStartUp(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Compute the first time step given the supplied startup stepper.
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 setAppAction(Teuchos::RCP< StepperBDF2AppAction< Scalar > > appAction)
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
virtual bool isValidSetup(Teuchos::FancyOStream &out) const
RCP< std::basic_ostream< char_type, traits_type > > getOStream()
Teuchos::RCP< StepperBDF2< Scalar > > createStepperBDF2(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &model, Teuchos::RCP< Teuchos::ParameterList > pl)
Nonmember constructor - ModelEvaluator and ParameterList.
Application Action for StepperBDF2.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
virtual void takeStep(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Take the specified timestep, dt, and return true if successful.
void setStartUpStepper(std::string startupStepperType)
Set the stepper to use in first step.
virtual void initialize()
Initialize during construction and after changing input parameters.
StepperBDF2()
Default constructor.
std::string toString(const T &t)