Xcos CURVE_c: avoid figure popup on error when edit window mode is off.
[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 or(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                 label = xxn.graphics.exprs(3);
107                 if grep(label, "/[a-zA-Z][a-zA-Z0-9_]*/", 'r') then
108                     arg1.graphics.style = "Sigbuilder;displayedLabel="+label;
109                 else
110                     arg1.graphics.style = "Sigbuilder";
111                 end
112                 newpar(size(newpar)+1)=path// Notify modification
113                 y=max(y,needcompile)
114             end
115         end
116         x=arg1
117         typ=newpar
118
119     case "define" then
120         scs_m_1=scicos_diagram(..
121         version="scicos4.2",..
122         props=scicos_params(..
123         wpar=[600,450,0,0,600,450],..
124         Title=["Sigbuilder","./"],..
125         tol=[0.0001;0.000001;1.000D-10;100001;0;0;0],..
126         tf=100,..
127         context=" ",..
128         void1=[],..
129         options=tlist(["scsopt","3D","Background","Link","ID","Cmap"],list(%t,33),[8,1],[1,5],..
130         list([5,1],[4,1]),[0.8,0.8,0.8]),..
131         void2=[],..
132         void3=[],..
133         doc=list()))
134         scs_m_1.objs(1)=scicos_block(..
135         gui="CURVE_c",..
136         graphics=scicos_graphics(..
137         orig=[329.63473,606.18517],..
138         sz=[40,40],..
139         exprs=["3";"[0,1,2]";"[10,20,-30]";"y";"n"],..
140         pin=[],..
141         pout=6,..
142         pein=4,..
143         peout=2,..
144         gr_i=[],..
145         id="",..
146         in_implicit=[],..
147         out_implicit="E"),..
148         model=scicos_model(..
149         sim=list("curve_c",4),..
150         in=[],..
151         in2=[],..
152         intyp=1,..
153         out=1,..
154         out2=[],..
155         outtyp=1,..
156         evtin=1,..
157         evtout=1,..
158         state=[],..
159         dstate=[],..
160         odstate=list(),..
161         rpar=[0;1;2;10;20;-30],..
162         ipar=[3;3;1],..
163         opar=list(),..
164         blocktype="c",..
165         firing=0,..
166         dep_ut=[%f,%t],..
167         label="",..
168         nzcross=0,..
169         nmode=0,..
170         equations=list()),..
171         doc=list())
172         scs_m_1.objs(2)=scicos_link(..
173         xx=[349.63473;349.49528],..
174         yy=[600.47089;565.10704],..
175         id="drawlink",..
176         thick=[0,0],..
177         ct=[5,-1],..
178         from=[1,1,0],..
179         to=[3,1,1])
180         scs_m_1.objs(3)=scicos_block(..
181         gui="CLKSPLIT_f",..
182         graphics=scicos_graphics(..
183         orig=[349.49528;565.10704],..
184         sz=[0.3333333,0.3333333],..
185         exprs=[],..
186         pin=[],..
187         pout=[],..
188         pein=2,..
189         peout=[8;4],..
190         gr_i=[],..
191         id="",..
192         in_implicit=[],..
193         out_implicit=[]),..
194         model=scicos_model(..
195         sim="split",..
196         in=[],..
197         in2=[],..
198         intyp=1,..
199         out=[],..
200         out2=[],..
201         outtyp=1,..
202         evtin=1,..
203         evtout=[1;1],..
204         state=[],..
205         dstate=[],..
206         odstate=list(),..
207         rpar=[],..
208         ipar=[],..
209         opar=list(),..
210         blocktype="d",..
211         firing=[%f,%f,%f],..
212         dep_ut=[%f,%f],..
213         label="",..
214         nzcross=0,..
215         nmode=0,..
216         equations=list()),..
217         doc=list())
218         scs_m_1.objs(4)=scicos_link(..
219         xx=[349.49528;266.69602;266.69602;270.35525;342.80795;342.80795;349.63473],..
220         yy=[565.10704;565.10704;680.99483;680.99483;680.99483;651.89946;651.89946],..
221         id="drawlink",..
222         thick=[0,0],..
223         ct=[5,-1],..
224         from=[3,2,0],..
225         to=[1,1,1])
226         scs_m_1.objs(5)=scicos_block(..
227         gui="OUT_f",..
228         graphics=scicos_graphics(..
229         orig=[398.20616,616.18517],..
230         sz=[20,20],..
231         exprs="1",..
232         pin=6,..
233         pout=[],..
234         pein=[],..
235         peout=[],..
236         gr_i=[],..
237         id="",..
238         in_implicit="E",..
239         out_implicit=[]),..
240         model=scicos_model(..
241         sim="output",..
242         in=-1,..
243         in2=-2,..
244         intyp=-1,..
245         out=[],..
246         out2=[],..
247         outtyp=1,..
248         evtin=[],..
249         evtout=[],..
250         state=[],..
251         dstate=[],..
252         odstate=list(),..
253         rpar=[],..
254         ipar=1,..
255         opar=list(),..
256         blocktype="c",..
257         firing=[],..
258         dep_ut=[%f,%f],..
259         label="",..
260         nzcross=0,..
261         nmode=0,..
262         equations=list()),..
263         doc=list())
264         scs_m_1.objs(6)=scicos_link(..
265         xx=[378.20616;398.20616],..
266         yy=[626.18517;626.18517],..
267         id="drawlink",..
268         thick=[0,0],..
269         ct=[1,1],..
270         from=[1,1,0],..
271         to=[5,1,1])
272         scs_m_1.objs(7)=scicos_block(..
273         gui="CLKOUTV_f",..
274         graphics=scicos_graphics(..
275         orig=[339.49528,505.10704],..
276         sz=[20,30],..
277         exprs="1",..
278         pin=[],..
279         pout=[],..
280         pein=8,..
281         peout=[],..
282         gr_i=[],..
283         id="",..
284         in_implicit=[],..
285         out_implicit=[]),..
286         model=scicos_model(..
287         sim="output",..
288         in=[],..
289         in2=[],..
290         intyp=1,..
291         out=[],..
292         out2=[],..
293         outtyp=1,..
294         evtin=1,..
295         evtout=[],..
296         state=[],..
297         dstate=[],..
298         odstate=list(),..
299         rpar=[],..
300         ipar=1,..
301         opar=list(),..
302         blocktype="d",..
303         firing=[],..
304         dep_ut=[%f,%f],..
305         label="",..
306         nzcross=0,..
307         nmode=0,..
308         equations=list()),..
309         doc=list())
310         scs_m_1.objs(8)=scicos_link(..
311         xx=[349.49528;349.49528],..
312         yy=[565.10704;535.10704],..
313         id="drawlink",..
314         thick=[0,0],..
315         ct=[5,-1],..
316         from=[3,1,0],..
317         to=[7,1,1])
318         model=scicos_model(..
319         sim="csuper",..
320         in=[],..
321         in2=[],..
322         intyp=1,..
323         out=-1,..
324         out2=[],..
325         outtyp=1,..
326         evtin=[],..
327         evtout=1,..
328         state=[],..
329         dstate=[],..
330         odstate=list(),..
331         rpar=scs_m_1,..
332         ipar=[],..
333         opar=list(),..
334         blocktype="h",..
335         firing=[],..
336         dep_ut=[%f,%f],..
337         label="",..
338         nzcross=0,..
339         nmode=0,..
340         equations=list())
341         gr_i=[]
342
343         x=standard_define([3 2],model,[],gr_i)
344     end
345 endfunction
346
347
348 //=========================================================
349 function [X,Y,orpar]=Do_Spline2(N,order,x,y)
350
351     X=[];Y=[];orpar=[];
352     METHOD=getmethod(order);
353
354     if (METHOD=="zero order") then
355         X=x(1);Y=y(1);
356         for i=1:N-1
357             X=[X;x(i);x(i+1);x(i+1)];
358             Y=[Y;y(i);y(i);y(i+1)];
359         end
360         return;
361     end
362     //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
363     if (METHOD=="linear") then
364         X=[];
365         for i=1:N
366             X=[X;x(i)];
367             Y=[Y;y(i)];
368         end
369         return;
370     end
371     //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
372     if (N<20) then
373         NP=4;
374     else
375         if (N<40) then
376             NP=2;
377         else
378             if (N<100) then
379                 NP=1;
380             else
381                 NP=0;
382             end;
383         end;
384     end
385     for i=1:N-1
386         X=[X;linspace(x(i),x(i+1),NP+2)']; // pour tous sauf "linear" et "zero order"
387     end
388     //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
389     if (N>2) & (METHOD=="order 2") then
390         Z=ORDER2(x,y);
391         A=Z(1:N-1);
392         B=Z(N:2*N-2);
393         C=Z(2*N-1:3*N-3);
394
395         for j=1:size(X,"*")
396             for i=N-1:-1:1
397                 if X(j)>=x(i) then,break;end
398             end
399             Y(j)=A(i)*(X(j)-x(i))^2+B(i)*(X(j)-x(i))+C(i);
400         end
401         orpar=matrix(Z,-1,1)
402     end
403     //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
404     if (METHOD=="not_a_knot") then
405         try
406             d = splin(x, y, METHOD);
407             Y = interp(X, x, y, d);
408             orpar=d(:);
409         catch
410             gcf().info_message = "ERROR in SPLINE: "+METHOD;
411         end
412
413     end
414     //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
415     if (METHOD=="periodic") then
416         if y(1)<>y(N) then
417             y(N)=y(1)
418         end
419         try
420             d = splin(x, y,METHOD);
421             Y = interp(X, x, y, d);
422             orpar=d(:);
423         catch
424             gcf().info_message = "ERROR in SPLINE: "+METHOD;
425         end
426     end
427     //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
428     if (METHOD=="monotone" ) then
429         try
430             d = splin(x, y, METHOD);
431             Y = interp(X, x, y, d);
432             orpar=d(:);
433         catch
434             gcf().info_message = "ERROR in SPLINE: "+METHOD;
435         end
436
437     end
438     //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
439     if (METHOD=="fast") then
440         try
441             d = splin(x, y, METHOD);
442             Y = interp(X, x, y, d);
443             orpar=d(:);
444         catch
445             gcf().info_message = "ERROR in SPLINE:  "+METHOD;
446         end
447     end
448     //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
449     if (METHOD=="clamped") then
450         try
451             d = splin(x, y, METHOD,[0;0]);
452             Y = interp(X, x, y, d);
453             orpar=d(:);
454         catch
455             gcf().info_message = "ERROR in SPLINE: "+METHOD;
456         end
457     end
458
459 endfunction
460
461 function METHOD=getmethod(order)
462     select order
463     case 0 then, METHOD="zero order"
464     case 1 then, METHOD="linear"
465     case 2 then, METHOD="order 2"
466     case 3 then, METHOD="not_a_knot"
467     case 4 then, METHOD="periodic"
468     case 5 then, METHOD="monotone"
469     case 6 then, METHOD="fast"
470     case 7 then, METHOD="clamped"
471     end
472 endfunction
473
474
475 function [Z]=ORDER2(x,y)
476     N=size(x,"*")-1;
477     A=zeros(3*N-1,N*3);
478     B=zeros(3*N-1,1);
479     for i=1:N
480         j=3*(i-1)+1;
481         A(j,i+2*N)=1;
482         B(j)=y(i);
483         A(j+1,i)=(x(i+1)-x(i))^2;
484         A(j+1,i+N)=x(i+1)-x(i);
485         A(j+1,i+2*N)=1;
486         B(j+1)=y(i+1);
487     end
488
489     for i=1:N-1
490         j=3*(i-1)+1;
491         A(j+2,i)=2*(x(i+1)-x(i));
492         A(j+2,i+N)=1;
493         A(j+2,i+N+1)=-1;
494     end
495
496     Q=zeros(3*N,3*N);
497     for i=1:N
498         Q(i,i)=4*(x(i+1)-x(i))^2
499         Q(i,i+N)=2*(x(i+1)-x(i))
500         Q(i+N,i)=2*(x(i+1)-x(i))
501         Q(i+N,i+N)=1;
502     end
503
504     At=[Q,A';A,zeros(3*N-1,3*N-1)]
505     Bt=[zeros(3*N,1);B]
506     Zt=At\Bt;
507     Z=Zt(1:3*N,1)
508 endfunction
509 //===================================================
510
511
512
513
514
515