|
| 1 | +/* |
| 2 | + * AD tool to FORCESPRO Template - missing information to be filled in by createADTool.m |
| 3 | + * (C) embotech AG, Zurich, Switzerland, 2013-2023. All rights reserved. |
| 4 | + * |
| 5 | + * This file is part of the FORCESPRO client, and carries the same license. |
| 6 | + */ |
| 7 | + |
| 8 | +#ifdef __cplusplus |
| 9 | +extern "C" { |
| 10 | +#endif |
| 11 | + |
| 12 | +#include "include/MPC_SOLVER.h" |
| 13 | + |
| 14 | +#ifndef NULL |
| 15 | +#define NULL ((void *) 0) |
| 16 | +#endif |
| 17 | + |
| 18 | +#include "MPC_SOLVER_model.h" |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | +/* copies data from sparse matrix into a dense one */ |
| 23 | +static void MPC_SOLVER_sparse2fullcopy(solver_int32_default nrow, solver_int32_default ncol, const solver_int32_default *colidx, const solver_int32_default *row, MPC_SOLVER_callback_float *data, MPC_SOLVER_float *out) |
| 24 | +{ |
| 25 | + solver_int32_default i, j; |
| 26 | + |
| 27 | + /* copy data into dense matrix */ |
| 28 | + for(i=0; i<ncol; i++) |
| 29 | + { |
| 30 | + for(j=colidx[i]; j<colidx[i+1]; j++) |
| 31 | + { |
| 32 | + out[i*nrow + row[j]] = ((MPC_SOLVER_float) data[j]); |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | +/* AD tool to FORCESPRO interface */ |
| 41 | +extern solver_int32_default MPC_SOLVER_adtool2forces(MPC_SOLVER_float *x, /* primal vars */ |
| 42 | + MPC_SOLVER_float *y, /* eq. constraint multiplers */ |
| 43 | + MPC_SOLVER_float *l, /* ineq. constraint multipliers */ |
| 44 | + MPC_SOLVER_float *p, /* parameters */ |
| 45 | + MPC_SOLVER_float *f, /* objective function (scalar) */ |
| 46 | + MPC_SOLVER_float *nabla_f, /* gradient of objective function */ |
| 47 | + MPC_SOLVER_float *c, /* dynamics */ |
| 48 | + MPC_SOLVER_float *nabla_c, /* Jacobian of the dynamics (column major) */ |
| 49 | + MPC_SOLVER_float *h, /* inequality constraints */ |
| 50 | + MPC_SOLVER_float *nabla_h, /* Jacobian of inequality constraints (column major) */ |
| 51 | + MPC_SOLVER_float *hess, /* Hessian (column major) */ |
| 52 | + solver_int32_default stage, /* stage number (0 indexed) */ |
| 53 | + solver_int32_default iteration, /* iteration number of solver */ |
| 54 | + solver_int32_default threadID /* Id of caller thread */) |
| 55 | +{ |
| 56 | + /* AD tool input and output arrays */ |
| 57 | + const MPC_SOLVER_callback_float *in[4]; |
| 58 | + MPC_SOLVER_callback_float *out[7]; |
| 59 | + |
| 60 | + |
| 61 | + /* Allocate working arrays for AD tool */ |
| 62 | + |
| 63 | + MPC_SOLVER_callback_float w[46]; |
| 64 | + |
| 65 | + /* temporary storage for AD tool sparse output */ |
| 66 | + MPC_SOLVER_callback_float this_f = (MPC_SOLVER_callback_float) 0.0; |
| 67 | + MPC_SOLVER_float nabla_f_sparse[5]; |
| 68 | + MPC_SOLVER_float h_sparse[6]; |
| 69 | + MPC_SOLVER_float nabla_h_sparse[17]; |
| 70 | + MPC_SOLVER_float c_sparse[4]; |
| 71 | + MPC_SOLVER_float nabla_c_sparse[10]; |
| 72 | + |
| 73 | + |
| 74 | + /* pointers to row and column info for |
| 75 | + * column compressed format used by AD tool */ |
| 76 | + solver_int32_default nrow, ncol; |
| 77 | + const solver_int32_default *colind, *row; |
| 78 | + |
| 79 | + /* set inputs for AD tool */ |
| 80 | + in[0] = x; |
| 81 | + in[1] = p; |
| 82 | + in[2] = l; |
| 83 | + in[3] = y; |
| 84 | + |
| 85 | + if ((0 <= stage && stage <= 23)) |
| 86 | + { |
| 87 | + |
| 88 | + |
| 89 | + out[0] = &this_f; |
| 90 | + out[1] = nabla_f_sparse; |
| 91 | + MPC_SOLVER_objective_0(in, out, NULL, w, 0); |
| 92 | + if( nabla_f ) |
| 93 | + { |
| 94 | + nrow = MPC_SOLVER_objective_0_sparsity_out(1)[0]; |
| 95 | + ncol = MPC_SOLVER_objective_0_sparsity_out(1)[1]; |
| 96 | + colind = MPC_SOLVER_objective_0_sparsity_out(1) + 2; |
| 97 | + row = MPC_SOLVER_objective_0_sparsity_out(1) + 2 + (ncol + 1); |
| 98 | + MPC_SOLVER_sparse2fullcopy(nrow, ncol, colind, row, nabla_f_sparse, nabla_f); |
| 99 | + } |
| 100 | + |
| 101 | + out[0] = c_sparse; |
| 102 | + out[1] = nabla_c_sparse; |
| 103 | + MPC_SOLVER_dynamics_0(in, out, NULL, w, 0); |
| 104 | + if( c ) |
| 105 | + { |
| 106 | + nrow = MPC_SOLVER_dynamics_0_sparsity_out(0)[0]; |
| 107 | + ncol = MPC_SOLVER_dynamics_0_sparsity_out(0)[1]; |
| 108 | + colind = MPC_SOLVER_dynamics_0_sparsity_out(0) + 2; |
| 109 | + row = MPC_SOLVER_dynamics_0_sparsity_out(0) + 2 + (ncol + 1); |
| 110 | + MPC_SOLVER_sparse2fullcopy(nrow, ncol, colind, row, c_sparse, c); |
| 111 | + } |
| 112 | + if( nabla_c ) |
| 113 | + { |
| 114 | + nrow = MPC_SOLVER_dynamics_0_sparsity_out(1)[0]; |
| 115 | + ncol = MPC_SOLVER_dynamics_0_sparsity_out(1)[1]; |
| 116 | + colind = MPC_SOLVER_dynamics_0_sparsity_out(1) + 2; |
| 117 | + row = MPC_SOLVER_dynamics_0_sparsity_out(1) + 2 + (ncol + 1); |
| 118 | + MPC_SOLVER_sparse2fullcopy(nrow, ncol, colind, row, nabla_c_sparse, nabla_c); |
| 119 | + } |
| 120 | + |
| 121 | + out[0] = h_sparse; |
| 122 | + out[1] = nabla_h_sparse; |
| 123 | + MPC_SOLVER_inequalities_0(in, out, NULL, w, 0); |
| 124 | + if( h ) |
| 125 | + { |
| 126 | + nrow = MPC_SOLVER_inequalities_0_sparsity_out(0)[0]; |
| 127 | + ncol = MPC_SOLVER_inequalities_0_sparsity_out(0)[1]; |
| 128 | + colind = MPC_SOLVER_inequalities_0_sparsity_out(0) + 2; |
| 129 | + row = MPC_SOLVER_inequalities_0_sparsity_out(0) + 2 + (ncol + 1); |
| 130 | + MPC_SOLVER_sparse2fullcopy(nrow, ncol, colind, row, h_sparse, h); |
| 131 | + } |
| 132 | + if( nabla_h ) |
| 133 | + { |
| 134 | + nrow = MPC_SOLVER_inequalities_0_sparsity_out(1)[0]; |
| 135 | + ncol = MPC_SOLVER_inequalities_0_sparsity_out(1)[1]; |
| 136 | + colind = MPC_SOLVER_inequalities_0_sparsity_out(1) + 2; |
| 137 | + row = MPC_SOLVER_inequalities_0_sparsity_out(1) + 2 + (ncol + 1); |
| 138 | + MPC_SOLVER_sparse2fullcopy(nrow, ncol, colind, row, nabla_h_sparse, nabla_h); |
| 139 | + } |
| 140 | + } |
| 141 | + if ((24 == stage)) |
| 142 | + { |
| 143 | + |
| 144 | + |
| 145 | + out[0] = &this_f; |
| 146 | + out[1] = nabla_f_sparse; |
| 147 | + MPC_SOLVER_objective_1(in, out, NULL, w, 0); |
| 148 | + if( nabla_f ) |
| 149 | + { |
| 150 | + nrow = MPC_SOLVER_objective_1_sparsity_out(1)[0]; |
| 151 | + ncol = MPC_SOLVER_objective_1_sparsity_out(1)[1]; |
| 152 | + colind = MPC_SOLVER_objective_1_sparsity_out(1) + 2; |
| 153 | + row = MPC_SOLVER_objective_1_sparsity_out(1) + 2 + (ncol + 1); |
| 154 | + MPC_SOLVER_sparse2fullcopy(nrow, ncol, colind, row, nabla_f_sparse, nabla_f); |
| 155 | + } |
| 156 | + |
| 157 | + out[0] = h_sparse; |
| 158 | + out[1] = nabla_h_sparse; |
| 159 | + MPC_SOLVER_inequalities_1(in, out, NULL, w, 0); |
| 160 | + if( h ) |
| 161 | + { |
| 162 | + nrow = MPC_SOLVER_inequalities_1_sparsity_out(0)[0]; |
| 163 | + ncol = MPC_SOLVER_inequalities_1_sparsity_out(0)[1]; |
| 164 | + colind = MPC_SOLVER_inequalities_1_sparsity_out(0) + 2; |
| 165 | + row = MPC_SOLVER_inequalities_1_sparsity_out(0) + 2 + (ncol + 1); |
| 166 | + MPC_SOLVER_sparse2fullcopy(nrow, ncol, colind, row, h_sparse, h); |
| 167 | + } |
| 168 | + if( nabla_h ) |
| 169 | + { |
| 170 | + nrow = MPC_SOLVER_inequalities_1_sparsity_out(1)[0]; |
| 171 | + ncol = MPC_SOLVER_inequalities_1_sparsity_out(1)[1]; |
| 172 | + colind = MPC_SOLVER_inequalities_1_sparsity_out(1) + 2; |
| 173 | + row = MPC_SOLVER_inequalities_1_sparsity_out(1) + 2 + (ncol + 1); |
| 174 | + MPC_SOLVER_sparse2fullcopy(nrow, ncol, colind, row, nabla_h_sparse, nabla_h); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + /* add to objective */ |
| 179 | + if (f != NULL) |
| 180 | + { |
| 181 | + *f += ((MPC_SOLVER_float) this_f); |
| 182 | + } |
| 183 | + |
| 184 | + return 0; |
| 185 | +} |
| 186 | + |
| 187 | +#ifdef __cplusplus |
| 188 | +} /* extern "C" */ |
| 189 | +#endif |
0 commit comments