Differences From Artifact [29966c0f0c]:

To Artifact [1e9b2c92d0]:


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
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








-
-
-
+
+
+














+
+
+







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.
global local_u_index
global local_y_index
global at_top_level
  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.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.
% %%
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
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







-
+



-
+



-
-
-
-
-
-
+
+
+
+
+
+



-
+







-
+

-
+



-
-
-
+
+
+


-
+




-
-
+
+

-
+

-
+


-
-
-
-
+
+
+
+


-
+




-
+







% %%
% %% 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 = '%';
pc = "%";

% Set up the names corresponding to the structure matrix.
structure_name = [
  'state        ',
  'nonstate     ',
  'input        ',
  'output       ',
  'zero_output  ',
  'unknown_input'];
  "state        ",
  "nonstate     ",
  "input        ",
  "output       ",
  "zero_output  ",
  "unknown_input"];

	
% Are we at the top level of the heirarchy?
at_top_level = strcmp(full_name, '');
at_top_level = strcmp(full_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 = [full_name, "_", system_name];
  full_name_repetition = [full_name_repetition, ...
	'_', system_name, '_', num2str(repetition)];
	"_", system_name, "_", num2str(repetition)];
end;


% $$$ full_name_type = [full_name, '_', system_type];
% $$$ cbg_name = [full_name_type, '_cbg'];
cbg_name = [full_name, '_cbg'];
% $$$ full_name_type = [full_name, "_", system_type];
% $$$ cbg_name = [full_name_type, "_cbg"];
cbg_name = [full_name, "_cbg"];
% Return if cbg file doesn't exist
if exist(cbg_name)~=2
  disp([cbg_name, ' does not exist']);
  disp([cbg_name, " does not exist"]);
  return
end;

% Setup files
ese_name = [full_name_repetition, '_ese.r'];
ese_file = fopen(ese_name, 'w');
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', ...
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);
fprintf(ese_file, "%s%s Generated by MTT\n\n", pc, pc);

% Evaluate the system function to get the bonds
  eval(['[bonds,status,system_type,components] = ', cbg_name, ';']);
  abg_name = [system_type, '_abg'];
  cmp_name = [system_type, '_cmp'];
  alias_name = [system_type, '_alias'];
  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, ';']);
%  eval(["[junk,components]=", abg_name, ";"]);

  % Find number of bonds
  [n_bonds,columns] = size(bonds);
  if (columns ~= 2)&(n_bonds>0)
    error('Incorrect bonds matrix: must have 2 columns');
    error("Incorrect bonds matrix: must have 2 columns");
  end;

  % Find number of components
  [n_components,columns] = size(components);
  n_components = n_components
  
  % Set up the first dummy bond number - needed for repetative components
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
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







-
+

-
+














-
+

-
+


-
-
+
+

-
-
+
+







-
-
+
+







  for i = 1:n_components
    comp = nozeros(components(i,:));
    bond_list = abs(comp);
    direction = sign(comp)'*[1 1];
    % Convert from arrow orientated to component orientated causality
    comp_bonds = bonds(bond_list,:).*direction;

disp('---- Component ---');    
disp("---- Component ---");    
    % Get the component details
    eval([ '[comp_type,comp_name,cr,args,repetitions] = ', cmp_name, '(i)' ...
    eval([ "[comp_type,comp_name,cr,args,repetitions] = ", cmp_name, "(i)" ...
	  ]);
						   
    % Alias the args list -- if not at top level
    message = sprintf("\tfor component  %s (%s) within %s",\
                      comp_name,comp_type,full_name);    
    if (length(system_args)>0)
      args = alias_args(args,alias,";",message,infofilenum)
    end;
    if (length(system_cr)>0)
      cr = alias_args(cr,alias,";",message,infofilenum)
    end;

    % Substitute positional ($1 etc) arguments
    cr = subs_arg(cr,system_cr, ...
	'lin',full_name,comp_type,comp_name,infofilenum);
	"lin",full_name,comp_type,comp_name,infofilenum);
    args = subs_arg(args,system_args, ...
	'1',full_name,comp_type,comp_name,infofilenum);
	"1",full_name,comp_type,comp_name,infofilenum);
    
    % change name of 0 and 1 components -- matlab doesn't like numbers here
    if strcmp(comp_type,'0')
      comp_type = 'zero';
    if strcmp(comp_type,"0")
      comp_type = "zero";
    end;
    if strcmp(comp_type,'1')
      comp_type = 'one';
    if strcmp(comp_type,"1")
      comp_type = "one";
    end;
    
    ports = length(bond_list);
    
    if repetitions>1
      port_pairs = ports/2;
      if round(port_pairs)~=port_pairs;
	mtt_info(['Repeated component ', comp_name, ...
	      ' has an odd number of ports - ignoring repetitions'], infofilenum);
	mtt_info(["Repeated component ", comp_name, ...
	      " has an odd number of ports - ignoring repetitions"], infofilenum);
	repetitions = 1;
      end;
    end;
    
    if repetitions>1
      odd_bonds = bond_list(1:2:ports-1);
      even_bonds = bond_list(2:2:ports);
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
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
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
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







