- 3.0.2 core module.
Integrator-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 STATE_DIM, typename SCALAR>
14  const IntegrationType& intType,
15  const EventHandlerPtrVector& eventHandlers)
16  : system_(system), observer_(eventHandlers)
17 {
18  changeIntegrationType(intType);
19  setupSystem();
20 }
21 
22 template <size_t STATE_DIM, typename SCALAR>
24  const IntegrationType& intType,
25  const EventHandlerPtr& eventHandler)
26  : system_(system), observer_(EventHandlerPtrVector(1, eventHandler))
27 {
28  changeIntegrationType(intType);
29  setupSystem();
30 }
31 
32 template <size_t STATE_DIM, typename SCALAR>
34 {
35  initializeCTSteppers(intType);
36  initializeAdaptiveSteppers(intType);
37  initializeODEIntSteppers(intType);
38  if (!integratorStepper_)
39  throw std::runtime_error("Unknown integration type");
40 }
41 
42 
43 template <size_t STATE_DIM, typename SCALAR>
45 {
46  integratorStepper_->setAdaptiveErrorTolerances(absErrTol, relErrTol);
47 }
48 
49 template <size_t STATE_DIM, typename SCALAR>
51  const SCALAR& startTime,
52  size_t numSteps,
53  SCALAR dt,
54  StateVectorArray<STATE_DIM, SCALAR>& stateTrajectory,
55  tpl::TimeArray<SCALAR>& timeTrajectory)
56 {
57  reset();
58  integratorStepper_->integrate_n_steps(
59  observer_.observeWrapWithLogging, systemFunction_, state, startTime, numSteps, dt);
60  retrieveTrajectoriesFromObserver(stateTrajectory, timeTrajectory);
61 }
62 
63 template <size_t STATE_DIM, typename SCALAR>
65  const SCALAR& startTime,
66  size_t numSteps,
67  SCALAR dt)
68 {
69  reset();
70  integratorStepper_->integrate_n_steps(observer_.observeWrap, systemFunction_, state, startTime, numSteps, dt);
71 }
72 
73 template <size_t STATE_DIM, typename SCALAR>
75  const SCALAR& startTime,
76  const SCALAR& finalTime,
77  SCALAR dt,
78  StateVectorArray<STATE_DIM, SCALAR>& stateTrajectory,
79  tpl::TimeArray<SCALAR>& timeTrajectory)
80 {
81  reset();
82  integratorStepper_->integrate_const(
83  observer_.observeWrapWithLogging, systemFunction_, state, startTime, finalTime, dt);
84  retrieveTrajectoriesFromObserver(stateTrajectory, timeTrajectory);
85 }
86 
87 template <size_t STATE_DIM, typename SCALAR>
89  const SCALAR& startTime,
90  const SCALAR& finalTime,
91  SCALAR dt)
92 {
93  reset();
94  integratorStepper_->integrate_const(observer_.observeWrap, systemFunction_, state, startTime, finalTime, dt);
95 }
96 
97 template <size_t STATE_DIM, typename SCALAR>
99  const SCALAR& startTime,
100  const SCALAR& finalTime,
101  StateVectorArray<STATE_DIM, SCALAR>& stateTrajectory,
102  tpl::TimeArray<SCALAR>& timeTrajectory,
103  const SCALAR dtInitial)
104 {
105  reset();
106  integratorStepper_->integrate_adaptive(
107  observer_.observeWrapWithLogging, systemFunction_, state, startTime, finalTime, dtInitial);
108  retrieveTrajectoriesFromObserver(stateTrajectory, timeTrajectory);
109  state = stateTrajectory.back();
110 }
111 
112 template <size_t STATE_DIM, typename SCALAR>
114  const SCALAR& startTime,
115  const SCALAR& finalTime,
116  SCALAR dtInitial)
117 {
118  reset();
119  integratorStepper_->integrate_adaptive(
120  observer_.observeWrap, systemFunction_, state, startTime, finalTime, dtInitial);
121 }
122 
123 template <size_t STATE_DIM, typename SCALAR>
125  const tpl::TimeArray<SCALAR>& timeTrajectory,
126  StateVectorArray<STATE_DIM, SCALAR>& stateTrajectory,
127  SCALAR dtInitial)
128 {
129  reset();
130  integratorStepper_->integrate_times(
131  observer_.observeWrapWithLogging, systemFunction_, state, timeTrajectory, dtInitial);
132  retrieveStateVectorArrayFromObserver(stateTrajectory);
133 }
134 
135 
136 template <size_t STATE_DIM, typename SCALAR>
138 {
139  switch (intType)
140  {
141  case EULERCT:
142  {
143  integratorStepper_ = std::shared_ptr<internal::StepperEulerCT<Eigen::Matrix<SCALAR, STATE_DIM, 1>, SCALAR>>(
145  break;
146  }
147 
148  case RK4CT:
149  {
150  integratorStepper_ = std::shared_ptr<internal::StepperRK4CT<Eigen::Matrix<SCALAR, STATE_DIM, 1>, SCALAR>>(
152  break;
153  }
154 
155  default:
156  break;
157  }
158 }
159 
160 template <size_t STATE_DIM, typename SCALAR>
162 {
163  observer_.reset();
164 }
165 
166 template <size_t STATE_DIM, typename SCALAR>
168  StateVectorArray<STATE_DIM, SCALAR>& stateTrajectory,
169  tpl::TimeArray<SCALAR>& timeTrajectory)
170 {
171  stateTrajectory.swap(observer_.states_);
172  timeTrajectory.swap(observer_.times_);
173 }
174 
175 template <size_t STATE_DIM, typename SCALAR>
177  StateVectorArray<STATE_DIM, SCALAR>& stateTrajectory)
178 {
179  stateTrajectory.swap(observer_.states_);
180 }
181 
182 
183 template <size_t STATE_DIM, typename SCALAR>
185 {
186  systemFunction_ = [this](
187  const Eigen::Matrix<SCALAR, STATE_DIM, 1>& x, Eigen::Matrix<SCALAR, STATE_DIM, 1>& dxdt, SCALAR t) {
188  const StateVector<STATE_DIM, SCALAR>& xState(static_cast<const StateVector<STATE_DIM, SCALAR>&>(x));
189  StateVector<STATE_DIM, SCALAR>& dxdtState(static_cast<StateVector<STATE_DIM, SCALAR>&>(dxdt));
190  system_->computeDynamics(xState, t, dxdtState);
191  observer_.observeInternal(xState, t);
192  };
193 
194  reset();
195 }
196 }
197 }
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
Custom implementation of the euler stepper.
Definition: SteppersCT.h:81
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef std::shared_ptr< EventHandler< STATE_DIM, SCALAR > > EventHandlerPtr
Definition: Integrator.h:66
clear all close all load ct GNMSLog0 mat reformat t
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:39
ct::core::StateVector< state_dim > x
An array in time.
Definition: TimeArray.h:22
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
Interface class for a general system described by an ordinary differential equation (ODE) ...
Definition: System.h:38
Custom implementation of the rk4 integration scheme.
Definition: SteppersCT.h:107
Definition: Integrator.h:40
void setApadativeErrorTolerances(const SCALAR absErrTol, const SCALAR &relErrTol)
Sets the adaptive error tolerances.
Definition: Integrator-impl.h:44
void swap(DiscreteArray &other)
swaps the content of two arrays
Definition: DiscreteArray.h:84
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