Overview
Comment:Move the if status .. to the beginning of the main loop.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | origin/master | trunk
Files: files | file ages | folders
SHA3-256: cf8931efd8a9005db588f96ba1f5271bda32dc65d5761ec8f20c2c35750e2d3f
User & Date: gawthrop@users.sourceforge.net on 1997-08-07 16:10:13
Other Links: branch diff | manifest | tags
Context
1997-08-07
16:12:36
Fixed sorting bug: now puts the jth component from the unsorted list
into the kth component of the sorted list .. not vice versa!
check-in: 518cabdfbd user: gawthrop@users.sourceforge.net tags: origin/master, trunk
16:10:13
Move the if status .. to the beginning of the main loop. check-in: cf8931efd8 user: gawthrop@users.sourceforge.net tags: origin/master, trunk
1997-08-06
21:43:19
Corrected error in creating component list: the kth component of the
list is given by the jth component of the original list NOT vice
versa.
check-in: 43156f5519 user: gawthrop@users.sourceforge.net tags: origin/master, trunk
Changes

Modified mttroot/mtt/bin/trans/m/abg2cbg.m from [0176f779b9] to [669cc5e30a].

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
function [port_bonds, status] = abg2cbg(system_name, ...
    system_type, full_name, ...
    port_bonds, typefile, infofile)

% abg2cbg - acausal to causal bg conversion
%
%     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
%     %%%%% Model Transformation Tools %%%%%
%     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% Matlab function  abg2cbg.m
% Acausal bond graph to causal bond graph: mfile format
% [bonds,status] = abg2cbg(system_name, system_type, full_name, port_bonds, infofile)

% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %% Version control history
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %% $Id$
% %% $Log$




% %% Revision 1.15  1997/01/05 12:25:59  peterg
% %% More informative message about port bonds incompatible with  ports
% %%
% %% Revision 1.14  1996/12/31 16:20:42  peterg
% %% Just write causality information at top level -- it gets a bit
% %% voluminous if written at deeper levels.
% %%


|
















>
>
>
>







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
function [port_bonds, status] = abg2cbg(system_name, ...
    system_type, full_name, ...
    port_bonds, port_status, typefile, infofile)

% abg2cbg - acausal to causal bg conversion
%
%     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
%     %%%%% Model Transformation Tools %%%%%
%     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% Matlab function  abg2cbg.m
% Acausal bond graph to causal bond graph: mfile format
% [bonds,status] = abg2cbg(system_name, system_type, full_name, port_bonds, infofile)

% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %% Version control history
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %% $Id$
% %% $Log$
% %% Revision 1.16  1997/08/04 13:11:19  peterg
% %% Only change to component-orientated causality for simple components
% %% NOT for compound components.
% %%
% %% Revision 1.15  1997/01/05 12:25:59  peterg
% %% More informative message about port bonds incompatible with  ports
% %%
% %% Revision 1.14  1996/12/31 16:20:42  peterg
% %% Just write causality information at top level -- it gets a bit
% %% voluminous if written at deeper levels.
% %%
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
% %% Now recursively does causality on subsystems.
% %%
% %% Revision 1.1  1996/08/04 17:55:55  peter
% %% Initial revision
% %%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


pc = '%';
if nargin<1
  system_name = 'no_name';
end;

if nargin<4
  port_bonds = [];







|







68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
% %% Now recursively does causality on subsystems.
% %%
% %% Revision 1.1  1996/08/04 17:55:55  peter
% %% Initial revision
% %%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

mtt_info(system_name, infofile);
pc = '%';
if nargin<1
  system_name = 'no_name';
end;

if nargin<4
  port_bonds = [];
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
% onto components
if at_top_level % Then at top level
  fprintf(typefile, '# Commands to map types onto subsystems\n');
  fprintf(typefile, '# File %s_type.sh\n', system_name);
  fprintf(typefile, '# Generated by MTT on %s\n\n', date);
end;

full_name
length(full_name)

% Create the (full) system name
if at_top_level
  full_name = system_name;
  system_type = system_name;
else
  full_name = [full_name, '_', system_name];
end;


% Add to file to contain shell commands which map subsystem types 
% onto components
fprintf(typefile, '$1%s$2%s$3\n', system_type, full_name);

fun_name = [system_type, '_abg']





% If no such function - then there is nothing to be done.
if exist(fun_name)~=2
  mtt_info(['m-file ', fun_name, ' does not exist'], infofile);
  bonds = [];
  status = [];
  return
end;

% Evaluate the system function to get the bonds
eval(['[bonds,components,n_ports]=', fun_name, ';']);

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

 
% Find number of components
[n_components,columns] = size(components);
if n_components==0 % there is nothing to be done
  return
end;




