- 3.0.2 core module.
SteppersCT.h
Go to the documentation of this file.
1 /**********************************************************************************************************************
2 This file is part of the Control Toolbox (https://github.com/ethz-adrl/control-toolbox), copyright by ETH Zurich.
3 Licensed under the BSD-2 license (see LICENSE file in main directory)
4 **********************************************************************************************************************/
5 
6 #pragma once
7 
9 #include "StepperBase.h"
10 
11 namespace ct {
12 namespace core {
13 namespace internal {
14 
15 
22 template <typename MATRIX, typename SCALAR = double>
23 class StepperCTBase : public StepperBase<MATRIX, SCALAR>
24 {
25 public:
26  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
27 
28  virtual void integrate_n_steps(const std::function<void(const MATRIX&, MATRIX&, SCALAR)>& rhs,
29  MATRIX& state,
30  const SCALAR& startTime,
31  size_t numSteps,
32  SCALAR dt) override
33  {
34  SCALAR time = startTime;
35  for (size_t i = 0; i < numSteps; ++i)
36  {
37  do_step(rhs, state, time, dt);
38  time += dt;
39  }
40  }
41 
42  virtual void integrate_n_steps(std::function<void(const MATRIX& x, const SCALAR& t)> observe,
43  const std::function<void(const MATRIX&, MATRIX&, SCALAR)>& rhs,
44  MATRIX& state,
45  const SCALAR& startTime,
46  size_t numSteps,
47  SCALAR dt) override
48  {
49  SCALAR time = startTime;
50 
51  for (size_t i = 0; i < numSteps; ++i)
52  {
53  do_step(rhs, state, time, dt);
54  time += dt;
55  observe(state, time);
56  }
57  }
58 
67  virtual void do_step(const std::function<void(const MATRIX&, MATRIX&, SCALAR)>& rhs,
68  MATRIX& stateInOut,
69  const SCALAR time,
70  const SCALAR dt) = 0;
71 };
72 
73 
80 template <typename MATRIX, typename SCALAR = double>
81 class StepperEulerCT : public StepperCTBase<MATRIX, SCALAR>
82 {
83 public:
84  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
85 
87 private:
88  virtual void do_step(const std::function<void(const MATRIX&, MATRIX&, SCALAR)>& rhs,
89  MATRIX& stateInOut,
90  const SCALAR time,
91  const SCALAR dt) override
92  {
93  rhs(stateInOut, derivative_, time);
94  stateInOut += dt * derivative_;
95  }
96 
97  MATRIX derivative_;
98 };
99 
106 template <typename MATRIX, typename SCALAR = double>
107 class StepperRK4CT : public StepperCTBase<MATRIX, SCALAR>
108 {
109 public:
110  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
111 
112  StepperRK4CT() : oneSixth_(SCALAR(1.0 / 6.0)) {}
113 private:
114  virtual void do_step(const std::function<void(const MATRIX&, MATRIX&, SCALAR)>& rhs,
115  MATRIX& stateInOut,
116  const SCALAR time,
117  const SCALAR dt) override
118  {
119  SCALAR halfStep = SCALAR(0.5) * dt;
120  SCALAR timePlusHalfStep = time + halfStep;
121  rhs(stateInOut, k1_, time);
122  rhs(stateInOut + halfStep * k1_, k2_, timePlusHalfStep);
123  rhs(stateInOut + halfStep * k2_, k3_, timePlusHalfStep);
124  rhs(stateInOut + dt * k3_, k4_, time + dt);
125  stateInOut += oneSixth_ * dt * (k1_ + SCALAR(2.0) * k2_ + SCALAR(2.0) * k3_ + k4_);
126  }
127 
128  MATRIX k1_;
129  MATRIX k2_;
130  MATRIX k3_;
131  MATRIX k4_;
132  SCALAR oneSixth_;
133 };
134 }
135 }
136 }
virtual void integrate_n_steps(std::function< void(const MATRIX &x, const SCALAR &t)> observe, const std::function< void(const MATRIX &, MATRIX &, SCALAR)> &rhs, MATRIX &state, const SCALAR &startTime, size_t numSteps, SCALAR dt) override
Performs numSteps integration steps.
Definition: SteppersCT.h:42
Custom implementation of the euler stepper.
Definition: SteppersCT.h:81
This class serves as a common interface between the ODEInt and our custom integrators.
Definition: StepperBase.h:20
EIGEN_MAKE_ALIGNED_OPERATOR_NEW StepperRK4CT()
Definition: SteppersCT.h:112
const double dt
CppAD::AD< CppAD::cg::CG< double > > SCALAR
virtual EIGEN_MAKE_ALIGNED_OPERATOR_NEW void integrate_n_steps(const std::function< void(const MATRIX &, MATRIX &, SCALAR)> &rhs, MATRIX &state, const SCALAR &startTime, size_t numSteps, SCALAR dt) override
Performs numSteps integration steps.
Definition: SteppersCT.h:28
for i
Custom implementation of the rk4 integration scheme.
Definition: SteppersCT.h:107
EIGEN_MAKE_ALIGNED_OPERATOR_NEW StepperEulerCT()
Definition: SteppersCT.h:86
virtual void do_step(const std::function< void(const MATRIX &, MATRIX &, SCALAR)> &rhs, MATRIX &stateInOut, const SCALAR time, const SCALAR dt)=0
Implements a single step of the integration scheme.
The stepper interface for custom steppers.
Definition: SteppersCT.h:23