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
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
|
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
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
|
-
-
-
-
-
-
-
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
-
+
+
-
-
-
+
+
+
-
-
+
+
-
+
-
+
-
+
-
-
+
+
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
-
-
-
-
+
+
+
+
-
-
-
-
-
-
+
+
+
+
+
+
-
+
-
+
-
-
+
+
-
+
|
function structure = cbg2ese(system_name, system_type, system_cr, ...
system_args, full_name, full_name_repetition, ...
repetition,...
structure, structure_file,infofilenum)
# Set up globals to count the component inputs and outputs. This relies on
# the named SS (the ports) being in the correct order. Using globals here
# avoids changing the common argument list for all _eqn files for something
# which is only used for named SS components.
system_args, full_name, full_name_repetition, ...
repetition,...
structure, structure_file,infofilenum)
## Set up globals to count the component inputs and outputs. This relies on
## the named SS (the ports) being in the correct order. Using globals here
## avoids changing the common argument list for all _eqn files for something
## which is only used for named SS components.
global local_u_index
global local_y_index
global at_top_level
#
# ######################################
# ##### Model Transformation Tools #####
# ######################################
#
# Matlab function cbg2ese.m
# Acausal bond graph to causal bond graph: mfile format
# Structure matrix [states,nonstates,inputs,outputs,zero_outputs]
# ###############################################################
# ## Version control history
# ###############################################################
# ## $Id$
# ## $Log$
# ## Revision 1.31 1998/09/24 12:57:44 peterg
# ## Now ignores aliasing if no arguments given.
# ##
# ## Revision 1.30 1998/09/02 11:14:23 peterg
# ## Revised to use ordered lists of subsystems and ports
# ##
# ## Revision 1.29 1998/08/25 09:22:34 peterg
# ## Correctely recognises port SSs its now easy -- they are in there own
# ## field
# ##
# ## Revision 1.28 1998/08/25 08:31:42 peterg
# ## Fixed bug - didn't find the ports - use deblank
# ##
# ## Revision 1.27 1998/08/25 07:16:49 peterg
# ## Modified to data struture representation
# ##
# ## Revision 1.26 1998/08/24 14:53:55 peterg
# ## Uses new _cbg structure.
# ##
# ## Revision 1.25 1998/07/28 19:05:12 peterg
# ## Sttill has vector SS port bug?
# ##
# ## Revision 1.24 1998/07/27 10:26:02 peterg
# ## No change - but fixed bug in alias_args
# ##
# ## Revision 1.23 1998/07/08 12:33:51 peterg
# ## Reinstated the infofilenum parameter.
# ##
# ## Revision 1.22 1998/07/04 07:10:27 peterg
# ## Don't evaluate alias if no constitutive relationship or args and write
# ## message.
# ##
# ## Revision 1.21 1998/07/03 18:58:58 peterg
# ## Put arg alias stuff within function alias_args
# ## Called recursively to handle arithmetic expressions
# ##
# ## Revision 1.20 1998/07/03 14:39:09 peterg
# ## Added info messages a bit busy now!
# ##
# ## Revision 1.19 1998/04/12 11:58:19 peterg
# ## Rename port components by changing name_r to [name_r
##
## ######################################
## ##### Model Transformation Tools #####
## ######################################
##
## Matlab function cbg2ese.m
## Acausal bond graph to causal bond graph: mfile format
## Structure matrix [states,nonstates,inputs,outputs,zero_outputs]
## ###############################################################
## ## Version control history
## ###############################################################
## ## $Id$
## ## $Log$
## ## Revision 1.32 1998/11/16 13:01:19 peterg
## ## Fixed problem with repetitions>1 due to new data structures -- now
## ## computes initial next_bond correctly
## ##
## ## Revision 1.31 1998/09/24 12:57:44 peterg
## ## Now ignores aliasing if no arguments given.
## ##
## ## Revision 1.30 1998/09/02 11:14:23 peterg
## ## Revised to use ordered lists of subsystems and ports
## ##
## ## Revision 1.29 1998/08/25 09:22:34 peterg
## ## Correctely recognises port SSs its now easy -- they are in there own
## ## field
## ##
## ## Revision 1.28 1998/08/25 08:31:42 peterg
## ## Fixed bug - didn't find the ports - use deblank
## ##
## ## Revision 1.27 1998/08/25 07:16:49 peterg
## ## Modified to data struture representation
## ##
## ## Revision 1.26 1998/08/24 14:53:55 peterg
## ## Uses new _cbg structure.
## ##
## ## Revision 1.25 1998/07/28 19:05:12 peterg
## ## Sttill has vector SS port bug?
## ##
## ## Revision 1.24 1998/07/27 10:26:02 peterg
## ## No change - but fixed bug in alias_args
## ##
## ## Revision 1.23 1998/07/08 12:33:51 peterg
## ## Reinstated the infofilenum parameter.
## ##
## ## Revision 1.22 1998/07/04 07:10:27 peterg
## ## Don't evaluate alias if no constitutive relationship or args and write
## ## message.
## ##
## ## Revision 1.21 1998/07/03 18:58:58 peterg
## ## Put arg alias stuff within function alias_args
## ## Called recursively to handle arithmetic expressions
## ##
## ## Revision 1.20 1998/07/03 14:39:09 peterg
## ## Added info messages a bit busy now!
## ##
## ## Revision 1.19 1998/04/12 11:58:19 peterg
## ## Rename port components by changing name_r to [name_r
# ##
# ## Revision 1.18 1998/04/11 18:59:16 peterg
# ## at_top_level now global - passed to SS components
# ##
# ## Revision 1.17 1998/04/04 10:47:31 peterg
# ## Uses (coerced) components from _cbg file - _abg not now used here.
# ##
# ## Revision 1.16 1998/03/06 09:38:58 peterg
# ## Now writes correct structure file for multiport C & I
# ##
# ## Revision 1.15 1997/12/16 18:24:33 peterg
# ## Added unknown_input to structure list
# ##
# ## Revision 1.14 1997/08/26 07:51:10 peterg
# ## Now counts the local input and outputs by order of appearence rather
# ## than by port number - it therfore handles ports with bicausality correctely.
# ##
# ## Revision 1.13 1997/05/19 16:45:56 peterg
# ## Fixed ISW bug -- deleted spurious ISW_eqn.m file
# ##
# ## Revision 1.12 1997/04/15 09:17:26 peterg
# ## Added the structure file - contains details of states etc.
# ##
# ## Revision 1.11 1996/12/07 18:20:11 peterg
# ## Replaces null argument by a default and tells user.
# ##
# ## Revision 1.10 1996/12/07 17:37:07 peterg
# ## Now handles numbered SS ports appearing at top level.
# ##
# ## Revision 1.9 1996/12/04 21:49:47 peterg
# ## Compares full-name with empty string (instead of testing for zero
# ## length)
# ##
# ## Revision 1.8 1996/08/30 16:36:08 peter
# ## More info written to ese files.
# ##
# ## Revision 1.7 1996/08/30 11:23:13 peter
# ## Argument substitution implemented.
# ##
# ## Revision 1.6 1996/08/27 08:04:52 peterg
# ## Handles complex components and repetative components.
# ##
# ## Revision 1.5 1996/08/24 15:02:23 peter
# ## Writes `END;' to keep reduce happy.
# ##
# ## Revision 1.4 1996/08/19 09:03:41 peter
# ## Handles repeating components.
# ##
# ## Revision 1.3 1996/08/18 20:08:02 peter
# ## Included additional structure: structure(5) = zero_outputs.
# ##
# ## Revision 1.2 1996/08/08 18:08:11 peter
# ## Sorted out file naming sceme
# ##
# ## Revision 1.1 1996/08/08 15:53:23 peter
# ## Initial revision
# ##
# #############################################################
## ##
## ## Revision 1.18 1998/04/11 18:59:16 peterg
## ## at_top_level now global - passed to SS components
## ##
## ## Revision 1.17 1998/04/04 10:47:31 peterg
## ## Uses (coerced) components from _cbg file - _abg not now used here.
## ##
## ## Revision 1.16 1998/03/06 09:38:58 peterg
## ## Now writes correct structure file for multiport C & I
## ##
## ## Revision 1.15 1997/12/16 18:24:33 peterg
## ## Added unknown_input to structure list
## ##
## ## Revision 1.14 1997/08/26 07:51:10 peterg
## ## Now counts the local input and outputs by order of appearence rather
## ## than by port number - it therfore handles ports with bicausality correctely.
## ##
## ## Revision 1.13 1997/05/19 16:45:56 peterg
## ## Fixed ISW bug -- deleted spurious ISW_eqn.m file
## ##
## ## Revision 1.12 1997/04/15 09:17:26 peterg
## ## Added the structure file - contains details of states etc.
## ##
## ## Revision 1.11 1996/12/07 18:20:11 peterg
## ## Replaces null argument by a default and tells user.
## ##
## ## Revision 1.10 1996/12/07 17:37:07 peterg
## ## Now handles numbered SS ports appearing at top level.
## ##
## ## Revision 1.9 1996/12/04 21:49:47 peterg
## ## Compares full-name with empty string (instead of testing for zero
## ## length)
## ##
## ## Revision 1.8 1996/08/30 16:36:08 peter
## ## More info written to ese files.
## ##
## ## Revision 1.7 1996/08/30 11:23:13 peter
## ## Argument substitution implemented.
## ##
## ## Revision 1.6 1996/08/27 08:04:52 peterg
## ## Handles complex components and repetative components.
## ##
## ## Revision 1.5 1996/08/24 15:02:23 peter
## ## Writes `END;' to keep reduce happy.
## ##
## ## Revision 1.4 1996/08/19 09:03:41 peter
## ## Handles repeating components.
## ##
## ## Revision 1.3 1996/08/18 20:08:02 peter
## ## Included additional structure: structure(5) = zero_outputs.
## ##
## ## Revision 1.2 1996/08/08 18:08:11 peter
## ## Sorted out file naming sceme
## ##
## ## Revision 1.1 1996/08/08 15:53:23 peter
## ## Initial revision
## ##
## #############################################################
system_name, system_type, full_name,
pc = "%";
# Set up the names corresponding to the structure matrix.
## Set up the names corresponding to the structure matrix.
structure_name = [
"state ",
"nonstate ",
"input ",
"output ",
"zero_output ",
"unknown_input"];
# Are we at the top level of the heirarchy?
## Are we at the top level of the heirarchy?
at_top_level = strcmp(full_name, "");
# Create the (full) system name
## Create the (full) system name
if at_top_level
full_name = system_name;
full_name_repetition = system_name;
system_type = system_name;
else
full_name = [full_name, "_", system_name];
full_name_repetition = [full_name_repetition, ...
"_", system_name, "_", num2str(repetition)];
end;
cbg_name = [full_name, "_cbg"];
if exist(cbg_name)~=2 # Return if cbg file doesn't exist
disp([cbg_name, " does not exist"]);
return
end;
# Setup files
## Setup files
ese_name = [full_name_repetition, "_ese.r"];
ese_file = fopen(ese_name, "w");
fprintf(ese_file, "\n%s%s Equation file for system %s (file %s)\n", ...
pc, pc, full_name_repetition, ese_name);
fprintf(ese_file, "%s%s Generated by MTT\n\n", pc, pc);
# Evaluate the system function to get the bonds
## Evaluate the system function to get the bonds
eval(["CBG = ", cbg_name, ";"]);
#eval(["[bonds,status,system_type,components] = ", cbg_name, ";"]);
# abg_name = [system_type, "_abg"];
# cmp_name = [system_type, "_cmp"];
# alias_name = [system_type, "_alias"];
# No longer needed - cbg now has components:
# eval(["[junk,components]=", abg_name, ";"]);
# Find number of bonds
##eval(["[bonds,status,system_type,components] = ", cbg_name, ";"]);
## abg_name = [system_type, "_abg"];
## cmp_name = [system_type, "_cmp"];
## alias_name = [system_type, "_alias"];
## No longer needed - cbg now has components:
## eval(["[junk,components]=", abg_name, ";"]);
## Find number of bonds
[n_bonds,columns] = size(CBG.bonds);
if (columns ~= 2)&(n_bonds>0)
error("Incorrect bonds matrix: must have 2 columns");
endif;
# # Find number of components
# [n_components,columns] = size(components);
# n_components = n_components
## ## Find number of components
## [n_components,columns] = size(components);
## n_components = n_components
# Set up the first dummy bond number - needed for repetative components
# next_bond = max(max(abs(components)))+1;
next_bond = n_bonds+1;
# Set up the counters for the labelled SS. These are, by definition,
# encountered first and so the counters will not be messed up by subsystems.
## Set up the first dummy bond number - needed for repetative components
## next_bond = max(max(abs(components)))+1;
next_bond = n_bonds+1;
## Set up the counters for the labelled SS. These are, by definition,
## encountered first and so the counters will not be messed up by subsystems.
local_u_index = 0;
local_y_index = 0;
if (length(system_args)==0)
mtt_info(sprintf("No arguments given so no argument aliasing done for system %s(%s)",\
system_name,system_type), infofilenum);
AliasingArguments=0;
else
AliasingArguments=1;
endif;
if (length(system_cr)==0)
mtt_info(sprintf("No cr given so no cr aliasing done for system %s(%s)",\
system_name,system_type), infofilenum);
AliasingCRs=0;
else
AliasingCRs=1; endif;
fields=["ports";"subsystems"]; # Do for both ports and subsystems -
# ports first
## ports first
lists=["portlist";"subsystemlist"];
for i=1:2
field=deblank(fields(i,:));
list=deblank(lists(i,:));
if struct_contains(CBG,list);
eval(["namelist=CBG.",list,";"]); # List of ports/subsystems
[N,M]=size(namelist); # Number of ports/subsystems
|
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
|
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
|
-
+
-
+
-
+
|
if AliasingArguments # Alias the args list if appropriate
message = sprintf("\tfor component %s (%s) within %s",\
comp_name,subsystem.type,full_name);
if struct_contains(CBG,"alias")
subsystem.arg = alias_args(subsystem.arg,CBG.alias,";",message,infofilenum)
endif;
endif;
if AliasingCRs # Alias the CR list if appropriate
message = sprintf("\tfor component %s (%s) within %s",\
comp_name,subsystem.type,full_name);
if struct_contains(CBG,"alias")
subsystem.cr = alias_args(subsystem.cr,CBG.alias,";",message,infofilenum)
endif;
endif;
# Substitute positional ($1 etc) arguments
## Substitute positional ($1 etc) arguments
subsystem.cr = subs_arg(subsystem.cr,system_cr, ...
"lin",full_name,subsystem.type,comp_name,infofilenum);
subsystem.arg = subs_arg(subsystem.arg,system_args, ...
"1",full_name,subsystem.type,comp_name,infofilenum);
# change name of 0 and 1 components -- matlab doesn't like numbers here
## change name of 0 and 1 components -- matlab doesn't like numbers here
if strcmp(subsystem.type,"0")
subsystem.type = "zero";
endif;
if strcmp(subsystem.type,"1")
subsystem.type = "one";
endif;
|
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
|
-
+
-
+
-
+
-
+
-
+
-
+
-
-
+
+
-
+
-
+
-
+
-
-
+
+
|
new_bonds = [next_bond:next_bond+port_pairs-1];
next_bond = next_bond+port_pairs;
bond_list(2:2:ports) = new_bonds;
endif;
endif;
# Invoke the appropriate equation-generating procedure
## Invoke the appropriate equation-generating procedure
name_r = full_name_repetition;
eqn_name = [subsystem.type, "_eqn"]
if exist(eqn_name)~=2 # Try a compound component
if exist(eqn_name)~=2 ## Try a compound component
disp("---PUSH---"); bond_list, comp_name, subsystem.type
structure = cbg2ese(comp_name, subsystem.type, subsystem.cr, subsystem.arg, ...
full_name, full_name_repetition, ...
k, structure, structure_file, infofilenum);
# Link up the bonds
## Link up the bonds
fprintf(ese_file, ...
"\n\t%s Equations linking up subsystem %s (%s)\n\n", ...
pc, comp_name, subsystem.type);
name_comp_name = sprintf("%s_%s_%d", ...
full_name_repetition, comp_name, k);
printf("\n\t%s Equations linking up subsystem %s (%s)\n\n",\
pc, comp_name, subsystem.type);
bond_list
u_index = 0; y_index = 0; # Count the inputs and outputs
u_index = 0; y_index = 0; ## Count the inputs and outputs
for port_number=1:length(bond_list)
# Effort
## Effort
if comp_bonds(port_number,1)==1 # Source
u_index = u_index + 1;
fprintf(ese_file, "%s_MTTu%d := %s;\n", ...
name_comp_name, u_index, varname(name_r, ...
bond_list(port_number),1));
else # Sensor
y_index = y_index + 1;
fprintf(ese_file, "%s := %s_MTTy%d;\n", ...
varname(name_r, ...
bond_list(port_number),1), name_comp_name, y_index);
end;
# Flow
## Flow
if comp_bonds(port_number,2)==-1 # Source
u_index = u_index + 1;
fprintf(ese_file, "%s_MTTu%d := %s;\n", ...
name_comp_name, u_index, varname(name_r, ...
bond_list(port_number),-1));
else # Sensor
y_index = y_index + 1;
fprintf(ese_file, "%s := %s_MTTy%d;\n", ...
varname(name_r, ...
bond_list(port_number),-1), name_comp_name, y_index);
end;
end;
disp("---POP---");
else # its a simple component
fprintf(ese_file, "\n\t%s Equations for component %s (%s), repetition %d\n\n", ...
pc, comp_name, subsystem.type,k);
# # Is it a named port? (Name begins with [)
# Named_Port = (comp_name(1)=="[");
## # Is it a named port? (Name begins with [)
## Named_Port = (comp_name(1)=="[");
if strcmp(field,"ports") #Add [ to start of name
name_r = ["[" name_r];
endif;
# Save the current structure
## Save the current structure
old_structure = structure;
# Generate the simple component equations
## Generate the simple component equations
eval(["structure = ", ...
eqn_name, ...
"(name_r,bond_list,comp_bonds, ...
direction,subsystem.cr,subsystem.arg,structure,ese_file);" ]);
# If structure has changed, write info to structure file.
## If structure has changed, write info to structure file.
structure_change = structure-old_structure;
# The following line is to avoid probs with multiport C or I
# it needs changing to handle multi ports correctly
## The following line is to avoid probs with multiport C or I
## it needs changing to handle multi ports correctly
structure_changes = sum(structure_change);
structure_change = structure_change>zeros(size(structure_change));
if structure_changes>0
which_indices = getindex(structure_change,1);
which_indices = which_indices(:,2)';
for which_index=which_indices
|