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