Medusa  1.1
Coordinate Free Mehless Method implementation
AdamsBashforth_fwd.hpp
Go to the documentation of this file.
1 #ifndef MEDUSA_BITS_INTEGRATORS_ADAMSBASHFORTH_FWD_HPP_
2 #define MEDUSA_BITS_INTEGRATORS_ADAMSBASHFORTH_FWD_HPP_
3 
11 #include <medusa/Config.hpp>
12 #include <Eigen/Core>
14 #include "RKExplicit_fwd.hpp"
16 
17 namespace mm {
18 
28 template <typename Scalar, int num_steps>
30  public:
31  typedef Scalar scalar_t;
32  enum {
33  steps = num_steps
34  };
35  private:
36  Eigen::Matrix<scalar_t, steps, 1> b_;
37 
38  public:
44  AdamsBashforth(const Eigen::Matrix<scalar_t, steps, 1>& b) : b_(b) {
45  assert_msg(std::abs(b_.sum() - 1.0) < 1e-15, "AB method coefficients do not sum up to 1, "
46  "but %.16f instead.", b_.sum());
47  }
48 
59  template <typename func_t, typename initial_method_t = RKExplicit<Scalar, num_steps>>
60  class Integrator {
61  const initial_method_t initial_method_;
65  int max_steps;
66  Eigen::VectorXd y0_;
67  const func_t& func_;
68 
69  public:
75  const initial_method_t& initial_method,
76  const func_t& func, scalar_t t0, scalar_t t_max, scalar_t dt, Eigen::VectorXd y0)
77  : initial_method_(initial_method), method_(method), t0_(t0),
78  dt_(dt), max_steps(iceil((t_max - t0) / dt)), y0_(std::move(y0)),
79  func_(func) {}
80 
85  class IterationStep : public std::iterator<
86  std::forward_iterator_tag, // iterator_category
87  IterationStep, // value_type
88  int, // difference_type
89  const IterationStep*, // pointer
90  IterationStep // reference
91  > {
94  Eigen::Matrix<scalar_t, Eigen::Dynamic, steps> last_ys;
95  int cur_step;
96 
97  public:
101  last_ys(integrator.y0_.size(), static_cast<int>(steps)), cur_step(0) {
102  last_ys.col(steps-1) = integrator.y0_;
103  }
104 
108 
111 
118  IterationStep retval = *this;
119  ++(*this);
120  return retval;
121  }
122 
124  bool operator==(const IterationStep& other) const { return cur_step == other.cur_step; }
125 
127  bool operator!=(const IterationStep& other) const { return !(*this == other); }
128 
130  IterationStep& operator*() { return *this; }
131 
133  const IterationStep& operator*() const { return *this; }
134 
136  explicit operator bool() {
137  return cur_step <= integrator.max_steps;
138  }
139 
141  bool is_last() const {
142  return cur_step == integrator.max_steps;
143  }
144 
146  scalar_t time() const { return t; }
147 
149  typename Eigen::Matrix<scalar_t, Eigen::Dynamic, steps>::ConstColXpr value() const {
150  return last_ys.col(steps-1);
151  }
152 
154  scalar_t& time() { return t; }
155 
157  typename Eigen::Matrix<scalar_t, Eigen::Dynamic, steps>::ColXpr value() {
158  return last_ys.col(steps-1);
159  }
160 
162  friend std::ostream& operator<<(std::ostream& os, const IterationStep& step) {
163  return os << "t = " << step.t << "; y = " << step.last_ys.col(steps-1);
164  }
165  };
166 
169 
171  iterator begin() { return IterationStep(*this); }
172 
174  const_iterator cbegin() { return IterationStep(*this); }
175 
178  iterator end() { return IterationStep(*this, 0); }
179 
181  const_iterator cend() { return IterationStep(*this, 0); }
182 
184  friend std::ostream& operator<<(std::ostream& os, const Integrator& integrator) {
185  return os << "Multistep integrator using " << integrator.method_ << " method from "
186  << "time " << integrator.t0_ << " with step " << integrator.dt_ << " for "
187  << integrator.max_steps << " steps.";
188  }
189  };
190 
202  template <typename func_t>
203  Integrator<func_t> solve(const func_t& func, scalar_t t0, scalar_t t_max, scalar_t dt,
204  const Eigen::VectorXd& y0) const {
205  return Integrator<func_t, RKExplicit<scalar_t, num_steps>>(
206  *this, integrators::Explicit::of_order<num_steps>(), func, t0, t_max, dt, y0);
207  }
208 
210  template <typename initial_method_t, typename func_t>
211  Integrator<func_t, initial_method_t> solve(const initial_method_t& method, const func_t& func,
212  scalar_t t0, scalar_t t_max, scalar_t dt,
213  const Eigen::VectorXd& y0) const {
214  return Integrator<func_t, initial_method_t>(*this, method, func, t0, t_max, dt, y0);
215  }
216 
217  private:
219  template <typename func_t>
220  Eigen::VectorXd step(const func_t& func, scalar_t t,
221  const Eigen::Matrix<scalar_t, Eigen::Dynamic, steps>& last_ys,
222  scalar_t dt) const {
223  Eigen::VectorXd result = last_ys.col(steps-1);
224  for (int i = 0; i < steps; ++i) {
225  result += dt * b_(i) * func(t - i*dt, last_ys.col(i));
226  }
227  return result;
228  }
229 
230  public:
232  template <typename scalar_t, int stages>
233  friend std::ostream& operator<<(std::ostream& os, const AdamsBashforth<scalar_t, stages>&);
234 };
235 
236 namespace integrators {
237 
244 namespace ExplicitMultistep {
245 
247 template <class scalar_t = double>
249  Eigen::Matrix<scalar_t, 1, 1> b(1); b << 1.0;
250  return {b};
251 }
252 
254 template <class scalar_t = double>
256  Eigen::Matrix<scalar_t, 2, 1> b(2); b << -1./2., 3./2.;
257  return {b};
258 }
259 
261 template <class scalar_t = double>
263  Eigen::Matrix<scalar_t, 3, 1> b(3); b << 5./12., -4./3., 23./12.;
264  return {b};
265 }
266 
268 template <class scalar_t = double>
270  Eigen::Matrix<scalar_t, 4, 1> b(4); b << -3./8., 37./24., -59./24., 55./24.;
271  return {b};
272 }
273 
275 template <class scalar_t = double>
277  Eigen::Matrix<scalar_t, 5, 1> b(5);
278  b << 251./720., -637./360., 109./30., -1387./360., 1901./720.;
279  return {b};
280 }
281 
282 // Internal namespace containing implementation details for Explicit::of_order function.
284 namespace of_order_internal {
285 template <int order, class scalar_t>
286 struct _of_order_impl {
288 };
289 } // namespace of_order_internal
291 
295 template <int order, class scalar_t = double>
298 }
299 
300 } // namespace ExplicitMultistep
301 } // namespace integrators
302 } // namespace mm
303 
304 #endif // MEDUSA_BITS_INTEGRATORS_ADAMSBASHFORTH_FWD_HPP_
mm::integrators::ExplicitMultistep::AB2
static AdamsBashforth< scalar_t, 2 > AB2()
Two step AB method.
Definition: AdamsBashforth_fwd.hpp:255
RKExplicit_fwd.hpp
mm
Root namespace for the whole library.
Definition: Gaussian.hpp:14
mm::AdamsBashforth::solve
Integrator< func_t > solve(const func_t &func, scalar_t t0, scalar_t t_max, scalar_t dt, const Eigen::VectorXd &y0) const
Returns a solver using this method.
Definition: AdamsBashforth_fwd.hpp:203
mm::AdamsBashforth::Integrator::end
iterator end()
Creates an invalid stepper at positioned past the last step, meant to be used for comparison it == en...
Definition: AdamsBashforth_fwd.hpp:178
mm::AdamsBashforth::Integrator::y0_
Eigen::VectorXd y0_
initial value
Definition: AdamsBashforth_fwd.hpp:66
mm::AdamsBashforth::Integrator::IterationStep::operator*
IterationStep & operator*()
Noop, used to conform to std::iterator requirements.
Definition: AdamsBashforth_fwd.hpp:130
mm::AdamsBashforth::scalar_t
Scalar scalar_t
floating point type
Definition: AdamsBashforth_fwd.hpp:31
mm::AdamsBashforth::Integrator::IterationStep::is_last
bool is_last() const
Returns true if integrator just completed its last step.
Definition: AdamsBashforth_fwd.hpp:141
mm::AdamsBashforth::Integrator::IterationStep::value
Eigen::Matrix< scalar_t, Eigen::Dynamic, steps >::ConstColXpr value() const
Get current value.
Definition: AdamsBashforth_fwd.hpp:149
mm::AdamsBashforth::Integrator::IterationStep::IterationStep
IterationStep(const Integrator &integrator)
Construct an iterator at the initial values of the equation.
Definition: AdamsBashforth_fwd.hpp:99
mm::AdamsBashforth::Integrator
Integrator class for AB methods.
Definition: AdamsBashforth_fwd.hpp:60
mm::integrators::ExplicitMultistep::AB1
static AdamsBashforth< scalar_t, 1 > AB1()
Standard Euler's method.
Definition: AdamsBashforth_fwd.hpp:248
mm::AdamsBashforth::Integrator::IterationStep::operator++
IterationStep & operator++()
Advance the stepper for one time step, returning the new value.
Definition: AdamsBashforth.hpp:19
mm::AdamsBashforth::Integrator::func_
const func_t & func_
function to integrate
Definition: AdamsBashforth_fwd.hpp:67
mm::AdamsBashforth::Integrator::cbegin
const_iterator cbegin()
Same as Integrator::begin().
Definition: AdamsBashforth_fwd.hpp:174
numutils.hpp
mm::AdamsBashforth::step
Eigen::VectorXd step(const func_t &func, scalar_t t, const Eigen::Matrix< scalar_t, Eigen::Dynamic, steps > &last_ys, scalar_t dt) const
Returns next value.
Definition: AdamsBashforth_fwd.hpp:220
mm::AdamsBashforth::operator<<
friend std::ostream & operator<<(std::ostream &os, const AdamsBashforth< scalar_t, stages > &)
Output the method's tableau for debugging.
assert_msg
#define assert_msg(cond,...)
Assert with better error reporting.
Definition: assert.hpp:75
mm::AdamsBashforth::Integrator::t0_
scalar_t t0_
start time
Definition: AdamsBashforth_fwd.hpp:63
mm::AdamsBashforth::Integrator::iterator
IterationStep iterator
iterator type
Definition: AdamsBashforth_fwd.hpp:167
mm::AdamsBashforth::Integrator::IterationStep::t
scalar_t t
current time
Definition: AdamsBashforth_fwd.hpp:93
mm::AdamsBashforth::Integrator::initial_method_
const initial_method_t initial_method_
method used for first num_steps steps
Definition: AdamsBashforth_fwd.hpp:61
mm::AdamsBashforth::AdamsBashforth
AdamsBashforth(const Eigen::Matrix< scalar_t, steps, 1 > &b)
Construct an num_steps-step Adams Bashforth method with coefficients b, given as .
Definition: AdamsBashforth_fwd.hpp:44
mm::integrators::ExplicitMultistep::of_order
static AdamsBashforth< scalar_t, order > of_order()
Returns Adams-Bashforth explicit method of requested order with given floating point type.
Definition: AdamsBashforth_fwd.hpp:296
mm::AdamsBashforth::Integrator::const_iterator
IterationStep const_iterator
const iterator type
Definition: AdamsBashforth_fwd.hpp:168
Config.hpp
mm::AdamsBashforth::Integrator::IterationStep::time
scalar_t time() const
Get current time.
Definition: AdamsBashforth_fwd.hpp:146
mm::iceil
int iceil(T x)
Ceils a floating point to an integer.
Definition: numutils.hpp:27
mm::AdamsBashforth::Integrator::Integrator
Integrator(const AdamsBashforth< scalar_t, num_steps > &method, const initial_method_t &initial_method, const func_t &func, scalar_t t0, scalar_t t_max, scalar_t dt, Eigen::VectorXd y0)
Constructs stepper with given initial method.
Definition: AdamsBashforth_fwd.hpp:74
mm::AdamsBashforth::Integrator::IterationStep::operator<<
friend std::ostream & operator<<(std::ostream &os, const IterationStep &step)
Output current state of the stepper.
Definition: AdamsBashforth_fwd.hpp:162
mm::AdamsBashforth::Integrator::operator<<
friend std::ostream & operator<<(std::ostream &os, const Integrator &integrator)
Output information about this integrator.
Definition: AdamsBashforth_fwd.hpp:184
mm::AdamsBashforth::Integrator::cend
const_iterator cend()
Same as Integrator::end().
Definition: AdamsBashforth_fwd.hpp:181
mm::AdamsBashforth::b_
Eigen::Matrix< scalar_t, steps, 1 > b_
coefficients of the multistep method
Definition: AdamsBashforth_fwd.hpp:36
mm::integrators::ExplicitMultistep::AB4
static AdamsBashforth< scalar_t, 4 > AB4()
Four step AB method.
Definition: AdamsBashforth_fwd.hpp:269
mm::AdamsBashforth::Integrator::begin
iterator begin()
Creates stepper at positioned at initial value.
Definition: AdamsBashforth_fwd.hpp:171
assert.hpp
mm::AdamsBashforth::Integrator::max_steps
int max_steps
number of steps
Definition: AdamsBashforth_fwd.hpp:65
mm::AdamsBashforth::Integrator::dt_
scalar_t dt_
time step
Definition: AdamsBashforth_fwd.hpp:64
mm::AdamsBashforth::Integrator::IterationStep::operator!=
bool operator!=(const IterationStep &other) const
Negation of IterationStep::operator==.
Definition: AdamsBashforth_fwd.hpp:127
mm::AdamsBashforth::Integrator::IterationStep::operator*
const IterationStep & operator*() const
const version of IterationStep::operator*
Definition: AdamsBashforth_fwd.hpp:133
mm::AdamsBashforth::Integrator::IterationStep::operator++
IterationStep operator++(int)
Advance the stepper for one time step, returning the old value.
Definition: AdamsBashforth_fwd.hpp:117
mm::integrators::ExplicitMultistep::AB5
static AdamsBashforth< scalar_t, 5 > AB5()
Five step AB method.
Definition: AdamsBashforth_fwd.hpp:276
mm::AdamsBashforth::Integrator::IterationStep::cur_step
int cur_step
current step
Definition: AdamsBashforth_fwd.hpp:95
mm::AdamsBashforth::Integrator::IterationStep::operator==
bool operator==(const IterationStep &other) const
Compare two steppers if they are on the same step.
Definition: AdamsBashforth_fwd.hpp:124
mm::AdamsBashforth::steps
@ steps
number of steps of the multistep method
Definition: AdamsBashforth_fwd.hpp:33
mm::AdamsBashforth::Integrator::method_
const AdamsBashforth< scalar_t, num_steps > method_
method used for all next steps
Definition: AdamsBashforth_fwd.hpp:62
mm::integrators::ExplicitMultistep::AB3
static AdamsBashforth< scalar_t, 3 > AB3()
Three step AB method.
Definition: AdamsBashforth_fwd.hpp:262
mm::AdamsBashforth::Integrator::IterationStep::time
scalar_t & time()
Read-write access to current time.
Definition: AdamsBashforth_fwd.hpp:154
mm::AdamsBashforth
Class representing an AdamsBashforth method, an explicit linear multistep method.
Definition: AdamsBashforth_fwd.hpp:29
mm::AdamsBashforth::Integrator::IterationStep::value
Eigen::Matrix< scalar_t, Eigen::Dynamic, steps >::ColXpr value()
Read-write access to current value.
Definition: AdamsBashforth_fwd.hpp:157
mm::AdamsBashforth::Integrator::IterationStep::integrator
const Integrator & integrator
reference to underlying integrator
Definition: AdamsBashforth_fwd.hpp:92
mm::AdamsBashforth::solve
Integrator< func_t, initial_method_t > solve(const initial_method_t &method, const func_t &func, scalar_t t0, scalar_t t_max, scalar_t dt, const Eigen::VectorXd &y0) const
Same as AdamsBashforth::solve, but offers the option of supplying your own initial method.
Definition: AdamsBashforth_fwd.hpp:211
mm::AdamsBashforth::Integrator::IterationStep
Class representing a step in the integration process.
Definition: AdamsBashforth_fwd.hpp:85
mm::AdamsBashforth::Integrator::IterationStep::IterationStep
IterationStep(const Integrator &integrator, int)
Construct an (invalid) iterator pointing past the last step.
Definition: AdamsBashforth_fwd.hpp:106
mm::AdamsBashforth::Integrator::IterationStep::last_ys
Eigen::Matrix< scalar_t, Eigen::Dynamic, steps > last_ys
current value
Definition: AdamsBashforth_fwd.hpp:94