Linux GNU 11.4.0 Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 0.0% 0 / 0 / 136
Functions: 0.0% 0 / 0 / 2
Branches: 0.0% 0 / 0 / 82

OMCompiler/SimulationRuntime/c/optimization/DataManagement/DebugeOptimization.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 /*DugebeOptimization.c
29 */
30
31 #include "../OptimizerData.h"
32 #include "../OptimizerLocalFunction.h"
33 #include "../../util/omc_file.h"
34
35
36 /*!
37 * generated csv-file with optimizer variabl in optimizer steps
38 * author: Vitalij Ruge
39 **/
40 ✗ void debugeSteps(OptData * optData, modelica_real*vopt, modelica_real * lambda){
41 FILE * pFile = NULL;
42 char buffer[250];
43 ✗ const int nv = optData->dim.nv;
44 ✗ const int nx = optData->dim.nx;
45 ✗ const int nu = optData->dim.nu;
46 ✗ const int nsi = optData->dim.nsi;
47 ✗ const int np = optData->dim.np;
48 ✗ const int nJ = optData->dim.nJ;
49 ✗ DATA*data = optData->data;
50 char *name;
51
52 int i,j,k,jj;
53
54 ✗ char ** inputName = optData->dim.inputName;
55 ✗ const modelica_real * vnom = optData->bounds.vnom;
56 double tmp;
57
58 ✗ sprintf(buffer, "%s_%d.csv", optData->ipop.csvOstep,optData->dim.iter);
59 ✗ pFile = omc_fopen(buffer, "wt");
60 ✗ if (!pFile) {
61 ✗ fprintf(stderr, "Warning: cannot open %s for writing debug output.\n", buffer);
62 ✗ return;
63 }
64
65 fprintf(pFile, "%s", "\"time\"");
66 ✗ for(i = 0; i < nx; ++i){
67 ✗ name = (char*)data->modelData->realVarsData[i].info.name;
68 fprintf(pFile, ",\"%s\"", name);
69 fprintf(pFile, ",\"%s_lambda\"", name);
70 }
71
72 ✗ for(i = 0; i < nu; ++i){
73 ✗ name = inputName[i];
74 fprintf(pFile, ",\"%s\"", name);
75 }
76 ✗ for(j=0,k=0;j<nsi;++j){
77 ✗ for(jj=0;jj<np;++jj, k += nJ){
78 fprintf(pFile, "\n");
79 ✗ tmp = (modelica_real) optData->time.t[j][jj];
80 fprintf(pFile, "%lf", tmp);
81 ✗ for(i = 0; i < nx; ++i){
82 ✗ tmp = vopt[i + k]*vnom[i];
83 fprintf(pFile, ",%lf", tmp);
84 ✗ tmp = lambda[i+k];
85 fprintf(pFile, ",%lf", tmp);
86 }
87 ✗ for(; i < nv; ++i){
88 ✗ tmp = vopt[i + k]*vnom[i];
89 fprintf(pFile, ",%lf", tmp);
90 }
91 }
92 }
93 ✗ fclose(pFile);
94 }
95
96
97
98 /*!
99 * generated csv and python script for jacobian
100 * author: Vitalij Ruge
101 **/
102 ✗ void debugeJac(OptData * optData, ipnumber* vopt){
103 int i,j,k, jj, kk ,ii;
104 ✗ const int nv = optData->dim.nv;
105 ✗ const int nx = optData->dim.nx;
106 ✗ const int nu = optData->dim.nu;
107 ✗ const int nsi = optData->dim.nsi;
108 const int nJ = optData->dim.nJ;
109 ✗ const int np = optData->dim.np;
110 ✗ const int nc = optData->dim.nc;
111 const int npv = np*nv;
112 ✗ const int nt = optData->dim.nt;
113 const int NRes = optData->dim.NRes;
114 ✗ const int nReal = optData->dim.nReal;
115 ✗ const int NV = optData->dim.NV;
116 ✗ ipnumber *vopt_shift = (ipnumber*)malloc(NV * sizeof(ipnumber));
117 ✗ long double *h = (long double*)malloc(nv * nsi * np * sizeof(long double));
118 #define H(k,i,j) h[(k)*nsi*np + (i)*np + (j)]
119 long double hh;
120 ✗ const modelica_real * const vmax = optData->bounds.vmax;
121 ✗ const modelica_real * const vmin = optData->bounds.vmin;
122 const modelica_real * vnom = optData->bounds.vnom;
123 ✗ modelica_real *vv = (modelica_real*)malloc(nsi * np * nReal * sizeof(modelica_real));
124 #define VV(i,j,r) vv[(i)*np*nReal + (j)*nReal + (r)]
125 FILE *pFile;
126 char buffer[4096];
127 long double *sdt;
128 ✗ modelica_real *JJ = (modelica_real*)malloc(nsi * np * nv * nx * sizeof(modelica_real));
129 #define JJM(a,b,c,d) JJ[(a)*np*nv*nx + (b)*nv*nx + (c)*nx + (d)]
130 modelica_boolean **sJ;
131 modelica_real tmpJ;
132
133 ✗ sJ = optData->s.JderCon;
134 ✗ sprintf(buffer, "jac_ana_step_%i.csv", optData->iter_);
135 ✗ pFile = omc_fopen(buffer, "wt");
136 ✗ if (!pFile) {
137 ✗ fprintf(stderr, "Warning: cannot open %s for writing debug output.\n", buffer);
138 ✗ free(vopt_shift);
139 ✗ free(h);
140 ✗ free(vv);
141 ✗ free(JJ);
142 ✗ return;
143 }
144
145 fprintf(pFile,"name;time;");
146 ✗ for(j = 0; j < nx; ++j)
147 ✗ fprintf(pFile,"%s;",optData->data->modelData->realVarsData[j].info.name);
148 ✗ for(j = 0; j < nu; ++j)
149 ✗ fprintf(pFile, "%s;", optData->dim.inputName[j]);
150 fprintf(pFile,"\n");
151
152 ✗ for(i=0;i < nsi; ++i){
153 ✗ for(j = 0; j < np; ++j){
154 ✗ for(k = 0; k < nx; ++k){
155 ✗ fprintf(pFile,"%s;%f;",optData->data->modelData->realVarsData[k].info.name,(float)optData->time.t[i][j]);
156 ✗ for(jj = 0; jj < nv; ++jj){
157 ✗ tmpJ = (sJ[k][jj]) ? (optData->J[i][j][k][jj]) : 0.0;
158 fprintf(pFile,"%lf;", tmpJ);
159 }
160 fprintf(pFile,"\n");
161 }
162 }
163 }
164 ✗ fclose(pFile);
165
166 #define DF_STEP(v) (1e-5*fabsl(v) + 1e-7)
167 memcpy(vopt_shift ,vopt, NV*sizeof(ipnumber));
168 ✗ optData->index = 0;
169 ✗ for(k=0; k < nv; ++k){
170 ✗ for(i=0, jj=k; i < nsi; ++i){
171 ✗ for(j = 0; j < np; ++j, jj += nv){
172 ✗ hh = DF_STEP(vopt_shift[jj]);
173 ✗ while(vopt_shift[jj] + hh >= vmax[k]){
174 ✗ hh *= -1.0;
175 ✗ if(vopt_shift[jj] + hh <= vmin[k])
176 ✗ hh *= 0.9;
177 else
178 break;
179 ✗ if(fabsl(hh) < 1e-32){
180 printf("\nWarning: StepSize for FD became very small!\n");
181 break;
182 }
183 }
184 ✗ vopt_shift[jj] += hh;
185 ✗ H(k,i,j) = hh;
186 ✗ memcpy(&VV(i,j,0), optData->v[i][j], nReal*sizeof(modelica_real));
187 }
188 }
189
190 ✗ optData2ModelData(optData, vopt_shift, optData->index);
191 memcpy(vopt_shift,vopt , NV*sizeof(modelica_real));
192
193 ✗ for(i = 0; i < nsi; ++i){
194 ✗ sdt = optData->bounds.scaldt[i];
195 ✗ for(j = 0; j < np; ++j){
196 ✗ for(kk = 0, ii = nx; kk<nx;++kk, ++ii){
197 ✗ hh = H(k,i,j);
198 ✗ JJM(i,j,kk,k) = (optData->v[i][j][ii] - VV(i,j,ii))*sdt[kk]/hh;
199 }
200 ✗ memcpy(optData->v[i][j], &VV(i,j,0), nReal*sizeof(modelica_real));
201 }
202 }
203 }
204
205 ✗ optData->index = 1;
206 #undef DF_STEP
207 ✗ sprintf(buffer, "jac_num_step_%i.csv", optData->iter_);
208 ✗ pFile = omc_fopen(buffer, "wt");
209 ✗ if (!pFile) {
210 ✗ fprintf(stderr, "Warning: cannot open %s for writing debug output.\n", buffer);
211 ✗ free(vopt_shift);
212 ✗ free(h);
213 ✗ free(vv);
214 ✗ free(JJ);
215 ✗ return;
216 }
217
218 fprintf(pFile,"name;time;");
219 ✗ for(j = 0; j < nx; ++j)
220 ✗ fprintf(pFile,"%s;",optData->data->modelData->realVarsData[j].info.name);
221 ✗ for(j = 0; j < nu; ++j)
222 ✗ fprintf(pFile, "%s;", optData->dim.inputName[j]);
223 fprintf(pFile,"\n");
224
225 ✗ for(i=0;i < nsi; ++i){
226 ✗ for(j = 0; j < np; ++j){
227 ✗ for(k = 0; k < nx; ++k){
228 ✗ fprintf(pFile,"%s;%f;",optData->data->modelData->realVarsData[k].info.name,(float)optData->time.t[i][j]);
229 ✗ for(jj = 0; jj < nv; ++jj){
230 ✗ tmpJ = (sJ[k][jj]) ? (JJM(i,j,k,jj)) : 0.0;
231 fprintf(pFile,"%lf;",tmpJ);
232 }
233 fprintf(pFile,"\n");
234 }
235 }
236 }
237 ✗ fclose(pFile);
238
239 ✗ optData2ModelData(optData, vopt, optData->index);
240
241 ✗ if(optData->iter_ < 2){
242 ✗ pFile = omc_fopen("omc_check_jac.py", "wt");
243 ✗ if (!pFile) {
244 ✗ fprintf(stderr, "Warning: cannot open omc_check_jac.py for writing debug output.\n");
245 ✗ free(vopt_shift);
246 ✗ free(h);
247 ✗ free(vv);
248 ✗ free(JJ);
249 ✗ return;
250 }
251 fprintf(pFile,"\"\"\"\nautomatically generated code for analyse derivatives\n\n");
252 fprintf(pFile," Input i:\n");
253 ✗ for(j = 0; j < nx; ++j)
254 ✗ fprintf(pFile," i = %i -> der(%s)\n",j,optData->data->modelData->realVarsData[j].info.name);
255 fprintf(pFile," Input j:\n");
256 ✗ for(j = 0; j < nx; ++j)
257 ✗ fprintf(pFile," j = %i -> %s\n",j,optData->data->modelData->realVarsData[j].info.name);
258 ✗ for(j = 0; j < nu; ++j)
259 ✗ fprintf(pFile," j = %i -> %s\n",nx+j,optData->dim.inputName[j]);
260 fprintf(pFile,"\n\nVitalij Ruge, vruge@fh-bielefeld.de\n\"\"\"\n\n");
261
262 fprintf(pFile,"%s\n%s\n%s\n\n","import numpy as np","import matplotlib.pyplot as plt","from numpy import linalg as LA");
263 fprintf(pFile,"class OMC_JAC:\n def __init__(self, filename):\n self.filename = filename\n");
264 fprintf(pFile," self.states = [");
265 ✗ if(nx > 0)
266 ✗ fprintf(pFile,"'%s'",optData->data->modelData->realVarsData[0].info.name);
267 ✗ for(j = 1; j < nx; ++j)
268 ✗ fprintf(pFile,",'%s'",optData->data->modelData->realVarsData[j].info.name);
269 fprintf(pFile,"]\n");
270 fprintf(pFile," self.inputs = [");
271 ✗ if(nu > 0)
272 ✗ fprintf(pFile,"'%s'",optData->dim.inputName[0]);
273 ✗ for(j = 1; j < nu; ++j)
274 ✗ fprintf(pFile,",'%s'",optData->dim.inputName[j]);
275
276 fprintf(pFile,"]\n");
277 fprintf(pFile," self.number_of_states = %i\n",nx);
278 fprintf(pFile," self.number_of_inputs = %i\n",nu);
279 fprintf(pFile," self.number_of_constraints = %i\n",nc);
280 fprintf(pFile," self.number_of_timepoints = %i\n",nt);
281 fprintf(pFile," self.t = np.zeros(self.number_of_timepoints)\n");
282 fprintf(pFile," self.dx = np.zeros(self.number_of_states)\n");
283 fprintf(pFile," self.J = np.zeros([self.number_of_states, self.number_of_states + self.number_of_inputs, self.number_of_timepoints])\n");
284 fprintf(pFile," self.__read_csv__()\n\n");
285 fprintf(pFile," def __read_csv__(self):\n");
286 fprintf(pFile," with open(self.filename,'r') as f:\n");
287 fprintf(pFile,"%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
288 " f.readline() # name",
289 " for l in xrange(self.number_of_timepoints):",
290 " for k in xrange(self.number_of_states):",
291 " l1 = f.readline()",
292 " l1 = l1.split(\";\")",
293 " l1 = [e for e in l1]",
294 " if len(l1) <= 1:",
295 " break",
296 " self.t[l] = float(l1[1])",
297 " for n,r in enumerate(l1[2:-1]):",
298 " self.J[k,n,l] = float(r)",
299 " f.close()\n",
300 " def __str__(self):",
301 " print \"read file %s\"%self.filename"," print \"states: \", self.states",
302 " print \"inputs: \", self.inputs"," print \"t0 = %g, t = %g\"%(self.t[0],self.t[-1])",
303 " return \"\"\n");
304 fprintf(pFile," def get_value_of_jacobian(self,i, j):\n\n");
305 fprintf(pFile," return self.J[i,j,:]\n\n");
306 fprintf(pFile," def plot_jacobian_element(self, i, j, filename):\n");
307 fprintf(pFile,"%s\n"," J = self.get_value_of_jacobian(i, j)");
308 fprintf(pFile,"%s\n"," plt.figure()");
309 fprintf(pFile,"%s\n"," plt.show(False)");
310 fprintf(pFile,"%s\n"," plt.plot(self.t, J)");
311 fprintf(pFile,"%s\n"," if j < self.number_of_states:");
312 fprintf(pFile,"%s\n"," plt_name = \"der(\" + self.states[i] + \")/\" + self.states[j]");
313 fprintf(pFile,"%s\n"," else:");
314 fprintf(pFile,"%s\n"," plt_name = \"der(\" + self.states[i] + \")/\" + self.inputs[j-self.number_of_states]");
315 fprintf(pFile,"%s\n"," plt.legend([plt_name])");
316 fprintf(pFile,"%s\n"," plt.xlabel('time')");
317 fprintf(pFile,"%s\n\n\n"," plt.savefig(filename = filename, format='png')");
318
319
320 fprintf(pFile,"%s\n"," def plot_jacian_elements_nz(self,i,filename):");
321 fprintf(pFile,"%s\n"," for j in xrange(self.number_of_states):");
322 fprintf(pFile,"%s\n"," J = self.get_value_of_jacobian(i, j)");
323 fprintf(pFile,"%s\n"," if LA.norm(J) > 0:");
324 fprintf(pFile,"%s\n"," plt.figure()");
325 fprintf(pFile,"%s\n"," plt.plot(self.t, J)");
326 fprintf(pFile,"%s\n"," plt_name = \"der(\" + self.states[i] + \")/\" + self.states[j]");
327 fprintf(pFile,"%s\n"," plt.legend([plt_name])");
328 fprintf(pFile,"%s\n"," plt.xlabel('time')");
329 fprintf(pFile,"%s\n"," plt.savefig(filename = \"der_\"+ str(i) +\"_state\"+ str(j) + filename, format='png')\n");
330 fprintf(pFile,"%s\n"," for j in xrange(self.number_of_inputs):");
331 fprintf(pFile,"%s\n"," J = self.get_value_of_jacobian(i, j + self.number_of_states)");
332 fprintf(pFile,"%s\n"," if LA.norm(J) > 0:");
333 fprintf(pFile,"%s\n"," plt.figure()");
334 fprintf(pFile,"%s\n"," plt.plot(self.t, J)");
335 fprintf(pFile,"%s\n"," plt_name = \"der(\" + self.states[i] + \")/\" + self.inputs[j]");
336 fprintf(pFile,"%s\n"," plt.legend([plt_name])");
337 fprintf(pFile,"%s\n"," plt.xlabel('time')");
338 fprintf(pFile,"%s\n\n\n"," plt.savefig(filename = \"der_\"+ str(i) +\"_input\"+ str(j) + filename, format='png')");
339
340 fprintf(pFile,"%s\n"," def compare_plt_jac(self, i, J2, filename):");
341 fprintf(pFile,"%s\n"," for j in xrange(self.number_of_states):");
342 fprintf(pFile,"%s\n"," J = self.get_value_of_jacobian(i, j)");
343 fprintf(pFile,"%s\n"," J_ = J2.get_value_of_jacobian(i, j)");
344 fprintf(pFile,"%s\n"," if LA.norm(J-J_)> 0:");
345 fprintf(pFile,"%s\n"," plt.figure()");
346 fprintf(pFile,"%s\n"," plt.hold(False)");
347 fprintf(pFile,"%s\n"," plt.plot(self.t, J,'r', self.t,J_,'k--', linewidth=2.0)");
348 fprintf(pFile,"%s\n"," plt_name = \"der(\" + self.states[i] + \")/\" + self.states[j]");
349 fprintf(pFile,"%s\n"," plt.legend([plt_name, plt_name + '_'])");
350 fprintf(pFile,"%s\n"," plt.xlabel('time')");
351 fprintf(pFile,"%s\n"," plt.savefig(filename = \"der_\"+ str(i) +\"_state\"+ str(j) + filename, format='png')\n");
352 fprintf(pFile,"%s\n"," for j in xrange(self.number_of_inputs):");
353 fprintf(pFile,"%s\n"," J = self.get_value_of_jacobian(i, j+self.number_of_states)");
354 fprintf(pFile,"%s\n"," J_ = J2.get_value_of_jacobian(i, j+self.number_of_states)");
355 fprintf(pFile,"%s\n"," if LA.norm(J-J_) > 0:");
356 fprintf(pFile,"%s\n"," plt.figure()");
357 fprintf(pFile,"%s\n"," plt.hold(False)");
358 fprintf(pFile,"%s\n"," plt.plot(self.t, J,'r',self.t,J_,'k--',linewidth=2.0)");
359 fprintf(pFile,"%s\n"," plt_name = \"der(\" + self.states[i] + \")/\" + self.inputs[j]");
360 fprintf(pFile,"%s\n"," plt.legend([plt_name, plt_name + '_'])");
361 fprintf(pFile,"%s\n"," plt.xlabel('time')");
362 fprintf(pFile,"%s\n\n\n"," plt.savefig(filename = \"der_\"+ str(i) +\"_input\"+ str(j) + filename, format='png')");
363
364 fprintf(pFile,"%s\n","J_ana = OMC_JAC('jac_ana_step_1.csv')");
365 fprintf(pFile,"%s\n","#J_ana.plot_jacian_elements_nz(0,'pltJac_ana.png')");
366 fprintf(pFile,"%s\n","J_num = OMC_JAC('jac_num_step_1.csv')");
367 fprintf(pFile,"%s\n","#J_num.plot_jacian_elements_nz(0,'pltJac_num.png')");
368 fprintf(pFile,"%s\n","for i in xrange(J_ana.number_of_states):");
369 fprintf(pFile,"%s\n"," J_ana.compare_plt_jac(i,J_num,'pltJac_compare.png')");
370
371
372 ✗ fclose(pFile);
373 }
374
375 ✗ free(vopt_shift);
376 ✗ free(h);
377 ✗ free(vv);
378 ✗ free(JJ);
379 }
380