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