- 3.0.2 core module.
Integrator.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 
8 #include <type_traits>
9 #include <functional>
10 #include <cmath>
11 
12 #include "EventHandler.h"
13 #include "Observer.h"
14 #include "eigenIntegration.h"
15 
16 #include "internal/StepperBase.h"
17 
19 #include "internal/SteppersCT.h"
20 
21 #include <ct/core/types/AutoDiff.h>
22 
23 namespace ct {
24 namespace core {
25 
26 
31 {
33  RK4,
43 };
44 
45 
47 
61 template <size_t STATE_DIM, typename SCALAR = double>
63 {
64 public:
65  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
66  typedef std::shared_ptr<EventHandler<STATE_DIM, SCALAR>> EventHandlerPtr;
67  typedef std::vector<EventHandlerPtr, Eigen::aligned_allocator<EventHandlerPtr>> EventHandlerPtrVector;
68 
70 
78  Integrator(const std::shared_ptr<System<STATE_DIM, SCALAR>>& system,
80  const EventHandlerPtrVector& eventHandlers = EventHandlerPtrVector(0));
81 
82  Integrator(const std::shared_ptr<System<STATE_DIM, SCALAR>>& system,
83  const IntegrationType& intType,
84  const EventHandlerPtr& eventHandler);
85 
91  void changeIntegrationType(const IntegrationType& intType);
92 
93 
100  void setApadativeErrorTolerances(const SCALAR absErrTol, const SCALAR& relErrTol);
101 
103 
117  const SCALAR& startTime,
118  size_t numSteps,
119  SCALAR dt,
120  StateVectorArray<STATE_DIM, SCALAR>& stateTrajectory,
121  tpl::TimeArray<SCALAR>& timeTrajectory);
122 
124 
133  void integrate_n_steps(StateVector<STATE_DIM, SCALAR>& state, const SCALAR& startTime, size_t numSteps, SCALAR dt);
134 
136 
150  const SCALAR& startTime,
151  const SCALAR& finalTime,
152  SCALAR dt,
153  StateVectorArray<STATE_DIM, SCALAR>& stateTrajectory,
154  tpl::TimeArray<SCALAR>& timeTrajectory);
155 
157 
167  const SCALAR& startTime,
168  const SCALAR& finalTime,
169  SCALAR dt);
170 
172 
189  const SCALAR& startTime,
190  const SCALAR& finalTime,
191  StateVectorArray<STATE_DIM, SCALAR>& stateTrajectory,
192  tpl::TimeArray<SCALAR>& timeTrajectory,
193  const SCALAR dtInitial = SCALAR(0.01));
194 
196 
209  const SCALAR& startTime,
210  const SCALAR& finalTime,
211  SCALAR dtInitial = SCALAR(0.01));
212 
214 
225  const tpl::TimeArray<SCALAR>& timeTrajectory,
226  StateVectorArray<STATE_DIM, SCALAR>& stateTrajectory,
227  SCALAR dtInitial = SCALAR(0.01));
228 
229 private:
235  void initializeCTSteppers(const IntegrationType& intType);
243  template <typename S = SCALAR>
244  typename std::enable_if<std::is_same<S, double>::value, void>::type initializeAdaptiveSteppers(
245  const IntegrationType& intType)
246  {
247  switch (intType)
248  {
249  case ODE45:
250  {
251  integratorStepper_ =
252  std::shared_ptr<internal::StepperODEIntControlled<internal::runge_kutta_dopri5_t<STATE_DIM, SCALAR>,
253  Eigen::Matrix<SCALAR, STATE_DIM, 1>, SCALAR>>(
255  Eigen::Matrix<SCALAR, STATE_DIM, 1>, SCALAR>());
256  break;
257  }
258 
259  case RK5VARIABLE:
260  {
261  integratorStepper_ = std::shared_ptr<internal::StepperODEIntDenseOutput<
262  internal::runge_kutta_dopri5_t<STATE_DIM, SCALAR>, Eigen::Matrix<SCALAR, STATE_DIM, 1>, SCALAR>>(
264  Eigen::Matrix<SCALAR, STATE_DIM, 1>, SCALAR>());
265  break;
266  }
267 
268  case BULIRSCHSTOER:
269  {
270  integratorStepper_ =
271  std::shared_ptr<internal::StepperODEInt<internal::bulirsch_stoer_t<STATE_DIM, SCALAR>,
272  Eigen::Matrix<SCALAR, STATE_DIM, 1>, SCALAR>>(
274  Eigen::Matrix<SCALAR, STATE_DIM, 1>, SCALAR>());
275  break;
276  }
277  default:
278  break;
279  }
280  }
281 
282  template <typename S = SCALAR>
283  typename std::enable_if<!std::is_same<S, double>::value, void>::type initializeAdaptiveSteppers(
284  const IntegrationType& intType)
285  {
286  }
287 
288 #ifdef CPPADCG
289  template <typename S = SCALAR>
290  typename std::enable_if<std::is_same<S, ADCGScalar>::value, void>::type initializeODEIntSteppers(
291  const IntegrationType& intType)
292  {
293  }
294 #endif
295 
303  template <typename S = SCALAR>
304  typename std::enable_if<std::is_same<S, double>::value, void>::type initializeODEIntSteppers(
305  const IntegrationType& intType)
306  {
307  switch (intType)
308  {
309  case EULER:
310  {
311  integratorStepper_ = std::shared_ptr<internal::StepperODEInt<internal::euler_t<STATE_DIM, SCALAR>,
312  Eigen::Matrix<SCALAR, STATE_DIM, 1>, SCALAR>>(
314  Eigen::Matrix<SCALAR, STATE_DIM, 1>, SCALAR>());
315  break;
316  }
317 
318  case RK4:
319  {
320  integratorStepper_ =
321  std::shared_ptr<internal::StepperODEInt<internal::runge_kutta_4_t<STATE_DIM, SCALAR>,
322  Eigen::Matrix<SCALAR, STATE_DIM, 1>, SCALAR>>(
324  Eigen::Matrix<SCALAR, STATE_DIM, 1>, SCALAR>());
325  break;
326  }
327 
328  case MODIFIED_MIDPOINT:
329  {
330  integratorStepper_ =
331  std::shared_ptr<internal::StepperODEInt<internal::modified_midpoint_t<STATE_DIM, SCALAR>,
332  Eigen::Matrix<SCALAR, STATE_DIM, 1>, SCALAR>>(
334  Eigen::Matrix<SCALAR, STATE_DIM, 1>, SCALAR>());
335  break;
336  }
337 
338  case RK78:
339  {
340  integratorStepper_ =
341  std::shared_ptr<internal::StepperODEInt<internal::runge_kutta_fehlberg78_t<STATE_DIM, SCALAR>,
342  Eigen::Matrix<SCALAR, STATE_DIM, 1>, SCALAR>>(
344  Eigen::Matrix<SCALAR, STATE_DIM, 1>, SCALAR>());
345 
346  break;
347  }
348  default:
349  break;
350  }
351  }
352 
353 
355  void reset();
356 
363  void retrieveTrajectoriesFromObserver(StateVectorArray<STATE_DIM, SCALAR>& stateTrajectory,
364  tpl::TimeArray<SCALAR>& timeTrajectory);
370  void retrieveStateVectorArrayFromObserver(StateVectorArray<STATE_DIM, SCALAR>& stateTrajectory);
371 
373  void setupSystem();
374 
375  std::shared_ptr<System<STATE_DIM, SCALAR>> system_;
376  std::function<void(const Eigen::Matrix<SCALAR, STATE_DIM, 1>&, Eigen::Matrix<SCALAR, STATE_DIM, 1>&, SCALAR)>
377  systemFunction_;
378  std::shared_ptr<internal::StepperBase<Eigen::Matrix<SCALAR, STATE_DIM, 1>, SCALAR>> integratorStepper_;
379  Observer<STATE_DIM, SCALAR> observer_;
380 };
381 }
382 }
An discrete array (vector) of a particular data type.
Definition: DiscreteArray.h:22
void integrate_n_steps(StateVector< STATE_DIM, SCALAR > &state, const SCALAR &startTime, size_t numSteps, SCALAR dt, StateVectorArray< STATE_DIM, SCALAR > &stateTrajectory, tpl::TimeArray< SCALAR > &timeTrajectory)
Equidistant integration based on number of time steps and step length.
Definition: Integrator-impl.h:50
std::vector< EventHandlerPtr, Eigen::aligned_allocator< EventHandlerPtr > > EventHandlerPtrVector
Definition: Integrator.h:67
void integrate_adaptive(StateVector< STATE_DIM, SCALAR > &state, const SCALAR &startTime, const SCALAR &finalTime, StateVectorArray< STATE_DIM, SCALAR > &stateTrajectory, tpl::TimeArray< SCALAR > &timeTrajectory, const SCALAR dtInitial=SCALAR(0.01))
integrate forward from an initial to a final time using an adaptive scheme
Definition: Integrator-impl.h:98
Definition: Integrator.h:32
Observer for Integrator.
Definition: Observer.h:27
The interface to call ODEInt Dense Output Integration routines.
Definition: SteppersODEInt.h:110
Definition: Integrator.h:35
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef std::shared_ptr< EventHandler< STATE_DIM, SCALAR > > EventHandlerPtr
Definition: Integrator.h:66
void changeIntegrationType(const IntegrationType &intType)
Changes the integration type.
Definition: Integrator-impl.h:33
CppAD::AD< CppAD::cg::CG< double > > SCALAR
void integrate_times(StateVector< STATE_DIM, SCALAR > &state, const tpl::TimeArray< SCALAR > &timeTrajectory, StateVectorArray< STATE_DIM, SCALAR > &stateTrajectory, SCALAR dtInitial=SCALAR(0.01))
Integrate system using a given time trajectory.
Definition: Integrator-impl.h:124
Definition: StateVector.h:12
Standard Integrator.
Definition: Integrator.h:62
Definition: Integrator.h:33
Definition: Integrator.h:39
An array in time.
Definition: TimeArray.h:22
The interface to call ODEInt Controlled integration routines.
Definition: SteppersODEInt.h:167
Definition: Integrator.h:42
Definition: Integrator.h:37
void integrate_const(StateVector< STATE_DIM, SCALAR > &state, const SCALAR &startTime, const SCALAR &finalTime, SCALAR dt, StateVectorArray< STATE_DIM, SCALAR > &stateTrajectory, tpl::TimeArray< SCALAR > &timeTrajectory)
Equidistant integration based on initial and final time as well as step length.
Definition: Integrator-impl.h:74
Definition: Integrator.h:36
Definition: Integrator.h:41
Definition: Integrator.h:38
Interface class for a general system described by an ordinary differential equation (ODE) ...
Definition: System.h:38
boost::numeric::odeint::runge_kutta_dopri5< Eigen::Matrix< SCALAR, STATE_DIM, 1 >, SCALAR, Eigen::Matrix< SCALAR, STATE_DIM, 1 >, SCALAR, boost::numeric::odeint::vector_space_algebra > runge_kutta_dopri5_t
Runge-Kutta Dormand Price 5 stepper.
Definition: SteppersODEIntDefinitions.h:46
Definition: Integrator.h:40
void setApadativeErrorTolerances(const SCALAR absErrTol, const SCALAR &relErrTol)
Sets the adaptive error tolerances.
Definition: Integrator-impl.h:44
Definition: Integrator.h:34
IntegrationType
The available integration types.
Definition: Integrator.h:30
Integrator(const std::shared_ptr< System< STATE_DIM, SCALAR >> &system, const IntegrationType &intType=IntegrationType::EULERCT, const EventHandlerPtrVector &eventHandlers=EventHandlerPtrVector(0))
constructor
Definition: Integrator-impl.h:13
The interface to call the integration routines from ODEInt.
Definition: SteppersODEInt.h:23