summaryrefslogtreecommitdiff
path: root/macros/Misc/AUTOMAT.sci
blob: 6ef82717c11eb89e568c75cd21a2fc6fac329a3b (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
//  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]=AUTOMAT(job,arg1,arg2)
    x=[];
    y=[];
    typ=[];
    select job
    case "set" then
        x=arg1;
        graphics=arg1.graphics;
        exprs=graphics.exprs
        model=arg1.model;
        ipar=model.ipar;
        NMode=ipar(1)
        NX=ipar(3)
        while %t do
            CX="C1";
            MSG0="''Jump from Mode ";
            MSG2=":[..;M_final(Guard=In(";
            MSG3=").i);..]''"
            MSG=MSG0+"1"+MSG2+"1"+MSG3;
            VEC="''mat'',[-1,1]";
            for i=2:NMode
                CX=CX+","+"C"+string(i);
                MSG=MSG+";"+MSG0+string(i)+MSG2+string(i)+MSG3;
                VEC=VEC+","+"''mat'',[-1,1]";
            end
            //===========================================
            GTV="[ok,NMode,Minitial,NX,X0,XP,"+CX+",exprs]=scicos_getvalue(''Set Finite state machine model'',..
            [''Number (finite-state) Modes'';''Initial Mode'';''Number of continuous-time states'';''Continuous-time states intial values'';''Xproperties of continuous-time states in each Mode'';"+MSG+"],..
            list(''vec'',1,''vec'',1,''vec'',1,''mat'',[-1,-1],''mat'',[-1,-1],"+VEC+"),exprs)"
            execstr(GTV);
            if ~ok then
                break,
            end
            NMode_old=size(exprs,"*")-5;//-number of fileds before CX
            ModifEncore=%f;

            if (NMode_old>NMode) then
                exprs(NMode+6:NMode_old+5)=[];// number of fileds
                ModifEncore=%t;
            end
            if (NMode_old<NMode) then
                exprs(NMode_old+6:NMode+5)=exprs(NMode_old+4);// number of fileds
                ModifEncore=%t;
            end
            if (NX<>size(X0,"*")) then
                messagebox("the size of intial continuous-time states should be NX="+string(NX),"modal","error");
                ModifEncore=%t;
            end

            [rXP,cXP]=size(XP)
            if cXP<>NX then
                messagebox("Xproperty matrix is not valid: it should have NX="+string(NX)+" columns","modal","error");
                ModifEncore=%t;
            elseif ((rXP<>NMode) & (rXP>1))
                messagebox("Xproperty matrix is not valid: it should have NMode="+string(NMode)+" or 1 row(s)","modal","error");
                ModifEncore=%t;
            elseif (rXP==1)
                for i=1:NMode-1
                    XP=[XP;XP(1,:)];// xproprties are identical in modes.
                end
            end

            if (NMode_old==NMode)&(~ModifEncore) then
                XP=matrix(XP',NMode*NX,1);// put XP in column vector to be stocked in ipar
                ipar=[NMode;Minitial;NX;XP];
                rpar=matrix(X0,NX,1);// put X0 in a column vector;
                INP=ones(NMode,1);
                if NX>0 then
                    OUT=[2;2*NX];
                else
                    OUT=[2];
                end
                MaxModes=1;
                nzcross=0;
                for i=1:NMode
                    Ci=evstr(exprs(5+i));// number of fileds
                    ipar=[ipar;Ci];
                    INP(i,1)=2*NX+length(Ci);
                    if (nzcross<length(Ci)) then
                        nzcross=length(Ci);
                    end
                    if (MaxModes<max(Ci)) then
                        MaxModes=max(Ci);
                        imax=i;
                    end
                end

                if MaxModes>NMode then
                    messagebox(["Number of Modes should be "+string(MaxModes);..
                    "A destination Mode in Mode#"+string(imax)+"''s targets is invalid!"],"modal","error");
                    ModifEncore=%t;
                end
                if MaxModes<NMode then
                    messagebox(["There is an unused Mode or the Number of Modes should be "+string(MaxModes)],"modal","error");
                    ModifEncore=%t;
                end
            end
            if ~ModifEncore then
                [model,graphics,ok]=check_io(model,graphics,INP,OUT,[],[1])
                if ~ok then
                    break,
                end
                model.nzcross=nzcross;
                model.state=ones(2*NX,1);
                graphics.gr_i(1)(1)="txt=[''Automaton'';''nM="+string(NMode)+",nX="+string(NX)+"''];"
                graphics.exprs=exprs;
                x.graphics=graphics;
                model.ipar=ipar;
                model.rpar=rpar;
                x.model=model;
                break
            end
        end;//while
        //-----------------------------------------------
    case "define" then
        NMode=2; //number of Modes
        Minitial=1; //initial Mode
        NX=1; //number of states (NX is identical for all Modes)
        X0=[0.0]
        XP=[1;1]//xproperties for each Mode
        C1=[2];//final_Mode after Jump du to in(2*Nx+i)
        C2=[1];
        exprs=[string(NMode);string(Minitial);string(NX);sci2exp(X0);sci2exp(XP);sci2exp(C1);sci2exp(C2)];
        ipar=[NMode;Minitial;NX;XP;C1;C2];
        rpar=[X0];

        model=scicos_model()
        model.sim=list("automat",10004)
        model.in=[2*NX+1;2*NX+1];//number of Modes
        model.out=[2;2*NX];// [Mode;(x,xd)]
        model.state=ones(2*NX,1);// [x;xd]
        model.nzcross=1;// max(taille_zc(Mode_i))
        model.blocktype="c";
        model.evtout=1
        model.firing=-1;
        model.dep_ut=[%f %t];
        model.ipar=ipar;
        model.rpar=rpar;

        gr_i=[]

        x=standard_define([4 2],model,exprs,gr_i);
    end
endfunction