Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Tempus_StepperBDF2_decl.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_decl_hpp
10 #define Tempus_StepperBDF2_decl_hpp
11 
12 #include "Tempus_StepperImplicit.hpp"
14 #include "Tempus_StepperObserverComposite.hpp"
16 
17 
18 namespace Tempus {
19 
20 /** \brief BDF2 (Backward-Difference-Formula-2) time stepper.
21  *
22  * For the implicit ODE system, \f$f(\dot{x},x,t) = 0\f$,
23  * the solution, \f$\dot{x}\f$ and \f$x\f$, is determined using a
24  * solver (e.g., a non-linear solver, like NOX). This stepper allows
25  * for a variable time-step, \f$\Delta t\f$. It is a 2-step method.
26  *
27  * <b> Algorithm </b>
28  * - For \f$n=0\f$, set the initial condition, \f$x_0\f$.
29  * - For \f$n=1\f$, use a one-step startup stepper, e.g., Backward Euler
30  * or RK4. The default startup stepper is 'IRK 1 Stage Theta Method'
31  * which second order.
32  * - For \f$n>1\f$, solve for \f$x_n\f$ via
33  * \f$ f\left(x_n, \dot{x}_n, t_n\right) = 0\f$
34  * where \f$
35  * \dot{x}_{n} = \frac{2\tau_n + \tau_{n-1}}{\tau_n + \tau_{n-1}}
36  * \left[ \frac{x_n-x_{n-1}}{\tau_n}\right]
37  * - \frac{\tau_n}{\tau_n + \tau_{n-1}}
38  * \left[ \frac{x_{n-1}-x_{n-2}}{\tau_{n-1}}\right], \f$
39  * and \f$\Delta t_n = \tau_n = t_n - t_{n-1}\f$.
40  * - \f$\dot{x}_n \leftarrow
41  * \dot{x}_{n} = \frac{2\tau_n + \tau_{n-1}}{\tau_n + \tau_{n-1}}
42  * \left[ \frac{x_n-x_{n-1}}{\tau_n}\right]
43  * - \frac{\tau_n}{\tau_n + \tau_{n-1}}
44  * \left[ \frac{x_{n-1}-x_{n-2}}{\tau_{n-1}}\right], \f$
45  *
46  * The First-Step-As-Last (FSAL) principle is not needed BDF2.
47  * The default is to set useFSAL=false, however useFSAL=true will also work
48  * but have no affect (i.e., no-op).
49  */
50 template<class Scalar>
51 class StepperBDF2 : virtual public Tempus::StepperImplicit<Scalar>
52 {
53 public:
54 
55  /** \brief Default constructor.
56  *
57  * - Requires the following calls before takeStep():
58  * setModel(), setSolver(), setStartUpStepper() and initialize().
59  */
60  StepperBDF2();
61 
62  /** \brief Constructor.
63  *
64  * Constructs a fully initialized stepper.
65  */
67  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> >& appModel,
68  const Teuchos::RCP<StepperObserver<Scalar> >& obs,
69  const Teuchos::RCP<Thyra::NonlinearSolverBase<Scalar> >& solver,
70  const Teuchos::RCP<Stepper<Scalar> >& startUpStepper,
71  bool useFSAL,
72  std::string ICConsistency,
73  bool ICConsistencyCheck,
74  bool zeroInitialGuess);
75 
76  /// \name Basic stepper methods
77  //@{
78  virtual void setObserver(
79  Teuchos::RCP<StepperObserver<Scalar> > obs = Teuchos::null);
80 
81  virtual Teuchos::RCP<StepperObserver<Scalar> > getObserver() const
82  { return this->stepperObserver_; }
83 
84  /// Set the stepper to use in first step
85  void setStartUpStepper(std::string startupStepperType =
86  "DIRK 1 Stage Theta Method");
87  void setStartUpStepper(Teuchos::RCP<Stepper<Scalar> > startupStepper);
88 
89  /// Initialize during construction and after changing input parameters.
90  virtual void initialize();
91 
92  /// Set the initial conditions and make them consistent.
93  virtual void setInitialConditions (
94  const Teuchos::RCP<SolutionHistory<Scalar> >& solutionHistory);
95 
96  /// Take the specified timestep, dt, and return true if successful.
97  virtual void takeStep(
98  const Teuchos::RCP<SolutionHistory<Scalar> >& solutionHistory);
99 
100  /// Get a default (initial) StepperState
101  virtual Teuchos::RCP<Tempus::StepperState<Scalar> > getDefaultStepperState();
102  virtual Scalar getOrder() const {return order_;}
103  virtual Scalar getOrderMin() const {return 1.0;}
104  virtual Scalar getOrderMax() const {return 2.0;}
105 
106  virtual bool isExplicit() const {return false;}
107  virtual bool isImplicit() const {return true;}
108  virtual bool isExplicitImplicit() const
109  {return isExplicit() and isImplicit();}
110  virtual bool isOneStepMethod() const {return false;}
111  virtual bool isMultiStepMethod() const {return !isOneStepMethod();}
112 
113  virtual OrderODE getOrderODE() const {return FIRST_ORDER_ODE;}
114  //@}
115 
116  /// Return alpha = d(xDot)/dx.
117  virtual Scalar getAlpha(const Scalar dt) const {return getAlpha(dt,dt);}
118  virtual Scalar getAlpha(const Scalar dt, const Scalar dtOld) const
119  { return (Scalar(2.0)*dt + dtOld)/(dt*(dt + dtOld)); }
120  /// Return beta = d(x)/dx.
121  virtual Scalar getBeta (const Scalar ) const { return Scalar(1.0); }
122 
123  /// Compute the first time step given the supplied startup stepper
124  virtual void computeStartUp(
125  const Teuchos::RCP<SolutionHistory<Scalar> >& solutionHistory);
126 
127  virtual bool getICConsistencyCheckDefault() const { return false; }
128  Teuchos::RCP<const Teuchos::ParameterList> getValidParameters() const;
129 
130  /// \name Overridden from Teuchos::Describable
131  //@{
132  virtual void describe(Teuchos::FancyOStream & out,
133  const Teuchos::EVerbosityLevel verbLevel) const;
134  //@}
135 
136 private:
137 
138  Teuchos::RCP<Stepper<Scalar> > startUpStepper_;
139  Teuchos::RCP<StepperObserverComposite<Scalar> > stepperObserver_;
140  Teuchos::RCP<StepperBDF2Observer<Scalar> > stepperBDF2Observer_;
141  Scalar order_;
142 };
143 
144 /** \brief Time-derivative interface for BDF2.
145  *
146  * Given the state \f$x_n\f$, compute the BDF2 time-derivative,
147  * \f[
148  * \dot{x}_{n} = \frac{2\tau_n + \tau_{n-1}}{\tau_n + \tau_{n-1}}
149  * \left[ \frac{x_n-x_{n-1}}{\tau_n}\right]
150  * - \frac{\tau_n}{\tau_n + \tau_{n-1}}
151  * \left[ \frac{x_{n-1}-x_{n-2}}{\tau_{n-1}}\right]
152  * \f]
153  * where
154  * \f[
155  * \tau_n = t_n - t_{n-1}.
156  * \f]
157  * \f$\ddot{x}\f$ is not used and set to null.
158  */
159 template <typename Scalar>
161  : virtual public Tempus::TimeDerivative<Scalar>
162 {
163 public:
164 
165  /// Constructor
167  Scalar dt, Scalar dtOld, Teuchos::RCP<const Thyra::VectorBase<Scalar> > xOld,
168  Teuchos::RCP<const Thyra::VectorBase<Scalar> > xOldOld)
169  { initialize(dt, dtOld, xOld, xOldOld); }
170 
171  /// Destructor
173 
174  /// Compute the time derivative.
175  virtual void compute(
176  Teuchos::RCP<const Thyra::VectorBase<Scalar> > x,
177  Teuchos::RCP< Thyra::VectorBase<Scalar> > xDot,
178  Teuchos::RCP< Thyra::VectorBase<Scalar> > xDotDot = Teuchos::null)
179  {
180  xDotDot = Teuchos::null;
181  // Calculate the BDF2 x dot vector
182  const Scalar a = ((Scalar(2.0)*dt_ + dtOld_)/(dt_ + dtOld_))/dt_;
183  const Scalar b = ( dt_/(dt_ + dtOld_))/dtOld_;
184  //xDot = a*(x_n - x_{n-1}) - b*(x_{n-1} - x_{n-2})
185  Thyra::V_StVpStV(xDot.ptr(), a, *x, -(a+b), *xOld_);
186  Thyra::Vp_StV(xDot.ptr(), b, *xOldOld_);
187  }
188 
189  virtual void initialize(Scalar dt, Scalar dtOld,
190  Teuchos::RCP<const Thyra::VectorBase<Scalar> > xOld,
191  Teuchos::RCP<const Thyra::VectorBase<Scalar> > xOldOld)
192  { dt_ = dt; dtOld_ = dtOld; xOld_ = xOld; xOldOld_ = xOldOld;}
193 
194 private:
195 
196  Teuchos::RCP<const Thyra::VectorBase<Scalar> > xOld_;
197  Teuchos::RCP<const Thyra::VectorBase<Scalar> > xOldOld_;
198  Scalar dt_; // = t_n - t_{n-1}
199  Scalar dtOld_; // = t_{n-1} - t_{n-2}
200 };
201 
202 
203 } // namespace Tempus
204 
205 #endif // Tempus_StepperBDF2_decl_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 compute(Teuchos::RCP< const Thyra::VectorBase< Scalar > > x, Teuchos::RCP< Thyra::VectorBase< Scalar > > xDot, Teuchos::RCP< Thyra::VectorBase< Scalar > > xDotDot=Teuchos::null)
Compute the time derivative.
virtual void setObserver(Teuchos::RCP< StepperObserver< Scalar > > obs=Teuchos::null)
Set Observer.
virtual Teuchos::RCP< StepperObserver< Scalar > > getObserver() const
Get Observer.
virtual bool isExplicit() const
void setStartUpStepper(std::string startupStepperType="DIRK 1 Stage Theta Method")
Set the stepper to use in first step.
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
virtual Scalar getBeta(const Scalar) const
Return beta = d(x)/dx.
Teuchos::RCP< Stepper< Scalar > > startUpStepper_
Teuchos::RCP< StepperObserverComposite< Scalar > > stepperObserver_
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.
Thyra Base interface for implicit time steppers.
virtual Scalar getAlpha(const Scalar dt) const
Return alpha = d(xDot)/dx.
Teuchos::RCP< StepperBDF2Observer< Scalar > > stepperBDF2Observer_
virtual Scalar getOrderMin() const
StepperObserver class for Stepper class.
Teuchos::RCP< SolutionHistory< Scalar > > solutionHistory(Teuchos::RCP< Teuchos::ParameterList > pList=Teuchos::null)
Nonmember constructor.
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
Teuchos::RCP< const Thyra::VectorBase< Scalar > > xOld_
virtual OrderODE getOrderODE() const
Stepper integrates first-order ODEs.
virtual Scalar getOrder() const
StepperBDF2TimeDerivative(Scalar dt, Scalar dtOld, Teuchos::RCP< const Thyra::VectorBase< Scalar > > xOld, Teuchos::RCP< const Thyra::VectorBase< Scalar > > xOldOld)
Constructor.
virtual bool isOneStepMethod() const
virtual bool isExplicitImplicit() const
This interface defines the time derivative connection between an implicit Stepper and WrapperModelEva...
virtual bool isMultiStepMethod() const
virtual void initialize(Scalar dt, Scalar dtOld, Teuchos::RCP< const Thyra::VectorBase< Scalar > > xOld, Teuchos::RCP< const Thyra::VectorBase< Scalar > > xOldOld)
virtual Scalar getAlpha(const Scalar dt, const Scalar dtOld) const
virtual Scalar getOrderMax() const
virtual bool getICConsistencyCheckDefault() const
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
Teuchos::RCP< const Thyra::VectorBase< Scalar > > xOldOld_
virtual void takeStep(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Take the specified timestep, dt, and return true if successful.
virtual void initialize()
Initialize during construction and after changing input parameters.
StepperBDF2()
Default constructor.
virtual bool isImplicit() const