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
|
# P.J.Gawthrop May 1996
# Copyright (c) P.J.Gawthrop, 1996.
while [ -n "`echo $1 | grep '^-'`" ]; do
case $1 in
-I )
info=info;;
*)
echo "$1 is an invalid argument - ignoring" ;;
esac
shift
done
# Remove the old log file
rm -f rbg2abg_m.log
rm -f $1_abg.m
#Inform user
echo Creating $1_abg.m
# Use matrix manipulation to accomplish the transformation
$MATRIX > rbg2abg_m.log 2>mtt_error.txt << EOF
name = '$1'
infofile = fopen('mtt_info.txt', 'w');
errorfile = fopen('mtt_error.txt', 'w');
|
>
>
|
>
>
>
|
|
|
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
|
# P.J.Gawthrop May 1996
# Copyright (c) P.J.Gawthrop, 1996.
while [ -n "`echo $1 | grep '^-'`" ]; do
case $1 in
-I )
info=info;;
-nounits )
units=no;;
*)
echo "$1 is an invalid argument - ignoring" ;;
esac
shift
done
## Set up some vars
sys=$1
lbl_file=${sys}_lbl.txt
abg_file=${sys}_abg.m
# Remove the old log file
rm -f rbg2abg_m.log
rm -f ${abg_file}
#Inform user
echo Creating ${abg_file}
# Use matrix manipulation to accomplish the transformation
$MATRIX > rbg2abg_m.log 2>mtt_error.txt << EOF
name = '$1'
infofile = fopen('mtt_info.txt', 'w');
errorfile = fopen('mtt_error.txt', 'w');
|
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
|
rbonds = rbonds(:,1:6); # Strip the directional information
[bonds,components,n_vector_bonds] = rbg2abg(name,rbonds,rstrokes,rcomponents,port_coord,port_name,\
infofile,errorfile);
%Write the function m-file for the causal bond graph
write_abg(name,bonds,components,n_vector_bonds);
# filename = '$1_abg.m';
# filenum = fopen(filename,'w');
# c = '%';
# fprintf(filenum, 'function [bonds,components,n_ports,N_ports] = $1_abg\n');
# fprintf(filenum, '%s [bonds,components,n_ports] = $1_abg\n', c);
# fprintf(filenum, '%s Acausal bond graph created by MTT on %s\n\n', ...
# c, date);
# mat2mfile(bonds, 'bonds', filenum);
# mat2mfile(components, 'components', filenum);
# fprintf(filenum, 'n_ports = %1.0f;\n', n_ports);
# fprintf(filenum, 'N_ports = %1.0f;\n', N_ports);
EOF
# Append any VAR declarations
if [ -f "$1_lbl.txt" ]; then
echo "# Explicit variable declarations" >> $1_abg.m
grep '^[\s]*[%|#][V|P]AR' $1_lbl.txt | tr '%' '#' >> $1_abg.m
grep '^[\s]*[%|#]NOT[V|P]AR' $1_lbl.txt | tr '%' '#' >> $1_abg.m
fi
## Close off the function
echo "endfunction" >> $1_abg.m
## Errors and info
if [ "$info" = "info" ]; then
cat mtt_info.txt
fi
if mtt_error mtt_error.txt
|
>
>
>
>
>
|
|
|
<
|
<
>
>
>
>
>
|
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
|
>
|
>
|
|
>
>
>
>
>
>
>
|
|
|
|
|
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
|
rbonds = rbonds(:,1:6); # Strip the directional information
[bonds,components,n_vector_bonds] = rbg2abg(name,rbonds,rstrokes,rcomponents,port_coord,port_name,\
infofile,errorfile);
%Write the function m-file for the causal bond graph
write_abg(name,bonds,components,n_vector_bonds);
EOF
## Sets the units for the abg file
function test_units
{
grep '^[\s]*[#|%]UNITS' < ${lbl_file} >/dev/null
if [ $? = "0" ]; then
grep '^[\s]*[#|%]UNITS' < ${lbl_file} |\
awk '{
printf("mtt_units.sh %s %s %s %s %s\n", sys, $2, $3, $4, $5)
}' sys=${sys} | sh | grep ERROR
if [ $? = "0" ]; then
echo " " *MTT_ERRROR: domains and units are not OK - exiting
exit 1
else
echo " " domains and units are OK
fi
else
echo " no domains or units declared"
fi
}
function check_ports_exist
{
declared_ports=`grep '^[\s]*[#|%]UNITS' < ${lbl_file} | awk '{print $2}'`
for declared_port in $declared_ports; do
grep "${sys}\.ports\.${declared_port}\.type" ${abg_file} >/dev/null
if [ $? = "1" ]; then
echo "*MTT_ERRROR: Units declared for non-existent port ${declared_port}"
exit 1
fi
done
}
function set_units
{
grep '^[\s]*[#|%]UNITS' < ${lbl_file} |\
awk '{
printf(" %s.ports.%s.domain = \"%s\";\n", sys, $2, $3);
printf(" %s.ports.%s.units.effort = \"%s\";\n", sys, $2, $4);
printf(" %s.ports.%s.units.flow = \"%s\";\n", sys, $2, $5);
}' sys=${sys}
}
if [ -z "$units" ]; then
echo Checking port domains and units
check_ports_exist;
test_units;
echo "## Port domain and units" >> ${abg_file}
set_units >> ${abg_file}
fi
# Append any VAR declarations
if [ -f "$1_lbl.txt" ]; then
echo "## Explicit variable declarations" >> ${abg_file}
grep '^[\s]*[%|#][V|P]AR' $1_lbl.txt | tr '%' '#' >> ${abg_file}
grep '^[\s]*[%|#]NOT[V|P]AR' $1_lbl.txt | tr '%' '#' >> ${abg_file}
fi
## Close off the function
echo "endfunction" >> ${abg_file}
## Errors and info
if [ "$info" = "info" ]; then
cat mtt_info.txt
fi
if mtt_error mtt_error.txt
|