12 #define SYMPLECTIC_ENABLED \ 13 template <size_t V, size_t P> \ 14 typename std::enable_if<(V > 0 && P > 0), void>::type 15 #define SYMPLECTIC_DISABLED \ 16 template <size_t V, size_t P> \ 17 typename std::enable_if<(V <= 0 || P <= 0), void>::type 32 template <
size_t STATE_DIM,
34 size_t P_DIM = STATE_DIM / 2,
35 size_t V_DIM = STATE_DIM / 2,
40 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
61 bool timeVarying =
true)
62 : timeVarying_(timeVarying), symplectic_(false), dt_(dt), substep_(0), k_(0), controller_(controller)
89 stepper_ = std::shared_ptr<ct::core::internal::StepperCTBase<sensitivities_matrix_t, SCALAR>>(
97 stepper_ = std::shared_ptr<ct::core::internal::StepperCTBase<sensitivities_matrix_t, SCALAR>>(
103 throw std::runtime_error(
"Integration type not supported by sensitivity integrator");
120 linearSystem_ = linearSystem;
139 sensitivities_matrix_t AB;
145 for (
size_t i = 0;
i < numSteps; ++
i)
147 stepper_->do_step(dFdxDot_, AB, k * dt_, dt_);
150 A = AB.template leftCols<STATE_DIM>();
151 B = AB.template rightCols<CONTROL_DIM>();
167 const size_t numSteps,
169 state_control_matrix_t& B)
override 173 throw std::runtime_error(
"SensitivityIntegrator.h: Cached trajectories not set.");
176 std::cout <<
"length x: " << this->
xSubstep_->size() << std::endl;
177 std::cout <<
"length u: " << this->
uSubstep_->size() << std::endl;
178 std::cout <<
"n: " << n << std::endl;
179 throw std::runtime_error(
"SensitivityIntegrator.h: Cached trajectories too short.");
182 throw std::runtime_error(
"no state substeps available for requested time index");
184 throw std::runtime_error(
"no control substeps available for requested time index");
187 throw std::runtime_error(
"substep state trajectory length for given time index is zero");
189 throw std::runtime_error(
"substep control trajectory length for given time index is zero");
191 assert(x == this->
xSubstep_->operator[](n)->operator[](0) &&
"cached trajectory does not match provided state");
192 assert(u == this->
uSubstep_->operator[](n)->operator[](0) &&
"cached trajectory does not match provided input");
199 Aconst_ = linearSystem_->getDerivativeState(x, u, n * dt_);
200 Bconst_ = linearSystem_->getDerivativeControl(x, u, n * dt_);
214 state_vector_t x_next_;
216 state_matrix_t Aconst_;
217 state_control_matrix_t Bconst_;
219 std::shared_ptr<ct::core::LinearSystem<STATE_DIM, CONTROL_DIM, SCALAR>> linearSystem_;
220 std::shared_ptr<ct::core::Controller<STATE_DIM, CONTROL_DIM, SCALAR>> controller_;
223 std::function<void(const sensitivities_matrix_t&, sensitivities_matrix_t&, const SCALAR)> dFdxDot_;
225 std::shared_ptr<ct::core::internal::StepperCTBase<sensitivities_matrix_t, SCALAR>> stepper_;
228 inline void integrateSensitivities(
const state_matrix_t& A,
229 const state_control_matrix_t& B,
230 const state_vector_t&
x,
231 const sensitivities_matrix_t& dX0In,
232 sensitivities_matrix_t& dX0dt,
235 dX0dt.template leftCols<STATE_DIM>() = A * dX0In.template leftCols<STATE_DIM>();
236 dX0dt.template rightCols<CONTROL_DIM>() =
237 A * dX0In.template rightCols<CONTROL_DIM>() + B * controller_->getDerivativeU0(x, t);
242 dFdxDot_ = [
this](
const sensitivities_matrix_t& dX0In, sensitivities_matrix_t& dX0dt,
const SCALAR t) {
245 throw std::runtime_error(
"substeps not correctly initialized");
255 if (!xSubstep || xSubstep->size() <= this->substep_)
257 throw std::runtime_error(
"substeps not correctly initialized");
260 const state_vector_t& x = xSubstep->operator[](this->substep_);
261 const control_vector_t&
u = uSubstep->operator[](this->substep_);
265 state_matrix_t A_sym;
266 state_control_matrix_t B_sym;
267 const state_vector_t* x_next;
269 if (this->substep_ + 1 < xSubstep->size())
270 x_next = &xSubstep->operator[](this->substep_ + 1);
274 getSymplecticAandB<V_DIM, P_DIM>(t, x, *x_next, u, A_sym, B_sym);
276 integrateSensitivities(A_sym, B_sym, x, dX0In, dX0dt, t);
282 state_matrix_t A = linearSystem_->getDerivativeState(x, u, t);
283 state_control_matrix_t B = linearSystem_->getDerivativeControl(x, u, t);
285 integrateSensitivities(A, B, x, dX0In, dX0dt, t);
289 integrateSensitivities(Aconst_, Bconst_, x, dX0In, dX0dt, t);
298 const state_vector_t& x,
299 const state_vector_t& x_next,
300 const control_vector_t&
u,
301 state_matrix_t& A_sym,
302 state_control_matrix_t& B_sym)
305 state_vector_t x_interm = x;
306 x_interm.template topRows<P_DIM>() = x_next.template topRows<P_DIM>();
309 linearSystem_->getDerivativeState(x, u, t);
310 state_control_matrix_t Bc1 =
311 linearSystem_->getDerivativeControl(x, u, t);
312 state_matrix_t Ac2 = linearSystem_->getDerivativeState(
314 state_control_matrix_t Bc2 = linearSystem_->getDerivativeControl(
318 typedef Eigen::Matrix<SCALAR, P_DIM, P_DIM> p_matrix_t;
319 typedef Eigen::Matrix<SCALAR, V_DIM, V_DIM> v_matrix_t;
320 typedef Eigen::Matrix<SCALAR, P_DIM, V_DIM> p_v_matrix_t;
321 typedef Eigen::Matrix<SCALAR, V_DIM, P_DIM> v_p_matrix_t;
322 typedef Eigen::Matrix<SCALAR, P_DIM, CONTROL_DIM> p_control_matrix_t;
323 typedef Eigen::Matrix<SCALAR, V_DIM, CONTROL_DIM> v_control_matrix_t;
327 const Eigen::Ref<const p_matrix_t> A11 = Ac1.template topLeftCorner<P_DIM, P_DIM>();
328 const Eigen::Ref<const p_v_matrix_t> A12 = Ac1.template topRightCorner<P_DIM, V_DIM>();
329 const Eigen::Ref<const p_control_matrix_t> B1 = Bc1.template topRows<P_DIM>();
332 const Eigen::Ref<const v_p_matrix_t> A21 = Ac2.template bottomLeftCorner<V_DIM, P_DIM>();
333 const Eigen::Ref<const v_matrix_t> A22 = Ac2.template bottomRightCorner<V_DIM, V_DIM>();
334 const Eigen::Ref<const v_control_matrix_t> B2 = Bc2.template bottomRows<V_DIM>();
337 A_sym.template topLeftCorner<P_DIM, P_DIM>() = A11;
338 A_sym.template topRightCorner<P_DIM, V_DIM>() = A12;
339 A_sym.template bottomLeftCorner<V_DIM, P_DIM>() = (A21 * (p_matrix_t::Identity() + dt_ * A11));
340 A_sym.template bottomRightCorner<V_DIM, V_DIM>() = (A22 + dt_ * A21 * A12);
343 B_sym.template topRows<P_DIM>() = B1;
344 B_sym.template bottomRows<V_DIM>() = (B2 + dt_ * A21 * B1);
348 const state_vector_t& x,
349 const state_vector_t& x_next,
350 const control_vector_t& u,
351 state_matrix_t& A_sym,
352 state_control_matrix_t& B_sym)
354 throw std::runtime_error(
"Cannot compute sensitivities for symplectic system with these dimensions.");
361 #undef SYMPLECTIC_ENABLED 362 #undef SYMPLECTIC_DISABLED interface class for a general linear system or linearized system
Definition: LinearSystem.h:23
std::shared_ptr< ControlVectorArray< CONTROL_DIM, SCALAR > > ControlVectorArrayPtr
Definition: Sensitivity.h:39
void setStepper(const ct::core::IntegrationType stepperType)
Initializes the steppers.
Definition: SensitivityIntegrator.h:79
std::vector< ControlVectorArrayPtr, Eigen::aligned_allocator< ControlVectorArrayPtr > > * uSubstep_
Definition: Sensitivity.h:88
StateMatrix< STATE_DIM, SCALAR > state_matrix_t
state Jacobian type
Definition: SensitivityIntegrator.h:44
Definition: Integrator.h:32
virtual void setTimeDiscretization(const SCALAR &dt) override
update the time discretization
Definition: SensitivityIntegrator.h:124
ct::core::ControlVector< control_dim > u
Custom implementation of the euler stepper.
Definition: SteppersCT.h:81
Eigen::Matrix< SCALAR, STATE_DIM, STATE_DIM+CONTROL_DIM > sensitivities_matrix_t
Definition: SensitivityIntegrator.h:48
#define SYMPLECTIC_ENABLED
Definition: SensitivityIntegrator.h:12
Definition: StateMatrix.h:12
virtual void setLinearSystem(const std::shared_ptr< ct::core::LinearSystem< STATE_DIM, CONTROL_DIM, SCALAR >> &linearSystem) override
Prepares the integrator to provide first order sensitivity generation by setting a linearsystem...
Definition: SensitivityIntegrator.h:117
clear all close all load ct GNMSLog0 mat reformat t
StateControlMatrix< STATE_DIM, CONTROL_DIM, SCALAR > state_control_matrix_t
input Jacobian type
Definition: SensitivityIntegrator.h:46
Definition: ControlVector.h:12
#define SYMPLECTIC_DISABLED
Definition: SensitivityIntegrator.h:15
CppAD::AD< CppAD::cg::CG< double > > SCALAR
SensitivityIntegrator(const SCALAR dt, const std::shared_ptr< ct::core::LinearSystem< STATE_DIM, CONTROL_DIM, SCALAR >> &linearSystem, const std::shared_ptr< ct::core::Controller< STATE_DIM, CONTROL_DIM, SCALAR >> &controller, const ct::core::IntegrationType stepperType=ct::core::IntegrationType::EULERCT, bool timeVarying=true)
Constructor.
Definition: SensitivityIntegrator.h:57
constexpr size_t n
Definition: MatrixInversionTest.cpp:14
Definition: StateVector.h:12
Definition: Integrator.h:33
Definition: Integrator.h:39
virtual void getAandB(const StateVector< STATE_DIM, SCALAR > &x, const ControlVector< CONTROL_DIM, SCALAR > &u, const StateVector< STATE_DIM, SCALAR > &x_next, const int n, const size_t numSteps, state_matrix_t &A, state_control_matrix_t &B) override
Definition: SensitivityIntegrator.h:163
ct::core::StateVector< state_dim > x
Definition: Sensitivity.h:33
void integrateSensitivity(const size_t k, const size_t numSteps, state_matrix_t &A, state_control_matrix_t &B)
Integrates the sensitivity ODE of the integrator with respect to the initial state x0...
Definition: SensitivityIntegrator.h:134
Definition: Integrator.h:41
ControlMatrix< CONTROL_DIM, SCALAR > control_matrix_t
state Jacobian type
Definition: SensitivityIntegrator.h:45
ct::core::ControlVector< CONTROL_DIM, SCALAR > control_vector_t
Definition: SensitivityIntegrator.h:43
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef ct::core::StateVector< STATE_DIM, SCALAR > state_vector_t
Definition: SensitivityIntegrator.h:42
This class can integrate a controlled system Furthermore, it provides first order derivatives with re...
Definition: SensitivityIntegrator.h:37
std::vector< StateVectorArrayPtr, Eigen::aligned_allocator< StateVectorArrayPtr > > * xSubstep_
Definition: Sensitivity.h:87
Custom implementation of the rk4 integration scheme.
Definition: SteppersCT.h:107
Definition: Integrator.h:40
~SensitivityIntegrator() override=default
Destroys the object.
Definition: ControlMatrix.h:12
Definition: StateControlMatrix.h:12
Interface class for all controllers.
Definition: Controller.h:26
IntegrationType
The available integration types.
Definition: Integrator.h:30