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
|
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
228
229
230
231
232
233
234
235
236
237
238
239
240
|
+
+
+
+
+
+
+
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
-
+
-
+
-
+
-
-
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
-
-
+
+
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
+
-
-
-
+
+
+
-
+
-
+
-
+
|
#! /bin/sh
######################################
##### Model Transformation Tools #####
######################################
# Bourne shell script: cse2scse_r
# Reduce constrained-state equations to sensitivity version
# P.J.Gawthrop 10th May 199, 8th August 1991, April 1994, Dec 1994
# Copyright (c) P.J.Gawthrop, 1999
# $Id$
# Arguments
system=$1;
system_def=$1_def.r
system_cse=$1_cse.r
system_scse=$1_scse.r
# The sensitivity parameter
theta=$2
thetas=$2"s"
# Parameters
n=`echo $2 | sed 's/,/ /g' |wc -w`
echo $n_parameters
parameters=`echo $2 | sed 's/,/ /g' |\
awk '{
for (i=1; i<=NF; i++) {
printf("mttpar(%i,1) := %s;\n", i, $i);
printf("mttcoef(%i,1) := %ss;\n", i, $i);
}
}'`
matrix="matrix mttpar("$n",1); matrix mttcoef("$n",1);"
echo $parameters
echo $theta $thetas
echo $matrix
# Number of states
Nx=`grep "MTTNx " <$1_def.r | awk '{print $3}' | sed 's/;//'`
Nx=`grep "MTTNx " <$system_def | awk '{print $3}' | sed 's/;//'`
#Inform user
echo "Creating $1_scse.r (for parameter $theta, $Nx states)"
echo Creating $system_scse "(for parameters $2, $Nx states)"
# Remove the old log file
rm -f cse2scse_r.log
# Use reduce to accomplish the transformation
$SYMBOLIC << EOF >cse2scse_r.log
%Read the formatting function
in "$MTTPATH/trans/reduce_matrix.r";
%Read the definitions file
in "$1_def.r";
in "$system_def";
%Read the substitution file
in "$1_subs.r";
%Read the constrained-state equations file
in "$system_cse";
%Read the constrained-state equations file
in "$1_cse.r";
% Declare the parameter matrix and fill it
$matrix
$parameters
mtt_n_par := $n;
MTTNx2 := 2*MTTNx;
MTTNy2 := 2*MTTNy;
% Compute the sensitivity E matrix
matrix MTTssE(MTTNx,MTTNx);
clear MTTx; % Dont use - mkid is better
FOR ii := 1:MTTNx DO
FOR jj := 1:MTTNx DO
IF MTTE(ii,jj) NEQ 0 THEN IF MTTE(ii,jj) NEQ 1 THEN
BEGIN
% First with respect to theta...
FOR kk := 1:mtt_n_par DO
BEGIN
mttpar_k := mttpar(kk,1);
mttcoef_k := mttcoef(kk,1);
MTTssE(ii,jj) := df(MTTE(ii,jj), $theta)*$thetas;
MTTssE(ii,jj) := MTTssE(ii,jj) + df(MTTE(ii,jj), mttpar_k)*mttcoef_k;
END;
% then with respect to x
FOR i := 1:MTTNx DO
BEGIN
xi := mkid(MTTx,i);
sxi := mkid(MTTsx,i);
MTTssE(ii,jj) := MTTssE(ii,jj) + df(MTTE(ii,jj), xi)*sxi;
END;
END;
% Compute the sensitivity of the RHS of the cse
matrix MTTssEdx(MTTNx,1);
FOR ii := 1:MTTNx DO
BEGIN
% First with respect to theta
FOR kk := 1:mtt_n_par DO
BEGIN
mttpar_k := mttpar(kk,1);
mttcoef_k := mttcoef(kk,1);
MTTssEdx(ii,1) := $thetas*df(MTTEdx(ii,1), $theta);
MTTssEdx(ii,1) := MTTssEdx(ii,1) + df(MTTEdx(ii,1), mttpar_k)*mttcoef_k;
END;
% Then with respect to x
FOR i := 1:MTTNx DO
BEGIN
xi := mkid(MTTx,i);
sxi := mkid(MTTsx,i);
MTTssEdx(ii,1) := MTTssEdx(ii,1) + df(MTTEdx(ii,1), xi)*sxi;
END;
END;
% Sensitivity output function
matrix MTTssY(MTTNy,1);
FOR ii := 1:MTTNy DO
BEGIN
% First with respect to theta
FOR kk := 1:mtt_n_par DO
BEGIN
mttpar_k := mttpar(kk,1);
mttcoef_k := mttcoef(kk,1);
MTTssY(ii,1) := MTTssY(ii,1) + df(MTTY(ii,1), mttpar_k)*mttcoef_k;
END;
% Then with respect to x
FOR i := 1:MTTNx DO
BEGIN
xi := mkid(MTTx,i);
sxi := mkid(MTTsx,i);
MTTssY(ii,1) := MTTssY(ii,1) + df(MTTY(ii,1), xi)*sxi;
END;
END;
% Now reorganise everything into composite system
% - odd rows are the system
% - even rows are the sensitivity system
% NB at this stage, the states are numbered incorrectly - sorted out below.
%E matrix
MTTNx2 := 2*MTTNx;
matrix MTTsE(MTTNx2,MTTNx2);
FOR i := 1:MTTNx DO
FOR j := 1:MTTNx DO
BEGIN
MTTsE(2*i-1,2*j-1) := MTTE(i,j); % System
MTTsE(2*i,2*j) := MTTE(i,j); % Sensitivity system
MTTsE(2*i,2*j-1) := MTTssE(i,j); % Sensitivity system
END;
%dX matrix
matrix MTTsEdX(MTTNx2,1);
FOR i := 1:MTTNx DO
BEGIN
MTTsEdX(2*i-1,1) := MTTEdx(i,1);
MTTsEdX(2*i,1) := MTTssEdx(i,1);
END;
%Y matrix
matrix MTTsY(MTTNy2,1);%dX matrix
matrix MTTsEdX(MTTNx2,1);
FOR i := 1:MTTNx DO
BEGIN
MTTsEdX(2*i-1,1) := MTTEdx(i,1);
MTTsEdX(2*i,1) := MTTssEdx(i,1);
END;
FOR i := 1:MTTNy DO
BEGIN
MTTsY(2*i-1,1) := MTTY(i,1);
MTTsY(2*i,1) := MTTssY(i,1);
END;
OFF nat;
OUT "$1_scse.r";
OUT "$system_scse";
write "%File: $1_scse.r";
write "%File: $system_scse";
% E matrix
MTT_Matrix := MTTsE$
MTT_Matrix_name := "MTTsE"$
MTT_Matrix_n := MTTNx2$
MTT_Matrix_m := MTTNx2$
Reduce_Matrix()$
% State derivative
MTT_Matrix := MTTsEdX$
MTT_Matrix_name := "MTTsEdX"$
MTT_Matrix_n := MTTNx2$
MTT_Matrix_m := 1$
Reduce_Matrix()$
% Output
MTT_Matrix := MTTY$
MTT_Matrix_name := "MTTY"$
MTT_Matrix_n := MTTNy$
MTT_Matrix := MTTsY$
MTT_Matrix_name := "MTTsY"$
MTT_Matrix_n := MTTNy2$
MTT_Matrix_m := 1$
Reduce_Matrix()$
write "END;";
SHUT "$1_scse.r";
SHUT "$system_scse";
quit;
EOF
# Now invoke the standard error handling.
mtt_error_r cse2scse_r.log
## Now reorganise the states
mv -f $1_scse.r mtt_junk
mv -f $system_scse mtt_junk
##echo "Nx = $Nx"
awk '{
## Make sure all MTTn variables are followed by a space
gsub(/mttx[0-9]*/, "& ");
for (i=Nx;i>0;i--) {
state = sprintf("mttx%i ",i);
newstate = sprintf("mttx%i ",2*i-1);
sstate = sprintf("mttsx%i",i);
newsstate = sprintf("mttx%i",2*i);
gsub(state,newstate);
gsub(sstate,newsstate);
}
print $0
}' Nx=$Nx <mtt_junk > $1_scse.r
}' Nx=$Nx <mtt_junk > $system_scse
|