9 #ifndef Tempus_StepperBDF2_impl_hpp
10 #define Tempus_StepperBDF2_impl_hpp
12 #include "Tempus_WrapperModelEvaluatorBasic.hpp"
14 #include "Tempus_StepperFactory.hpp"
20 template<
class Scalar>
23 this->setStepperName(
"BDF2");
24 this->setStepperType(
"BDF2");
25 this->setUseFSAL(
false);
26 this->setICConsistency(
"None");
27 this->setICConsistencyCheck(
false);
28 this->setZeroInitialGuess(
false);
30 this->setAppAction(Teuchos::null);
31 this->setDefaultSolver();
32 this->setStartUpStepper(
"DIRK 1 Stage Theta Method");
36 template<
class Scalar>
42 std::string ICConsistency,
43 bool ICConsistencyCheck,
44 bool zeroInitialGuess,
47 this->setStepperName(
"BDF2");
48 this->setStepperType(
"BDF2");
49 this->setUseFSAL( useFSAL);
50 this->setICConsistency( ICConsistency);
51 this->setICConsistencyCheck( ICConsistencyCheck);
52 this->setZeroInitialGuess( zeroInitialGuess);
54 this->setAppAction(stepperBDF2AppAction);
55 this->setSolver(solver);
56 this->setStartUpStepper(startUpStepper);
58 if (appModel != Teuchos::null) {
59 this->setModel(appModel);
64 template<
class Scalar>
69 if (startUpStepper_->getModel() == Teuchos::null) {
70 startUpStepper_->setModel(appModel);
71 startUpStepper_->initialize();
74 this->isInitialized_ =
false;
78 template<
class Scalar>
82 if (this->wrapperModel_ != Teuchos::null &&
83 this->wrapperModel_->getAppModel() != Teuchos::null) {
84 startUpStepper_ = sf->createStepper(startupStepperType,
85 this->wrapperModel_->getAppModel());
87 startUpStepper_ = sf->createStepper(startupStepperType);
90 this->isInitialized_ =
false;
94 template<
class Scalar>
98 startUpStepper_ = startUpStepper;
100 if (this->wrapperModel_ != Teuchos::null) {
102 this->wrapperModel_->getAppModel() == Teuchos::null, std::logic_error,
103 "Error - Can not set the startUpStepper to Teuchos::null.\n");
105 if (startUpStepper->getModel() == Teuchos::null &&
106 this->wrapperModel_->getAppModel() != Teuchos::null) {
107 startUpStepper_->setModel(this->wrapperModel_->getAppModel());
108 startUpStepper_->initialize();
112 this->isInitialized_ =
false;
115 template<
class Scalar>
119 if (appAction == Teuchos::null) {
121 stepperBDF2AppAction_ =
125 stepperBDF2AppAction_ = appAction;
131 template<
class Scalar>
138 template<
class Scalar>
144 RCP<SolutionState<Scalar> > initialState = solutionHistory->getCurrentState();
147 if (initialState->getXDot() == Teuchos::null)
148 this->setStepperXDot(initialState->getX()->clone_v());
150 this->setStepperXDot(initialState->getXDot());
156 template<
class Scalar>
160 this->checkInitialized();
164 TEMPUS_FUNC_TIME_MONITOR(
"Tempus::StepperBDF2::takeStep()");
166 int numStates = solutionHistory->getNumStates();
168 RCP<Thyra::VectorBase<Scalar> > xOld;
169 RCP<Thyra::VectorBase<Scalar> > xOldOld;
174 computeStartUp(solutionHistory);
178 "Error in Tempus::StepperBDF2::takeStep(): numStates after \n"
179 <<
"startup stepper must be at least 3, whereas numStates = "
180 << numStates <<
"!\n" <<
"If running with Storage Type = Static, "
181 <<
"make sure Storage Limit > 2.\n");
186 RCP<StepperBDF2<Scalar> > thisStepper = Teuchos::rcpFromRef(*
this);
187 stepperBDF2AppAction_->execute(solutionHistory, thisStepper,
191 RCP<SolutionState<Scalar> > workingState=solutionHistory->getWorkingState();
192 RCP<SolutionState<Scalar> > currentState=solutionHistory->getCurrentState();
194 RCP<Thyra::VectorBase<Scalar> > x = workingState->getX();
195 if (workingState->getXDot() != Teuchos::null)
196 this->setStepperXDot(workingState->getXDot());
197 RCP<Thyra::VectorBase<Scalar> > xDot = this->getStepperXDot();
200 const Scalar time = workingState->getTime();
201 const Scalar dt = workingState->getTimeStep();
202 const Scalar dtOld = currentState->getTimeStep();
204 xOld = solutionHistory->getStateTimeIndexNM1()->getX();
205 xOldOld = solutionHistory->getStateTimeIndexNM2()->getX();
206 order_ = Scalar(2.0);
212 const Scalar alpha = getAlpha(dt, dtOld);
213 const Scalar beta = getBeta (dt);
216 timeDer, dt, alpha, beta));
217 stepperBDF2AppAction_->execute(solutionHistory, thisStepper,
221 this->solveImplicitODE(x, xDot, time, p);
223 stepperBDF2AppAction_->execute(solutionHistory, thisStepper,
227 if (workingState->getXDot() != Teuchos::null)
228 timeDer->compute(x, xDot);
230 workingState->setSolutionStatus(sStatus);
231 workingState->setOrder(getOrder());
232 workingState->computeNorms(currentState);
233 stepperBDF2AppAction_->execute(solutionHistory, thisStepper,
239 template<
class Scalar>
243 auto out = Teuchos::fancyOStream( this->getOStream() );
244 out->setOutputToRootOnly(0);
246 *out <<
"Warning -- Taking a startup step for BDF2 using '"
247 << startUpStepper_->getStepperType()<<
"'!" << std::endl;
250 startUpStepper_->takeStep(solutionHistory);
252 order_ = startUpStepper_->getOrder();
261 template<
class Scalar>
272 template<
class Scalar>
277 auto l_out = Teuchos::fancyOStream( out.
getOStream() );
278 l_out->setOutputToRootOnly(0);
283 *l_out <<
"--- StepperBDF2 ---\n";
284 if (startUpStepper_ != Teuchos::null) {
285 *l_out <<
" startup stepper type = "
286 << startUpStepper_->description() << std::endl;
288 *l_out <<
" startUpStepper_ = "
289 << startUpStepper_ << std::endl;
290 *l_out <<
" startUpStepper_->isInitialized() = "
292 *l_out <<
" stepperBDF2AppAction_ = "
293 << stepperBDF2AppAction_ << std::endl;
294 *l_out <<
"----------------------------" << std::endl;
295 *l_out <<
" order_ = " << order_ << std::endl;
296 *l_out <<
"-------------------" << std::endl;
300 template<
class Scalar>
303 bool isValidSetup =
true;
304 auto l_out = Teuchos::fancyOStream( out.
getOStream() );
305 l_out->setOutputToRootOnly(0);
310 if ( !this->startUpStepper_->isInitialized() ) {
311 isValidSetup =
false;
312 *l_out <<
"The startup stepper is not initialized!\n";
314 if (stepperBDF2AppAction_ == Teuchos::null) {
315 isValidSetup =
false;
316 *l_out <<
"The BDF2 AppAction is not set!\n";
322 template<
class Scalar>
326 auto pl = this->getValidParametersBasicImplicit();
327 pl->set(
"Start Up Stepper Type", startUpStepper_->getStepperType());
334 template<
class Scalar>
341 stepper->setStepperImplicitValues(pl);
343 if (model != Teuchos::null) {
344 stepper->setModel(model);
346 std::string startUpStepperName =
"DIRK 1 Stage Theta Method";
347 if (pl != Teuchos::null) startUpStepperName =
348 pl->
get<std::string>(
"Start Up Stepper Type", startUpStepperName);
349 stepper->setStartUpStepper(startUpStepperName);
350 stepper->initialize();
358 #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.
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)
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
virtual void setModel(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &appModel)
Default modifier for StepperBDF2.
virtual void initialize()
Initialize after construction and changing input parameters.
virtual void computeStartUp(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Compute the first time step given the supplied startup stepper.
Thyra Base interface for time steppers.
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.
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
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)