27 template <
size_t STATE_DIM,
size_t CONTROL_DIM>
31 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
60 controlSpliner_(controlSpliner),
66 dXdSi_history_(state_matrix_array_t(0)),
67 dXdQi_history_(state_control_matrix_array_t(0)),
68 dXdQip1_history_(state_control_matrix_array_t(0)),
69 dXdHi_history_(state_vector_array_t(0))
73 void setLogs(std::shared_ptr<state_vector_array_t> x_log,
74 std::shared_ptr<control_vector_array_t> u_log,
75 std::shared_ptr<time_array_t> t_log,
76 std::shared_ptr<state_matrix_array_t> A_log,
77 std::shared_ptr<state_control_matrix_array_t> B_log)
90 dXdSi_history_.clear();
91 dXdSi_history_.push_back(Eigen::MatrixXd::Identity(STATE_DIM, STATE_DIM));
97 size_t nSteps = A_log_->size() / 1;
98 for (
size_t i = 0;
i < nSteps;
i++)
100 double dt_sim = (*t_log_)[
i + 1] - (*t_log_)[
i];
102 state_matrix_t temp = Eigen::MatrixXd::Identity(STATE_DIM, STATE_DIM) + dt_sim * (*A_log_)[
i];
104 dXdSi_history_.push_back(temp * dXdSi_history_.back());
111 size_t nCycles = A_log_->size() / m;
117 double b1 = 1.0 / 6.0;
118 double b2 = 1.0 / 3.0;
119 double b3 = 1.0 / 3.0;
120 double b4 = 1.0 / 6.0;
122 for (
size_t i = 0;
i < nCycles;
i++)
124 double h = (*t_log_)[(
i + 1) * m] - (*t_log_)[
i * m];
127 state_matrix_t dK1dSi = (*A_log_)[
i * m] * dXdSi_history_.back();
130 state_matrix_t dK2dSi = (*A_log_)[
i * m + 1] * (dXdSi_history_.back() + h * a_21 * dK1dSi);
133 state_matrix_t dK3dSi = (*A_log_)[
i * m + 2] * (dXdSi_history_.back() + h * a_32 * dK2dSi);
136 state_matrix_t dK4dSi = (*A_log_)[
i * m + 3] * (dXdSi_history_.back() + h * a_43 * dK3dSi);
140 state_matrix_t temp =
141 dXdSi_history_.back() + h * (b1 * dK1dSi + b2 * dK2dSi + b3 * dK3dSi + b4 * dK4dSi);
143 dXdSi_history_.push_back(temp);
149 std::cerr <<
"... ERROR: sensitivity calculation not implemented for this integration type. Exiting" 160 dXdQi_history_.clear();
161 dXdQi_history_.push_back(state_control_matrix_t::Zero());
167 for (
size_t i = 0;
i < A_log_->size(); ++
i)
169 control_matrix_t dSpldQ0 = controlSpliner_->splineDerivative_q_i((*t_log_)[
i], shotIdx_);
171 double dt_sim = (*t_log_)[i + 1] - (*t_log_)[
i];
173 dXdQi_history_.push_back(dXdQi_history_.back() +
174 dt_sim * ((*A_log_)[
i] * dXdQi_history_.back() + (*B_log_)[
i] * dSpldQ0));
181 size_t nCycles = A_log_->size() / m;
187 double b1 = 1.0 / 6.0;
188 double b2 = 1.0 / 3.0;
189 double b3 = 1.0 / 3.0;
190 double b4 = 1.0 / 6.0;
192 for (
size_t i = 0;
i < nCycles;
i++)
194 assert(
i * m < A_log_->size());
196 double h = (*t_log_)[(
i + 1) * m] - (*t_log_)[
i * m];
200 state_control_matrix_t dK1dQi =
201 (*A_log_)[
i * m] * dXdQi_history_.back() +
202 (*B_log_)[
i * m] * controlSpliner_->splineDerivative_q_i((*t_log_)[
i * m], shotIdx_);
205 state_control_matrix_t dK2dQi =
206 (*A_log_)[
i * m + 1] * (dXdQi_history_.back() + h * a_21 * dK1dQi) +
207 (*B_log_)[
i * m + 1] * controlSpliner_->splineDerivative_q_i((*t_log_)[
i * m + 1], shotIdx_);
210 state_control_matrix_t dK3dQi =
211 (*A_log_)[
i * m + 2] * (dXdQi_history_.back() + h * a_32 * dK2dQi) +
212 (*B_log_)[
i * m + 2] * controlSpliner_->splineDerivative_q_i((*t_log_)[
i * m + 2], shotIdx_);
215 state_control_matrix_t dK4dQi =
216 (*A_log_)[
i * m + 3] * (dXdQi_history_.back() + h * a_43 * dK3dQi) +
217 (*B_log_)[
i * m + 3] * controlSpliner_->splineDerivative_q_i((*t_log_)[
i * m + 3], shotIdx_);
221 state_control_matrix_t temp =
222 dXdQi_history_.back() + h * (b1 * dK1dQi + b2 * dK2dQi + b3 * dK3dQi + b4 * dK4dQi);
224 dXdQi_history_.push_back(temp);
230 std::cerr <<
"... ERROR: sensitivity calculation not implemented for this integration type. Exiting" 241 dXdQip1_history_.clear();
242 dXdQip1_history_.push_back(state_control_matrix_t::Zero());
244 assert(t_log_->size() == A_log_->size() + 1);
250 for (
size_t i = 0;
i < A_log_->size(); ++
i)
252 control_matrix_t dSpldQ1 = controlSpliner_->splineDerivative_q_iplus1((*t_log_)[
i], shotIdx_);
254 assert(dSpldQ1 == dSpldQ1);
256 double dt_sim = (*t_log_)[i + 1] - (*t_log_)[
i];
258 dXdQip1_history_.push_back(
259 dXdQip1_history_.back() +
260 dt_sim * ((*A_log_)[
i] * dXdQip1_history_.back() + (*B_log_)[
i] * dSpldQ1));
267 size_t nCycles = A_log_->size() / m;
278 for (
size_t i = 0;
i < nCycles;
i++)
280 double h = (*t_log_)[(
i + 1) * m] - (*t_log_)[
i * m];
284 state_control_matrix_t dK1dQip1 =
285 (*A_log_)[
i * m] * dXdQip1_history_.back() +
286 (*B_log_)[
i * m] * controlSpliner_->splineDerivative_q_iplus1((*t_log_)[
i * m], shotIdx_);
289 state_control_matrix_t dK2dQip1 =
290 (*A_log_)[
i * m + 1] * (dXdQip1_history_.back() + h * a_21 * dK1dQip1) +
291 (*B_log_)[
i * m + 1] *
292 controlSpliner_->splineDerivative_q_iplus1((*t_log_)[
i * m + 1], shotIdx_);
295 state_control_matrix_t dK3dQip1 =
296 (*A_log_)[
i * m + 2] * (dXdQip1_history_.back() + h * a_32 * dK2dQip1) +
297 (*B_log_)[
i * m + 2] *
298 controlSpliner_->splineDerivative_q_iplus1((*t_log_)[
i * m + 2], shotIdx_);
301 state_control_matrix_t dK4dQip1 =
302 (*A_log_)[
i * m + 3] * (dXdQip1_history_.back() + h * a_43 * dK3dQip1) +
303 (*B_log_)[
i * m + 3] *
304 controlSpliner_->splineDerivative_q_iplus1((*t_log_)[
i * m + 3], shotIdx_);
308 state_control_matrix_t temp =
309 dXdQip1_history_.back() + h * (b1 * dK1dQip1 + b2 * dK2dQip1 + b3 * dK3dQip1 + b4 * dK4dQip1);
311 dXdQip1_history_.push_back(temp);
317 throw(std::runtime_error(
318 "... ERROR: sensitivity calculation not implemented for this integration type."));
323 void getdXdSiTraj(state_matrix_array_t& dXdSiTraj) { dXdSiTraj = dXdSi_history_; }
324 void getdXdQiTraj(state_control_matrix_array_t& dXdQiTraj) { dXdQiTraj = dXdQi_history_; }
325 void getdXdQip1Traj(state_control_matrix_array_t& dXdQip1Traj) { dXdQip1Traj = dXdQip1_history_; }
326 void getdXdHiTraj(state_vector_array_t& dXdHiTraj) { dXdHiTraj = dXdHi_history_; }
328 std::shared_ptr<SplinerBase<control_vector_t>> controlSpliner_;
329 const size_t shotIdx_;
332 std::shared_ptr<state_vector_array_t> x_log_;
333 std::shared_ptr<control_vector_array_t> u_log_;
334 std::shared_ptr<time_array_t> t_log_;
335 std::shared_ptr<state_matrix_array_t> A_log_;
336 std::shared_ptr<state_control_matrix_array_t> B_log_;
338 state_matrix_array_t dXdSi_history_;
339 state_control_matrix_array_t dXdQi_history_;
340 state_control_matrix_array_t dXdQip1_history_;
341 state_vector_array_t dXdHi_history_;
DIMENSIONS::state_matrix_t state_matrix_t
Definition: RKnDerivatives.h:40
void setLogs(std::shared_ptr< state_vector_array_t > x_log, std::shared_ptr< control_vector_array_t > u_log, std::shared_ptr< time_array_t > t_log, std::shared_ptr< state_matrix_array_t > A_log, std::shared_ptr< state_control_matrix_array_t > B_log)
Definition: RKnDerivatives.h:73
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef DmsDimensions< STATE_DIM, CONTROL_DIM > DIMENSIONS
Definition: RKnDerivatives.h:33
This class implements analytical sensitivity generation for the euler and rk4 integration scheme...
Definition: RKnDerivatives.h:28
void getdXdSiTraj(state_matrix_array_t &dXdSiTraj)
Definition: RKnDerivatives.h:323
DIMENSIONS::state_control_matrix_t state_control_matrix_t
Definition: RKnDerivatives.h:42
Abstract base class for the control input splining between the DMS shots.
Definition: SplinerBase.h:20
Definition: DmsSettings.h:28
void compute_dXdQi()
Definition: RKnDerivatives.h:158
RKnDerivatives(std::shared_ptr< SplinerBase< control_vector_t >> controlSpliner, size_t shotIdx, const DmsSettings &settings)
Custom constructor.
Definition: RKnDerivatives.h:55
DIMENSIONS::control_vector_t control_vector_t
Definition: RKnDerivatives.h:37
void getdXdHiTraj(state_vector_array_t &dXdHiTraj)
Definition: RKnDerivatives.h:326
DIMENSIONS::state_vector_array_t state_vector_array_t
Definition: RKnDerivatives.h:36
DIMENSIONS::time_array_t time_array_t
Definition: RKnDerivatives.h:39
DIMENSIONS::control_matrix_t control_matrix_t
Definition: RKnDerivatives.h:41
DIMENSIONS::state_vector_t state_vector_t
Definition: RKnDerivatives.h:35
for i
Definition: mpc_unittest_plotting.m:14
void compute_dXdQip1()
Definition: RKnDerivatives.h:239
Defines the DMS settings.
Definition: DmsSettings.h:23
DIMENSIONS::control_vector_array_t control_vector_array_t
Definition: RKnDerivatives.h:38
void getdXdQip1Traj(state_control_matrix_array_t &dXdQip1Traj)
Definition: RKnDerivatives.h:325
void getdXdQiTraj(state_control_matrix_array_t &dXdQiTraj)
Definition: RKnDerivatives.h:324
DIMENSIONS::state_control_matrix_array_t state_control_matrix_array_t
Definition: RKnDerivatives.h:44
void compute_dXdSi()
Definition: RKnDerivatives.h:88
IntegrationType_t integrationType_
Definition: DmsSettings.h:58
DIMENSIONS::state_matrix_array_t state_matrix_array_t
Definition: RKnDerivatives.h:43
Definition: DmsSettings.h:28