1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
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
|
-
-
+
+
+
+
+
|
#! /bin/sh
######################################
##### Model Transformation Tools #####
######################################
# Bourne shell script: dae_r2m
# Reduce DAE to simulab DAE
# P.J.Gawthrop 14 June 1991, 12 Jan 1994, April 1994, Jan 95.
# Reduce DAE to OCTAVE (DASSL) DAE
# P.J.Gawthrop 14 June 1991, 12 Jan 1994, April 1994, Jan 95, Aug 97.
# Copyright (c) P.J.Gawthrop 1991, 1994, 1995, 1996
###############################################################
## Version control history
###############################################################
## $Id$
## $Log$
# Revision 1.6 1997/08/28 14:07:16 peterg
# Changed MTTDx to MTTDeriv
#
# Revision 1.5 1996/08/24 14:27:29 peter
# Global parameters.
#
## Revision 1.4 1996/08/18 20:02:21 peter
## Include zero outputs.
##
## Revision 1.3 1996/08/16 13:10:53 peter
|
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
|
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
|
+
+
+
+
-
+
+
+
+
-
+
-
+
-
-
-
-
-
-
-
-
-
-
-
+
-
-
+
+
-
+
-
+
+
+
+
+
+
-
+
-
+
-
+
|
#Inform user
echo Creating $1_dae.m
echo Creating $1_daeo.m
# Remove the old log file
rm -f dae_r2m.log
# Remove temp. files
rm -f $1_dae.m?
rm -f $1_daeo.m?
# Use reduce to accomplish the transformation
reduce >dae_r2m.log << EOF
$SYMBOLIC >dae_r2m.log << EOF
%Read the reduce definitions file
in "$1_def.r";
%Read the reduce DAE file
in "$1_dae.r";
% Matrix output function
in"$MTTPATH/trans/matlab_matrix.r";
%Set up the number of argument variables to zero in case the user has forgotten
MTTNVar := 0;
%Read the parameter file
in "$1_sympar.r";
%%in "$1_sympar.r";
ON NERO; % Suppress zero elements
%% ON NERO; % Suppress zero elements
%Define the common part of the functions.
PROCEDURE common;
BEGIN
IF MTTNvar>0 THEN
BEGIN
write "% Set the parameters";
write "global ...;;";
FOR i := 1:MTTNvar DO
BEGIN
IF i<MTTNvar THEN write MTTVar(i,1), " ..."
ELSE write MTTVar(i,1), ";"
END;
END;
write "% Read in the input";
write "u = $1_input(t)";
write "% Read in the arguments";
write "$1_args";
% write "% Read in the arguments";
% write "$1_args";
write "% Set up the State variables";
FOR i := 1:MTTNx DO
BEGIN
write "MTTx", i, " = MTTx(", i, ");";
write "mttx", i, " = mttx(", i, ");";
END;
write "% Set up the non-state variables";
FOR i := 1:MTTNz DO
BEGIN
write "MTTz", i, " = MTTx(", MTTNx+ i, ");";
write "mttz", i, " = mttx(", MTTNx+ i, ");";
END;
write "% Set up the non-state derivatives";
FOR i := 1:MTTNz DO
BEGIN
write "mttdz", i, " = mttx(", MTTNx+MTTNz+ i, ");";
END;
write "% Set up the internal inputs";
FOR i := 1:MTTNz DO
FOR i := 1:MTTNyz DO
BEGIN
write "MTTui", i, " = MTTx(", MTTNx+MTTz+i, ");";
write "mttui", i, " = mttx(", MTTNx+2*MTTNz+i, ");";
END;
write "% Set up the Input variables";
IF MTTNu>0 THEN
FOR i := 1:MTTNu DO
BEGIN
write "MTTu", i, " = u(", i, ");";
write "mttu", i, " = u(", i, ");";
END;
END;
% Firstly do the residual = f(dx,x,t) function.
% MTTGx and u are the derivatives of z wrt x and u respectively
|
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
+
+
+
+
+
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
+
-
-
-
+
-
-
-
-
+
-
-
+
-
-
-
-
-
-
-
-
+
-
-
-
+
+
-
|
BEGIN
uj := MTTu(j,1);
FOR i := 1:MTTNz DO
MTTGu(i,j) := df(MTTZ(i,1), uj, 1);
END;
END;
IF MTTNz=0 THEN
BEGIN
MTTGx := 0;
MTTGu := 0;
MTTz := 0;
END;
OUT "$1_dae.m";
write "function residual = $1_dae(mttderiv0,mttx,t);";
OUT "$1_dae.m1";
write "function residual = $1_dae(mttx,deriv0,t);";
write "% residuals = $1_dae(dx,x,t);";
write "%DAE in DASSL $1;;";
write "%File $1_dae.m;;";
write "%Generated by MTT;;";
common();
SHUT "$1_dae.m1";
GENTRANOUT "$1_dae.m3";
write "% State derivative";
IF MTTNX>0 THEN
BEGIN
MTT_Matrix := MTTdX$
MTT_Matrix_name := "deriv"$
MTT_Matrix_n := MTTNx$
MTT_Matrix_m := 1$
matlab_Matrix()$
GENTRAN deriv := mtt_matrix$
END;
IF MTTNYz>0 THEN
BEGIN
MTT_Matrix := MTTYz$
MTT_Matrix_name := "MTTYz"$
MTT_Matrix_n := MTTNYz$
MTT_Matrix_m := 1$
matlab_Matrix()$
GENTRAN MTTYz := mtt_matrix$
END;
if MTTNz>0 THEN
BEGIN
MTT_Matrix := MTTz$
MTT_Matrix_name := "MTTz"$
MTT_Matrix_n := MTTNz$
MTT_Matrix_m := 1$
matlab_Matrix()$
GENTRAN MTTz := mtt_matrix$
MTT_Matrix := MTTGu$
MTT_Matrix_name := "MTTGu"$
MTT_Matrix_n := MTTNz$
MTT_Matrix_m := 1$
matlab_Matrix()$
GENTRAN MTTGu := mtt_matrix$
MTT_Matrix := MTTGu$
MTT_Matrix_name := "MTTGx"$
MTT_Matrix_n := MTTNz$
MTT_Matrix_m := 1$
matlab_Matrix()$
GENTRAN MTTGx := mtt_matrix$
END;
SHUT "$1_dae.m3";
OUT "$1_dae.m4";
write "% Set up the non-state derivatives";
FOR i := 1:MTTNz DO
BEGIN
write "mttdz", i, " = mttderiv0(", MTTNx+ i, ");";
END;
% write "% Set up the non-state derivatives";
% FOR i := 1:MTTNz DO
% write "mttdz", i, " = mttderiv0(", MTTNx+ i, ");";
% END;
write "j = 0;";
%Residual has 3 parts as follows:
%Part 1: dx/dt - dx_0/dt
if MTTNx>0 THEN
BEGIN
write "for i=1:", MTTNx, ";;";
write " j = j+1";
write " residual(j) = deriv(i)-deriv0(j);";
write "end;";
END;
%Part 2: dz/dt - dz_0/dt
IF MTTNz>0 THEN
BEGIN
if MTTNx>0 THEN
write "mttdzx = mttgx*mttdx"
ELSE
write "mttdzx = zeros(", MTTNz, ",1);";
if MTTNu>0 THEN
write "mttdzu = mttgu*mttdu"
ELSE
write "mttdzu = zeros(", MTTNz, ",1);";
write "mttdz = mttdzx + mttdzu";
write "for i=1:", MTTNz, ";;";
write " j = j+1";
write " residual(j) = mttdz(i)-mttderiv0(j);";
write "end;";
END;
%Part 3: v=0 (the algebraic bits)
IF MTTNyz>0 THEN
BEGIN
write "for i=1:", MTTNyz, ";;";
write " j = j+1";
write " residual(j) = mttyz(i)";
write "end;";
END;
SHUT "$1_dae.m4";
% Now do the y = g(x,t) function.
OUT "$1_daeo.m1";
write "function mtty = $1_daeo(mttx,t);";
write "% mtty = $1_daeo(mttx,t);";
write "%DAE in Simulab form for system $1;;";
write "%File $1_daeo.m;;";
write "%Generated by MTT;;";
SHUT "$1_daeo.m1";
OUT "$1_daeo.m3";
common();
%Fortran switches - one line expressions
OFF echo;
ON fort$
cardno!* := 1$
fortwidth!* := 100$
fortwidth!* := 500$
OFF period$
Mttderiv := MTTdX;
IF MTTNz>0 THEN
MTTGx := MTTGx;
MTTy := MTTy;
OFF fort;
write "for i=1:", MTTNx, ";;";
write " residual(i) = mttderiv(i)-mttderiv0(i);";
write "end;";
IF MTTNz>0 THEN
BEGIN
write "dxz = MTTGx*Mttderiv";
write "for i=1:", MTTNz, ";;";
write " residual(i+", MTTNx, ") = dxz(i)-mttderiv0(i+", MTTNx,");";
write "end;";
END;
SHUT "$1_dae.m";
SHUT "$1_daeo.m3";
OFF fort;
EOF
% Now do the y = g(x,t) function.
OUT "$1_daeo.m";
# Create the globals
write "function MTTy = $1_daeo(MTTx,t);";
write "% dX = $1_daeo(MTTx,t);";
write "%DAE in Simulab form for system $1;;";
write "%File $1_daeo.m;;";
sympar2global_txt2m $1> $1_dae.m2
write "%Generated by MTT;;";
sympar2global_txt2m $1> $1_daeo.m2
common();
%Fortran switches - one line expressions
OFF echo;
ON fort$
cardno!* := 1$
fortwidth!* := 100$
OFF period$
# Put together the pieces
MTTy := MTTy;
SHUT "$1_daeo.m";
cat $1_dae.m?>$1_dae.m
cat $1_daeo.m?>$1_daeo.m
|