c431ddd30d203e7e35187a28790b37acdc1394e5
[scilab.git] / scilab / modules / scicos_blocks / macros / Sources / Sigbuilder.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] = Sigbuilder(job,arg1,arg2)
23     //** updated for Scilab 5.1 by Simone Mannori
24     x=[];
25     y=[];
26     typ=[];
27
28     select job
29
30     case "set" then
31         // look for the internal curve block
32         ppath = list(0);
33         for i=1:length(arg1.model.rpar.objs) do
34             o = arg1.model.rpar.objs(i);
35             if typeof(o) == "Block" & o.gui == "CURVE_c" then
36                 ppath(1) = i;
37                 break;
38             end
39         end
40         newpar = list();
41         y = 0;
42         for path = ppath do
43             np = size(path,"*")
44             spath = list()
45             for k=1:np
46                 spath($+1)="model"
47                 spath($+1)="rpar"
48                 spath($+1)="objs"
49                 spath($+1)=path(k)
50             end
51             xx=arg1(spath) // get the block
52             execstr("xxn="+xx.gui+"(''set'',xx)")
53             if diffobjs(xxn,xx) then
54                 model=xx.model
55                 model_n=xxn.model
56                 if ~is_modelica_block(xx) then
57                     modified=or(model.sim<>model_n.sim)|..
58                     ~isequal(model.state,model_n.state)|..
59                     ~isequal(model.dstate,model_n.dstate)|..
60                     ~isequal(model.odstate,model_n.odstate)|..
61                     ~isequal(model.rpar,model_n.rpar)|..
62                     ~isequal(model.ipar,model_n.ipar)|..
63                     ~isequal(model.opar,model_n.opar)|..
64                     ~isequal(model.label,model_n.label)
65                     if or(model.in<>model_n.in)|or(model.out<>model_n.out)|..
66                         or(model.in2<>model_n.in2)|or(model.out2<>model_n.out2)|..
67                         or(model.outtyp<>model_n.outtyp)|or(model.intyp<>model_n.intyp) then
68                         needcompile=1
69                     end
70                     if or(model.firing<>model_n.firing) then
71                         needcompile=2
72                     end
73                     if (size(model.in,"*")<>size(model_n.in,"*"))|..
74                         (size(model.out,"*")<>size(model_n.out,"*")) then
75                         needcompile=4
76                     end
77                     if model.sim=="input"|model.sim=="output" then
78                         if model.ipar<>model_n.ipar then
79                             needcompile=4
80                         end
81                     end
82                     if or(model.blocktype<>model_n.blocktype)|..
83                         or(model.dep_ut<>model_n.dep_ut) then
84                         needcompile=4
85                     end
86                     if (model.nzcross<>model_n.nzcross)|(model.nmode<>model_n.nmode) then
87                         needcompile=4
88                     end
89                     if prod(size(model_n.sim))>1 then
90                         if model_n.sim(2)>1000 then
91                             if model.sim(1)<>model_n.sim(1) then
92                                 needcompile=4
93                             end
94                         end
95                     end
96                 else
97                     modified=or(model_n<>model)
98                     eq=model.equations;eqn=model_n.equations;
99                     if or(eq.model<>eqn.model)|or(eq.inputs<>eqn.inputs)|..
100                         or(eq.outputs<>eqn.outputs) then
101                         needcompile=4
102                     end
103                 end
104                 //parameter or states changed
105                 arg1(spath)=xxn// Update
106                 newpar(size(newpar)+1)=path// Notify modification
107                 y=max(y,needcompile)
108             end
109         end
110         x=arg1
111         typ=newpar
112
113     case "define" then
114         scs_m_1=scicos_diagram(..
115         version="scicos4.2",..
116         props=scicos_params(..
117         wpar=[600,450,0,0,600,450],..
118         Title=["Sigbuilder","./"],..
119         tol=[0.0001;0.000001;1.000D-10;100001;0;0;0],..
120         tf=100,..
121         context=" ",..
122         void1=[],..
123         options=tlist(["scsopt","3D","Background","Link","ID","Cmap"],list(%t,33),[8,1],[1,5],..
124         list([5,1],[4,1]),[0.8,0.8,0.8]),..
125         void2=[],..
126         void3=[],..
127         doc=list()))
128         scs_m_1.objs(1)=scicos_block(..
129         gui="CURVE_c",..
130         graphics=scicos_graphics(..
131         orig=[329.63473,606.18517],..
132         sz=[40,40],..
133         exprs=["3";"[0,1,2]";"[10,20,-30]";"y";"n"],..
134         pin=[],..
135         pout=6,..
136         pein=4,..
137         peout=2,..
138         gr_i=[],..
139         id="",..
140         in_implicit=[],..
141         out_implicit="E"),..
142         model=scicos_model(..
143         sim=list("curve_c",4),..
144         in=[],..
145         in2=[],..
146         intyp=1,..
147         out=1,..
148         out2=[],..
149         outtyp=1,..
150         evtin=1,..
151         evtout=1,..
152         state=[],..
153         dstate=[],..
154         odstate=list(),..
155         rpar=[0;1;2;10;20;-30],..
156         ipar=[3;3;1],..
157         opar=list(),..
158         blocktype="c",..
159         firing=0,..
160         dep_ut=[%f,%t],..
161         label="",..
162         nzcross=0,..
163         nmode=0,..
164         equations=list()),..
165         doc=list())
166         scs_m_1.objs(2)=scicos_link(..
167         xx=[349.63473;349.49528],..
168         yy=[600.47089;565.10704],..
169         id="drawlink",..
170         thick=[0,0],..
171         ct=[5,-1],..
172         from=[1,1,0],..
173         to=[3,1,1])
174         scs_m_1.objs(3)=scicos_block(..
175         gui="CLKSPLIT_f",..
176         graphics=scicos_graphics(..
177         orig=[349.49528;565.10704],..
178         sz=[0.3333333,0.3333333],..
179         exprs=[],..
180         pin=[],..
181         pout=[],..
182         pein=2,..
183         peout=[8;4],..
184         gr_i=[],..
185         id="",..
186         in_implicit=[],..
187         out_implicit=[]),..
188         model=scicos_model(..
189         sim="split",..
190         in=[],..
191         in2=[],..
192         intyp=1,..
193         out=[],..
194         out2=[],..
195         outtyp=1,..
196         evtin=1,..
197         evtout=[1;1],..
198         state=[],..
199         dstate=[],..
200         odstate=list(),..
201         rpar=[],..
202         ipar=[],..
203         opar=list(),..
204         blocktype="d",..
205         firing=[%f,%f,%f],..
206         dep_ut=[%f,%f],..
207         label="",..
208         nzcross=0,..
209         nmode=0,..
210         equations=list()),..
211         doc=list())
212         scs_m_1.objs(4)=scicos_link(..
213         xx=[349.49528;266.69602;266.69602;270.35525;342.80795;342.80795;349.63473],..
214         yy=[565.10704;565.10704;680.99483;680.99483;680.99483;651.89946;651.89946],..
215         id="drawlink",..
216         thick=[0,0],..
217         ct=[5,-1],..
218         from=[3,2,0],..
219         to=[1,1,1])
220         scs_m_1.objs(5)=scicos_block(..
221         gui="OUT_f",..
222         graphics=scicos_graphics(..
223         orig=[398.20616,616.18517],..
224         sz=[20,20],..
225         exprs="1",..
226         pin=6,..
227         pout=[],..
228         pein=[],..
229         peout=[],..
230         gr_i=[],..
231         id="",..
232         in_implicit="E",..
233         out_implicit=[]),..
234         model=scicos_model(..
235         sim="output",..
236         in=-1,..
237         in2=-2,..
238         intyp=-1,..
239         out=[],..
240         out2=[],..
241         outtyp=1,..
242         evtin=[],..
243         evtout=[],..
244         state=[],..
245         dstate=[],..
246         odstate=list(),..
247         rpar=[],..
248         ipar=1,..
249         opar=list(),..
250         blocktype="c",..
251         firing=[],..
252         dep_ut=[%f,%f],..
253         label="",..
254         nzcross=0,..
255         nmode=0,..
256         equations=list()),..
257         doc=list())
258         scs_m_1.objs(6)=scicos_link(..
259         xx=[378.20616;398.20616],..
260         yy=[626.18517;626.18517],..
261         id="drawlink",..
262         thick=[0,0],..
263         ct=[1,1],..
264         from=[1,1,0],..
265         to=[5,1,1])
266         scs_m_1.objs(7)=scicos_block(..
267         gui="CLKOUTV_f",..
268         graphics=scicos_graphics(..
269         orig=[339.49528,505.10704],..
270         sz=[20,30],..
271         exprs="1",..
272         pin=[],..
273         pout=[],..
274         pein=8,..
275         peout=[],..
276         gr_i=[],..
277         id="",..
278         in_implicit=[],..
279         out_implicit=[]),..
280         model=scicos_model(..
281         sim="output",..
282         in=[],..
283         in2=[],..
284         intyp=1,..
285         out=[],..
286         out2=[],..
287         outtyp=1,..
288         evtin=1,..
289         evtout=[],..
290         state=[],..
291         dstate=[],..
292         odstate=list(),..
293         rpar=[],..
294         ipar=1,..
295         opar=list(),..
296         blocktype="d",..
297         firing=[],..
298         dep_ut=[%f,%f],..
299         label="",..
300         nzcross=0,..
301         nmode=0,..
302         equations=list()),..
303         doc=list())
304         scs_m_1.objs(8)=scicos_link(..
305         xx=[349.49528;349.49528],..
306         yy=[565.10704;535.10704],..
307         id="drawlink",..
308         thick=[0,0],..
309         ct=[5,-1],..
310         from=[3,1,0],..
311         to=[7,1,1])
312         model=scicos_model(..
313         sim="csuper",..
314         in=[],..
315         in2=[],..
316         intyp=1,..
317         out=-1,..
318         out2=[],..
319         outtyp=1,..
320         evtin=[],..
321         evtout=1,..
322         state=[],..
323         dstate=[],..
324         odstate=list(),..
325         rpar=scs_m_1,..
326         ipar=[],..
327         opar=list(),..
328         blocktype="h",..
329         firing=[],..
330         dep_ut=[%f,%f],..
331         label="",..
332         nzcross=0,..
333         nmode=0,..
334         equations=list())
335         gr_i=[]
336
337         x=standard_define([3 2],model,[],gr_i)
338     end
339 endfunction
340
341
342 //=========================================================
343 function [X,Y,orpar]=Do_Spline2(N,order,x,y)
344
345     X=[];Y=[];orpar=[];
346     METHOD=getmethod(order);
347
348     if (METHOD=="zero order") then
349         X=x(1);Y=y(1);
350         for i=1:N-1
351             X=[X;x(i);x(i+1);x(i+1)];
352             Y=[Y;y(i);y(i);y(i+1)];
353         end
354         return;
355     end
356     //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
357     if (METHOD=="linear") then
358         X=[];
359         for i=1:N
360             X=[X;x(i)];
361             Y=[Y;y(i)];
362         end
363         return;
364     end
365     //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
366     if (N<20) then
367         NP=4;
368     else
369         if (N<40) then
370             NP=2;
371         else
372             if (N<100) then
373                 NP=1;
374             else
375                 NP=0;
376             end;
377         end;
378     end
379     for i=1:N-1
380         X=[X;linspace(x(i),x(i+1),NP+2)']; // pour tous sauf "linear" et "zero order"
381     end
382     //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
383     if (N>2) & (METHOD=="order 2") then
384         Z=ORDER2(x,y);
385         A=Z(1:N-1);
386         B=Z(N:2*N-2);
387         C=Z(2*N-1:3*N-3);
388
389         for j=1:size(X,"*")
390             for i=N-1:-1:1
391                 if X(j)>=x(i) then,break;end
392             end
393             Y(j)=A(i)*(X(j)-x(i))^2+B(i)*(X(j)-x(i))+C(i);
394         end
395         orpar=matrix(Z,-1,1)
396     end
397     //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
398     if (METHOD=="not_a_knot") then
399         try
400             d = splin(x, y, METHOD);
401             Y = interp(X, x, y, d);
402             orpar=d(:);
403         catch
404             xinfo("ERROR in SPLINE: "+METHOD)
405         end
406
407     end
408     //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
409     if (METHOD=="periodic") then
410         if y(1)<>y(N) then
411             y(N)=y(1)
412         end
413         try
414             d = splin(x, y,METHOD);
415             Y = interp(X, x, y, d);
416             orpar=d(:);
417         catch
418             xinfo("ERROR in SPLINE: "+METHOD)
419         end
420     end
421     //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
422     if (METHOD=="monotone" ) then
423         try
424             d = splin(x, y, METHOD);
425             Y = interp(X, x, y, d);
426             orpar=d(:);
427         catch
428             xinfo("ERROR in SPLINE: "+METHOD)
429         end
430
431     end
432     //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
433     if (METHOD=="fast") then
434         try
435             d = splin(x, y, METHOD);
436             Y = interp(X, x, y, d);
437             orpar=d(:);
438         catch
439             xinfo("ERROR in SPLINE:  "+METHOD)
440         end
441     end
442     //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
443     if (METHOD=="clamped") then
444         try
445             d = splin(x, y, METHOD,[0;0]);
446             Y = interp(X, x, y, d);
447             orpar=d(:);
448         catch
449             xinfo("ERROR in SPLINE: "+METHOD)
450         end
451     end
452
453 endfunction
454
455 function METHOD=getmethod(order)
456     select order
457     case 0 then, METHOD="zero order"
458     case 1 then, METHOD="linear"
459     case 2 then, METHOD="order 2"
460     case 3 then, METHOD="not_a_knot"
461     case 4 then, METHOD="periodic"
462     case 5 then, METHOD="monotone"
463     case 6 then, METHOD="fast"
464     case 7 then, METHOD="clamped"
465     end
466 endfunction
467
468
469 function [Z]=ORDER2(x,y)
470     N=size(x,"*")-1;
471     A=zeros(3*N-1,N*3);
472     B=zeros(3*N-1,1);
473     for i=1:N
474         j=3*(i-1)+1;
475         A(j,i+2*N)=1;
476         B(j)=y(i);
477         A(j+1,i)=(x(i+1)-x(i))^2;
478         A(j+1,i+N)=x(i+1)-x(i);
479         A(j+1,i+2*N)=1;
480         B(j+1)=y(i+1);
481     end
482
483     for i=1:N-1
484         j=3*(i-1)+1;
485         A(j+2,i)=2*(x(i+1)-x(i));
486         A(j+2,i+N)=1;
487         A(j+2,i+N+1)=-1;
488     end
489
490     Q=zeros(3*N,3*N);
491     for i=1:N
492         Q(i,i)=4*(x(i+1)-x(i))^2
493         Q(i,i+N)=2*(x(i+1)-x(i))
494         Q(i+N,i)=2*(x(i+1)-x(i))
495         Q(i+N,i+N)=1;
496     end
497
498     At=[Q,A';A,zeros(3*N-1,3*N-1)]
499     Bt=[zeros(3*N,1);B]
500     Zt=At\Bt;
501     Z=Zt(1:3*N,1)
502 endfunction
503 //===================================================
504
505
506
507
508
509