988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
|
set_global_value ("MTT_data", data);
#endif // (CODEGENTARGET == STANDALONE)
row = 0;
fcputime.close();
}
}
#if (CODEGENTARGET == STANDALONE)
void dump_data (std::ostream &file)
{
ColumnVector null (0);
mtt_write (0.0, null, null, 0, true, file);
}
void handle_signal (int signum)
{
// handle some signals to ensure data is written.
std::cerr << "# Writing data to MTT.core (signal " << signum << ")" << std::endl;
std::ofstream corefile ("MTT.core");
dump_data (corefile);
switch (signum)
{
case SIGFPE:
// Intel chips do not raise SIGFPE for DIVZERO :-(
// raise (SIGABRT);
break;
case SIGINT:
break;
case SIGQUIT:
signal (SIGQUIT, SIG_DFL);
raise (SIGQUIT);
break;
default:
std::cerr << "# Warning: make_ode2odes needs updating!" << std::endl;
signal (signum, SIG_DFL);
raise (signum);
break;
}
corefile.close ();
set_signal_handlers ();
}
void set_signal_handlers (void)
{
signal (SIGFPE, handle_signal);
signal (SIGINT, handle_signal);
signal (SIGQUIT, handle_signal);
}
int main (void) {
set_signal_handlers ();
#elif (CODEGENTARGET == OCTAVEDLD)
DEFUN_DLD (${sys}_ode2odes, args, ,
"Octave ode2odes representation of system with $method integration method\nUsage: mtt_data = ${sys}_ode2odes (x0, par, simpar)\n")
{
static octave_value_list retval;
#endif // (CODEGENTARGET == STANDALONE)
static ColumnVector x0;
static ColumnVector par;
static ColumnVector simpar;
static double
first = 0.0,
dt = 0.0,
last = 0.0,
stepfactor = 0.0;
#if (CODEGENTARGET == OCTAVEDLD)
int nargin = args.length ();
switch (nargin)
{
case 3:
first = args (2).${vector_value} ()(0);
dt = args (2).${vector_value} ()(1);
last = args (2).${vector_value} ()(2);
stepfactor = args (2).${vector_value} ()(3);
par = args (1).${vector_value} ();
x0 = args (0).${vector_value} ();
break;
case 2:
first = mtt_simpar ()(0);
dt = mtt_simpar ()(1);
last = mtt_simpar ()(2);
stepfactor = mtt_simpar ()(3);
par = args (1).${vector_value} ();
x0 = args (0).${vector_value} ();
break;
case 1:
first = mtt_simpar ()(0);
dt = mtt_simpar ()(1);
last = mtt_simpar ()(2);
stepfactor = mtt_simpar ()(3);
par = mtt_numpar ();
x0 = args (0).${vector_value} ();
break;
case 0:
#endif // (CODEGENTARGET == OCTAVEDLD)
first = mtt_simpar ()(0);
dt = mtt_simpar ()(1);
last = mtt_simpar ()(2);
stepfactor = mtt_simpar ()(3);
par = mtt_numpar ();
x0 = mtt_state (par);
#if (CODEGENTARGET == OCTAVEDLD)
break;
default:
usage("${sys}_ode2odes (x par simpar)", nargin);
error("aborting.");
}
#endif // (CODEGENTARGET == OCTAVEDLD)
static ColumnVector dx (MTTNX, 0.0);
static ColumnVector x (MTTNX, 0.0);
static ColumnVector u (MTTNU, 0.0);
static ColumnVector y (MTTNY, 0.0);
static Matrix AA (MTTNX, MTTNX, 0.0);
static ColumnVector AAx (MTTNX, 0.0);
|
<
<
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
|
|
|
|
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
|
set_global_value ("MTT_data", data);
#endif // (CODEGENTARGET == STANDALONE)
row = 0;
fcputime.close();
}
}
void
${sys}_ode2odes (ColumnVector &state0, ColumnVector &numpar, ColumnVector &simpar)
{
static double first, dt, last, stepfactor;
first = simpar (0);
dt = simpar (1);
last = simpar (2);
stepfactor = simpar (3);
static ColumnVector dx (MTTNX, 0.0);
static ColumnVector x (MTTNX, 0.0);
static ColumnVector u (MTTNU, 0.0);
static ColumnVector y (MTTNY, 0.0);
static Matrix AA (MTTNX, MTTNX, 0.0);
static ColumnVector AAx (MTTNX, 0.0);
|
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
|
for (register int i = 0; i < MTTNY; i++)
{
y (i) = 0.0;
}
for (register int i = 0; i < MTTNX; i++)
{
x (i) = x0 (i);
}
for (register int j = 0, i = 1; i <= ilast; i++)
{
u = mtt_input (x, y, t, par);
y = mtt_output (x, u, t, par);
if (0 == j)
{
mtt_write (t, x, y, nrows);
}
EOF
case "$method" in
"rk4")
|
|
|
|
|
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
|
for (register int i = 0; i < MTTNY; i++)
{
y (i) = 0.0;
}
for (register int i = 0; i < MTTNX; i++)
{
x (i) = state0 (i);
}
for (register int j = 0, i = 1; i <= ilast; i++)
{
u = mtt_input (x, y, t, numpar);
y = mtt_output (x, u, t, numpar);
if (0 == j)
{
mtt_write (t, x, y, nrows);
}
EOF
case "$method" in
"rk4")
|
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
|
t2 = t + ddt;
ColumnVector
x1 (x),
x2 (x),
x3 (x);
k1 = ddt * mtt_rate (x , u, t , par); x1 += k1 * 0.5;
k2 = ddt * mtt_rate (x1, u, t1, par); x2 += k2 * 0.5;
k3 = ddt * mtt_rate (x2, u, t1, par); x3 += k3;
k4 = ddt * mtt_rate (x3, u, t2, par);
dx = (k1 + 2.0 * (k2 + k3) + k4) / (6.0 * ddt);
}
EOF
;;
"dassl")
;;
"implicit")
cat << EOF >> $filename
dx = mtt_rate (x, u, t, par);
AA = mtt_smxa (x, u, ddt, par);
AAx = mtt_smxax (x, u, ddt, par);
EOF
;;
"euler" | *)
cat << EOF >> $filename
dx = mtt_rate (x, u, t, par);
EOF
;;
esac
## Common stuff
cat <<EOF >> $filename
open_switches = mtt_logic (x, u, t, par);
x = $algorithm;
t += ddt;
j++;
j = (j == static_cast<int> (stepfactor)) ? 0 : j;
}
#if (CODEGENTARGET == STANDALONE)
return 0;
#elif (CODEGENTARGET == OCTAVEDLD)
/*
retval (0) = octave_value (y);
retval (1) = octave_value (x);
retval (2) = octave_value (t);
*/
retval = octave_value (get_global_value ("MTT_data"));
return (retval);
#endif // (CODEGENTARGET == STANDALONE)
}
EOF
}
case ${lang} in
m)
make_m
;;
cc)
make_cc
;;
*)
echo Language ${lang} is not supported
esac
|
|
|
|
|
|
|
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
|
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
|
>
|
|
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
|
t2 = t + ddt;
ColumnVector
x1 (x),
x2 (x),
x3 (x);
k1 = ddt * mtt_rate (x , u, t , numpar); x1 += k1 * 0.5;
k2 = ddt * mtt_rate (x1, u, t1, numpar); x2 += k2 * 0.5;
k3 = ddt * mtt_rate (x2, u, t1, numpar); x3 += k3;
k4 = ddt * mtt_rate (x3, u, t2, numpar);
dx = (k1 + 2.0 * (k2 + k3) + k4) / (6.0 * ddt);
}
EOF
;;
"dassl")
;;
"implicit")
cat << EOF >> $filename
dx = mtt_rate (x, u, t, numpar);
AA = mtt_smxa (x, u, ddt, numpar);
AAx = mtt_smxax (x, u, ddt, numpar);
EOF
;;
"euler" | *)
cat << EOF >> $filename
dx = mtt_rate (x, u, t, numpar);
EOF
;;
esac
## Common stuff
cat <<EOF >> $filename
open_switches = mtt_logic (x, u, t, numpar);
x = $algorithm;
t += ddt;
j++;
j = (j == static_cast<int> (stepfactor)) ? 0 : j;
}
}
#if (CODEGENTARGET == STANDALONE)
#include <csignal>
void set_signal_handlers (void);
void dump_data (std::ostream &file)
{
ColumnVector null (0);
mtt_write (0.0, null, null, 0, true, file);
}
void handle_signal (int signum)
{
// handle some signals to ensure data is written.
std::cerr << "# Writing data to MTT.core (signal " << signum << ")" << std::endl;
std::ofstream corefile ("MTT.core");
dump_data (corefile);
switch (signum)
{
case SIGFPE:
// Intel chips do not raise SIGFPE for DIVZERO :-(
// raise (SIGABRT);
break;
case SIGINT:
break;
case SIGQUIT:
signal (SIGQUIT, SIG_DFL);
raise (SIGQUIT);
break;
default:
std::cerr << "# Warning: make_ode2odes needs updating!" << std::endl;
signal (signum, SIG_DFL);
raise (signum);
break;
}
corefile.close ();
set_signal_handlers ();
}
void set_signal_handlers (void)
{
signal (SIGFPE, handle_signal);
signal (SIGINT, handle_signal);
signal (SIGQUIT, handle_signal);
}
int
main (void)
{
set_signal_handlers ();
static ColumnVector simpar (8), numpar (MTTNPAR), state0 (MTTNX);
simpar = mtt_simpar ();
numpar = mtt_numpar ();
state0 = mtt_state (numpar);
${sys}_ode2odes (state0, numpar, simpar);
return 0;
}
#elif (CODEGENTARGET == OCTAVEDLD)
DEFUN_DLD (${sys}_ode2odes, args, ,
"Octave ode2odes representation of system with $method integration method\nUsage: mtt_data = ${sys}_ode2odes (state0, numpar, simpar)\n")
{
static octave_value_list retval;
static ColumnVector state0 (MTTNX);
static ColumnVector numpar (MTTNPAR);
static ColumnVector simpar (8);
int nargin = args.length ();
switch (nargin)
{
case 3:
simpar = args(2).${vector_value} ();
numpar = args(1).${vector_value} ();
state0 = args(0).${vector_value} ();
break;
case 2:
simpar = mtt_simpar ();
numpar = args(1).${vector_value} ();
state0 = args(0).${vector_value} ();
break;
case 1:
simpar = mtt_simpar ();
numpar = mtt_numpar ();
state0 = args(0).${vector_value} ();
break;
case 0:
simpar = mtt_simpar ();
numpar = mtt_numpar ();
state0 = mtt_state (numpar);
break;
default:
usage("${sys}_ode2odes (x par simpar)", nargin);
error("aborting.");
}
${sys}_ode2odes (state0, numpar, simpar);
retval = octave_value (get_global_value ("MTT_data"));
return (retval);
}
#endif // (CODEGENTARGET == STANDALONE)
EOF
}
case ${lang} in
m)
make_m
;;
cc)
make_cc
;;
*)
echo Language ${lang} is not supported
esac
|