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
|
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
|
-
+
+
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
-
-
-
+
+
+
-
+
+
+
+
+
+
+
+
+
+
-
+
+
+
+
+
-
+
+
-
+
+
-
+
+
+
-
+
-
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
|
# Find system constants
Nx=`mtt_getsize $Sys x` # States
Nu=`mtt_getsize $Sys u` # Inputs
Ny=`mtt_getsize $Sys y` # Inputs
Npar=`wc -l $Sys\_sympar.txt | awk '{print $1}'`
# Header
lang_header -noglobals $1 sim m 'x0,u,t,par,sensitivities' '[y,x]' > $1_sim.m
lang_header -noglobals $1 sim m 'x0,u,t,par,sensitivities' '[y,ys]' > $1_sim.m
cat >> $1_sim.m <<EOF
## tick=time;
if nargin<5
sensitivities = [];
endif;
## Initialise
[ui] = zero_input($Nu); # Zero the input
[xi] = x0; # Read in initial state
##Sizes
N = length(t);
[n_u,N_u] = size(u);
## Doing sensitivities (assumes sensitivity system is invoked)
doing_sensitivities = (length(sensitivities)>0);
if doing_sensitivities
n_y = $Ny/2;
doing_state=0;
else
n_y = $Ny;
doing_state=(nargout>1);
endif;
##Sizes
N = length(t);
## Initialise arrays
x = zeros($Nx,N);
y = zeros($Ny,N);
## Initialise
ui = zeros(n_u,1); # Initial control
[xi] = x0; # Read in initial state
## Timing parameters
first = t(1);
dt = t(2) - t(1);
last = t(length(t))+dt;
last = t(length(t));
n_sens = length(sensitivities);
EOF
if [ "$computation" = "c" ]; then
cat >> $1_sim.m <<EOF
## Create the system input file
t1 = [0:N_u-1]*dt; # Create time vector from zero (to fit u);
ut = [t1' u'];
## tick1=time;
save -ascii $1_input.dat ut
## save_time = time-tick1
##system("strip_comments <mtt_junk.dat >mtt_u.dat;"); #Strip the leading comments
## Create the command string
## tick1=time;
command = "";
for sensitivity_index = [0 sensitivities]
args = "";
for i=1:$Nx
args = sprintf("%s %g", args, x0(i));
endfor
par(sensitivities) = zeros(1,n_sens);
if sensitivity_index>0
par(sensitivity_index) = 1;
i_1 = $Ny/2 + 2;
i_1 = 2+n_y;
else
i_1 = 2;
endif;
if doing_state
i_2 = i_1 + n_y + $Nx;
else
i_2 = i_1 + $Ny/2 -1;
i_2 = i_1 + n_y -1;
endif;
for i=1:$Npar
args = sprintf("%s %g", args, par(i));
endfor
args = sprintf("%s %g %g %g", args, first, dt, last);
command = sprintf("%s ./$1_ode2odes.out %s | cut -f %i-%i;", command, args, i_1, i_2);
command = sprintf("%s ./$1_ode2odes.out< $1_input.dat %s | cut -f %i-%i;", command, args, i_1, i_2);
endfor;
## string_time = time-tick1
command
## And execute it ...
## tick1=time;
yy=str2num(system(command));
## command_time = time-tick1
[N_yy,M_yy] = size(yy);
[Ny,My] = size(yy);
## Reshape
yy = reshape(yy,N,M_yy*(1+n_sens))';
y = yy(1:n_y,:); # Output
if doing_sensitivities
i_1 = n_y+1;
i_2 = i_1 + n_y*n_sens - 1;
ys = yy(i_1:i_2,:); # sensitivity
endif;
if doing_state
i_1 = n_y+2;
i_2 = i_1 + $Nx - 1;
ys = yy(i_1:i_2,:); # state
endif;
Ny/N,N
y = reshape(yy',N,Ny/N)';
## sim_time = time-tick
endfunction
EOF
else
cat >> $1_sim.m <<EOF
|