Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_StepperBDF2_impl.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ****************************************************************************
3 // Tempus: Copyright (2017) Sandia Corporation
4 //
5 // Distributed under BSD 3-clause license (See accompanying file Copyright.txt)
6 // ****************************************************************************
7 // @HEADER
8 
9 #ifndef Tempus_StepperBDF2_impl_hpp
10 #define Tempus_StepperBDF2_impl_hpp
11 
12 #include "Tempus_WrapperModelEvaluatorBasic.hpp"
14 #include "Tempus_StepperFactory.hpp"
15 
16 
17 namespace Tempus {
18 
19 
20 template<class Scalar>
22 {
23  this->setStepperName( "BDF2");
24  this->setStepperType( "BDF2");
25  this->setUseFSAL( false);
26  this->setICConsistency( "None");
27  this->setICConsistencyCheck( false);
28  this->setZeroInitialGuess( false);
29 
30  this->setAppAction(Teuchos::null);
31  this->setDefaultSolver();
32  this->setStartUpStepper("DIRK 1 Stage Theta Method");
33 
34 }
35 
36 template<class Scalar>
38  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> >& appModel,
40  const Teuchos::RCP<Stepper<Scalar> >& startUpStepper,
41  bool useFSAL,
42  std::string ICConsistency,
43  bool ICConsistencyCheck,
44  bool zeroInitialGuess,
45  const Teuchos::RCP<StepperBDF2AppAction<Scalar> >& stepperBDF2AppAction)
46 {
47  this->setStepperName( "BDF2");
48  this->setStepperType( "BDF2");
49  this->setUseFSAL( useFSAL);
50  this->setICConsistency( ICConsistency);
51  this->setICConsistencyCheck( ICConsistencyCheck);
52  this->setZeroInitialGuess( zeroInitialGuess);
53 
54  this->setAppAction(stepperBDF2AppAction);
55  this->setSolver(solver);
56  this->setStartUpStepper(startUpStepper);
57 
58  if (appModel != Teuchos::null) {
59  this->setModel(appModel);
60  this->initialize();
61  }
62 }
63 
64 template<class Scalar>
66  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> >& appModel)
67 {
69  if (startUpStepper_->getModel() == Teuchos::null) {
70  startUpStepper_->setModel(appModel);
71  startUpStepper_->initialize();
72  }
73 
74  this->isInitialized_ = false;
75 }
76 
77 
78 template<class Scalar>
79 void StepperBDF2<Scalar>::setStartUpStepper(std::string startupStepperType)
80 {
81  auto sf = Teuchos::rcp(new StepperFactory<Scalar>());
82  if (this->wrapperModel_ != Teuchos::null &&
83  this->wrapperModel_->getAppModel() != Teuchos::null) {
84  startUpStepper_ = sf->createStepper(startupStepperType,
85  this->wrapperModel_->getAppModel());
86  } else {
87  startUpStepper_ = sf->createStepper(startupStepperType);
88  }
89 
90  this->isInitialized_ = false;
91 }
92 
93 
94 template<class Scalar>
96  Teuchos::RCP<Stepper<Scalar> > startUpStepper)
97 {
98  startUpStepper_ = startUpStepper;
99 
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");
104 
105  if (startUpStepper->getModel() == Teuchos::null &&
106  this->wrapperModel_->getAppModel() != Teuchos::null) {
107  startUpStepper_->setModel(this->wrapperModel_->getAppModel());
108  startUpStepper_->initialize();
109  }
110  }
111 
112  this->isInitialized_ = false;
113 }
114 
115 template<class Scalar>
118 {
119  if (appAction == Teuchos::null) {
120  // Create default appAction
121  stepperBDF2AppAction_ =
123  }
124  else {
125  stepperBDF2AppAction_ = appAction;
126  }
127 }
128 
129 
130 
131 template<class Scalar>
133 {
135 }
136 
137 
138 template<class Scalar>
140  const Teuchos::RCP<SolutionHistory<Scalar> >& solutionHistory)
141 {
142  using Teuchos::RCP;
143 
144  RCP<SolutionState<Scalar> > initialState = solutionHistory->getCurrentState();
145 
146  // Check if we need Stepper storage for xDot
147  if (initialState->getXDot() == Teuchos::null)
148  this->setStepperXDot(initialState->getX()->clone_v());
149  else
150  this->setStepperXDot(initialState->getXDot());
151 
153 }
154 
155 
156 template<class Scalar>
158  const Teuchos::RCP<SolutionHistory<Scalar> >& solutionHistory)
159 {
160  this->checkInitialized();
161 
162  using Teuchos::RCP;
163 
164  TEMPUS_FUNC_TIME_MONITOR("Tempus::StepperBDF2::takeStep()");
165  {
166  int numStates = solutionHistory->getNumStates();
167 
168  RCP<Thyra::VectorBase<Scalar> > xOld;
169  RCP<Thyra::VectorBase<Scalar> > xOldOld;
170 
171  // If there are less than 3 states (e.g., first time step), call
172  // startup stepper and return.
173  if (numStates < 3) {
174  computeStartUp(solutionHistory);
175  return;
176  }
177  TEUCHOS_TEST_FOR_EXCEPTION( (numStates < 3), std::logic_error,
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");
182 
183  //IKT, FIXME: add error checking regarding states being consecutive and
184  //whether interpolated states are OK to use.
185 
186  RCP<StepperBDF2<Scalar> > thisStepper = Teuchos::rcpFromRef(*this);
187  stepperBDF2AppAction_->execute(solutionHistory, thisStepper,
189 
190 
191  RCP<SolutionState<Scalar> > workingState=solutionHistory->getWorkingState();
192  RCP<SolutionState<Scalar> > currentState=solutionHistory->getCurrentState();
193 
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();
198 
199  //get time, dt and dtOld
200  const Scalar time = workingState->getTime();
201  const Scalar dt = workingState->getTimeStep();
202  const Scalar dtOld = currentState->getTimeStep();
203 
204  xOld = solutionHistory->getStateTimeIndexNM1()->getX();
205  xOldOld = solutionHistory->getStateTimeIndexNM2()->getX();
206  order_ = Scalar(2.0);
207 
208  // Setup TimeDerivative
210  Teuchos::rcp(new StepperBDF2TimeDerivative<Scalar>(dt, dtOld, xOld, xOldOld));
211 
212  const Scalar alpha = getAlpha(dt, dtOld);
213  const Scalar beta = getBeta (dt);
214 
216  timeDer, dt, alpha, beta));
217  stepperBDF2AppAction_->execute(solutionHistory, thisStepper,
219 
220  const Thyra::SolveStatus<Scalar> sStatus =
221  this->solveImplicitODE(x, xDot, time, p);
222 
223  stepperBDF2AppAction_->execute(solutionHistory, thisStepper,
225 
226 
227  if (workingState->getXDot() != Teuchos::null)
228  timeDer->compute(x, xDot);
229 
230  workingState->setSolutionStatus(sStatus); // Converged --> pass.
231  workingState->setOrder(getOrder());
232  workingState->computeNorms(currentState);
233  stepperBDF2AppAction_->execute(solutionHistory, thisStepper,
235  }
236  return;
237 }
238 
239 template<class Scalar>
241  const Teuchos::RCP<SolutionHistory<Scalar> >& solutionHistory)
242 {
243  auto out = Teuchos::fancyOStream( this->getOStream() );
244  out->setOutputToRootOnly(0);
245  Teuchos::OSTab ostab(out,1,"StepperBDF2::computeStartUp()");
246  *out << "Warning -- Taking a startup step for BDF2 using '"
247  << startUpStepper_->getStepperType()<<"'!" << std::endl;
248 
249  //Take one step using startUpStepper_
250  startUpStepper_->takeStep(solutionHistory);
251 
252  order_ = startUpStepper_->getOrder();
253 }
254 
261 template<class Scalar>
265 {
267  rcp(new StepperState<Scalar>(this->getStepperType()));
268  return stepperState;
269 }
270 
271 
272 template<class Scalar>
275  const Teuchos::EVerbosityLevel verbLevel ) const
276 {
277  auto l_out = Teuchos::fancyOStream( out.getOStream() );
278  l_out->setOutputToRootOnly(0);
279  *l_out << std::endl;
280  Stepper<Scalar>::describe(out, verbLevel);
281  StepperImplicit<Scalar>::describe(out, verbLevel);
282 
283  *l_out << "--- StepperBDF2 ---\n";
284  if (startUpStepper_ != Teuchos::null) {
285  *l_out << " startup stepper type = "
286  << startUpStepper_->description() << std::endl;
287  }
288  *l_out << " startUpStepper_ = "
289  << startUpStepper_ << std::endl;
290  *l_out << " startUpStepper_->isInitialized() = "
291  << Teuchos::toString(startUpStepper_->isInitialized()) << std::endl;
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;
297 }
298 
299 
300 template<class Scalar>
302 {
303  bool isValidSetup = true;
304  auto l_out = Teuchos::fancyOStream( out.getOStream() );
305  l_out->setOutputToRootOnly(0);
306 
307  if ( !Stepper<Scalar>::isValidSetup(out) ) isValidSetup = false;
308  if ( !StepperImplicit<Scalar>::isValidSetup(out) ) isValidSetup = false;
309 
310  if ( !this->startUpStepper_->isInitialized() ) {
311  isValidSetup = false;
312  *l_out << "The startup stepper is not initialized!\n";
313  }
314  if (stepperBDF2AppAction_ == Teuchos::null) {
315  isValidSetup = false;
316  *l_out << "The BDF2 AppAction is not set!\n";
317  }
318  return isValidSetup;
319 }
320 
321 
322 template<class Scalar>
325 {
326  auto pl = this->getValidParametersBasicImplicit();
327  pl->set("Start Up Stepper Type", startUpStepper_->getStepperType());
328  return pl;
329 }
330 
331 
332 // Nonmember constructor - ModelEvaluator and ParameterList
333 // ------------------------------------------------------------------------
334 template<class Scalar>
337  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> >& model,
339 {
340  auto stepper = Teuchos::rcp(new StepperBDF2<Scalar>());
341  stepper->setStepperImplicitValues(pl);
342 
343  if (model != Teuchos::null) {
344  stepper->setModel(model);
345 
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();
351  }
352 
353  return stepper;
354 }
355 
356 
357 } // namespace Tempus
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)
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)