OMCompiler/SimulationRuntime/c/optimization/eval_all/EvalG.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file belongs to the OpenModelica Run-Time System | ||
| 3 | * | ||
| 4 | * Copyright (c) 1998-2026, Open Source Modelica Consortium (OSMC), c/o Linköpings | ||
| 5 | * universitet, Department of Computer and Information Science, SE-58183 Linköping, Sweden. All rights | ||
| 6 | * reserved. | ||
| 7 | * | ||
| 8 | * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THE BSD NEW LICENSE OR THE | ||
| 9 | * AGPL VERSION 3 LICENSE OR THE OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.8. ANY | ||
| 10 | * USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S | ||
| 11 | * ACCEPTANCE OF THE BSD NEW LICENSE OR THE OSMC PUBLIC LICENSE OR THE AGPL | ||
| 12 | * VERSION 3, ACCORDING TO RECIPIENTS CHOICE. | ||
| 13 | * | ||
| 14 | * The OpenModelica software and the OSMC (Open Source Modelica Consortium) Public License | ||
| 15 | * (OSMC-PL) are obtained from OSMC, either from the above address, from the URLs: | ||
| 16 | * http://www.openmodelica.org or https://github.com/OpenModelica/ or | ||
| 17 | * http://www.ida.liu.se/projects/OpenModelica, and in the OpenModelica distribution. GNU | ||
| 18 | * AGPL version 3 is obtained from: https://www.gnu.org/licenses/licenses.html#GPL. The BSD NEW | ||
| 19 | * License is obtained from: http://www.opensource.org/licenses/BSD-3-Clause. | ||
| 20 | * | ||
| 21 | * This program is distributed WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY | ||
| 23 | * SET FORTH IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF | ||
| 24 | * OSMC-PL. | ||
| 25 | * | ||
| 26 | */ | ||
| 27 | |||
| 28 | /*! EvalG.c | ||
| 29 | */ | ||
| 30 | |||
| 31 | #include "../OptimizerData.h" | ||
| 32 | #include "../OptimizerLocalFunction.h" | ||
| 33 | #include "../../simulation/results/simulation_result.h" | ||
| 34 | #include "../../simulation/options.h" | ||
| 35 | #include "om_format.h" | ||
| 36 | |||
| 37 | static inline void generated_jac_struc(OptData *, int*, int*); | ||
| 38 | static inline void set_row(int *, int *, int *, const modelica_boolean *const, | ||
| 39 | const int, const int , const int ); | ||
| 40 | |||
| 41 | static inline void set_cell(int*, int*, int*, const int, const int); | ||
| 42 | |||
| 43 | static inline void structJac01(const long double * const a, const modelica_real *const Jj, double * values, const int nv, int *k, const int j, | ||
| 44 | const modelica_boolean * const sJj); | ||
| 45 | |||
| 46 | static inline void structJac1(const long double * const a, const modelica_real *const Jj, double * values, const int nv, int *k, const int j, | ||
| 47 | const modelica_boolean * const sJj); | ||
| 48 | |||
| 49 | static inline void structJac02(const long double * const a, const modelica_real *const Jj, double * values, const int nv, int *k, const int j, | ||
| 50 | const modelica_boolean * const sJj); | ||
| 51 | |||
| 52 | static inline void structJac2(const long double * const a, const modelica_real *const Jj, double * values, const int nv, int *k, const int j, | ||
| 53 | const modelica_boolean * const sJj); | ||
| 54 | |||
| 55 | static inline void structJac03(const long double * const a, const modelica_real *const Jj, double * values, const int nv, int *k, const int j, | ||
| 56 | const modelica_boolean * const sJj); | ||
| 57 | |||
| 58 | static inline void structJac3(const long double * const a, const modelica_real *const Jj, double * values, const int nv, int *k, const int j, | ||
| 59 | const modelica_boolean * const sJj); | ||
| 60 | |||
| 61 | static inline void structJacC(const modelica_real *const J, double *values, | ||
| 62 | const int nv, int *k, const modelica_boolean * const Jj); | ||
| 63 | |||
| 64 | static inline void printMaxError(ipnumber *g, const int m, const int nx, const int nJ, long double **t, | ||
| 65 | const int np, const int nsi, DATA * data, OptData * optData); | ||
| 66 | |||
| 67 | |||
| 68 | /* eval constraints | ||
| 69 | */ | ||
| 70 | ✗ | Bool evalfG(ipindex n, double * vopt, Bool new_x, int m, ipnumber *g, void * useData){ | |
| 71 | OptData *optData = (OptData*)useData; | ||
| 72 | |||
| 73 | ✗ | const int nx = optData->dim.nx; | |
| 74 | ✗ | const int nv = optData->dim.nv; | |
| 75 | ✗ | const int nc = optData->dim.nc; | |
| 76 | ✗ | const int ncf = optData->dim.ncf; | |
| 77 | ✗ | const int nsi = optData->dim.nsi; | |
| 78 | ✗ | const int np = optData->dim.np; | |
| 79 | ✗ | const int index_con = optData->dim.index_con; | |
| 80 | ✗ | const int index_conf = optData->dim.index_conf; | |
| 81 | |||
| 82 | modelica_real ***v; | ||
| 83 | long double a[5][5]; | ||
| 84 | long double *sdt; | ||
| 85 | ✗ | double **vv = (double**)malloc((np+1) * sizeof(double*)); | |
| 86 | int i, j, k, shift; | ||
| 87 | |||
| 88 | |||
| 89 | ✗ | if(new_x){ | |
| 90 | ✗ | optData2ModelData(optData, vopt, optData->index); | |
| 91 | } | ||
| 92 | |||
| 93 | ✗ | v = optData->v; | |
| 94 | ✗ | memcpy(a ,optData->rk.a, sizeof(optData->rk.a)); | |
| 95 | |||
| 96 | ✗ | vv[0] = optData->sv0; | |
| 97 | ✗ | vv[1] = vopt; | |
| 98 | ✗ | for(j = 1; j < np; ++j){ | |
| 99 | ✗ | vv[j + 1] = vv[j] + nv; | |
| 100 | } | ||
| 101 | |||
| 102 | ✗ | if(np == 3){ | |
| 103 | ✗ | for(i = 0, shift = 0; i <nsi; ++i){ | |
| 104 | ✗ | sdt = optData->bounds.scaldt[i]; | |
| 105 | /*1*/ | ||
| 106 | ✗ | for(k=0; k< nx; ++k){ | |
| 107 | ✗ | g[shift++] = (a[0][0]*vv[0][k] + a[0][3]*vv[3][k] + sdt[k]*v[i][0][k+nx]) | |
| 108 | ✗ | -(a[0][1]*vv[1][k] + a[0][2]*vv[2][k]); | |
| 109 | } | ||
| 110 | ✗ | memcpy(g + shift, &v[i][0][index_con], nc*sizeof(double)); | |
| 111 | ✗ | shift += nc; | |
| 112 | |||
| 113 | /*2*/ | ||
| 114 | ✗ | for(k=0; k< nx; ++k){ | |
| 115 | ✗ | g[shift++] = (a[1][1]*vv[1][k] + sdt[k]*v[i][1][k+nx]) | |
| 116 | ✗ | - (a[1][0]*vv[0][k] + a[1][2]*vv[2][k] + a[1][3]*vv[3][k]); | |
| 117 | } | ||
| 118 | ✗ | memcpy(g + shift, &v[i][1][index_con], nc*sizeof(double)); | |
| 119 | ✗ | shift +=nc; | |
| 120 | |||
| 121 | /*3*/ | ||
| 122 | ✗ | for(k=0; k< nx; ++k){ | |
| 123 | ✗ | g[shift++] = (a[2][0]*vv[0][k] + a[2][2]*vv[2][k] + sdt[k]*v[i][2][nx+k]) | |
| 124 | ✗ | -(a[2][1]*vv[1][k] + a[2][3]*vv[3][k]); | |
| 125 | } | ||
| 126 | ✗ | memcpy(g + shift, &v[i][2][index_con], nc*sizeof(double)); | |
| 127 | ✗ | shift +=nc; | |
| 128 | ✗ | vv[0] = vv[np]; | |
| 129 | ✗ | for(j = 0; j < np; ++j) | |
| 130 | ✗ | vv[j + 1] = vv[j] + nv; | |
| 131 | } | ||
| 132 | /*terminal constraint(s)*/ | ||
| 133 | ✗ | memcpy(g + shift, &v[nsi-1][2][index_conf], ncf*sizeof(double)); | |
| 134 | |||
| 135 | ✗ | }else if(np == 1){ | |
| 136 | ✗ | for(i = 0, shift = 0; i <nsi; ++i){ | |
| 137 | ✗ | sdt = optData->bounds.scaldt[i]; | |
| 138 | ✗ | for(k = 0; k < nx; ++k) | |
| 139 | ✗ | g[shift++] = vv[0][k] + (sdt[k]*v[i][0][k+nx] - vv[1][k]); | |
| 140 | |||
| 141 | ✗ | memcpy(g + shift, &v[i][0][index_con], nc*sizeof(double)); | |
| 142 | ✗ | shift += nc; | |
| 143 | ✗ | vv[0] = vv[np]; | |
| 144 | ✗ | for(j = 0; j < np; ++j) | |
| 145 | ✗ | vv[j + 1] = vv[j] + nv; | |
| 146 | } | ||
| 147 | /*terminal constraint(s)*/ | ||
| 148 | ✗ | memcpy(g + m - ncf, &v[nsi-1][0][index_conf], ncf*sizeof(double)); | |
| 149 | } | ||
| 150 | ✗ | if(OMC_ACTIVE_STREAM(OMC_LOG_IPOPT_ERROR)){ | |
| 151 | ✗ | const int nJ = optData->dim.nJ; | |
| 152 | ✗ | printMaxError(g, m, nx, nJ, optData->time.t, np ,nsi ,optData->data, optData); | |
| 153 | } | ||
| 154 | ✗ | free(vv); | |
| 155 | ✗ | return TRUE; | |
| 156 | } | ||
| 157 | |||
| 158 | /*! | ||
| 159 | * eval derivation of s.t. | ||
| 160 | * author: Vitalij Ruge | ||
| 161 | **/ | ||
| 162 | ✗ | Bool evalfDiffG(ipindex n, double * vopt, Bool new_x, ipindex m, ipindex njac, ipindex *iRow, ipindex *iCol, ipnumber *values, void * useData){ | |
| 163 | OptData *optData = (OptData*)useData; | ||
| 164 | |||
| 165 | ✗ | if(!values){ | |
| 166 | ✗ | generated_jac_struc(optData, iRow, iCol); | |
| 167 | #if 0 | ||
| 168 | { | ||
| 169 | const int nsi = optData->dim.nsi; | ||
| 170 | int i; | ||
| 171 | FILE *pFile; | ||
| 172 | char buffer[4096]; | ||
| 173 | pFile = omc_fopen("jac_struct.m", "wt"); | ||
| 174 | if(pFile == NULL) | ||
| 175 | printf("\n\nError"); | ||
| 176 | fprintf(pFile, "%s", "clear J\n"); | ||
| 177 | fprintf(pFile, "%s", "%%%%%%%%%%%%%%%%%%%%%%\n"); | ||
| 178 | fprintf(pFile, "%s", "nz = "); | ||
| 179 | fprintf(pFile, "%i", njac); | ||
| 180 | fprintf(pFile, "%s", "\nnumberVars = "); | ||
| 181 | fprintf(pFile, "%i", n); | ||
| 182 | fprintf(pFile, "%s", "\nnumberconstraints = "); | ||
| 183 | fprintf(pFile, "%i", m); | ||
| 184 | fprintf(pFile, "%s", "\nNumberOfIntervalls = "); | ||
| 185 | fprintf(pFile, "%i", nsi); | ||
| 186 | fprintf(pFile, "\nH = sparse(%i,%i);\n",m,n); | ||
| 187 | fprintf(pFile, "%s", "%%%%%%%%%%%%%%%%%%%%%%\n"); | ||
| 188 | for(i=0; i< njac; ++i){ | ||
| 189 | sprintf(buffer, "H(%i,%i) = 1;\n", iRow[i]+1, iCol[i]+1); | ||
| 190 | fprintf(pFile,"%s", buffer); | ||
| 191 | } | ||
| 192 | fprintf(pFile, "%s", "%%%%%%%%%%%%%%%%%%%%%%\n"); | ||
| 193 | fprintf(pFile, "%s", "spy(H)\n"); | ||
| 194 | } | ||
| 195 | assert(0); | ||
| 196 | #endif | ||
| 197 | }else{ | ||
| 198 | ✗ | const int nsi = optData->dim.nsi; | |
| 199 | ✗ | const int np = optData->dim.np; | |
| 200 | ✗ | const int nx = optData->dim.nx; | |
| 201 | ✗ | const int nv = optData->dim.nv; | |
| 202 | ✗ | const int nJ = optData->dim.nJ; | |
| 203 | ✗ | const int ncf = optData->dim.ncf; | |
| 204 | ✗ | modelica_boolean ** J = optData->s.JderCon; | |
| 205 | ✗ | modelica_boolean ** Jf = optData->s.J[2]; | |
| 206 | int i, j, k, l, ii, cindex; | ||
| 207 | ✗ | ++optData->iter_; | |
| 208 | ✗ | if(new_x){ | |
| 209 | ✗ | optData2ModelData(optData, vopt, 1); | |
| 210 | } | ||
| 211 | ✗ | if(np == 3){ | |
| 212 | /*****************************/ | ||
| 213 | ✗ | for(j = 0, k = 0; j < np; ++j){ | |
| 214 | ✗ | for(l = 0; l < nx; ++l){ | |
| 215 | ✗ | switch(j){ | |
| 216 | ✗ | case 0: | |
| 217 | ✗ | structJac01(optData->rk.a[j], optData->J[0][j][l], | |
| 218 | ✗ | values, nv, &k, l, J[l]); | |
| 219 | ✗ | break; | |
| 220 | ✗ | case 1: | |
| 221 | ✗ | structJac02(optData->rk.a[j], optData->J[0][j][l], | |
| 222 | ✗ | values, nv, &k, l, J[l]); | |
| 223 | ✗ | break; | |
| 224 | ✗ | case 2: | |
| 225 | ✗ | structJac03(optData->rk.a[j], optData->J[0][j][l], | |
| 226 | ✗ | values, nv, &k, l, J[l]); | |
| 227 | ✗ | break; | |
| 228 | } | ||
| 229 | } | ||
| 230 | ✗ | for(; l< nJ; ++l){ | |
| 231 | ✗ | structJacC(optData->J[0][j][l], values, nv, &k, J[l]); | |
| 232 | } | ||
| 233 | } | ||
| 234 | |||
| 235 | /*****************************/ | ||
| 236 | ✗ | for(i = 1; i < nsi; ++i){ | |
| 237 | ✗ | for(j = 0; j < np; ++j){ | |
| 238 | ✗ | for(l = 0; l < nx; ++l){ | |
| 239 | ✗ | switch(j){ | |
| 240 | ✗ | case 0: | |
| 241 | ✗ | structJac1(optData->rk.a[j], optData->J[i][j][l], | |
| 242 | ✗ | values, nv, &k, l, J[l]); | |
| 243 | ✗ | break; | |
| 244 | ✗ | case 1: | |
| 245 | ✗ | structJac2(optData->rk.a[j], optData->J[i][j][l], | |
| 246 | ✗ | values, nv, &k, l, J[l]); | |
| 247 | ✗ | break; | |
| 248 | ✗ | case 2: | |
| 249 | ✗ | structJac3(optData->rk.a[j], optData->J[i][j][l], | |
| 250 | ✗ | values, nv, &k, l, J[l]); | |
| 251 | ✗ | break; | |
| 252 | } | ||
| 253 | } | ||
| 254 | ✗ | for(; l< nJ; ++l){ | |
| 255 | ✗ | structJacC(optData->J[i][j][l], values, nv, &k, J[l]); | |
| 256 | } | ||
| 257 | } | ||
| 258 | } | ||
| 259 | /*terminal constraint(s)*/ | ||
| 260 | ✗ | for(l = 0; l< ncf; ++l){ | |
| 261 | ✗ | structJacC(optData->Jf[l], values, nv, &k, Jf[l]); | |
| 262 | } | ||
| 263 | |||
| 264 | ✗ | }else if(np == 1){ | |
| 265 | /*****************************/ | ||
| 266 | ✗ | for(j = 0, k = 0; j < np; ++j){ | |
| 267 | ✗ | for(l = 0; l < nx; ++l){ | |
| 268 | ✗ | for(ii = 0; ii < nv; ++ii) | |
| 269 | ✗ | if(J[l][ii]){ | |
| 270 | ✗ | values[k++] = (modelica_real)((ii == l) ? optData->J[0][j][l][ii] - 1.0 : optData->J[0][j][l][ii]); | |
| 271 | } | ||
| 272 | } | ||
| 273 | ✗ | for(; l < nJ; ++l){ | |
| 274 | ✗ | for(ii = 0; ii < nv; ++ii) | |
| 275 | ✗ | if(J[l][ii]){ | |
| 276 | ✗ | values[k++] = (modelica_real)(optData->J[0][j][l][ii]); | |
| 277 | } | ||
| 278 | } | ||
| 279 | |||
| 280 | } | ||
| 281 | /*****************************/ | ||
| 282 | ✗ | for(i = 1; i < nsi; ++i){ | |
| 283 | ✗ | for(j = 0; j < np; ++j){ | |
| 284 | ✗ | for(l = 0; l < nx; ++l){ | |
| 285 | if(l < nx) | ||
| 286 | ✗ | values[k++] = 1.0; | |
| 287 | ✗ | for(ii = 0; ii < nv; ++ii){ | |
| 288 | ✗ | if(J[l][ii]){ | |
| 289 | ✗ | values[k++] = (modelica_real)((ii == l) ? optData->J[i][j][l][ii] - 1.0 : optData->J[i][j][l][ii]); | |
| 290 | } | ||
| 291 | } | ||
| 292 | } | ||
| 293 | ✗ | for(; l < nJ; ++l){ | |
| 294 | ✗ | for(ii = 0; ii < nv; ++ii){ | |
| 295 | ✗ | if(J[l][ii]){ | |
| 296 | ✗ | values[k++] = (modelica_real)(optData->J[i][j][l][ii]); | |
| 297 | } | ||
| 298 | } | ||
| 299 | } | ||
| 300 | } | ||
| 301 | } | ||
| 302 | /*terminal constraint(s)*/ | ||
| 303 | ✗ | for(l=0; l< ncf; ++l){ | |
| 304 | ✗ | structJacC(optData->Jf[l], values, nv, &k, Jf[l]); | |
| 305 | } | ||
| 306 | } | ||
| 307 | /*****************************/ | ||
| 308 | /* | ||
| 309 | { | ||
| 310 | printf("\n\n%i = %i",njac,k); | ||
| 311 | assert(0); | ||
| 312 | } | ||
| 313 | */ | ||
| 314 | /* | ||
| 315 | for(i = 0; i< njac; ++i) | ||
| 316 | printf("\nvalues[%i] = %g",i,values[i]); | ||
| 317 | assert(0); | ||
| 318 | */ | ||
| 319 | { | ||
| 320 | ✗ | if(optData->ipop.debugeJ){ | |
| 321 | int ijac = 0; | ||
| 322 | ijac = atoi(optData->ipop.debugeJ); | ||
| 323 | ✗ | if(ijac >= optData->iter_) | |
| 324 | ✗ | debugeJac(optData, vopt); | |
| 325 | } | ||
| 326 | } | ||
| 327 | } | ||
| 328 | |||
| 329 | ✗ | return TRUE; | |
| 330 | } | ||
| 331 | |||
| 332 | /*! | ||
| 333 | * helper evalfDiffG | ||
| 334 | * author: Vitalij Ruge | ||
| 335 | **/ | ||
| 336 | ✗ | static inline void structJac01(const long double * const a, const modelica_real *const Jj, double * values, const int nv, int *k, const int j, | |
| 337 | const modelica_boolean * const sJj){ | ||
| 338 | |||
| 339 | int l; | ||
| 340 | |||
| 341 | /*1*/ | ||
| 342 | ✗ | for(l = 0; l < nv; ++l){ | |
| 343 | ✗ | if(sJj[l]){ | |
| 344 | ✗ | values[(*k)++] = (modelica_real)((j == l) ? Jj[l] - a[1] : Jj[l]); | |
| 345 | } | ||
| 346 | } | ||
| 347 | |||
| 348 | /*2*/ | ||
| 349 | ✗ | values[(*k)++] = -a[2]; | |
| 350 | |||
| 351 | /*3*/ | ||
| 352 | ✗ | values[(*k)++] = a[3]; | |
| 353 | ✗ | } | |
| 354 | |||
| 355 | /*! | ||
| 356 | * helper evalfDiffG | ||
| 357 | * author: Vitalij Ruge | ||
| 358 | **/ | ||
| 359 | ✗ | static inline void structJac1(const long double * const a, const modelica_real *const Jj, double * values, const int nv, int *k, const int j, | |
| 360 | const modelica_boolean * const sJj){ | ||
| 361 | |||
| 362 | int l; | ||
| 363 | ✗ | values[(*k)++] = a[0]; | |
| 364 | /*1*/ | ||
| 365 | ✗ | for(l = 0; l < nv; ++l){ | |
| 366 | ✗ | if(sJj[l]){ | |
| 367 | ✗ | values[(*k)++] = (j == l) ? Jj[l] - a[1] : Jj[l]; | |
| 368 | } | ||
| 369 | } | ||
| 370 | |||
| 371 | /*2*/ | ||
| 372 | ✗ | values[(*k)++] = -a[2]; | |
| 373 | |||
| 374 | /*3*/ | ||
| 375 | ✗ | values[(*k)++] = a[3]; | |
| 376 | ✗ | } | |
| 377 | |||
| 378 | /*! | ||
| 379 | * helper evalfDiffG | ||
| 380 | * author: Vitalij Ruge | ||
| 381 | **/ | ||
| 382 | ✗ | static inline void structJac02(const long double * const a, const modelica_real *const Jj, double * values, const int nv, int *k, const int j, | |
| 383 | const modelica_boolean * const sJj){ | ||
| 384 | |||
| 385 | int l; | ||
| 386 | |||
| 387 | /*1*/ | ||
| 388 | ✗ | values[(*k)++] = a[1]; | |
| 389 | |||
| 390 | /*2*/ | ||
| 391 | ✗ | for(l = 0; l< nv; ++l){ | |
| 392 | ✗ | if(sJj[l]){ | |
| 393 | ✗ | values[(*k)++] = ((j == l)? Jj[l] - a[2] : Jj[l]); | |
| 394 | } | ||
| 395 | } | ||
| 396 | |||
| 397 | /*3*/ | ||
| 398 | ✗ | values[(*k)++] = -a[3]; | |
| 399 | ✗ | } | |
| 400 | |||
| 401 | /*! | ||
| 402 | * helper evalfDiffG | ||
| 403 | * author: Vitalij Ruge | ||
| 404 | **/ | ||
| 405 | ✗ | static inline void structJac2(const long double * const a, const modelica_real *const Jj, double * values, const int nv, int *k, const int j, | |
| 406 | const modelica_boolean * const sJj){ | ||
| 407 | |||
| 408 | int l; | ||
| 409 | /*0*/ | ||
| 410 | ✗ | values[(*k)++] = -a[0]; | |
| 411 | |||
| 412 | /*1*/ | ||
| 413 | ✗ | values[(*k)++] = a[1]; | |
| 414 | |||
| 415 | /*2*/ | ||
| 416 | ✗ | for(l = 0; l< nv; ++l){ | |
| 417 | ✗ | if(sJj[l]){ | |
| 418 | ✗ | values[(*k)++] = ((j == l)? Jj[l] - a[2] : Jj[l]); | |
| 419 | } | ||
| 420 | } | ||
| 421 | |||
| 422 | /*3*/ | ||
| 423 | ✗ | values[(*k)++] = -a[3]; | |
| 424 | ✗ | } | |
| 425 | |||
| 426 | |||
| 427 | /*! | ||
| 428 | * helper evalfDiffG | ||
| 429 | * author: Vitalij Ruge | ||
| 430 | **/ | ||
| 431 | ✗ | static inline void structJac03(const long double * const a, const modelica_real *const Jj, double * values, const int nv, int *k, const int j, | |
| 432 | const modelica_boolean * const sJj){ | ||
| 433 | |||
| 434 | int l; | ||
| 435 | /*1*/ | ||
| 436 | ✗ | values[(*k)++] = -a[1]; | |
| 437 | /*2*/ | ||
| 438 | ✗ | values[(*k)++] = a[2]; | |
| 439 | /*3*/ | ||
| 440 | ✗ | for(l = 0; l< nv; ++l) | |
| 441 | ✗ | if(sJj[l]) | |
| 442 | ✗ | values[(*k)++] = ((j == l)? Jj[l] - a[3] : Jj[l]); | |
| 443 | |||
| 444 | ✗ | } | |
| 445 | |||
| 446 | /*! | ||
| 447 | * helper evalfDiffG | ||
| 448 | * author: Vitalij Ruge | ||
| 449 | **/ | ||
| 450 | ✗ | static inline void structJac3(const long double * const a, const modelica_real *const Jj, double * values, const int nv, int *k, const int j, | |
| 451 | const modelica_boolean * const sJj){ | ||
| 452 | |||
| 453 | int l; | ||
| 454 | /*0*/ | ||
| 455 | ✗ | values[(*k)++] = a[0]; | |
| 456 | /*1*/ | ||
| 457 | ✗ | values[(*k)++] = -a[1]; | |
| 458 | /*2*/ | ||
| 459 | ✗ | values[(*k)++] = a[2]; | |
| 460 | /*3*/ | ||
| 461 | ✗ | for(l = 0; l< nv; ++l) | |
| 462 | ✗ | if(sJj[l]) | |
| 463 | ✗ | values[(*k)++] = ((j == l)? Jj[l] - a[3] : Jj[l]); | |
| 464 | |||
| 465 | ✗ | } | |
| 466 | |||
| 467 | /*! | ||
| 468 | * helper evalfDiffG | ||
| 469 | * author: Vitalij Ruge | ||
| 470 | **/ | ||
| 471 | static inline void structJacC(const modelica_real *const J, double *values, | ||
| 472 | const int nv, int *k, const modelica_boolean * const Jj){ | ||
| 473 | int l; | ||
| 474 | ✗ | for(l = 0; l<nv; ++l) | |
| 475 | ✗ | if(Jj[l]){ | |
| 476 | ✗ | values[(*k)++] = J[l]; | |
| 477 | } | ||
| 478 | } | ||
| 479 | |||
| 480 | |||
| 481 | /*! | ||
| 482 | * helper for generated_jac_struc (row) | ||
| 483 | * author: Vitalij Ruge | ||
| 484 | **/ | ||
| 485 | static inline void set_row(int *k, int *iRow, int *iCol, const modelica_boolean * const Jj, | ||
| 486 | const int nv, const int r, const int c){ | ||
| 487 | |||
| 488 | int i; | ||
| 489 | ✗ | for(i = 0; i<nv; ++i){ | |
| 490 | ✗ | if(Jj[i]){ | |
| 491 | ✗ | iRow[*k] = r; | |
| 492 | ✗ | iCol[(*k)++] = c + i; | |
| 493 | } | ||
| 494 | } | ||
| 495 | |||
| 496 | } | ||
| 497 | |||
| 498 | /*! | ||
| 499 | * helper for generated_jac_struc (cell) | ||
| 500 | * author: Vitalij Ruge | ||
| 501 | **/ | ||
| 502 | static inline void set_cell(int*k, int*iRow, int*iCol, const int r, const int c){ | ||
| 503 | ✗ | iRow[*k] = r; | |
| 504 | ✗ | iCol[(*k)++] = c; | |
| 505 | } | ||
| 506 | |||
| 507 | /*! | ||
| 508 | * generated global jacobian struct | ||
| 509 | * author: Vitalij Ruge | ||
| 510 | **/ | ||
| 511 | ✗ | static inline void generated_jac_struc(OptData * optData, int *iRow, int* iCol){ | |
| 512 | |||
| 513 | ✗ | const int nv = optData->dim.nv; | |
| 514 | ✗ | const int nx = optData->dim.nx; | |
| 515 | ✗ | const int nsi = optData->dim.nsi; | |
| 516 | ✗ | const int nJ = optData->dim.nJ; | |
| 517 | ✗ | const int np = optData->dim.np; | |
| 518 | ✗ | const int npv = np*nv; | |
| 519 | ✗ | const int ncf = optData->dim.ncf; | |
| 520 | |||
| 521 | ✗ | modelica_boolean ** J = optData->s.JderCon; | |
| 522 | ✗ | modelica_boolean ** Jf = optData->s.J[2]; | |
| 523 | int r, c, tmp_r, tmp_c; | ||
| 524 | int i, j, k; | ||
| 525 | |||
| 526 | /**********************************/ | ||
| 527 | r = 0; | ||
| 528 | c = 0; | ||
| 529 | k = 0; | ||
| 530 | ✗ | if(np == 3){ | |
| 531 | /* 1 */ | ||
| 532 | ✗ | for(j = 0; j <nx; ++j){ | |
| 533 | tmp_r = r + j; | ||
| 534 | tmp_c = c + j; | ||
| 535 | |||
| 536 | ✗ | set_row(&k, iRow, iCol, J[j], nv, tmp_r, c); | |
| 537 | ✗ | set_cell(&k, iRow, iCol, tmp_r, tmp_c + nv); | |
| 538 | ✗ | set_cell(&k, iRow, iCol, tmp_r, tmp_c + 2*nv); | |
| 539 | |||
| 540 | } | ||
| 541 | ✗ | for(; j<nJ; ++j){ | |
| 542 | ✗ | set_row(&k, iRow, iCol, J[j], nv, r+j, c); | |
| 543 | } | ||
| 544 | |||
| 545 | r += nJ; | ||
| 546 | /* 2 */ | ||
| 547 | ✗ | for(j = 0; j <nx; ++j){ | |
| 548 | ✗ | tmp_r = r + j; | |
| 549 | tmp_c = c + j; | ||
| 550 | |||
| 551 | set_cell(&k, iRow, iCol, tmp_r, tmp_c); | ||
| 552 | ✗ | set_row(&k, iRow, iCol, J[j], nv, tmp_r, c + nv); | |
| 553 | ✗ | set_cell(&k, iRow, iCol, tmp_r, tmp_c + 2*nv); | |
| 554 | } | ||
| 555 | ✗ | for(; j<nJ; ++j){ | |
| 556 | ✗ | set_row(&k, iRow, iCol, J[j], nv, r+j, c + nv); | |
| 557 | } | ||
| 558 | |||
| 559 | ✗ | r += nJ; | |
| 560 | /* 3 */ | ||
| 561 | ✗ | for(j = 0; j <nx; ++j){ | |
| 562 | ✗ | tmp_r = r + j; | |
| 563 | tmp_c = c + j; | ||
| 564 | |||
| 565 | set_cell(&k, iRow, iCol, tmp_r, tmp_c); | ||
| 566 | ✗ | set_cell(&k, iRow, iCol, tmp_r, tmp_c + nv); | |
| 567 | ✗ | set_row(&k, iRow, iCol, J[j], nv, tmp_r, c + 2*nv); | |
| 568 | } | ||
| 569 | ✗ | for(; j<nJ; ++j){ | |
| 570 | ✗ | set_row(&k, iRow, iCol, J[j], nv, r+j, c + 2*nv); | |
| 571 | } | ||
| 572 | |||
| 573 | /**********************************/ | ||
| 574 | ✗ | r += nJ; | |
| 575 | ✗ | c = (np-1)*nv; | |
| 576 | ✗ | for(i = 1; i < nsi; ++i, r += nJ, c += npv){ | |
| 577 | /* 1 */ | ||
| 578 | ✗ | for(j = 0; j <nx; ++j){ | |
| 579 | ✗ | tmp_r = r + j; | |
| 580 | ✗ | tmp_c = c + j; | |
| 581 | |||
| 582 | set_cell(&k, iRow, iCol, tmp_r, tmp_c); | ||
| 583 | ✗ | set_row(&k, iRow, iCol, J[j], nv, tmp_r, c + nv); | |
| 584 | ✗ | set_cell(&k, iRow, iCol, tmp_r, tmp_c + 2*nv); | |
| 585 | ✗ | set_cell(&k, iRow, iCol, tmp_r, tmp_c + 3*nv); | |
| 586 | } | ||
| 587 | ✗ | for(; j<nJ; ++j){ | |
| 588 | ✗ | set_row(&k, iRow, iCol, J[j], nv, r + j, c+nv); | |
| 589 | } | ||
| 590 | |||
| 591 | ✗ | r += nJ; | |
| 592 | /* 2 */ | ||
| 593 | ✗ | for(j = 0; j <nx; ++j){ | |
| 594 | ✗ | tmp_r = r + j; | |
| 595 | ✗ | tmp_c = c + j; | |
| 596 | |||
| 597 | set_cell(&k, iRow, iCol, tmp_r, tmp_c); | ||
| 598 | ✗ | set_cell(&k, iRow, iCol, tmp_r, tmp_c + nv); | |
| 599 | ✗ | set_row(&k, iRow, iCol, J[j], nv, tmp_r, c + 2*nv); | |
| 600 | ✗ | set_cell(&k, iRow, iCol, tmp_r, tmp_c + 3*nv); | |
| 601 | } | ||
| 602 | ✗ | for(; j<nJ; ++j){ | |
| 603 | ✗ | set_row(&k, iRow, iCol, J[j], nv, r+j, c+2*nv); | |
| 604 | } | ||
| 605 | |||
| 606 | ✗ | r += nJ; | |
| 607 | /* 3 */ | ||
| 608 | ✗ | for(j = 0; j <nx; ++j){ | |
| 609 | ✗ | tmp_r = r + j; | |
| 610 | ✗ | tmp_c = c + j; | |
| 611 | |||
| 612 | set_cell(&k, iRow, iCol, tmp_r, tmp_c); | ||
| 613 | ✗ | set_cell(&k, iRow, iCol, tmp_r, tmp_c + nv); | |
| 614 | ✗ | set_cell(&k, iRow, iCol, tmp_r, tmp_c + 2*nv); | |
| 615 | ✗ | set_row(&k, iRow, iCol, J[j], nv, tmp_r, c + 3*nv); | |
| 616 | } | ||
| 617 | ✗ | for(; j<nJ; ++j){ | |
| 618 | ✗ | set_row(&k, iRow, iCol, J[j], nv, r+j, c+3*nv); | |
| 619 | } | ||
| 620 | } | ||
| 621 | /*terminal constraint(s)*/ | ||
| 622 | ✗ | for(j = 0; j<ncf; ++j){ | |
| 623 | ✗ | set_row(&k, iRow, iCol, Jf[j], nv, r+j, c); | |
| 624 | } | ||
| 625 | ✗ | }else if(np == 1){ | |
| 626 | |||
| 627 | ✗ | for(j = 0; j <nJ; ++j){ | |
| 628 | ✗ | set_row(&k, iRow, iCol, J[j], nv, r+j, c); | |
| 629 | } | ||
| 630 | |||
| 631 | r += nJ; | ||
| 632 | c = (np-1)*nv; | ||
| 633 | |||
| 634 | ✗ | for(i = 1; i < nsi; ++i, r += nJ, c += npv){ | |
| 635 | ✗ | for(j = 0; j <nx; ++j){ | |
| 636 | ✗ | tmp_r = r + j; | |
| 637 | ✗ | tmp_c = c + j; | |
| 638 | |||
| 639 | set_cell(&k, iRow, iCol, tmp_r, tmp_c); | ||
| 640 | ✗ | set_row(&k, iRow, iCol, J[j], nv, tmp_r, c + nv); | |
| 641 | } | ||
| 642 | ✗ | for(; j<nJ; ++j){ | |
| 643 | ✗ | set_row(&k, iRow, iCol, J[j], nv, r+j, c+nv); | |
| 644 | } | ||
| 645 | |||
| 646 | } | ||
| 647 | /*terminal constraint(s)*/ | ||
| 648 | ✗ | for(j = 0; j<ncf; ++j){ | |
| 649 | ✗ | set_row(&k, iRow, iCol, Jf[j], nv, r+j, c); | |
| 650 | } | ||
| 651 | |||
| 652 | } | ||
| 653 | /* | ||
| 654 | { | ||
| 655 | const int NJ = optData->dim.nJderx; | ||
| 656 | int njac = np*(NJ*nsi + nx*(np*nsi - 1)); | ||
| 657 | printf("\n\n%i = %i",njac,k); | ||
| 658 | assert(0); | ||
| 659 | } | ||
| 660 | */ | ||
| 661 | |||
| 662 | ✗ | } | |
| 663 | |||
| 664 | |||
| 665 | ✗ | static inline void printMaxError(ipnumber *g, const int m, const int nx, const int nJ, | |
| 666 | long double **t, const int np, const int nsi, DATA * data, OptData * optData){ | ||
| 667 | |||
| 668 | int index = 0; | ||
| 669 | int index_x = 0; | ||
| 670 | double gmax = -1; | ||
| 671 | int i, j, k, l; | ||
| 672 | int ii=-1, jj=-1, kk = -1; | ||
| 673 | double tmp, tmp1; | ||
| 674 | |||
| 675 | ✗ | for(i = 0, l = 0; i < nsi; ++i){ | |
| 676 | ✗ | for(j = 0; j < np; ++j){ | |
| 677 | ✗ | for(k=0; k< nx; ++k){ | |
| 678 | ✗ | tmp = fabs(g[l++]); | |
| 679 | ✗ | if(tmp > gmax){ | |
| 680 | ii = i; | ||
| 681 | jj = j; | ||
| 682 | kk = k; | ||
| 683 | gmax = tmp; | ||
| 684 | } | ||
| 685 | } | ||
| 686 | ✗ | for(; k< nJ; ++k, ++l){ | |
| 687 | ✗ | tmp1 = g[l] - optData->ipop.gmax[l]; // > 0 | |
| 688 | ✗ | tmp = optData->ipop.gmin[l] - g[l]; // >0 | |
| 689 | ✗ | tmp = fmaxl(fmaxl(tmp, tmp1),0.0); | |
| 690 | ✗ | if(tmp > gmax){ | |
| 691 | ii = i; | ||
| 692 | jj = j; | ||
| 693 | kk = k; | ||
| 694 | gmax = tmp; | ||
| 695 | } | ||
| 696 | } | ||
| 697 | } | ||
| 698 | } | ||
| 699 | |||
| 700 | /*final constraints*/ | ||
| 701 | ✗ | for(k=nJ; k< nJ+optData->dim.ncf; ++k, ++l){ | |
| 702 | ✗ | tmp1 = g[l] - optData->ipop.gmax[l]; // > 0 | |
| 703 | ✗ | tmp = optData->ipop.gmin[l] - g[l]; // >0 | |
| 704 | ✗ | tmp = fmaxl(fmaxl(tmp, tmp1),0.0); | |
| 705 | ✗ | if(tmp > gmax){ | |
| 706 | ✗ | ii = nsi- 1; | |
| 707 | ✗ | jj = np-1; | |
| 708 | kk = k; | ||
| 709 | gmax = tmp; | ||
| 710 | } | ||
| 711 | } | ||
| 712 | |||
| 713 | ✗ | if(kk>-1){ | |
| 714 | char buf1[32], buf2[32]; | ||
| 715 | ✗ | ryu_hr_tdzp_buf(gmax, buf1); | |
| 716 | ✗ | ryu_hr_tdzp_buf(t[ii][jj], buf2); | |
| 717 | ✗ | if(kk < nx){ | |
| 718 | ✗ | infoStreamPrint(OMC_LOG_IPOPT_ERROR, 0, "max error is %s for the approximation of the state %s(time = %s)\n", | |
| 719 | ✗ | buf1, data->modelData->realVarsData[kk].info.name, buf2); | |
| 720 | ✗ | }else if(kk < nJ){ | |
| 721 | ✗ | const int ll = kk - nx + optData->dim.index_con; | |
| 722 | ✗ | infoStreamPrint(OMC_LOG_IPOPT_ERROR, 0,"max violation is %s for the constraint %s(time = %s)\n", | |
| 723 | ✗ | buf1, data->modelData->realVarsData[ll].info.name, buf2); | |
| 724 | }else{ | ||
| 725 | ✗ | const int ll = kk - nx + optData->dim.index_con; | |
| 726 | ✗ | infoStreamPrint(OMC_LOG_IPOPT_ERROR, 0,"max violation is %s for the final constraint %s(time = %s)\n", buf1, data->modelData->realVarsData[ll].info.name, buf2); | |
| 727 | } | ||
| 728 | } | ||
| 729 | ✗ | } | |
| 730 | |||
| 731 | |||
| 732 | |||
| 733 |