-
+


-
+






-
+

-
+

-
+
+
+
+
+





-
+




-
+






-
+




-
+





-
+

-
+




-
+


-
+



-
+


-
-
-
+
+
+









-
+

-
-
+
+

















-
+







	  bond_list(2:2:ports) = new_bonds;
	end;
	
      end;
	
      % Invoke the appropriate equation-generating procedure
      name_r = full_name_repetition;
      eqn_name = [comp_type, '_eqn']
      eqn_name = [comp_type, "_eqn"]
      
      if exist(eqn_name)~=2 % Try a compound component
	disp('---PUSH---');
	disp("---PUSH---"); bond_list, comp_name, comp_type
	structure = cbg2ese(comp_name, comp_type, cr, args, ...
	    full_name, full_name_repetition, ...
	    k, structure,  structure_file, infofilenum);
	
	% Link up the bonds
	fprintf(ese_file, ...
	    '\n\t%s Equations linking up subsystem %s (%s)\n\n', ...
	    "\n\t%s Equations linking up subsystem %s (%s)\n\n", ...
	    pc, comp_name, comp_type);
	name_comp_name = sprintf('%s_%s_%d', ...
	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, comp_type);
bond_list

	u_index = 0; y_index = 0; % Count the inputs and outputs
	for port_number=1:length(bond_list)
	  % Effort
	  if comp_bonds(port_number,1)==1 % Source
	     u_index = u_index + 1;
	     fprintf(ese_file, '%s_MTTu%d := %s;\n', ...
	     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', ...
	     fprintf(ese_file, "%s := %s_MTTy%d;\n", ...
		 varname(name_r, ...
		 bond_list(port_number),1), name_comp_name, y_index);
	  end;
	  % Flow
	  if comp_bonds(port_number,2)==-1 % Source
	     u_index = u_index + 1;
	     fprintf(ese_file, '%s_MTTu%d := %s;\n', ...
	     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', ...
	     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---');
	disp("---POP---");
      else % its a simple component
	fprintf(ese_file, '\n\t%s Equations for component %s (%s), repetition %d\n\n', ...
	fprintf(ese_file, "\n\t%s Equations for component %s (%s), repetition %d\n\n", ...
	    pc, comp_name, comp_type,k);
	
	 
	% Is it a named port? (Name begins with [)
        Named_Port = (comp_name(1)=='[');
        Named_Port = (comp_name(1)=="[");

       if Named_Port %Add [ to start of name
	 name_r = ['[' name_r];
	 name_r = ["[" name_r];
       end;

#	% Take port SS to be ordinary SS at top level
#	if at_top_level & strcmp(comp_type, 'SS') 
#	if at_top_level & strcmp(comp_type, "SS") 
#	  effort_attribute = cr;
#	  flow_attribute = args;
#	  if strcmp(effort_attribute, 'MTT_port') % Its a numbered port
#	    effort_attribute = 'external';
#	    flow_attribute = 'external';
#	  if strcmp(effort_attribute, "MTT_port") % Its a numbered port
#	    effort_attribute = "external";
#	    flow_attribute = "external";
#	    cr = effort_attribute;
#	    args = flow_attribute;
#	  end;
#	end;

	% Save the current structure
	old_structure = structure;
	
	% Generate the simple component equations
	eval(['structure = ', ...
	eval(["structure = ", ...
	      eqn_name, ...
	      '(name_r,bond_list,comp_bonds, ...
	      direction,cr,args,structure,ese_file);' ]);
	      "(name_r,bond_list,comp_bonds, ...
	      direction,cr,args,structure,ese_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
	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
 	    value = structure(which_index);
	    value_change=value-old_structure(which_index);
	    for k=1:value_change
	      fprintf(structure_file, ...
		  '%s\t%1.0f\t%s\t%s\t%1.0f\n', ...
		  "%s\t%1.0f\t%s\t%s\t%1.0f\n", ...
		  structure_name(which_index,:), value-k+1, ...
		  comp_name, full_name, repetition);
	      end;
 	    end;
 	  end;
	
      end;

MTT: Model Transformation Tools
GitHub | SourceHut | Sourceforge | Fossil RSS ]