% If not at top level, then sort out the port bonds.
if at_top_level==0 
  % Find number of port bonds
  [n_port_bonds,columns] = size(port_bonds);

  % Check compatibility - if ok copy port bonds to the internal bonds list.
  if n_port_bonds~=n_ports
    mtt_info(sprintf('%s: %1.0f port bonds incompatible with %1.0f ports', ...
	full_name, n_port_bonds, n_ports), infofile);

  else % Copy the port bonds -- but only if not set already
    for i = 1:n_ports      % The port SSs come first
      j = abs(components(i,1)); % Get the bonds attached to the ports
      direction = -sign(components(i,1));
      for k=1:2
	if bonds(j,k)==0 % causality not set yet - so copy.
	  bonds(j,k) = direction*port_bonds(i,k);
	end;
      end;
    end;
  end;
end;


% Set initial status
status = -ones(n_components,1);
total = 2*n_bonds;

done = sum(sum(abs(bonds)))/total*100;

% Outer while loop sets preferred causality
ci_index=1; 

while( ci_index>0)
  old_done = inf;
  % Inner loop propagates causality
  while done~=old_done
    disp(sprintf('Causality is %3.0f%s complete.', done, pc));
    old_done = done;
  
    for i = 1:n_components
      if status(i) ~= 0 % only do this if causality not yet complete
	
	% Get the bonds on this component
	comp = nozeros(components(i,:));
	bond_list = abs(comp);
	direction = sign(comp)'*[1 1];


	% Get the component details
	eval([ '[comp_type,name,cr,arg] = ', system_type, '_cmp(i);' ]);

	% Change name of 0 and 1 components -- matlab doesn't like numbers here
	if strcmp(comp_type,'0')
	  comp_type = 'zero';
	end;
	if strcmp(comp_type,'1')
	  comp_type = 'one';
	end;
      
	% Component causality procedure name
	cause_name = [comp_type, '_cause'];
	
        % Bonds on this component (arrow-orientated)
      	comp_bonds = bonds(bond_list,:);

      % Invoke  the appropriate causality procedure
      if exist(cause_name)~=2 % Try a compound component




	[comp_bonds,s] = abg2cbg(name, comp_type, full_name, comp_bonds, ...

	    typefile, infofile);
	
	% Create a single status from the status vector s
	if max(abs(s)) == 0 % Causal
	  status(i) = 0;
	else
	  if max(s) == 1 % At least one component is overcausal
	    status(i) = 1;
	  else           % no component is overcausal but some are undercausal
	    status(i) = -1;
	  end;
	end;

      else % its a simple component


	% Convert from arrow orientated to component orientated causality
	comp_bonds = comp_bonds.*direction;
	
	disp(['---', name, ' (', cause_name, ') ---']);

        % Evaluate the built-in causality procedure
	eval([ '[comp_bonds,status(i)] = ', cause_name, '(comp_bonds);' ]);

       % and convert from component orientated to arrow orientated causality
       comp_bonds = comp_bonds.*direction; 



      end;
      
      % Update the full bonds list
      bonds(bond_list,:) = comp_bonds; 
    end;
    end;
    
    done = sum(sum(abs(bonds)))/total*100;
    %  mtt_info(sprintf('Causality is %3.0f%s complete.', done, pc), infofile);
    
  end;
  
  % Set causality of a C or I which is not already set
  [ci_index,prefered] = getdynamic(status,system_type);
  if ci_index>0
    bond_index = components(ci_index,1) % its a one port







<
<
<







>





|
>
>
>
>









|















>
>
>
|
|





|
|
>
|
<
|
<
<
|
|
|
|
<
<
<

|
<
<

<









|




|

|
|
|
>
















|



>
>
>
>
|
>














>
>

|


>

|


|
>
>




|




|







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
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
% onto components
if at_top_level % Then at top level
  fprintf(typefile, '# Commands to map types onto subsystems\n');
  fprintf(typefile, '# File %s_type.sh\n', system_name);
  fprintf(typefile, '# Generated by MTT on %s\n\n', date);
end;




% Create the (full) system name
if at_top_level
  full_name = system_name;
  system_type = system_name;
else
  full_name = [full_name, '_', system_name];
end;


% Add to file to contain shell commands which map subsystem types 
% onto components
fprintf(typefile, '$1%s$2%s$3\n', system_type, full_name);

fun_name = [system_type, '_abg'];

disp('====================================');
disp(['BEGIN: ', full_name, ' (', fun_name, ')']);
disp('====================================');

% If no such function - then there is nothing to be done.
if exist(fun_name)~=2
  mtt_info(['m-file ', fun_name, ' does not exist'], infofile);
  bonds = [];
  status = [];
  return
end;

% Evaluate the system function to get the bonds and number of ports
eval(['[bonds,components,n_ports]=', fun_name, ';']);

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

 
% Find number of components
[n_components,columns] = size(components);
if n_components==0 % there is nothing to be done
  return
end;

% Set initial status
status = -ones(n_components,1);

