- 3.0.2 core module.
SystemDiscretizer-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 #define SYMPLECTIC_ENABLED \
9  template <size_t V, size_t P, size_t ST> \
10  typename std::enable_if<(V > 0 && P > 0 && (V + P == ST)), void>::type
11 #define SYMPLECTIC_DISABLED \
12  template <size_t V, size_t P, size_t ST> \
13  typename std::enable_if<(V <= 0 || P <= 0 || (V + P != ST)), void>::type
14 
15 namespace ct {
16 namespace core {
17 
18 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR>
20  : cont_constant_controller_(new ConstantController<STATE_DIM, CONTROL_DIM, SCALAR>())
21 {
22 }
23 
24 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR>
26  const ct::core::IntegrationType& integratorType,
27  const int& K_sim)
28  : dt_(dt),
29  K_sim_(K_sim),
30  integratorType_(integratorType),
31  cont_constant_controller_(new ConstantController<STATE_DIM, CONTROL_DIM, SCALAR>())
32 
33 {
35 }
36 
37 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR>
39  const SCALAR& dt,
40  const ct::core::IntegrationType& integratorType,
41  const int& K_sim)
42  : dt_(dt),
43  K_sim_(K_sim),
44  integratorType_(integratorType),
45  cont_constant_controller_(new ConstantController<STATE_DIM, CONTROL_DIM, SCALAR>())
46 
47 {
50 }
51 
52 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR>
54 {
55 }
56 
57 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR>
60 {
62 }
63 
64 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR>
67 {
68  return new SystemDiscretizer(*this);
69 }
70 
71 
72 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR>
74 {
75  return dt_ / (SCALAR)K_sim_;
76 }
77 
78 
79 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR>
81  const ct::core::IntegrationType& integratorType)
82 {
83  integratorType_ = integratorType;
84 }
85 
86 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR>
88 {
89  dt_ = dt;
90  K_sim_ = K_sim;
92 }
93 
94 
95 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR>
97 {
100 
102  {
103  integrator_ = std::shared_ptr<ct::core::Integrator<STATE_DIM, SCALAR>>(
105  }
106  initializeSymplecticIntegrator<V_DIM, P_DIM, STATE_DIM>();
107 }
108 
109 
110 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR>
113 {
114  cont_time_system_ = std::shared_ptr<ControlledSystem<STATE_DIM, CONTROL_DIM, SCALAR>>(dyn->clone());
116  initialize();
117 }
118 
119 
120 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR>
122  const StateVector<STATE_DIM, SCALAR>& state,
123  const time_t n,
124  const ControlVector<CONTROL_DIM, SCALAR>& control,
126 {
127  assert(K_sim_ > 0);
128  assert(dt_sim_ > 0.0);
129  assert(dt_ > 0.0);
130  assert(cont_time_system_ != nullptr);
131 
132  cont_constant_controller_->setControl(control);
133 
134  // reset substep recorder for every new control step
135  substepRecorder_->reset();
136 
137  // initialize state to propagate
138  stateNext = state;
139 
140  // perform integration
142  {
143  integrateSymplectic<V_DIM, P_DIM, STATE_DIM>(stateNext, n * dt_, K_sim_, dt_sim_);
144  }
145  else
146  {
147  integrator_->integrate_n_steps(stateNext, n * dt_, K_sim_, dt_sim_);
148  }
149 }
150 
151 
152 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR>
154 {
155  if (cont_time_system_->isSymplectic())
156  {
160  std::shared_ptr<ct::core::IntegratorSymplecticEuler<P_DIM, V_DIM, CONTROL_DIM, SCALAR>>(
165 
167  integratorRkSymplectic_ = std::shared_ptr<ct::core::IntegratorSymplecticRk<P_DIM, V_DIM, CONTROL_DIM, SCALAR>>(
171  }
172 }
173 
174 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR>
176 {
177  // leave empty
178 }
179 
180 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR>
183  const double& t,
184  const size_t& steps,
185  const double& dt_sim) const
186 {
187  if (!cont_time_system_->isSymplectic())
188  throw std::runtime_error("Trying to integrate using symplectic integrator, but system is not symplectic.");
189 
191  {
192  integratorEulerSymplectic_->integrate_n_steps(x0, t, steps, dt_sim);
193  }
195  {
196  integratorRkSymplectic_->integrate_n_steps(x0, t, steps, dt_sim);
197  }
198  else
199  {
200  throw std::runtime_error("invalid symplectic integrator specified");
201  }
202 }
203 
204 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR>
207  const double& t,
208  const size_t& steps,
209  const double& dt_sim) const
210 {
211  throw std::runtime_error("Symplectic integrator selected but invalid dimensions for it. Check V_DIM>1, P_DIM>1");
212 }
213 
214 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR>
215 const std::shared_ptr<ct::core::StateVectorArray<STATE_DIM, SCALAR>>&
217 {
218  return substepRecorder_->getSubstates();
219 }
220 
221 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR>
222 const std::shared_ptr<ct::core::ControlVectorArray<CONTROL_DIM, SCALAR>>&
224 {
225  return substepRecorder_->getSubcontrols();
226 }
227 
228 
229 } // namespace core
230 } // namespace ct
231 
232 #undef SYMPLECTIC_ENABLED
233 #undef SYMPLECTIC_DISABLED
SCALAR dt_
the time discretization interval
Definition: SystemDiscretizer.h:157
#define SYMPLECTIC_DISABLED
Definition: SystemDiscretizer-impl.h:11
virtual ~SystemDiscretizer()
destructor
Definition: SystemDiscretizer-impl.h:53
SCALAR getSimulationTimestep()
compute the simulation timestep
Definition: SystemDiscretizer-impl.h:73
SYMPLECTIC_ENABLED integrateSymplectic(ct::core::StateVector< STATE_DIM, SCALAR > &x0, const double &t, const size_t &steps, const double &dt_sim) const
integrateSymplectic, gets instantiated if the system is symplectic
Definition: SystemDiscretizer-impl.h:181
void setIntegrationType(const ct::core::IntegrationType &integratorType)
update integration type
Definition: SystemDiscretizer-impl.h:80
A constant controller.
Definition: ConstantController.h:20
IntegratorSymplecticRkPtr integratorRkSymplectic_
an integrator for forward integrating a symplectic continuous-time system with symplectic RK methods ...
Definition: SystemDiscretizer.h:181
ct::core::IntegrationType integratorType_
the integration type for forward integration
Definition: SystemDiscretizer.h:166
IntegratorPtr integrator_
an integrator for forward integrating a general continuous-time system with standard RK methods ...
Definition: SystemDiscretizer.h:175
ContinuousSystemPtr cont_time_system_
the continuous-time system to be discretized
Definition: SystemDiscretizer.h:169
SCALAR dt_sim_
the integration sub-step size, which is a function of dt_ and K_sim_
Definition: SystemDiscretizer.h:163
virtual void propagateControlledDynamics(const StateVector< STATE_DIM, SCALAR > &state, const time_t n, const ControlVector< CONTROL_DIM, SCALAR > &control, StateVector< STATE_DIM, SCALAR > &stateNext) override
propagate discrete-time dynamics by performing a numerical forward integration of the continuous-time...
Definition: SystemDiscretizer-impl.h:121
clear all close all load ct GNMSLog0 mat reformat t
Event handler to record substeps.
Definition: SubstepRecorder.h:20
Discretize a general, continuous-time non-linear dynamic system using forward integration.
Definition: SystemDiscretizer.h:44
void initialize()
initialize class
Definition: SystemDiscretizer-impl.h:96
std::shared_ptr< ControlledSystem< STATE_DIM, CONTROL_DIM, SCALAR > > ContinuousSystemPtr
Definition: SystemDiscretizer.h:57
Definition: ControlVector.h:12
SubstepRecorderPtr substepRecorder_
substep recorder which logs all the substeps required for later computing exact sensitivities ...
Definition: SystemDiscretizer.h:184
void setParameters(const SCALAR &dt, const int &K_sim=1)
update parameters
Definition: SystemDiscretizer-impl.h:87
CppAD::AD< CppAD::cg::CG< double > > SCALAR
virtual SystemDiscretizer< STATE_DIM, CONTROL_DIM, P_DIM, V_DIM, SCALAR > * clone() const override
deep cloning
Definition: SystemDiscretizer-impl.h:66
void changeContinuousTimeSystem(ContinuousSystemPtr newSystem)
update the SystemDiscretizer with a new nonlinear, continuous-time system
Definition: SystemDiscretizer-impl.h:111
constexpr size_t n
Definition: MatrixInversionTest.cpp:14
#define SYMPLECTIC_ENABLED
Definition: SystemDiscretizer-impl.h:8
Definition: StateVector.h:12
Standard Integrator.
Definition: Integrator.h:62
int K_sim_
the forward simulation can be discretized finer than dt_. K_sim_ is an integer number representing th...
Definition: SystemDiscretizer.h:160
SystemDiscretizer()
default constructor
Definition: SystemDiscretizer-impl.h:19
ContinuousConstantControllerPtr cont_constant_controller_
the continuous-time constant controller to be applied to the continuous-time dynamics ...
Definition: SystemDiscretizer.h:172
Definition: Integrator.h:42
Definition: Integrator.h:41
This class wraps the symplectic integrators from boost to this toolbox.
Definition: IntegratorSymplectic.h:34
Base::time_t time_t
Definition: DiscreteControlledSystem.h:51
IntegratorSymplecticEulerPtr integratorEulerSymplectic_
an integrator for forward integrating a symplectic continuous-time system with symplectic Euler ...
Definition: SystemDiscretizer.h:178
StateVector< state_dim > x0
std::shared_ptr< ct::core::SubstepRecorder< STATE_DIM, CONTROL_DIM, SCALAR > > SubstepRecorderPtr
Definition: SystemDiscretizer.h:60
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
SYMPLECTIC_ENABLED initializeSymplecticIntegrator()
initialize the symplectic integrator, if the system is symplectic
Definition: SystemDiscretizer-impl.h:153
const StateVectorArrayPtr & getSubstates() const
return a pointer to the substates recorded during integration
Definition: SystemDiscretizer-impl.h:216
const ControlVectorArrayPtr & getSubcontrols() const
reuturn a pointer to the subcontrols recorded during integration
Definition: SystemDiscretizer-impl.h:223
IntegrationType
The available integration types.
Definition: Integrator.h:30