summaryrefslogtreecommitdiff
path: root/macros/Misc/MPBLOCK.sci
blob: a249e9ba9edfc782195376ddee26fb1e0df98111 (plain)
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
//  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]=MPBLOCK(job,arg1,arg2)
    //
    // Copyright INRIA Oct 2006
    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(["MPBLOCK","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("MPBLOCK");

        while %t do
            //     [ok,Tin,Tintype,Tout,Touttype,Tparam,Tparamv,Tfunam,lab]=..
            //         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]=..
            getvalue("Set Modelica generic block parameters",..
            ["Input variables:       ";..
            "Input variables types: ";..
            "Output variables:      ";..
            "Output variables types:";..
            "Parameters in Modelica:";..
            "Parameters properties: ";..
            "Model name in packages:"],..
            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!
                //x_message("Error, try again please!")
                x_message("Error in evaluation of variables.")
                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
                    x_message(["Invalid variable name for the input "+string(i)+".";
                    """"+in(i)+"""";
                    "Please choose another variable name."] );
                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
                    x_message(["Invalid variable name for the output "+string(i)+".";
                    """"+out(i)+"""";
                    "Please choose another variable name."] );
                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
                    x_message(["Invalid variable name for the parameter "+string(i)+".";
                    """"+param(i)+"""";
                    "Please choose another variable name."] );
                end
            end

            // type checking
            //typeok=%t;
            if ok then
                for i=1:size(intype,"*")
                    if intype(i)<>"E"&intype(i)<>"I" then
                        //typeok=%f;
                        x_message("Input type should be ''E'' or ''I''!");
                        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;
                        x_message("Output type should be ''E'' or ''I''!");
                        ok=%f
                        break
                    end
                end
            end
            //   if ~typeok then break, end

            //cross size checking
            if ok then
                if or(size(intype)<>size(in)) then
                    x_message("Input variables are not well defined!");
                    ok=%f
                    //break;
                end
            end

            if ok then
                if or(size(outtype)<>size(out)) then
                    x_message("Output variables are not well defined!");
                    ok=%f
                    //break;
                end
            end

            //check param properties
            if ok then
                pprop = pprop(:)
                if (size(param,"*")<>size(pprop,"*")) then
                    x_message(["There is differences in";
                    "size of param and size ";
                    "of param properties." ])
                    ok=%f
                    //break;
                end
            end

            if ok then
                if max(pprop)>2 | min(pprop)<0 then
                    x_message(["Parameters properties must be :";
                    "0 : if it is a paramaters";
                    "1 : if it is an initial value of state,";
                    "2 : it it is a fixed initial state value." ])
                    ok=%f
                    //break;
                end
            end

            //check name of modelica file
            if ok then
                if funam=="" then
                    x_message("The model name is not defined!")
                    ok=%f
                    //break
                end
            end

            if ok then
                [dirF,nameF,extF]=fileparts(funam);
                if (extF<>"" )|(dirF<>"") then
                    x_message("Invalid model name!")
                    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:Tparam_sz
                            ee=evstr(exprs.param)
                            for j=1:size(ee,"r")
                                if ee(j)==Tparam_lab(i) then
                                    lab_2_tmp(i)=lab_2(j)
                                end
                            end
                        end
                        lab_2=lab_2_tmp
                    end
                end
                //generate lhs, label and rhs txt for 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 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
            //*****************************************************************************

            //============================---------MPBLOCK

            if ok then
                //------------------
                mo=modelica()
                mo.model=nameF
                mo.inputs=in;
                mo.outputs=out;
                if pprop~=[] then
                    if max(pprop)>0 then
                        mo.parameters=list(param',paramv,pprop')
                    else
                        mo.parameters=list(param',paramv)
                    end
                end
                model.equations=mo
                //------------------
                //model.rpar=paramv;
                model.rpar=[]
                for i=1:lstsize(paramv)
                    model.rpar=[model.rpar;
                    double(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="" // model is defined in the a package
                //label(2)=tt
                //------------------
                x.model=model
                graphics.gr_i(1)(1)="txt=['' " + 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=["u"]
        intype=["I"]
        out=["y1";"y2"]
        outtype=["I";"I"]
        param=[];
        paramv=list()
        pprop=[];
        nameF="myModel"

        //label=list([sci2exp(in),sci2exp(intype,sci2exp(out),..
        //           sci2exp(outtype),sci2exp(param),sci2exp(paramv),nameF]',[])

        exprs = tlist(["MPBLOCK","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=[%t %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,10004)
        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,exprs,gr_i)
        x.graphics.in_implicit =intype
        x.graphics.out_implicit=outtype
    end
endfunction