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
|
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
|
-
+
+
+
+
+
|
## -*-octave-*-
function [bonds,components,n_vector_bonds] = \
ibg2abg(name,bonds,infofile,errorfile)
## write useful quantity of data to log
struct_levels_to_print = 5;
struct_levels_to_print = 4;
###############################################
## Extract data from bonds structure (ibg.m) ##
###############################################
disp("--- Started extracting bonds data ---")
## unique number for each (sub-) bond
bond_number = 0;
## extract data from each bond
for [bond, bond_name] = bonds
i = str2num(split(bond_name, "bond")(2,:))
## extract type and name of component at each end
head.name = deblank(split(bond.head.component, ":")(2,:));
head.type = deblank(split(bond.head.component, ":")(1,:));
tail.name = deblank(split(bond.tail.component, ":")(2,:));
tail.type = deblank(split(bond.tail.component, ":")(1,:));
## capture directional info
head.end = "head";
tail.end = "tail";
## assign vector bond index
head.index = i;
tail.index = i;
## determine if internal port (and fix name) or subsystem
if (strcmp(head.type, "SS") & (index(head.name, "[") == 1))
|
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
|
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
|
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
elseif (head.n_sub_bonds > 1)
mtt_info(sprintf("Vector port %s matching %s",
head.label, tail.label), infofile);
endif
endif
## assign unique number and causality to each sub-bond
if (! (head.is_junction | tail.is_junction))
for sub_bond = 1:head.n_sub_bonds
++bond_number;
eval(sprintf("head.bond%i.index = %i", sub_bond, +bond_number));
eval(sprintf("tail.bond%i.index = %i", sub_bond, -bond_number));
## effort causality
if (strcmp(bond.causality.effort, "head"))
causality(bond_number, 1) = +1;
head.causality = +1;
elseif (strcmp(bond.causality.effort, "tail"))
causality(bond_number, 1) = -1;
head.causality = -1;
else
causality(bond_number, 1) = 0;
head.causality = 0;
endif
## flow causality
if (strcmp(bond.causality.flow, "head"))
causality(bond_number, 2) = -1;
tail.causality = -1;
elseif (strcmp(bond.causality.flow, "tail"))
causality(bond_number, 2) = +1;
tail.causality = +1;
else
causality(bond_number, 2) = 0;
tail.causality = 0;
endif
eval(sprintf("head.bond%i.port = deblank(head.sub_bonds(%i,:))",
sub_bond, sub_bond));
eval(sprintf("tail.port%i.port = deblank(tail.sub_bonds(%i,:))",
sub_bond, sub_bond));
endfor
for sub_bond = 1:head.n_sub_bonds
++bond_number;
eval(sprintf("head.bond%i.index = %i", sub_bond, +bond_number));
eval(sprintf("tail.bond%i.index = %i", sub_bond, -bond_number));
## effort causality
if (strcmp(bond.causality.effort, "head"))
causality(bond_number, 1) = +1;
head.causality = +1;
elseif (strcmp(bond.causality.effort, "tail"))
causality(bond_number, 1) = -1;
head.causality = -1;
else
causality(bond_number, 1) = 0;
head.causality = 0;
endif
## flow causality
if (strcmp(bond.causality.flow, "head"))
causality(bond_number, 2) = -1;
tail.causality = -1;
elseif (strcmp(bond.causality.flow, "tail"))
causality(bond_number, 2) = +1;
tail.causality = +1;
else
causality(bond_number, 2) = 0;
tail.causality = 0;
endif
eval(sprintf("head.bond%i.port = deblank(head.sub_bonds(%i,:))",
sub_bond, sub_bond));
eval(sprintf("tail.port%i.port = deblank(tail.sub_bonds(%i,:))",
sub_bond, sub_bond));
endfor
else
## save bond causality data within junction for later use
head.causality.effort = bond.causality.effort;
tail.causality.effort = bond.causality.effort;
head.causality.flow = bond.causality.flow;
tail.causality.flow = bond.causality.flow;
endif
## copy bond data to component structure (comp_s)
if (head.is_port)
S = sprintf("comp_s.port.%s", head.name);
eval(sprintf("%s.vec_bond%i = head", S, i));
eval(sprintf("%s.name = '%s'", S, head.name));
else
|
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
|
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
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
|
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
+
-
+
-
+
|
#############################
## Expand vector junctions ##
#############################
if (struct_contains(comp_s, "comp"))
for [comp, comp_name] = comp_s.comp
if (strcmp(comp.type, "0") | strcmp(comp.type, "1"))
n_ports = size(struct_elements(comp), 1) - 2;
## subtracted 2 for name and type
n_named_ports = 0;
## get labelled ports
for [vec_bond, vec_bond_name] = comp
if (! exist("vec_bond.label"))
vec_bond.label = "[]";
endif
if (index(vec_bond_name, "vec_bond") == 1)
if (! strcmp(vec_bond.label, "[]"))
n_named_ports += 1;
port_name = vec_bond.label;
endif
endif
eval(sprintf("comp.%s = vec_bond", vec_bond_name));
endfor
## attach labels to unlabelled ports
if (n_named_ports == 0)
for [vec_bond, vec_bond_name] = comp
if (index(vec_bond_name, "vec_bond") == 1)
vec_bond.label = "in";
endif
eval(sprintf("comp.%s = vec_bond", vec_bond_name));
endfor
elseif (n_named_ports == 1)
mtt_info(sprintf("Defaulting all ports on junction %s to %s",
comp_name, port_name), infofile);
for [vec_bond, vec_bond_name] = comp
if (index(vec_bond_name, "vec_bond") == 1)
vec_bond.label = port_name;
endif
eval(sprintf("comp.%s = vec_bond", vec_bond_name));
endfor
elseif ((n_named_ports != 0) && (n_named_ports != n_ports))
mtt_error(sprintf("Junction must have 0,1 or %i port labels",
n_ports), errorfile);
endif
# eval(sprintf("comp.%s.%s = vec_bond", comp_name, vec_bond_name))
## expand vector bonds
for [vec_bond, vec_bond_name] = comp
if (index(vec_bond_name, "vec_bond") == 1)
[sub_bonds, n_sub_bonds] = split_port(vec_bond.label)
for sub_bond = 1:n_sub_bonds
++bond_number;
S = sprintf("comp_s.comp.%s.%s", comp_name, \
vec_bond_name);
## FIXME: multiply bond_number by direction in next line
eval(sprintf("%s.bond%i.index = %i",
S, sub_bond, bond_number));
## FIXME: do causality
if (strcmp(vec_bond.end, "head"))
direction = +1;
else
direction = -1;
endif
eval(sprintf("vec_bond.bond%i.index = %i",
sub_bond, bond_number*direction));
## causality
if (strcmp(vec_bond.causality.effort, "head"))
causality(bond_number, 1) = +1;
elseif (strcmp(vec_bond.causality.effort, "tail"))
causality(bond_number, 1) = -1;
else
causality(bond_number, 1) = 0;
endif
if (strcmp(vec_bond.causality.flow, "head"))
causality(bond_number, 2) = -1;
elseif (strcmp(vec_bond.causality.flow, "tail"))
causality(bond_number, 2) = +1;
else
causality(bond_number, 2) = 0;
endif
endfor
endif
eval(sprintf("comp.%s = vec_bond", vec_bond_name));
endfor
endif
eval(sprintf("comp_s.comp.%s = comp", comp_name));
endfor
endif
## FIXME: add sub_bonds to junctions
bonds = causality;
#################################
## Map component data to cmp.m ##
#################################
## count number of components
if (struct_contains(comp_s, "comp"))
n_comps = size(struct_elements(comp_s.comp), 1);
n_comps = size(struct_elements(comp_s.comp), 1)
else
n_comps = 0;
endif
## count number of internal ports
if (struct_contains(comp_s, "port"))
n_ports = size(struct_elements(comp_s.port), 1);
n_ports = size(struct_elements(comp_s.port), 1)
else
n_ports = 0;
endif
## read data from _cmp.m
for cmp = 1:(n_comps + n_ports)
[this_type, this_name] = eval(sprintf("%s_cmp(%i)", name, cmp));
## determine if internal port (and fix name) or subsystem
if (strcmp(this_type, "SS") & (index(this_name, "[") == 1))
comp_or_port = "port";
this_name = mtt_strip_name(this_name);
this_name = mtt_strip_name(this_name)
else
comp_or_port = "comp";
endif
eval(sprintf("comp_s.%s.%s.index = cmp", comp_or_port, this_name));
endfor
disp("--- Finished reading _cmp.m ---");
comp_s
##########################
## Write n_vector_bonds ##
##########################
for [comp, comp_name] = comp_s.comp
i = 0;
for [val, key] = comp
if (index(key, "vec_bond") == 1)
|