9 #ifndef Tempus_StepperBDF2_impl_hpp
10 #define Tempus_StepperBDF2_impl_hpp
12 #include "Tempus_WrapperModelEvaluatorBasic.hpp"
14 #include "Tempus_StepperFactory.hpp"
18 template <
class Scalar>
21 this->setStepperName(
"BDF2");
22 this->setStepperType(
"BDF2");
23 this->setUseFSAL(
false);
24 this->setICConsistency(
"None");
25 this->setICConsistencyCheck(
false);
26 this->setZeroInitialGuess(
false);
28 this->setAppAction(Teuchos::null);
29 this->setDefaultSolver();
30 this->setStartUpStepper(
"DIRK 1 Stage Theta Method");
33 template <
class Scalar>
38 std::string ICConsistency,
bool ICConsistencyCheck,
bool zeroInitialGuess,
41 this->setStepperName(
"BDF2");
42 this->setStepperType(
"BDF2");
43 this->setUseFSAL(useFSAL);
44 this->setICConsistency(ICConsistency);
45 this->setICConsistencyCheck(ICConsistencyCheck);
46 this->setZeroInitialGuess(zeroInitialGuess);
48 this->setAppAction(stepperBDF2AppAction);
49 this->setSolver(solver);
50 this->setStartUpStepper(startUpStepper);
52 if (appModel != Teuchos::null) {
53 this->setModel(appModel);
58 template <
class Scalar>
64 if (startUpStepper_->getModel() == Teuchos::null) {
65 startUpStepper_->setModel(appModel);
66 startUpStepper_->initialize();
69 this->isInitialized_ =
false;
72 template <
class Scalar>
76 if (this->wrapperModel_ != Teuchos::null &&
77 this->wrapperModel_->getAppModel() != Teuchos::null) {
78 startUpStepper_ = sf->createStepper(startupStepperType,
79 this->wrapperModel_->getAppModel());
82 startUpStepper_ = sf->createStepper(startupStepperType);
85 this->isInitialized_ =
false;
88 template <
class Scalar>
92 startUpStepper_ = startUpStepper;
94 if (this->wrapperModel_ != Teuchos::null) {
96 this->wrapperModel_->getAppModel() == Teuchos::null, std::logic_error,
97 "Error - Can not set the startUpStepper to Teuchos::null.\n");
99 if (startUpStepper->getModel() == Teuchos::null &&
100 this->wrapperModel_->getAppModel() != Teuchos::null) {
101 startUpStepper_->setModel(this->wrapperModel_->getAppModel());
102 startUpStepper_->initialize();
106 this->isInitialized_ =
false;
109 template <
class Scalar>
113 if (appAction == Teuchos::null) {
115 stepperBDF2AppAction_ =
119 stepperBDF2AppAction_ = appAction;
123 template <
class Scalar>
129 template <
class Scalar>
135 RCP<SolutionState<Scalar> > initialState = solutionHistory->getCurrentState();
138 if (initialState->getXDot() == Teuchos::null)
139 this->setStepperXDot(initialState->getX()->clone_v());
141 this->setStepperXDot(initialState->getXDot());
146 template <
class Scalar>
150 this->checkInitialized();
154 TEMPUS_FUNC_TIME_MONITOR(
"Tempus::StepperBDF2::takeStep()");
156 int numStates = solutionHistory->getNumStates();
158 RCP<Thyra::VectorBase<Scalar> > xOld;
159 RCP<Thyra::VectorBase<Scalar> > xOldOld;
164 computeStartUp(solutionHistory);
168 (numStates < 3), std::logic_error,
169 "Error in Tempus::StepperBDF2::takeStep(): numStates after \n"
170 <<
"startup stepper must be at least 3, whereas numStates = "
171 << numStates <<
"!\n"
172 <<
"If running with Storage Type = Static, "
173 <<
"make sure Storage Limit > 2.\n");
178 RCP<StepperBDF2<Scalar> > thisStepper = Teuchos::rcpFromRef(*
this);
179 stepperBDF2AppAction_->execute(
180 solutionHistory, thisStepper,
183 RCP<SolutionState<Scalar> > workingState =
184 solutionHistory->getWorkingState();
185 RCP<SolutionState<Scalar> > currentState =
186 solutionHistory->getCurrentState();
188 RCP<Thyra::VectorBase<Scalar> > x = workingState->getX();
189 if (workingState->getXDot() != Teuchos::null)
190 this->setStepperXDot(workingState->getXDot());
191 RCP<Thyra::VectorBase<Scalar> > xDot = this->getStepperXDot();
194 const Scalar time = workingState->getTime();
195 const Scalar dt = workingState->getTimeStep();
196 const Scalar dtOld = currentState->getTimeStep();
198 xOld = solutionHistory->getStateTimeIndexNM1()->getX();
199 xOldOld = solutionHistory->getStateTimeIndexNM2()->getX();
200 order_ = Scalar(2.0);
206 const Scalar alpha = getAlpha(dt, dtOld);
207 const Scalar beta = getBeta(dt);
211 stepperBDF2AppAction_->execute(
212 solutionHistory, thisStepper,
216 this->solveImplicitODE(x, xDot, time, p);
218 stepperBDF2AppAction_->execute(
219 solutionHistory, thisStepper,
222 if (workingState->getXDot() != Teuchos::null) timeDer->compute(x, xDot);
224 workingState->setSolutionStatus(sStatus);
225 workingState->setOrder(getOrder());
226 workingState->computeNorms(currentState);
227 stepperBDF2AppAction_->execute(
228 solutionHistory, thisStepper,
234 template <
class Scalar>
238 auto out = Teuchos::fancyOStream(this->getOStream());
239 out->setOutputToRootOnly(0);
241 *out <<
"Warning -- Taking a startup step for BDF2 using '"
242 << startUpStepper_->getStepperType() <<
"'!" << std::endl;
245 startUpStepper_->takeStep(solutionHistory);
247 order_ = startUpStepper_->getOrder();
256 template <
class Scalar>
265 template <
class Scalar>
269 auto l_out = Teuchos::fancyOStream(out.
getOStream());
271 l_out->setOutputToRootOnly(0);
276 *l_out <<
"--- StepperBDF2 ---\n";
277 if (startUpStepper_ != Teuchos::null) {
278 *l_out <<
" startup stepper type = "
279 << startUpStepper_->description() << std::endl;
281 *l_out <<
" startUpStepper_ = " << startUpStepper_
283 *l_out <<
" startUpStepper_->isInitialized() = "
285 *l_out <<
" stepperBDF2AppAction_ = " << stepperBDF2AppAction_
287 *l_out <<
"----------------------------" << std::endl;
288 *l_out <<
" order_ = " << order_ << std::endl;
289 *l_out <<
"-------------------" << std::endl;
292 template <
class Scalar>
295 bool isValidSetup =
true;
296 auto l_out = Teuchos::fancyOStream(out.
getOStream());
297 l_out->setOutputToRootOnly(0);
302 if (!this->startUpStepper_->isInitialized()) {
303 isValidSetup =
false;
304 *l_out <<
"The startup stepper is not initialized!\n";
306 if (stepperBDF2AppAction_ == Teuchos::null) {
307 isValidSetup =
false;
308 *l_out <<
"The BDF2 AppAction is not set!\n";
313 template <
class Scalar>
317 auto pl = this->getValidParametersBasicImplicit();
318 pl->set(
"Start Up Stepper Type", startUpStepper_->getStepperType());
324 template <
class Scalar>
330 stepper->setStepperImplicitValues(pl);
332 std::string startUpStepperName =
"DIRK 1 Stage Theta Method";
333 if (pl != Teuchos::null)
335 pl->
get<std::string>(
"Start Up Stepper Type", startUpStepperName);
336 stepper->setStartUpStepper(startUpStepperName);
338 if (model != Teuchos::null) {
339 stepper->setModel(model);
340 stepper->initialize();
347 #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)