Bug Summary

File:OMCompiler/SimulationRuntime/c/./optimization/DataManagement/DebugeOptimization.c
Warning:line 120, column 25
Value stored to 'vnom' during its initialization is never read

Annotated Source Code

[?] Use j/k keys for keyboard navigation

1/*
2 * This file is part of OpenModelica.
3 *
4 * Copyright (c) 1998-2014, Open Source Modelica Consortium (OSMC),
5 * c/o Linköpings universitet, Department of Computer and Information Science,
6 * SE-58183 Linköping, Sweden.
7 *
8 * All rights reserved.
9 *
10 * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THE BSD NEW LICENSE OR THE
11 * GPL VERSION 3 LICENSE OR THE OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
12 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
13 * RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
14 * ACCORDING TO RECIPIENTS CHOICE.
15 *
16 * The OpenModelica software and the OSMC (Open Source Modelica Consortium)
17 * Public License (OSMC-PL) are obtained from OSMC, either from the above
18 * address, from the URLs: http://www.openmodelica.org or
19 * http://www.ida.liu.se/projects/OpenModelica, and in the OpenModelica
20 * distribution. GNU version 3 is obtained from:
21 * http://www.gnu.org/copyleft/gpl.html. The New BSD License is obtained from:
22 * http://www.opensource.org/licenses/BSD-3-Clause.
23 *
24 * This program is distributed WITHOUT ANY WARRANTY; without even the implied
25 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, EXCEPT AS
26 * EXPRESSLY SET FORTH IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE
27 * CONDITIONS OF OSMC-PL.
28 *
29 */
30
31/*DugebeOptimization.c
32 */
33
34#include "../OptimizerData.h"
35#include "../OptimizerLocalFunction.h"
36#include "../../util/omc_file.h"
37
38
39/*!
40 * generated csv-file with optimizer variabl in optimizer steps
41 * author: Vitalij Ruge
42 **/
43void debugeSteps(OptData * optData, modelica_real*vopt, modelica_real * lambda){
44 FILE * pFile = NULL((void*)0);
45 char buffer[250];
46 const int nv = optData->dim.nv;
47 const int nx = optData->dim.nx;
48 const int nu = optData->dim.nu;
49 const int nsi = optData->dim.nsi;
50 const int np = optData->dim.np;
51 const int nJ = optData->dim.nJ;
52 DATA*data = optData->data;
53 char *name;
54
55 int i,j,k,jj;
56
57 char ** inputName = optData->dim.inputName;
58 const modelica_real * vnom = optData->bounds.vnom;
59 double tmp;
60
61 sprintf(buffer, "%s_%d.csv", optData->ipop.csvOstep,optData->dim.iter);
62 pFile = omc_fopen(buffer, "wt");
63
64 fprintf(pFile, "%s", "\"time\"");
65 for(i = 0; i < nx; ++i){
66 name = (char*)data->modelData->realVarsData[i].info.name;
67 fprintf(pFile, ",\"%s\"", name);
68 fprintf(pFile, ",\"%s_lambda\"", name);
69 }
70
71 for(i = 0; i < nu; ++i){
72 name = inputName[i];
73 fprintf(pFile, ",\"%s\"", name);
74 }
75 for(j=0,k=0;j<nsi;++j){
76 for(jj=0;jj<np;++jj, k += nJ){
77 fprintf(pFile, "\n");
78 tmp = (modelica_real) optData->time.t[j][jj];
79 fprintf(pFile, "%lf", tmp);
80 for(i = 0; i < nx; ++i){
81 tmp = vopt[i + k]*vnom[i];
82 fprintf(pFile, ",%lf", tmp);
83 tmp = lambda[i+k];
84 fprintf(pFile, ",%lf", tmp);
85 }
86 for(; i < nv; ++i){
87 tmp = vopt[i + k]*vnom[i];
88 fprintf(pFile, ",%lf", tmp);
89 }
90 }
91 }
92 fclose(pFile);
93}
94
95
96
97/*!
98 * generated csv and python script for jacobian
99 * author: Vitalij Ruge
100 **/
101void debugeJac(OptData * optData, Number* vopt){
102 int i,j,k, jj, kk ,ii;
103 const int nv = optData->dim.nv;
104 const int nx = optData->dim.nx;
105 const int nu = optData->dim.nu;
106 const int nsi = optData->dim.nsi;
107 const int nJ = optData->dim.nJ;
108 const int np = optData->dim.np;
109 const int nc = optData->dim.nc;
110 const int npv = np*nv;
111 const int nt = optData->dim.nt;
112 const int NRes = optData->dim.NRes;
113 const int nReal = optData->data->modelData->nVariablesReal;
114 const int NV = optData->dim.NV;
115 Number vopt_shift[NV];
116 long double h[nv][nsi][np];
117 long double hh;
118 const modelica_real * const vmax = optData->bounds.vmax;
119 const modelica_real * const vmin = optData->bounds.vmin;
120 const modelica_real * vnom = optData->bounds.vnom;
Value stored to 'vnom' during its initialization is never read
121 modelica_real vv[nsi][np][nReal];
122 FILE *pFile;
123 char buffer[4096];
124 long double *sdt;
125 modelica_real JJ[nsi][np][nv][nx];
126 modelica_boolean **sJ;
127 modelica_real tmpJ;
128
129 sJ = optData->s.JderCon;
130 sprintf(buffer, "jac_ana_step_%i.csv", optData->iter_);
131 pFile = omc_fopen(buffer, "wt");
132
133 fprintf(pFile,"name;time;");
134 for(j = 0; j < nx; ++j)
135 fprintf(pFile,"%s;",optData->data->modelData->realVarsData[j].info.name);
136 for(j = 0; j < nu; ++j)
137 fprintf(pFile, "%s;", optData->dim.inputName[j]);
138 fprintf(pFile,"\n");
139
140 for(i=0;i < nsi; ++i){
141 for(j = 0; j < np; ++j){
142 for(k = 0; k < nx; ++k){
143 fprintf(pFile,"%s;%f;",optData->data->modelData->realVarsData[k].info.name,(float)optData->time.t[i][j]);
144 for(jj = 0; jj < nv; ++jj){
145 tmpJ = (sJ[k][jj]) ? (optData->J[i][j][k][jj]) : 0.0;
146 fprintf(pFile,"%lf;", tmpJ);
147 }
148 fprintf(pFile,"\n");
149 }
150 }
151 }
152 fclose(pFile);
153
154 #define DF_STEP(v) (1e-5*fabsl(v) + 1e-7)
155 memcpy(vopt_shift ,vopt, NV*sizeof(Number));
156 optData->index = 0;
157 for(k=0; k < nv; ++k){
158 for(i=0, jj=k; i < nsi; ++i){
159 for(j = 0; j < np; ++j, jj += nv){
160 hh = DF_STEP(vopt_shift[jj]);
161 while(vopt_shift[jj] + hh >= vmax[k]){
162 hh *= -1.0;
163 if(vopt_shift[jj] + hh <= vmin[k])
164 hh *= 0.9;
165 else
166 break;
167 if(fabsl(hh) < 1e-32){
168 printf("\nWarning: StepSize for FD became very small!\n");
169 break;
170 }
171 }
172 vopt_shift[jj] += hh;
173 h[k][i][j] = hh;
174 memcpy(vv[i][j] , optData->v[i][j], nReal*sizeof(modelica_real));
175 }
176 }
177
178 optData2ModelData(optData, vopt_shift, optData->index);
179 memcpy(vopt_shift,vopt , NV*sizeof(modelica_real));
180
181 for(i = 0; i < nsi; ++i){
182 sdt = optData->bounds.scaldt[i];
183 for(j = 0; j < np; ++j){
184 for(kk = 0, ii = nx; kk<nx;++kk, ++ii){
185 hh = h[k][i][j];
186 JJ[i][j][kk][k] = (optData->v[i][j][ii] - vv[i][j][ii])*sdt[kk]/hh;
187 }
188 memcpy(optData->v[i][j] , vv[i][j], nReal*sizeof(modelica_real));
189 }
190 }
191 }
192
193 optData->index = 1;
194#undef DF_STEP
195 sprintf(buffer, "jac_num_step_%i.csv", optData->iter_);
196 pFile = omc_fopen(buffer, "wt");
197
198 fprintf(pFile,"name;time;");
199 for(j = 0; j < nx; ++j)
200 fprintf(pFile,"%s;",optData->data->modelData->realVarsData[j].info.name);
201 for(j = 0; j < nu; ++j)
202 fprintf(pFile, "%s;", optData->dim.inputName[j]);
203 fprintf(pFile,"\n");
204
205 for(i=0;i < nsi; ++i){
206 for(j = 0; j < np; ++j){
207 for(k = 0; k < nx; ++k){
208 fprintf(pFile,"%s;%f;",optData->data->modelData->realVarsData[k].info.name,(float)optData->time.t[i][j]);
209 for(jj = 0; jj < nv; ++jj){
210 tmpJ = (sJ[k][jj]) ? (JJ[i][j][k][jj]) : 0.0;
211 fprintf(pFile,"%lf;",tmpJ);
212 }
213 fprintf(pFile,"\n");
214 }
215 }
216 }
217 fclose(pFile);
218
219 optData2ModelData(optData, vopt, optData->index);
220
221 if(optData->iter_ < 2){
222 pFile = omc_fopen("omc_check_jac.py", "wt");
223 fprintf(pFile,"\"\"\"\nautomatically generated code for analyse derivatives\n\n");
224 fprintf(pFile," Input i:\n");
225 for(j = 0; j < nx; ++j)
226 fprintf(pFile," i = %i -> der(%s)\n",j,optData->data->modelData->realVarsData[j].info.name);
227 fprintf(pFile," Input j:\n");
228 for(j = 0; j < nx; ++j)
229 fprintf(pFile," j = %i -> %s\n",j,optData->data->modelData->realVarsData[j].info.name);
230 for(j = 0; j < nu; ++j)
231 fprintf(pFile," j = %i -> %s\n",nx+j,optData->dim.inputName[j]);
232 fprintf(pFile,"\n\nVitalij Ruge, vruge@fh-bielefeld.de\n\"\"\"\n\n");
233
234 fprintf(pFile,"%s\n%s\n%s\n\n","import numpy as np","import matplotlib.pyplot as plt","from numpy import linalg as LA");
235 fprintf(pFile,"class OMC_JAC:\n def __init__(self, filename):\n self.filename = filename\n");
236 fprintf(pFile," self.states = [");
237 if(nx > 0)
238 fprintf(pFile,"'%s'",optData->data->modelData->realVarsData[0].info.name);
239 for(j = 1; j < nx; ++j)
240 fprintf(pFile,",'%s'",optData->data->modelData->realVarsData[j].info.name);
241 fprintf(pFile,"]\n");
242 fprintf(pFile," self.inputs = [");
243 if(nu > 0)
244 fprintf(pFile,"'%s'",optData->dim.inputName[0]);
245 for(j = 1; j < nu; ++j)
246 fprintf(pFile,",'%s'",optData->dim.inputName[j]);
247
248 fprintf(pFile,"]\n");
249 fprintf(pFile," self.number_of_states = %i\n",nx);
250 fprintf(pFile," self.number_of_inputs = %i\n",nu);
251 fprintf(pFile," self.number_of_constraints = %i\n",nc);
252 fprintf(pFile," self.number_of_timepoints = %i\n",nt);
253 fprintf(pFile," self.t = np.zeros(self.number_of_timepoints)\n");
254 fprintf(pFile," self.dx = np.zeros(self.number_of_states)\n");
255 fprintf(pFile," self.J = np.zeros([self.number_of_states, self.number_of_states + self.number_of_inputs, self.number_of_timepoints])\n");
256 fprintf(pFile," self.__read_csv__()\n\n");
257 fprintf(pFile," def __read_csv__(self):\n");
258 fprintf(pFile," with open(self.filename,'r') as f:\n");
259 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",
260 " f.readline() # name",
261 " for l in xrange(self.number_of_timepoints):",
262 " for k in xrange(self.number_of_states):",
263 " l1 = f.readline()",
264 " l1 = l1.split(\";\")",
265 " l1 = [e for e in l1]",
266 " if len(l1) <= 1:",
267 " break",
268 " self.t[l] = float(l1[1])",
269 " for n,r in enumerate(l1[2:-1]):",
270 " self.J[k,n,l] = float(r)",
271 " f.close()\n",
272 " def __str__(self):",
273 " print \"read file %s\"%self.filename"," print \"states: \", self.states",
274 " print \"inputs: \", self.inputs"," print \"t0 = %g, t = %g\"%(self.t[0],self.t[-1])",
275 " return \"\"\n");
276 fprintf(pFile," def get_value_of_jacobian(self,i, j):\n\n");
277 fprintf(pFile," return self.J[i,j,:]\n\n");
278 fprintf(pFile," def plot_jacobian_element(self, i, j, filename):\n");
279 fprintf(pFile,"%s\n"," J = self.get_value_of_jacobian(i, j)");
280 fprintf(pFile,"%s\n"," plt.figure()");
281 fprintf(pFile,"%s\n"," plt.show(False)");
282 fprintf(pFile,"%s\n"," plt.plot(self.t, J)");
283 fprintf(pFile,"%s\n"," if j < self.number_of_states:");
284 fprintf(pFile,"%s\n"," plt_name = \"der(\" + self.states[i] + \")/\" + self.states[j]");
285 fprintf(pFile,"%s\n"," else:");
286 fprintf(pFile,"%s\n"," plt_name = \"der(\" + self.states[i] + \")/\" + self.inputs[j-self.number_of_states]");
287 fprintf(pFile,"%s\n"," plt.legend([plt_name])");
288 fprintf(pFile,"%s\n"," plt.xlabel('time')");
289 fprintf(pFile,"%s\n\n\n"," plt.savefig(filename = filename, format='png')");
290
291
292 fprintf(pFile,"%s\n"," def plot_jacian_elements_nz(self,i,filename):");
293 fprintf(pFile,"%s\n"," for j in xrange(self.number_of_states):");
294 fprintf(pFile,"%s\n"," J = self.get_value_of_jacobian(i, j)");
295 fprintf(pFile,"%s\n"," if LA.norm(J) > 0:");
296 fprintf(pFile,"%s\n"," plt.figure()");
297 fprintf(pFile,"%s\n"," plt.plot(self.t, J)");
298 fprintf(pFile,"%s\n"," plt_name = \"der(\" + self.states[i] + \")/\" + self.states[j]");
299 fprintf(pFile,"%s\n"," plt.legend([plt_name])");
300 fprintf(pFile,"%s\n"," plt.xlabel('time')");
301 fprintf(pFile,"%s\n"," plt.savefig(filename = \"der_\"+ str(i) +\"_state\"+ str(j) + filename, format='png')\n");
302 fprintf(pFile,"%s\n"," for j in xrange(self.number_of_inputs):");
303 fprintf(pFile,"%s\n"," J = self.get_value_of_jacobian(i, j + self.number_of_states)");
304 fprintf(pFile,"%s\n"," if LA.norm(J) > 0:");
305 fprintf(pFile,"%s\n"," plt.figure()");
306 fprintf(pFile,"%s\n"," plt.plot(self.t, J)");
307 fprintf(pFile,"%s\n"," plt_name = \"der(\" + self.states[i] + \")/\" + self.inputs[j]");
308 fprintf(pFile,"%s\n"," plt.legend([plt_name])");
309 fprintf(pFile,"%s\n"," plt.xlabel('time')");
310 fprintf(pFile,"%s\n\n\n"," plt.savefig(filename = \"der_\"+ str(i) +\"_input\"+ str(j) + filename, format='png')");
311
312 fprintf(pFile,"%s\n"," def compare_plt_jac(self, i, J2, filename):");
313 fprintf(pFile,"%s\n"," for j in xrange(self.number_of_states):");
314 fprintf(pFile,"%s\n"," J = self.get_value_of_jacobian(i, j)");
315 fprintf(pFile,"%s\n"," J_ = J2.get_value_of_jacobian(i, j)");
316 fprintf(pFile,"%s\n"," if LA.norm(J-J_)> 0:");
317 fprintf(pFile,"%s\n"," plt.figure()");
318 fprintf(pFile,"%s\n"," plt.hold(False)");
319 fprintf(pFile,"%s\n"," plt.plot(self.t, J,'r', self.t,J_,'k--', linewidth=2.0)");
320 fprintf(pFile,"%s\n"," plt_name = \"der(\" + self.states[i] + \")/\" + self.states[j]");
321 fprintf(pFile,"%s\n"," plt.legend([plt_name, plt_name + '_'])");
322 fprintf(pFile,"%s\n"," plt.xlabel('time')");
323 fprintf(pFile,"%s\n"," plt.savefig(filename = \"der_\"+ str(i) +\"_state\"+ str(j) + filename, format='png')\n");
324 fprintf(pFile,"%s\n"," for j in xrange(self.number_of_inputs):");
325 fprintf(pFile,"%s\n"," J = self.get_value_of_jacobian(i, j+self.number_of_states)");
326 fprintf(pFile,"%s\n"," J_ = J2.get_value_of_jacobian(i, j+self.number_of_states)");
327 fprintf(pFile,"%s\n"," if LA.norm(J-J_) > 0:");
328 fprintf(pFile,"%s\n"," plt.figure()");
329 fprintf(pFile,"%s\n"," plt.hold(False)");
330 fprintf(pFile,"%s\n"," plt.plot(self.t, J,'r',self.t,J_,'k--',linewidth=2.0)");
331 fprintf(pFile,"%s\n"," plt_name = \"der(\" + self.states[i] + \")/\" + self.inputs[j]");
332 fprintf(pFile,"%s\n"," plt.legend([plt_name, plt_name + '_'])");
333 fprintf(pFile,"%s\n"," plt.xlabel('time')");
334 fprintf(pFile,"%s\n\n\n"," plt.savefig(filename = \"der_\"+ str(i) +\"_input\"+ str(j) + filename, format='png')");
335
336 fprintf(pFile,"%s\n","J_ana = OMC_JAC('jac_ana_step_1.csv')");
337 fprintf(pFile,"%s\n","#J_ana.plot_jacian_elements_nz(0,'pltJac_ana.png')");
338 fprintf(pFile,"%s\n","J_num = OMC_JAC('jac_num_step_1.csv')");
339 fprintf(pFile,"%s\n","#J_num.plot_jacian_elements_nz(0,'pltJac_num.png')");
340 fprintf(pFile,"%s\n","for i in xrange(J_ana.number_of_states):");
341 fprintf(pFile,"%s\n"," J_ana.compare_plt_jac(i,J_num,'pltJac_compare.png')");
342
343
344 fclose(pFile);
345 }
346}
347
348