- 3.0.2 core module.
IntegratorSymplectic-impl.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 
9 namespace ct {
10 namespace core {
11 
12 template <size_t POS_DIM, size_t VEL_DIM, size_t CONTROL_DIM, class Stepper, typename SCALAR>
14  const std::shared_ptr<SymplecticSystem<POS_DIM, VEL_DIM, CONTROL_DIM, SCALAR>> system,
15  const EventHandlerPtrVector& eventHandlers)
16  : systemSymplectic_(system), observer_(eventHandlers)
17 {
18  setupSystem();
19 }
20 
21 template <size_t POS_DIM, size_t VEL_DIM, size_t CONTROL_DIM, class Stepper, typename SCALAR>
23  const std::shared_ptr<SymplecticSystem<POS_DIM, VEL_DIM, CONTROL_DIM, SCALAR>> system,
24  const EventHandlerPtr& eventHandler)
25  : systemSymplectic_(system), observer_(EventHandlerPtrVector(1, eventHandler))
26 {
27  setupSystem();
28 }
29 
30 
31 template <size_t POS_DIM, size_t VEL_DIM, size_t CONTROL_DIM, class Stepper, typename SCALAR>
34  const SCALAR& startTime,
35  size_t numSteps,
36  SCALAR dt,
38  tpl::TimeArray<SCALAR>& timeTrajectory)
39 {
40  pair_t xPair;
41  xPair.first = state.head(POS_DIM);
42  xPair.second = state.tail(VEL_DIM);
43  SCALAR time = startTime;
44  stateTrajectory.push_back(state);
45  timeTrajectory.push_back(time);
46 
47  for (size_t i = 0; i < numSteps; ++i)
48  {
49  xCached_ = state;
50  stepper_.do_step(std::make_pair(systemFunctionPosition_, systemFunctionVelocity_), xPair, time, dt);
51  state.head(POS_DIM) = xPair.first;
52  state.tail(VEL_DIM) = xPair.second;
53  observer_.observeInternal(state, time);
54  time += dt;
55  stateTrajectory.push_back(state);
56  timeTrajectory.push_back(time);
57  }
58 }
59 
60 template <size_t POS_DIM, size_t VEL_DIM, size_t CONTROL_DIM, class Stepper, typename SCALAR>
63  const SCALAR& startTime,
64  size_t numSteps,
65  SCALAR dt)
66 {
67  pair_t xPair;
68  xPair.first = state.head(POS_DIM);
69  xPair.second = state.tail(VEL_DIM);
70  SCALAR time = startTime;
71  for (size_t i = 0; i < numSteps; ++i)
72  {
73  xCached_ = state;
74  stepper_.do_step(std::make_pair(systemFunctionPosition_, systemFunctionVelocity_), xPair, time, dt);
75  state.head(POS_DIM) = xPair.first;
76  state.tail(VEL_DIM) = xPair.second;
77  observer_.observeInternal(state, time);
78  time += dt;
79  }
80 }
81 
82 template <size_t POS_DIM, size_t VEL_DIM, size_t CONTROL_DIM, class Stepper, typename SCALAR>
84 {
85  observer_.reset();
86 }
87 
88 template <size_t POS_DIM, size_t VEL_DIM, size_t CONTROL_DIM, class Stepper, typename SCALAR>
90 {
91  systemFunctionPosition_ = [this](
92  const Eigen::Matrix<SCALAR, VEL_DIM, 1>& v, Eigen::Matrix<SCALAR, POS_DIM, 1>& dxdt) {
93  const StateVector<POS_DIM, SCALAR>& vState(static_cast<const StateVector<POS_DIM, SCALAR>&>(v));
94  StateVector<POS_DIM, SCALAR>& dxdtState(static_cast<StateVector<POS_DIM, SCALAR>&>(dxdt));
95  xCached_.template bottomRows<VEL_DIM>() = v;
96  systemSymplectic_->computePdot(xCached_, vState, dxdtState);
97  };
98 
99  systemFunctionVelocity_ = [this](
100  const Eigen::Matrix<SCALAR, POS_DIM, 1>& x, Eigen::Matrix<SCALAR, VEL_DIM, 1>& dvdt) {
101  const StateVector<VEL_DIM, SCALAR>& xState(static_cast<const StateVector<VEL_DIM, SCALAR>&>(x));
102  StateVector<VEL_DIM, SCALAR>& dvdtState(static_cast<StateVector<VEL_DIM, SCALAR>&>(dvdt));
103  xCached_.template topRows<POS_DIM>() = x;
104  systemSymplectic_->computeVdot(xCached_, xState, dvdtState);
105  };
106 }
107 }
108 }
std::vector< EventHandlerPtr, Eigen::aligned_allocator< EventHandlerPtr > > EventHandlerPtrVector
Definition: IntegratorSymplectic.h:41
void observeInternal(const StateVector< STATE_DIM, SCALAR > &x, const SCALAR &t)
Definition: Observer-impl.h:51
An discrete array (vector) of a particular data type.
Definition: DiscreteArray.h:22
void reset()
Definition: IntegratorSymplectic-impl.h:83
std::shared_ptr< EventHandler< POS_DIM+VEL_DIM, SCALAR > > EventHandlerPtr
Definition: IntegratorSymplectic.h:40
void integrate_n_steps(StateVector< POS_DIM+VEL_DIM, SCALAR > &state, const SCALAR &startTime, size_t numSteps, SCALAR dt, StateVectorArray< POS_DIM+VEL_DIM, SCALAR > &stateTrajectory, tpl::TimeArray< SCALAR > &timeTrajectory)
Equidistant integration based on number of time steps and step length.
Definition: IntegratorSymplectic-impl.h:32
CppAD::AD< CppAD::cg::CG< double > > SCALAR
for i
ct::core::StateVector< state_dim > x
An array in time.
Definition: TimeArray.h:22
This class wraps the symplectic integrators from boost to this toolbox.
Definition: IntegratorSymplectic.h:34
IntegratorSymplectic(const std::shared_ptr< SymplecticSystem< POS_DIM, VEL_DIM, CONTROL_DIM, SCALAR >> system, const EventHandlerPtrVector &eventHandlers=EventHandlerPtrVector(0))
The constructor. This integrator can only treat symplectic systems.
Definition: IntegratorSymplectic-impl.h:13
The base class for the implementation of a symplectic system. In a symplectic system, the position and the velocity update can be separated. During integration, the velocity gets update first and the position update uses the updated velocity.
Definition: SymplecticSystem.h:25
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef std::pair< Eigen::Matrix< SCALAR, POS_DIM, 1 >, Eigen::Matrix< SCALAR, VEL_DIM, 1 > > pair_t
Definition: IntegratorSymplectic.h:38
void reset()
reset the observer
Definition: Observer-impl.h:24