% If not at top level, then copy the port bonds.
if ~at_top_level 
  % Find number of port bonds
  [n_port_bonds,columns] = size(port_bonds);

  % Check compatibility - if ok copy port bonds to the internal bonds list.
  if n_port_bonds~=n_ports
  mtt_info(sprintf('%s: %1.0f port bonds incompatible with %1.0f ports', ...
               full_name, n_port_bonds, n_ports), infofile);

  else % Copy the port bonds & status

    j = abs(components(1:n_ports,1)); %relevant bond numbers


    bonds(j,:) = port_bonds;
    status(1:n_ports) = port_status;
  end
end;




% Causality indicator


total = 2*n_bonds;

done = sum(sum(abs(bonds)))/total*100;

% Outer while loop sets preferred causality
ci_index=1; 

while( ci_index>0)
  old_done = inf;
  % Inner loop propagates causality
  while done~=old_done
   % disp(sprintf('Causality is %3.0f%s complete.', done, pc));
    old_done = done;
  
    for i = 1:n_components
      if status(i) ~= 0 % only do this if causality not yet complete

	% Get the bonds on this component
	comp = nozeros(components(i,:))
	bond_list = abs(comp)
	direction = sign(comp)'*[1 1]
        n_bonds = length(bond_list);

	% Get the component details
	eval([ '[comp_type,name,cr,arg] = ', system_type, '_cmp(i);' ]);

	% Change name of 0 and 1 components -- matlab doesn't like numbers here
	if strcmp(comp_type,'0')
	  comp_type = 'zero';
	end;
	if strcmp(comp_type,'1')
	  comp_type = 'one';
	end;
      
	% Component causality procedure name
	cause_name = [comp_type, '_cause'];
	
        % Bonds on this component (arrow-orientated)
      	port_bonds = bonds(bond_list,:);

      % Invoke  the appropriate causality procedure
      if exist(cause_name)~=2 % Try a compound component
        % Port status depends on whether the corresponding bonds are
        %  fully causal at this stage.
        one = ones(n_bonds,1);
        port_status = (sum(abs(port_bonds'))'==2*one) - one;
	[port_bonds,s] = abg2cbg(name, comp_type, full_name, 
            port_bonds, port_status, ...
	    typefile, infofile);
	
	% Create a single status from the status vector s
	if max(abs(s)) == 0 % Causal
	  status(i) = 0;
	else
	  if max(s) == 1 % At least one component is overcausal
	    status(i) = 1;
	  else           % no component is overcausal but some are undercausal
	    status(i) = -1;
	  end;
	end;

      else % its a simple component
	port_bonds_in = port_bonds

	% Convert from arrow orientated to component orientated causality
	port_bonds = port_bonds.*direction;
	
	disp(['---', name, ' (', cause_name, ') ---']);

        % Evaluate the built-in causality procedure
	eval([ '[port_bonds,status(i)] = ', cause_name, '(port_bonds);' ]);

       % and convert from component orientated to arrow orientated causality
       port_bonds = port_bonds.*direction; 
       
       port_bonds_out = port_bonds

      end;
      
      % Update the full bonds list
      bonds(bond_list,:) = port_bonds
    end;
    end;
    
    done = sum(sum(abs(bonds)))/total*100;
    %disp(sprintf('Causality is %3.0f%s complete.', done, pc), infofile));
    
  end;
  
  % Set causality of a C or I which is not already set
  [ci_index,prefered] = getdynamic(status,system_type);
  if ci_index>0
    bond_index = components(ci_index,1) % its a one port
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
      mtt_info(sprintf('Component %s (%s) is undercausal', name, comp_type), ...
	  infofile);
    end;
  end;
end;

% $$$ file_name = [full_name, '_', system_type]
file_name = [full_name, '_cbg.m']

cbgfilenum = fopen(file_name,'w');
write_cbg(cbgfilenum,full_name,system_type,bonds,status);
fclose(cbgfilenum);

% Return the port bonds
for i = 1:n_ports % The port SSs come first
    j = abs(components(i,1)); % Get the bonds attached to the ports
    direction = -sign(components(i,1)); 
    port_bonds(i,:) = direction*bonds(j,:);
end;

disp('----------------------');
















|
>




|
|
|
<
|


<
|
|
|

<
<
<
<
<
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299

300
301
302

303
304
305
306





      mtt_info(sprintf('Component %s (%s) is undercausal', name, comp_type), ...
	  infofile);
    end;
  end;
end;

% $$$ file_name = [full_name, '_', system_type]
file_name = [full_name, '_cbg.m'];
disp(['Writing ', file_name]);
cbgfilenum = fopen(file_name,'w');
write_cbg(cbgfilenum,full_name,system_type,bonds,status);
fclose(cbgfilenum);

% Return the port bonds - arrow orientated causality
if ~at_top_level %Not at top level
  j = abs(components(1:n_ports,1)) %relevant bond numbers

  port_bonds = bonds(j,:)
end;


disp('====================================');
disp(['END: ', full_name, ' (', fun_name, ')']);
disp('====================================');







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