OMCompiler/SimulationRuntime/c/optimization/DataManagement/DerStructure.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 | /*! DerStructure.c | ||
| 29 | */ | ||
| 30 | |||
| 31 | #include "../OptimizerData.h" | ||
| 32 | #include "../OptimizerLocalFunction.h" | ||
| 33 | #include "../../simulation/options.h" | ||
| 34 | |||
| 35 | static inline void local_jac_struct(DATA * data, OptDataDim * dim, OptDataStructure *s, const modelica_real* const vnom); | ||
| 36 | static inline void print_local_jac_struct(DATA * data, OptDataDim * dim, OptDataStructure *s); | ||
| 37 | static inline void local_hessian_struct(DATA * data, OptDataDim * dim, OptDataStructure *s); | ||
| 38 | static inline void print_local_hessian_struct(DATA * data, OptDataDim * dim, OptDataStructure *s); | ||
| 39 | static inline void update_local_jac_struct(OptDataDim * dim, OptDataStructure *s); | ||
| 40 | static inline void copy_JacVars(OptData *optData); | ||
| 41 | |||
| 42 | /* pick up jac struct | ||
| 43 | */ | ||
| 44 | ✗ | void allocate_der_struct(OptDataStructure *s, OptDataDim * dim, DATA* data, OptData *optData){ | |
| 45 | ✗ | threadData_t *threadData = optData->threadData; | |
| 46 | ✗ | const int nv = dim->nv; | |
| 47 | ✗ | const int nsi = dim->nsi; | |
| 48 | ✗ | const int np = dim->np; | |
| 49 | ✗ | const int nJ = dim->nJ; | |
| 50 | ✗ | const int nJ2 = dim->nJ2; | |
| 51 | ✗ | const int ncf = dim->ncf; | |
| 52 | int i, j, k; | ||
| 53 | char * cflags; | ||
| 54 | ✗ | cflags = (char*)omc_flagValue[FLAG_UP_HESSIAN]; | |
| 55 | ✗ | if(cflags) | |
| 56 | { | ||
| 57 | ✗ | optData->dim.updateHessian = atoi(cflags); | |
| 58 | ✗ | if(optData->dim.updateHessian < 0) | |
| 59 | { | ||
| 60 | ✗ | warningStreamPrint(OMC_LOG_STDOUT, 0, "not support %i for keep hessian-matrix constant.", optData->dim.updateHessian); | |
| 61 | ✗ | optData->dim.updateHessian = 0; | |
| 62 | } | ||
| 63 | }else{ | ||
| 64 | ✗ | optData->dim.updateHessian = 0; | |
| 65 | } | ||
| 66 | |||
| 67 | ✗ | s->indexABCD[0] = -1; | |
| 68 | ✗ | s->indexABCD[1] = data->callback->INDEX_JAC_A; | |
| 69 | ✗ | s->indexABCD[2] = data->callback->INDEX_JAC_B; | |
| 70 | ✗ | s->indexABCD[3] = data->callback->INDEX_JAC_C; | |
| 71 | ✗ | s->indexABCD[4] = data->callback->INDEX_JAC_D; | |
| 72 | ✗ | s->matrix[0] = (modelica_boolean)0; | |
| 73 | ✗ | s->matrix[1] = (modelica_boolean)(data->callback->initialAnalyticJacobianA((void*) data, threadData, &(data->simulationInfo->analyticJacobians[s->indexABCD[1]])) == 0); | |
| 74 | ✗ | s->matrix[2] = (modelica_boolean)(data->callback->initialAnalyticJacobianB((void*) data, threadData, &(data->simulationInfo->analyticJacobians[s->indexABCD[2]])) == 0); | |
| 75 | ✗ | s->matrix[3] = (modelica_boolean)(data->callback->initialAnalyticJacobianC((void*) data, threadData, &(data->simulationInfo->analyticJacobians[s->indexABCD[3]])) == 0); | |
| 76 | ✗ | s->matrix[4] = (modelica_boolean)(data->callback->initialAnalyticJacobianD((void*) data, threadData, &(data->simulationInfo->analyticJacobians[s->indexABCD[4]])) == 0); | |
| 77 | |||
| 78 | ✗ | dim->nJderx = 0; | |
| 79 | ✗ | dim->nJfderx = 0; | |
| 80 | /*************************/ | ||
| 81 | ✗ | s->J = (modelica_boolean***) malloc(3*sizeof(modelica_boolean**)); | |
| 82 | ✗ | s->J[0] = (modelica_boolean**) malloc((nJ +1)*sizeof(modelica_boolean*)); | |
| 83 | ✗ | for(i = 0; i < (nJ +1); ++i) | |
| 84 | ✗ | s->J[0][i] = (modelica_boolean*)calloc(nv, sizeof(modelica_boolean)); | |
| 85 | |||
| 86 | ✗ | s->J[1] = (modelica_boolean**) malloc(nJ2*sizeof(modelica_boolean*)); | |
| 87 | ✗ | for(i = 0; i < nJ2; ++i) | |
| 88 | ✗ | s->J[1][i] = (modelica_boolean*)calloc(nv, sizeof(modelica_boolean)); | |
| 89 | |||
| 90 | ✗ | s->J[2] = (modelica_boolean**) malloc(ncf*sizeof(modelica_boolean*)); | |
| 91 | ✗ | for(i = 0; i < ncf; ++i) | |
| 92 | ✗ | s->J[2][i] = (modelica_boolean*)calloc(nv, sizeof(modelica_boolean)); | |
| 93 | |||
| 94 | ✗ | s->derIndex[0] = s->derIndex[1] = s->derIndex[2] = -1; | |
| 95 | ✗ | s->mayer = (modelica_boolean) (data->callback->mayer(data, &s->pmayer, &s->derIndex[0]) >= 0); | |
| 96 | ✗ | s->lagrange = (modelica_boolean) (data->callback->lagrange(data, &s->plagrange, &s->derIndex[1], &s->derIndex[2]) >= 0); | |
| 97 | |||
| 98 | ✗ | if(!s->mayer) | |
| 99 | ✗ | s->pmayer = NULL; | |
| 100 | ✗ | if(!s->lagrange) | |
| 101 | ✗ | s->plagrange = NULL; | |
| 102 | |||
| 103 | ✗ | local_jac_struct(data, dim, s, optData->bounds.vnom); | |
| 104 | ✗ | copy_JacVars(optData); | |
| 105 | |||
| 106 | ✗ | if(OMC_ACTIVE_STREAM(OMC_LOG_IPOPT_JAC) || OMC_ACTIVE_STREAM(OMC_LOG_IPOPT_HESSE)) | |
| 107 | ✗ | print_local_jac_struct(data, dim, s); | |
| 108 | |||
| 109 | ✗ | local_hessian_struct(data, dim, s); | |
| 110 | ✗ | if(OMC_ACTIVE_STREAM(OMC_LOG_IPOPT_JAC) || OMC_ACTIVE_STREAM(OMC_LOG_IPOPT_HESSE)) | |
| 111 | ✗ | print_local_hessian_struct(data, dim, s); | |
| 112 | |||
| 113 | update_local_jac_struct(dim, s); | ||
| 114 | |||
| 115 | ✗ | optData->J = (modelica_real****) malloc(nsi*sizeof(modelica_real***)); | |
| 116 | ✗ | for(i = 0; i < nsi; ++i){ | |
| 117 | ✗ | optData->J[i] = (modelica_real***) malloc(np*sizeof(modelica_real**)); | |
| 118 | ✗ | for(j = 0; j< np; ++j){ | |
| 119 | ✗ | optData->J[i][j] = (modelica_real**) malloc(nJ2*sizeof(modelica_real*)); | |
| 120 | ✗ | for(k = 0; k < nJ2; ++k){ | |
| 121 | ✗ | optData->J[i][j][k] = (modelica_real*) calloc(nv, sizeof(modelica_real)); | |
| 122 | } | ||
| 123 | } | ||
| 124 | } | ||
| 125 | |||
| 126 | ✗ | optData->tmpJ = (modelica_real**) malloc(nJ2*sizeof(modelica_real*)); | |
| 127 | ✗ | for(k = 0; k < nJ2; ++k){ | |
| 128 | ✗ | optData->tmpJ[k] = (modelica_real*) calloc(nv, sizeof(modelica_real)); | |
| 129 | } | ||
| 130 | |||
| 131 | ✗ | optData->Jf = (modelica_real**) malloc(ncf*sizeof(modelica_real*)); | |
| 132 | ✗ | for(k = 0; k < ncf; ++k){ | |
| 133 | ✗ | optData->Jf[k] = (modelica_real*) calloc(nv, sizeof(modelica_real)); | |
| 134 | } | ||
| 135 | ✗ | optData->tmpJf = (modelica_real**) malloc(ncf*sizeof(modelica_real*)); | |
| 136 | ✗ | for(k = 0; k < ncf; ++k){ | |
| 137 | ✗ | optData->tmpJf[k] = (modelica_real*) calloc(nv, sizeof(modelica_real)); | |
| 138 | } | ||
| 139 | |||
| 140 | ✗ | if(s->mayer){ | |
| 141 | ✗ | dim->index_mayer = -1; | |
| 142 | ✗ | for(i = 0; i < dim->nReal; ++i){ | |
| 143 | ✗ | if(&data->localData[0]->realVars[i] == s->pmayer){ | |
| 144 | ✗ | dim->index_mayer = i; | |
| 145 | ✗ | break; | |
| 146 | } | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | ✗ | if(s->lagrange){ | |
| 151 | ✗ | dim->index_lagrange = -1; | |
| 152 | ✗ | for(i = 0; i < dim->nReal; ++i){ | |
| 153 | ✗ | if(&data->localData[0]->realVars[i] == s->plagrange){ | |
| 154 | ✗ | dim->index_lagrange = i; | |
| 155 | ✗ | break; | |
| 156 | } | ||
| 157 | } | ||
| 158 | } | ||
| 159 | |||
| 160 | ✗ | optData->H = (long double ***) malloc(nJ*sizeof(long double**)); | |
| 161 | ✗ | for(i = 0; i < nJ; ++i){ | |
| 162 | ✗ | optData->H[i] = (long double **) malloc(nv*sizeof(long double*)); | |
| 163 | ✗ | for(j = 0; j < nv; ++j) | |
| 164 | ✗ | optData->H[i][j] = (long double *) calloc(nv, sizeof(long double)); | |
| 165 | } | ||
| 166 | |||
| 167 | ✗ | optData->Hcf = (long double ***) malloc(ncf*sizeof(long double**)); | |
| 168 | ✗ | for(i = 0; i < ncf; ++i){ | |
| 169 | ✗ | optData->Hcf[i] = (long double **) malloc(nv*sizeof(long double*)); | |
| 170 | ✗ | for(j = 0; j < nv; ++j) | |
| 171 | ✗ | optData->Hcf[i][j] = (long double *) calloc(nv, sizeof(long double)); | |
| 172 | } | ||
| 173 | |||
| 174 | ✗ | optData->Hm = (long double **)malloc(nv*sizeof(long double*)); | |
| 175 | ✗ | for(j = 0; j < nv; ++j) | |
| 176 | ✗ | optData->Hm[j] = (long double *)calloc(nv, sizeof(long double)); | |
| 177 | |||
| 178 | ✗ | optData->Hl = (long double **) malloc(nv*sizeof(long double*)); | |
| 179 | ✗ | for(j = 0; j < nv; ++j) | |
| 180 | ✗ | optData->Hl[j] = (long double *)calloc(nv, sizeof(long double)); | |
| 181 | |||
| 182 | ✗ | if(optData->dim.updateHessian > 0){ | |
| 183 | ✗ | const int nH0 = optData->dim.nH0_; | |
| 184 | ✗ | const int nH1 = optData->dim.nH1_; | |
| 185 | ✗ | const int nhess = (nsi*np-1)*nH0+nH1; | |
| 186 | ✗ | optData->oldH = (double *)malloc(nhess*sizeof(double)); | |
| 187 | } | ||
| 188 | |||
| 189 | ✗ | optData->dim.iter_updateHessian = optData->dim.updateHessian-1; | |
| 190 | ✗ | } | |
| 191 | |||
| 192 | |||
| 193 | /* | ||
| 194 | * pick up the jacobian matrix struct | ||
| 195 | * author: Vitalij Ruge | ||
| 196 | */ | ||
| 197 | ✗ | static inline void local_jac_struct(DATA * data, OptDataDim * dim, OptDataStructure *s, const modelica_real * const vnom){ | |
| 198 | int sizeCols; | ||
| 199 | int maxColors; | ||
| 200 | int i,ii, j, l, index, tmp_index, tmpnJ, h_index; | ||
| 201 | unsigned int* lindex, *cC, *pindex; | ||
| 202 | |||
| 203 | ✗ | s->lindex = (unsigned int**)malloc(5*sizeof(unsigned int*)); | |
| 204 | ✗ | s->seedVec = (modelica_real ***)malloc(5*sizeof(modelica_real**)); | |
| 205 | |||
| 206 | ✗ | s->JderCon = (modelica_boolean **)malloc(dim->nJ*sizeof(modelica_boolean*)); | |
| 207 | ✗ | for(i = 0; i < dim->nJ; ++i) | |
| 208 | ✗ | s->JderCon[i] = (modelica_boolean *)calloc(dim->nv, sizeof(modelica_boolean)); | |
| 209 | |||
| 210 | ✗ | s->gradM = (modelica_boolean *)calloc(dim->nv, sizeof(modelica_boolean)); | |
| 211 | ✗ | s->gradL = (modelica_boolean *)calloc(dim->nv, sizeof(modelica_boolean)); | |
| 212 | ✗ | s->indexCon2 = (int *)malloc(dim->nc* sizeof(int)); | |
| 213 | ✗ | s->indexCon3 = (int *)malloc(dim->nc* sizeof(int)); | |
| 214 | |||
| 215 | ✗ | for(index = 2; index < 5; ++index){ | |
| 216 | |||
| 217 | ✗ | if(s->matrix[index]){ | |
| 218 | ✗ | tmp_index = index-2; | |
| 219 | ✗ | h_index = s->indexABCD[index]; | |
| 220 | /******************************/ | ||
| 221 | ✗ | sizeCols = data->simulationInfo->analyticJacobians[h_index].sizeCols; | |
| 222 | ✗ | maxColors = data->simulationInfo->analyticJacobians[h_index].sparsePattern->maxColors + 1; | |
| 223 | ✗ | cC = (unsigned int*) data->simulationInfo->analyticJacobians[h_index].sparsePattern->colorCols; | |
| 224 | ✗ | lindex = (unsigned int*) data->simulationInfo->analyticJacobians[h_index].sparsePattern->leadindex; | |
| 225 | ✗ | pindex = data->simulationInfo->analyticJacobians[h_index].sparsePattern->index; | |
| 226 | |||
| 227 | ✗ | s->seedVec[index] = (modelica_real **)malloc((maxColors)*sizeof(modelica_real*)); | |
| 228 | /**********************/ | ||
| 229 | ✗ | if(sizeCols > 0){ | |
| 230 | ✗ | for(ii = 1; ii < maxColors; ++ii){ | |
| 231 | ✗ | s->seedVec[index][ii] = (modelica_real*)calloc(sizeCols, sizeof(modelica_real)); | |
| 232 | ✗ | for(i = 0; i < sizeCols; i++){ | |
| 233 | ✗ | if(cC[i] == ii){ | |
| 234 | ✗ | s->seedVec[index][ii][i] = vnom[i]; | |
| 235 | ✗ | for(j = lindex[i]; j < lindex[i + 1]; ++j){ | |
| 236 | ✗ | l = pindex[j]; | |
| 237 | ✗ | s->J[tmp_index][l][i] = (modelica_boolean)1; | |
| 238 | } | ||
| 239 | } | ||
| 240 | } | ||
| 241 | } | ||
| 242 | |||
| 243 | ✗ | tmpnJ = dim->nJ; | |
| 244 | ✗ | if(s->lagrange) ++tmpnJ; | |
| 245 | ✗ | if(s->mayer && index == 3) ++tmpnJ; | |
| 246 | |||
| 247 | ✗ | for(ii = dim->nx, j= 0; ii < tmpnJ; ++ii){ | |
| 248 | ✗ | if(index == 2 && ii != s->derIndex[1]) | |
| 249 | ✗ | s->indexCon2[j++] = ii; | |
| 250 | ✗ | else if(index == 3 && ii != s->derIndex[2] && ii != s->derIndex[0]) | |
| 251 | ✗ | s->indexCon3[j++] = ii; | |
| 252 | } | ||
| 253 | } | ||
| 254 | /**********************/ | ||
| 255 | /* The optimizer lends its own seed vectors from here on; clear this so | ||
| 256 | freeing the Jacobian does not free it a second time. */ | ||
| 257 | ✗ | free(data->simulationInfo->analyticJacobians[h_index].seedVars); | |
| 258 | ✗ | data->simulationInfo->analyticJacobians[h_index].seedVars = NULL; | |
| 259 | } | ||
| 260 | } | ||
| 261 | |||
| 262 | |||
| 263 | ✗ | for(i = 0; i < dim->nJ ; ++i){ | |
| 264 | ✗ | for(j = 0; j < dim->nv; ++j){ | |
| 265 | ✗ | if(i < dim->nx){ | |
| 266 | ✗ | if(s->J[0][i][j]){ | |
| 267 | ✗ | s->JderCon[i][j] = (modelica_boolean)1; | |
| 268 | ✗ | ++dim->nJderx; | |
| 269 | } | ||
| 270 | }else{ | ||
| 271 | ✗ | if(s->J[0][s->indexCon2[i - dim->nx]][j]){ | |
| 272 | ✗ | s->JderCon[i][j] = (modelica_boolean)1; | |
| 273 | ✗ | ++dim->nJderx; | |
| 274 | } | ||
| 275 | } | ||
| 276 | } | ||
| 277 | } | ||
| 278 | |||
| 279 | ✗ | for(i = 0; i < dim->ncf ; ++i){ | |
| 280 | ✗ | for(j = 0; j < dim->nv; ++j){ | |
| 281 | ✗ | if(s->J[2][i][j]) | |
| 282 | ✗ | ++dim->nJfderx; | |
| 283 | } | ||
| 284 | } | ||
| 285 | |||
| 286 | ✗ | if(s->lagrange) | |
| 287 | ✗ | for(j = 0; j < dim->nv; ++j){ | |
| 288 | ✗ | if(s->J[0][s->derIndex[1]][j]) | |
| 289 | ✗ | s->gradL[j] = (modelica_boolean)1; | |
| 290 | } | ||
| 291 | |||
| 292 | ✗ | if(s->mayer) | |
| 293 | ✗ | for(j = 0; j < dim->nv; ++j){ | |
| 294 | ✗ | if(s->J[1][s->derIndex[0]][j]) | |
| 295 | ✗ | s->gradM[j] = (modelica_boolean)1; | |
| 296 | } | ||
| 297 | |||
| 298 | ✗ | s->indexJ2 = (int *) malloc((dim->nJ+1)* sizeof(int)); | |
| 299 | ✗ | s->indexJ3 = (int *) malloc(dim->nJ2* sizeof(int)); | |
| 300 | |||
| 301 | j = 0; | ||
| 302 | ✗ | for(i = 0; i < dim->nJ +1 ; ++i){ | |
| 303 | ✗ | if(i == s->derIndex[1]){ | |
| 304 | ✗ | s->indexJ2[i] = dim->nJ; | |
| 305 | }else{ | ||
| 306 | ✗ | s->indexJ2[i] = j++; | |
| 307 | } | ||
| 308 | } | ||
| 309 | |||
| 310 | j = 0; | ||
| 311 | ✗ | for(i = 0; i < dim->nJ2 ; ++i){ | |
| 312 | ✗ | if(i == s->derIndex[2]){ | |
| 313 | ✗ | s->indexJ3[i] = dim->nJ; | |
| 314 | ✗ | }else if(i == s->derIndex[0]){ | |
| 315 | ✗ | s->indexJ3[i] = dim->nJ + 1; | |
| 316 | }else{ | ||
| 317 | ✗ | s->indexJ3[i] = j++; | |
| 318 | } | ||
| 319 | } | ||
| 320 | ✗ | } | |
| 321 | |||
| 322 | /* | ||
| 323 | * copy analytic jacobians vars for each subintervall | ||
| 324 | * author: Vitalij Ruge | ||
| 325 | */ | ||
| 326 | ✗ | static inline void copy_JacVars(OptData *optData){ | |
| 327 | int i,j,l; | ||
| 328 | |||
| 329 | ✗ | DATA* data = optData->data; | |
| 330 | ✗ | int * indexBC = &optData->s.indexABCD[2]; | |
| 331 | ✗ | modelica_boolean * s= &optData->s.matrix[2]; | |
| 332 | const int nn = 3; | ||
| 333 | OptDataDim * dim = &optData->dim; | ||
| 334 | ✗ | const int np = dim->np; | |
| 335 | ✗ | const int nsi = dim->nsi; | |
| 336 | |||
| 337 | ✗ | dim->analyticJacobians_tmpVars = (modelica_real ****) malloc(nn*sizeof(modelica_real***)); | |
| 338 | ✗ | for(l = 0; l < nn ; ++l){ | |
| 339 | ✗ | if(s[l]){ | |
| 340 | ✗ | dim->dim_tmpVars[l] = data->simulationInfo->analyticJacobians[indexBC[l]].sizeTmpVars; | |
| 341 | ✗ | dim->analyticJacobians_tmpVars[l] = (modelica_real ***) malloc(nsi*sizeof(modelica_real**)); | |
| 342 | ✗ | for(i = 0; i< nsi; ++i){ | |
| 343 | ✗ | dim->analyticJacobians_tmpVars[l][i] = (modelica_real **) malloc(np*sizeof(modelica_real*)); | |
| 344 | ✗ | for(j = 0; j< np; ++j){ | |
| 345 | ✗ | dim->analyticJacobians_tmpVars[l][i][j] = (modelica_real *) malloc(dim->dim_tmpVars[l]*sizeof(modelica_real)); | |
| 346 | ✗ | memcpy(dim->analyticJacobians_tmpVars[l][i][j],data->simulationInfo->analyticJacobians[indexBC[l]].tmpVars,dim->dim_tmpVars[l]*sizeof(modelica_real)); | |
| 347 | } | ||
| 348 | } | ||
| 349 | } | ||
| 350 | } | ||
| 351 | ✗ | } | |
| 352 | |||
| 353 | /* | ||
| 354 | * print the jacobian matrix struct | ||
| 355 | * author: Vitalij Ruge | ||
| 356 | */ | ||
| 357 | ✗ | static inline void print_local_jac_struct(DATA * data, OptDataDim * dim, OptDataStructure *s){ | |
| 358 | |||
| 359 | ✗ | const int nv = dim->nv; | |
| 360 | ✗ | const int nJ = dim->nJ; | |
| 361 | ✗ | const int ncf = dim->ncf; | |
| 362 | |||
| 363 | int i, j; | ||
| 364 | |||
| 365 | printf("\nJacabian Structure %i x %i", nJ, nv); | ||
| 366 | printf("\n========================================================"); | ||
| 367 | ✗ | for(i = 0; i < nJ; ++i){ | |
| 368 | printf("\n"); | ||
| 369 | ✗ | for(j =0; j< nv; ++j) | |
| 370 | ✗ | printf("%s ", (s->JderCon[i][j])? "*":"0"); | |
| 371 | } | ||
| 372 | ✗ | if(ncf > 0){ | |
| 373 | printf("\n-------------------------------------------------------"); | ||
| 374 | printf("\nfinal constraint(s)"); | ||
| 375 | ✗ | for(i = 0; i < ncf; ++i){ | |
| 376 | printf("\n"); | ||
| 377 | ✗ | for(j =0; j< nv; ++j) | |
| 378 | ✗ | printf("%s ", (s->J[2][i][j])? "*":"0"); | |
| 379 | } | ||
| 380 | } | ||
| 381 | |||
| 382 | printf("\n========================================================"); | ||
| 383 | printf("\nGradient Structure"); | ||
| 384 | printf("\n========================================================"); | ||
| 385 | ✗ | if(s->lagrange){ | |
| 386 | printf("\nlagrange"); | ||
| 387 | printf("\n-------------------------------------------------------"); | ||
| 388 | printf("\n"); | ||
| 389 | ✗ | for(j = 0; j < nv; ++j) | |
| 390 | ✗ | printf("%s ", (s->gradL[j])? "*":"0"); | |
| 391 | } | ||
| 392 | ✗ | if(s->mayer){ | |
| 393 | printf("\nmayer"); | ||
| 394 | printf("\n-------------------------------------------------------"); | ||
| 395 | printf("\n"); | ||
| 396 | ✗ | for(j = 0; j < nv; ++j) | |
| 397 | ✗ | printf("%s ", (s->gradM[j])? "*":"0"); | |
| 398 | } | ||
| 399 | ✗ | } | |
| 400 | |||
| 401 | |||
| 402 | /* | ||
| 403 | * overestimation hessian matrix struct | ||
| 404 | * author: Vitalij Ruge | ||
| 405 | */ | ||
| 406 | ✗ | static inline void local_hessian_struct(DATA * data, OptDataDim * dim, OptDataStructure *s){ | |
| 407 | ✗ | const int nv = dim->nv; | |
| 408 | ✗ | const int nJ = dim->nJ; | |
| 409 | ✗ | const int ncf = dim->ncf; | |
| 410 | |||
| 411 | int i, j, l; | ||
| 412 | |||
| 413 | modelica_boolean ***Hg; | ||
| 414 | modelica_boolean ** Hm; | ||
| 415 | modelica_boolean ** Hl; | ||
| 416 | modelica_boolean ** H0; | ||
| 417 | modelica_boolean ** H1; | ||
| 418 | modelica_boolean *** Hcf; | ||
| 419 | modelica_boolean **J; | ||
| 420 | modelica_boolean tmp; | ||
| 421 | |||
| 422 | ✗ | dim->nH0 = 0; | |
| 423 | ✗ | dim->nH1 = 0; | |
| 424 | ✗ | dim->nH0_ = 0; | |
| 425 | ✗ | dim->nH1_ = 0; | |
| 426 | |||
| 427 | ✗ | s->Hg = (modelica_boolean ***) malloc(nJ*sizeof(modelica_boolean**)); | |
| 428 | |||
| 429 | ✗ | for(i = 0; i<nJ; ++i){ | |
| 430 | ✗ | s->Hg[i] = (modelica_boolean **) malloc(nv*sizeof(modelica_boolean*)); | |
| 431 | ✗ | for(j = 0; j < nv; ++j) | |
| 432 | ✗ | s->Hg[i][j] = (modelica_boolean *) calloc(nv, sizeof(modelica_boolean)); | |
| 433 | } | ||
| 434 | |||
| 435 | ✗ | s->Hcf = (modelica_boolean ***) malloc(ncf*sizeof(modelica_boolean**)); | |
| 436 | ✗ | for(i = 0; i<ncf; ++i){ | |
| 437 | ✗ | s->Hcf[i] = (modelica_boolean **) malloc(nv*sizeof(modelica_boolean*)); | |
| 438 | ✗ | for(j = 0; j < nv; ++j) | |
| 439 | ✗ | s->Hcf[i][j] = (modelica_boolean *) calloc(nv, sizeof(modelica_boolean)); | |
| 440 | } | ||
| 441 | |||
| 442 | ✗ | s->Hm = (modelica_boolean **) malloc(nv*sizeof(modelica_boolean*)); | |
| 443 | ✗ | s->Hl = (modelica_boolean **) malloc(nv*sizeof(modelica_boolean*)); | |
| 444 | ✗ | s->H0 = (modelica_boolean **) malloc(nv*sizeof(modelica_boolean*)); | |
| 445 | ✗ | s->H1 = (modelica_boolean **) malloc(nv*sizeof(modelica_boolean*)); | |
| 446 | |||
| 447 | |||
| 448 | ✗ | for(j = 0; j < nv; ++j){ | |
| 449 | ✗ | s->Hm[j] = (modelica_boolean *) calloc(nv, sizeof(modelica_boolean)); | |
| 450 | ✗ | s->Hl[j] = (modelica_boolean *) calloc(nv, sizeof(modelica_boolean)); | |
| 451 | ✗ | s->H0[j] = (modelica_boolean *) calloc(nv, sizeof(modelica_boolean)); | |
| 452 | ✗ | s->H1[j] = (modelica_boolean *) calloc(nv, sizeof(modelica_boolean)); | |
| 453 | } | ||
| 454 | |||
| 455 | /***********************************/ | ||
| 456 | Hg = s->Hg; | ||
| 457 | Hm = s->Hm; | ||
| 458 | Hl = s->Hl; | ||
| 459 | H0 = s->H0; | ||
| 460 | H1 = s->H1; | ||
| 461 | ✗ | J = s->JderCon; | |
| 462 | Hcf = s->Hcf; | ||
| 463 | |||
| 464 | /***********************************/ | ||
| 465 | ✗ | for(l = 0; l < nJ; ++l){ | |
| 466 | ✗ | for(i = 0; i < nv; ++i){ | |
| 467 | ✗ | for(j = 0; j <nv; ++j){ | |
| 468 | ✗ | if(J[l][i]*J[l][j]) | |
| 469 | ✗ | Hg[l][i][j] = (modelica_boolean)1; | |
| 470 | } | ||
| 471 | } | ||
| 472 | } | ||
| 473 | /***********************************/ | ||
| 474 | ✗ | for(l = 0; l < ncf; ++l){ | |
| 475 | ✗ | for(i = 0; i < nv; ++i){ | |
| 476 | ✗ | for(j = 0; j <nv; ++j){ | |
| 477 | ✗ | if(s->J[2][l][i]*s->J[2][l][j]) | |
| 478 | ✗ | Hcf[l][i][j] = (modelica_boolean)1; | |
| 479 | } | ||
| 480 | } | ||
| 481 | } | ||
| 482 | |||
| 483 | /***********************************/ | ||
| 484 | ✗ | if(s->lagrange){ | |
| 485 | ✗ | for(i = 0; i< nv; ++i){ | |
| 486 | ✗ | for(j = 0; j <nv; ++j){ | |
| 487 | ✗ | if(s->gradL[i]*s->gradL[j]){ | |
| 488 | ✗ | Hl[i][j] = (modelica_boolean)1; | |
| 489 | } | ||
| 490 | } | ||
| 491 | } | ||
| 492 | } | ||
| 493 | |||
| 494 | /***********************************/ | ||
| 495 | ✗ | if(s->mayer){ | |
| 496 | ✗ | for(i = 0; i< nv; ++i){ | |
| 497 | ✗ | for(j = 0; j <nv; ++j){ | |
| 498 | ✗ | if(s->gradM[i]*s->gradM[j]) | |
| 499 | ✗ | Hm[i][j] = (modelica_boolean)1; | |
| 500 | } | ||
| 501 | } | ||
| 502 | } | ||
| 503 | |||
| 504 | /***********************************/ | ||
| 505 | ✗ | for(i = 0; i< nv; ++i){ | |
| 506 | ✗ | for(j = 0; j <nv; ++j){ | |
| 507 | ✗ | if(nJ>0){ | |
| 508 | ✗ | for(l = 0; l <nJ; ++l){ | |
| 509 | ✗ | tmp = Hg[l][i][j] || Hl[i][j]; | |
| 510 | ✗ | if(tmp && !H0[i][j]){ | |
| 511 | ✗ | H0[i][j] = (modelica_boolean)1; | |
| 512 | ✗ | ++dim->nH0; | |
| 513 | ✗ | if(i <= j) | |
| 514 | ✗ | ++dim->nH0_; | |
| 515 | } | ||
| 516 | ✗ | if((tmp || Hm[i][j]) && !H1[i][j]){ | |
| 517 | ✗ | H1[i][j] = (modelica_boolean)1; | |
| 518 | ✗ | ++dim->nH1; | |
| 519 | ✗ | if(i <= j) | |
| 520 | ✗ | ++dim->nH1_; | |
| 521 | } | ||
| 522 | ✗ | if(H0[i][j] && H1[i][j]) | |
| 523 | break; | ||
| 524 | } | ||
| 525 | }else{ | ||
| 526 | ✗ | if(Hl[i][j]){ | |
| 527 | ✗ | H0[i][j] = (modelica_boolean)1; | |
| 528 | ✗ | ++dim->nH0; | |
| 529 | ✗ | if(i <= j) | |
| 530 | ✗ | ++dim->nH0_; | |
| 531 | } | ||
| 532 | ✗ | if(H0[i][j] || Hm[i][j]){ | |
| 533 | ✗ | H1[i][j] = (modelica_boolean)1; | |
| 534 | ✗ | ++dim->nH1; | |
| 535 | ✗ | if(i <= j) | |
| 536 | ✗ | ++dim->nH1_; | |
| 537 | } | ||
| 538 | } | ||
| 539 | } | ||
| 540 | } | ||
| 541 | |||
| 542 | /*******************************/ | ||
| 543 | ✗ | if(ncf > 0){ | |
| 544 | ✗ | for(i = 0; i< nv; ++i){ | |
| 545 | ✗ | for(j = 0; j <nv; ++j){ | |
| 546 | ✗ | for(l = 0; l <ncf; ++l){ | |
| 547 | ✗ | if(Hcf[l][i][j] && !H1[i][j]){ | |
| 548 | ✗ | H1[i][j] = (modelica_boolean)1; | |
| 549 | ✗ | ++dim->nH1; | |
| 550 | ✗ | if(i <= j) | |
| 551 | ✗ | ++dim->nH1_; | |
| 552 | break; | ||
| 553 | } | ||
| 554 | } | ||
| 555 | } | ||
| 556 | } | ||
| 557 | } | ||
| 558 | |||
| 559 | ✗ | } | |
| 560 | |||
| 561 | /* | ||
| 562 | * print hessian matrix struct | ||
| 563 | * author: Vitalij Ruge | ||
| 564 | */ | ||
| 565 | ✗ | static inline void print_local_hessian_struct(DATA * data, OptDataDim * dim, OptDataStructure *s){ | |
| 566 | ✗ | modelica_boolean **H0 = s->H0; | |
| 567 | ✗ | modelica_boolean **H1 = s->H1; | |
| 568 | ✗ | const int nv = dim->nv; | |
| 569 | ✗ | const int n1 = dim->nH1; | |
| 570 | ✗ | const int n0 = dim->nH0; | |
| 571 | |||
| 572 | int i, j, l; | ||
| 573 | |||
| 574 | printf("\n========================================================"); | ||
| 575 | printf("\nHessian Structure %i x %i\tnz = %i", nv, nv,n0); | ||
| 576 | printf("\n========================================================"); | ||
| 577 | ✗ | for(i = 0; i < nv; ++i){ | |
| 578 | printf("\n"); | ||
| 579 | ✗ | for(j =0; j< nv; ++j) | |
| 580 | ✗ | printf("%s ", (H0[i][j])? "*":"0"); | |
| 581 | } | ||
| 582 | |||
| 583 | ✗ | if(dim->nH1 != dim->nH0){ | |
| 584 | printf("\n========================================================"); | ||
| 585 | printf("\nHessian Structure %i x %i for t = stopTime \tnz = %i", nv, nv, n1); | ||
| 586 | printf("\n========================================================"); | ||
| 587 | ✗ | for(i = 0; i < nv; ++i){ | |
| 588 | printf("\n"); | ||
| 589 | ✗ | for(j =0; j< nv; ++j) | |
| 590 | ✗ | printf("%s ", (H1[i][j])? "*":"0"); | |
| 591 | } | ||
| 592 | } | ||
| 593 | ✗ | } | |
| 594 | |||
| 595 | /* | ||
| 596 | * update the jacobian matrix struct | ||
| 597 | * author: Vitalij Ruge | ||
| 598 | */ | ||
| 599 | static inline void update_local_jac_struct(OptDataDim * dim, OptDataStructure *s){ | ||
| 600 | ✗ | const int nx = dim->nx; | |
| 601 | int i; | ||
| 602 | modelica_boolean ** J; | ||
| 603 | |||
| 604 | ✗ | J = s->JderCon; | |
| 605 | ✗ | for(i = 0; i < nx; ++i){ | |
| 606 | ✗ | if(!J[i][i]){ | |
| 607 | ✗ | J[i][i] = (modelica_boolean)1; | |
| 608 | ✗ | ++dim->nJderx; | |
| 609 | } | ||
| 610 | } | ||
| 611 | } | ||
| 612 | |||
| 613 |