3 // Copyright (C) INRIA - METALAU Project <scicos@inria.fr>
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.
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.
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.
19 // See the file ../license.txt
22 function [txt,rpar,ipar] = create_modelica(blklst,corinvm,cmat,NvM,name,scs_m)
24 if exists("%Modelica_Init")==0 then
25 // Modelica_Init becomes true only in "Modelicainitialize_.sci"
28 if exists("%Modelica_ParEmb")==0 then
32 Parembed=%Modelica_ParEmb & ~%Modelica_Init;
35 rpar=[];//will contain all parameters associated with the all modelica blocs
36 ipar=[];//will contain the "adress" of each block in rpar
37 models=[]//will contain the model declaration part
38 eqns=[]//will contain the modelica equations part
52 np=size(mo.parameters(1),"*");
54 //** mo.parameters have size=2
55 //** it only contains parameters
57 if lstsize(mo.parameters)==2 then
58 mo.parameters(3)=zeros(1,np)
63 //## loop on number of param value
64 //## can be both scalar or array
65 Parj=mo.parameters(1)(j)
66 Parjv=mo.parameters(2)(j)
67 Parj_in=Parj+"_"+string(k)+string(j)
69 if type(Parjv)==1 then // if Real/Complex Integers are used with "fixed=true"
70 rpar=[rpar;matrix(Parjv,-1,1)]
71 ipar(k)=ipar(k)+size(Parjv,"*")
73 //======================================================
74 Params=[Params;construct_Pars(Parj_in,Parjv,Parembed)]
75 if mo.parameters(3)(j)==0 then
76 P=[P;Parj+"="+Parj_in]
77 elseif mo.parameters(3)(j)==1 then
78 //eParjv=construct_redeclar(Parjv)
79 P=[P;Parj+"(start="+Parj_in+")"];
80 elseif mo.parameters(3)(j)==2 then
81 //eParjv=construct_redeclar(Parjv)
82 P=[P;Parj+"(start="+Parj_in+",fixed=true)"];
84 //======================================================
90 Bnumbers=[Bnumbers k];
92 //## update list of names of modelica blocks
93 // Modelica compiler complains about ID containing dots
94 // So remove them Modelica.package.subpackage => Modelica_DOT_package_DOT_subpackage
95 Bnam = [Bnam, get_model_name(strsubst(mo.model, ".", "_DOT_"),Bnam)];
96 Bnames = [Bnames, Bnam($)]
100 " "+mo.model+" "+tab+Bnames($)];
103 " "+mo.model+" "+tab+Bnames($)+"("+strcat(P,", ")+")"];
106 //## Add gr_i identification in comments of models
107 if mo.model<>"OutPutPort" & mo.model<>"InPutPort" then
108 //## retrieve the object in the scs_m structure
109 o_scsm = scs_m(scs_full_path(corinvm(k)));
110 //## get the structure model
111 o_model = o_scsm.model;
112 //## get the identification field
113 id = stripblanks(o_model.label)
116 models($)=models($)+" """+id+""";"
118 models($)=models($)+";"
121 models($)=models($)+";"
126 ipar=cumsum([1;ipar(:)])
132 if from(1)==0 then //input port
134 Bnumbers=[Bnumbers nb];
135 Bnames=[Bnames,"B"+string(nb)];
136 models=[models;" InPutPort"+" "+tab+"B"+string(nb)+";"];
138 elseif from(3)==1 then
139 p1=blklst(from(1)).equations.inputs(from(2))
140 n1=Bnames(find(Bnumbers==from(1)))
142 p1=blklst(from(1)).equations.outputs(from(2))
143 n1=Bnames(find(Bnumbers==from(1)))
146 if to(1)==0 then //output port
148 Bnumbers=[Bnumbers nb];
149 Bnames=[Bnames,"B"+string(nb)];
150 models=[models;" OutPutPort"+" "+tab+"B"+string(nb)+";"];
153 p2=blklst(to(1)).equations.inputs(to(2))
154 n2=Bnames(find(Bnumbers==to(1)))
156 if size(blklst(to(1)).equations.outputs,"*")<to(2) then pause,end
157 p2=blklst(to(1)).equations.outputs(to(2))
158 n2=Bnames(find(Bnumbers==to(1)))
161 if or(blklst(from(1)).equations.model==["InPutPort","OutPutPort"]) ...
162 | or(blklst(to(1)).equations.model==["InPutPort","OutPutPort"]) ...
163 | (k > (size(cmat,1) - NvM)) ...
166 " "+n1+"."+p1+" = "+n2+"."+p2+";"]
169 " connect ("+n1+"."+p1+","+n2+"."+p2+");"]
182 function r=validvar_modelica(s)
186 bad_char=["%" "#" "$"]
187 for j=1:size(bad_char,2)
188 if strindex(s,bad_char(j)) then
198 function r=write_nD_format(x)
201 if size(sx,"*")==2 then // Matrix/Vector
203 if nD1==1 then // rows vector
204 r="{"+strcat(string(x),",")+"}"
205 r=strsubst(r,"D","e");
207 elseif nD2==1 then // column vector
215 // typeof(x)==hypermat
221 // for i=1:n-2;cmd=':,'+cmd;end;
226 cmdx="write_nD_format(x("+string(i)+cmd+")";
227 execstr("r(i)="+cmdx,"errcatch")
229 r="{"+strcat(r,",")+"}";
232 // a 2x3 matrix {{xx,xx,xx},{xx,xx,xx}}
235 // A[2,1] {{xx},{xx}}
236 // A[1,1,2] {{{xx,xx}}}
241 // a=rand(1,2,3,4,5);
242 // a=[1 2 3 4 1 4];a(:,:,2)=[5 6 7 8 1 5] ;
243 //if typeof(a)== 'hypermat' then
244 // disp('not supported')
246 //sa=write_nD_format(a)
249 function Pari=construct_Pars(Pari,opari,Parembed)
254 // Pars=' parameter Real '+Pars+'(fixed=false);'
256 format(20);// writing in long format
258 //erpar=string(rpar); will put 1e-16 to zero in a vector containing
264 if typeof(C)== "hypermat" then
265 messagebox(_("Hyper Matrix is not supported"),"error","modal")
276 elseif (typeof(C)=="int32") | (typeof(C)=="int16") |...
277 (typeof(C)=="int8") |(typeof(C)=="uint32") |...
278 (typeof(C)=="uint16") | (typeof(C)=="uint8") then
282 par_type="UnKnown_Type"
284 messagebox(_("Type not recognized"),"error","modal");ok=%f;
292 eopari=strsubst(string(C),"D","e");
293 fixings="(fixed="+FIXED+") "
295 eopari=write_nD_format(C)
296 fixings="(each fixed="+FIXED+") ";
299 Pari=Pari+"["+string(d2)+"]"; //[d2]
301 Pari=Pari+"["+string(d1)+","+string(d2)+"]"; //[d1,d2]
304 Pari=" parameter "+par_type+" "+Pari+ fixings+"="+eopari+ " """+Pari+""""+";"
305 format(atemp(2))// restituing the format
309 function eopari = construct_redeclar(opari)
312 format(20);// writing in long format
316 if typeof(C)== "hypermat" then
317 messagebox(_("Hyper Matrix is not supported"),"error","modal")
321 messagebox(_("Complex Matrix is not supported"),"error","modal")
326 eopari=strsubst(string(C),"D","e");
328 eopari=write_nD_format(C)
331 format(atemp(2))// restituing the format