1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
|
// Scicos
//
// Copyright (C) INRIA - METALAU Project <scicos@inria.fr>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// See the file ../license.txt
//
function [x,y,typ]=MBLOCK(job,arg1,arg2)
x=[];
y=[];
typ=[];
select job
case "set" then
x=arg1
model=arg1.model
graphics=arg1.graphics
//label=graphics.exprs;
exprs=graphics.exprs
//Compatibility
if type(exprs)==15 then
paramv=list();
pprop=[];
//must do something better than that !
for i=1:size(model.rpar,"*")
paramv($+1)=string(model.rpar(i))
pprop($+1) = 0
end
exprs = tlist(["MBLOCK","in","intype","out","outtype",...
"param","paramv","pprop","nameF","funtxt"],...
exprs(1)(1),..
exprs(1)(2),..
exprs(1)(3),..
exprs(1)(4),..
exprs(1)(5),..
paramv,...
sci2exp(pprop(:)),..
exprs(1)(7),exprs(2))
end
//lab_1 = [in,intype,out,outtype,param,nameF]
lab_1 = list(exprs.in,.. //1
exprs.intype,.. //2
exprs.out,.. //3
exprs.outtype,.. //4
exprs.param,.. //5
exprs.pprop,.. //6
exprs.nameF) //7
//lab_2 = [paramv]
lab_2 = exprs.paramv //already a list
//store exprs before modif
//exprs.prev_exprs=exprs;
//exprs.prev_exprs.prev_exprs=tlist("MBLOCK");
while %t do
// [ok,Tin,Tintype,Tout,Touttype,Tparam,Tparamv,Tfunam,lab]=..
// scicos_getvalue('Set Modelica generic block parameters',..
// ['Input variables: ';..
// 'Input variables types: ';..
// 'Output variables: ';..
// 'Output variables types:';..
// 'Parameters in Modelica:';..
// 'Parameter values: ';..
// 'Function name: '],..
// list('str',-1,'str',-1,'str',-1,'str',-1,'str',-1,'str',-1,'str',-1),label(1))
//warning here lab_1 is a list
[ok,Tin,Tintype,Tout,Touttype,Tparam,pprop,Tfunam,lab_1]=..
scicos_getvalue("Set Modelica generic block parameters",..
["Input variables: ";..
"Input variables types: ";..
"Output variables: ";..
"Output variables types:";..
"Parameters in Modelica:";..
"Parameters properties: ";..
"Function name: "],..
list("str",-1,"str",-1,"str",-1,"str",-1,"str",-1,..
"vec",-1,"str",-1),lab_1)
//Cancel case
if ~ok then
break
end
// ierr=execstr("in=stripblanks(evstr(Tin));..
// intype=stripblanks(evstr(Tintype));..
// out=stripblanks(evstr(Tout));..
// outtype=stripblanks(evstr(Touttype));..
// param=stripblanks(evstr(Tparam));..
// paramv=evstr(Tparamv);..
// funam=stripblanks(Tfunam)",'errcatch')
//
//first stage of checking variable name
//put string symbolic parameters in variable
ierr=execstr("in=stripblanks(evstr(Tin));..
intype=stripblanks(evstr(Tintype));..
out=stripblanks(evstr(Tout));..
outtype=stripblanks(evstr(Touttype));..
param=stripblanks(evstr(Tparam));..
funam=stripblanks(Tfunam)","errcatch")
if ierr<>0 then
//You lose! Try again! Insert coin!
//messagebox("Error, try again please!","modal","error");
messagebox("Error in evaluation of variables.","modal","error");
ok=%f
//break
end
//check for valid name variable
//in
if ok then
for i=1:size(in,"*")
r=%f;
ierr=execstr("r=validvar(in(i))","errcatch")
if ~r then
ok=%f
break
end
end
if ~ok then
messagebox(["Invalid variable name for the input "+string(i)+".";
""""+in(i)+"""";
"Please choose another variable name."],"modal","error");
end
end
//out
if ok then
for i=1:size(out,"*")
r=%f;
ierr=execstr("r=validvar(out(i))","errcatch")
if ~r then
ok=%f
break
end
end
if ~ok then
messagebox(["Invalid variable name for the output "+string(i)+".";
""""+out(i)+"""";
"Please choose another variable name."],"modal","error");
end
end
//param
if ok then
param=param(:)
for i=1:size(param,"*")
r=%f;
ierr=execstr("r=validvar(param(i))","errcatch")
if ~r then
ok=%f
break
end
end
if ~ok then
messagebox(["Invalid variable name for the parameter "+string(i)+".";
""""+param(i)+"""";
"Please choose another variable name."],"modal","error");
end
end
// type checking
//typeok=%t;
if ok then
for i=1:size(intype,"*")
if intype(i)<>"E"&intype(i)<>"I" then
//typeok=%f;
messagebox("Input type should be ''E'' or ''I''!","modal","error");
ok=%f
break
end
end
end
// if ~typeok then
// ok=%f
// //break
// end
if ok then
for i=1:size(outtype,"*")
if outtype(i)<>"E"&outtype(i)<>"I" then
//typeok=%f;
messagebox("Output type should be ''E'' or ''I''!","modal","error");
ok=%f
break
end
end
end
// if ~typeok then break, end
//cross size checking
if ok then
if or(size(intype)<>size(in)) then
messagebox("Input variables are not well defined!","modal","error");
ok=%f
//break;
end
end
if ok then
if or(size(outtype)<>size(out)) then
messagebox("Output variables are not well defined!","modal","error");
ok=%f
//break;
end
end
//check param properties
if ok then
pprop = pprop(:)
if (size(param,"*")<>size(pprop,"*")) then
messagebox(["There is differences in";
"size of param and size ";
"of param properties."],"modal","error");
ok=%f
//break;
end
end
if ok then
if max(pprop)>2 | min(pprop)<0 then
messagebox(["Parameters properties must be :";
"0 : for simple paramater,";
"1 : for initial state value,";
"2 : for a fixed initial state value."],"modal","error");
ok=%f
//break;
end
end
//check name of modelica file
if ok then
if funam=="" then
messagebox("The filename is not defined!","modal","error");
ok=%f
//break
end
end
if ok then
[dirF,nameF,extF]=fileparts(funam);
if (extF<>"" & extF<>".mo")|(dirF<>"" & extF<>".mo") then
messagebox("Filename extention should be ''.mo '' !","modal","error");
ok=%f
//break
end
end
if ok then
//set_io checking
intypex=find(intype=="I"); outtypex=find(outtype=="I");
//[model,graphics,ok]=check_io(model,graphics,ones(in),..
// ones(out),[],[],intypex,outtypex)
[model,graphics,ok]=set_io(model,graphics,list([ones(in),ones(in)],ones(in)),..
list([ones(out),ones(out)],ones(out)),..
[],[],intypex,outtypex)
end
//*****************************************************************************
//generate second dialog box from Tparam
if ok then
Tparam_lab=evstr(Tparam) //labels
Tparam_sz=size(Tparam_lab,"*") //numbers of labels
//adjust size of lab_2 according to size of Tparam
if Tparam_sz>lstsize(lab_2) then
for i=1:(Tparam_sz-lstsize(lab_2))
lab_2($+1)="0"
end
elseif Tparam_sz<lstsize(lab_2) then
lab_2_tmp=list()
if Tparam_sz<>0 then //if param
for i=1:(lstsize(lab_2)-Tparam_sz)
lab_2_tmp(i)=lab_2(i)
end
end
lab_2=lab_2_tmp
end
//generate lhs, label and rhs txt for scicos_getvalue
if Tparam_sz<>0 then //if param
lhs_txt="" //lhs txt
lab_txt="" //label txt
rhs_txt="" //rhs txt
for i=1:Tparam_sz
lhs_txt=lhs_txt+"%v"+string(i)+","
if pprop(i) == 0 then
lab_txt=lab_txt+"''"+Tparam_lab(i)+"'';"
elseif pprop(i) == 1 then
lab_txt=lab_txt+"''"+Tparam_lab(i)+" (state) '';"
elseif pprop(i) == 2 then
lab_txt=lab_txt+"''"+Tparam_lab(i)+" (fixed state) '';"
end
rhs_txt=rhs_txt+"''vec'',-1,"
end
lhs_txt=part(lhs_txt,1:length(lhs_txt)-1)
lab_txt=part(lab_txt,1:length(lab_txt)-1)
rhs_txt=part(rhs_txt,1:length(rhs_txt)-1)
//generate main scicos_getvalue cmd
//warning here lab_2 is a list in input and a string in output
getvalue_txt = "[ok,"+lhs_txt+",lab_2]=scicos_getvalue(''Set parameters values'',["+..
lab_txt+"],"+..
"list("+rhs_txt+"),lab_2)"
//display the second dialog box
execstr(getvalue_txt)
//restore original lab_2 if not ok
if ~ok then
lab_2 = exprs.paramv
end
end
end
//put output param in the form of a list
//in paramv
if ok then
paramv=list();
for i=1:Tparam_sz
execstr("paramv("+string(i)+")=%v"+string(i))
end
end
//*****************************************************************************
//============================---------MBLOCK2
if ok then
if extF==".mo" & fileinfo(funam)<>[] then
tt=mgetl(funam)
else
tt=exprs.funtxt
mo=model.equations
//if model.sim(1)<>nameF then
if mo.model<>nameF then
tt=[]
end
end
//============================
[ok,tt]=MODCOM(funam,tt,in,out,param,paramv,pprop)
if ~ok then
break
end
end
if ok then
//------------------
mo=modelica()
mo.model=nameF
mo.inputs=in;
mo.outputs=out;
if max(pprop)>0 then
mo.parameters=list(param',paramv,pprop')
else
mo.parameters=list(param',paramv)
end
model.equations=mo
//------------------
//model.rpar=paramv;
model.rpar=[]
for i=1:lstsize(paramv)
model.rpar=[model.rpar;
paramv(i)(:)]//value
end
model.sim(1)=funam
//------------------
exprs.in=lab_1(1)
exprs.intype=lab_1(2)
exprs.out=lab_1(3)
exprs.outtype=lab_1(4)
exprs.param=lab_1(5)
exprs.paramv=list();
if Tparam_sz<>0 then //if param
if type(lab_2)==15 then //for eval
for i=1:lstsize(lab_2)
exprs.paramv(i)=lab_2(i);//string
end
else
for i=1:size(lab_2,"*")
exprs.paramv(i)=lab_2(i);
end
end
end
exprs.pprop=lab_1(6)
exprs.nameF=lab_1(7)
exprs.funtxt=tt
//label(2)=tt
//------------------
x.model=model
graphics.gr_i(1)(1)="txt=[''Modelica'';'' " + nameF + " ''];"
graphics.in_implicit =intype
graphics.out_implicit=outtype
//graphics.exprs=label
graphics.exprs=exprs
x.graphics=graphics
break
end
end
//Define
case "define" then
in=["u1"]
intype=["I"]
out=["y1";"y2"]
outtype=["I";"E"]
param=["R";"L"];
//paramv=[0.1;0.0001];
paramv=list(0.1,.0001)
pprop=[0;0];
nameF="generic"
//label=list([sci2exp(in),sci2exp(intype,sci2exp(out),..
// sci2exp(outtype),sci2exp(param),sci2exp(paramv),nameF]',[])
exprs = tlist(["MBLOCK","in","intype","out","outtype",...
"param","paramv","pprop","nameF","funtxt"],...
sci2exp(in(:)),..
sci2exp(intype(:)),..
sci2exp(out(:)),..
sci2exp(outtype(:)),..
sci2exp(param(:)),..
list(string(0.1),string(.0001)),...
sci2exp(pprop(:)),..
nameF,[])
model=scicos_model()
model.blocktype="c"
model.dep_ut=[%f %t]
//model.rpar=paramv;
model.rpar=[]
for i=1:lstsize(paramv)
model.rpar=[model.rpar;
paramv(i)(:)]
end
mo=modelica()
mo.model=nameF
mo.parameters=list(param,paramv)
model.sim=list(mo.model,30004)
mo.inputs=in
mo.outputs=out
model.in=ones(size(mo.inputs,"r"),1)
model.out=ones(size(mo.outputs,"r"),1)
model.equations=mo
gr_i=[]
//x=standard_define([3 2],model,label,gr_i)
x=standard_define([3 2],model,exprs,gr_i)
x.graphics.in_implicit =intype
x.graphics.out_implicit=outtype
end
endfunction
|