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
|
// -*-c++-*-
// <mtt_model_name>_sfun.cc:
// Matlab S-function simulation of <mtt_model_name>
#define S_FUNCTION_NAME <mtt_model_name>_sfun
#define S_FUNCTION_LEVEL 2
#include "simstruc.h"
#include <iostream>
#include <string>
#include "<mtt_model_name>_def.h"
static mxArray *dX = mxCreateDoubleMatrix (MTTNX , 1, mxREAL);
static mxArray *P = mxCreateDoubleMatrix (MTTNPAR , 1, mxREAL);
static mxArray *T = mxCreateDoubleMatrix (1 , 1, mxREAL);
static mxArray *U = mxCreateDoubleMatrix (MTTNU , 1, mxREAL);
static mxArray *X = mxCreateDoubleMatrix (MTTNX , 1, mxREAL);
static mxArray *Y = mxCreateDoubleMatrix (MTTNY , 1, mxREAL);
static double *inputs = mxGetPr (U);
static double *outputs = mxGetPr (Y);
static double *parameters = mxGetPr (P);
static double *rates = mxGetPr (dX);
static double *states = mxGetPr (X);
static double *simtime = mxGetPr (T);
void
update_parameters_from_simulink (SimStruct *S)
{
std::cerr << "Parameters:\t";
for (int i = 0; i < MTTNPAR; i++) {
parameters [i] = *mxGetPr (ssGetSFcnParam (S, i));
std::cerr << parameters [i] << ' ';
}
std::cerr << std::endl;
}
void
update_states_from_simulink (SimStruct *S)
{
static double *x;
std::cerr << "States:\t\t";
x = ssGetContStates (S);
for (int i = 0; i < MTTNX; i++) {
states [i] = x [i];
std::cerr << states [i] << ' ';
}
std::cerr << std::endl;
}
void
update_inputs_from_simulink (SimStruct *S)
{
std::cerr << "Inputs:\t\t";
for (int i = 0; i < MTTNU; i++) {
inputs [i] = *ssGetInputPortRealSignalPtrs (S, i)[0];
std::cerr << inputs [i] << ' ';
}
std::cerr << std::endl;
}
void
update_simtime_from_simulink (SimStruct *S)
{
simtime [0] = ssGetT (S);
std::cerr << "Time:\t\t" << simtime [0] << std::endl;
}
// S-function methods
static void mdlInitializeSizes(SimStruct *S)
{
ssSetNumSFcnParams(S, MTTNPAR);
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
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
|
// -*-c++-*-
// <mtt_model_name>_sfun.cc:
// Matlab S-function simulation of <mtt_model_name>
#define S_FUNCTION_NAME <mtt_model_name>_sfun
#define S_FUNCTION_LEVEL 2
#include "simstruc.h"
#include <string>
#include "<mtt_model_name>_def.h"
static mxArray *P = mxCreateDoubleMatrix (MTTNPAR , 1, mxREAL);
static mxArray *T = mxCreateDoubleMatrix (1 , 1, mxREAL);
static mxArray *U = mxCreateDoubleMatrix (MTTNU , 1, mxREAL);
static mxArray *X = mxCreateDoubleMatrix (MTTNX , 1, mxREAL);
static double *inputs = mxGetPr (U);
static double *parameters = mxGetPr (P);
static double *states = mxGetPr (X);
static double *simtime = mxGetPr (T);
void
update_parameters_from_simulink (SimStruct *S)
{
for (int i = 0; i < MTTNPAR; i++) {
parameters [i] = *mxGetPr (ssGetSFcnParam (S, i));
}
}
void
update_states_from_simulink (SimStruct *S)
{
static double *x;
x = ssGetContStates (S);
for (int i = 0; i < MTTNX; i++) {
states [i] = x [i];
}
}
void
update_inputs_from_simulink (SimStruct *S)
{
for (int i = 0; i < MTTNU; i++) {
inputs [i] = *ssGetInputPortRealSignalPtrs (S, i)[0];
}
}
void
update_simtime_from_simulink (SimStruct *S)
{
simtime [0] = ssGetT (S);
}
// S-function methods
static void mdlInitializeSizes(SimStruct *S)
{
ssSetNumSFcnParams(S, MTTNPAR);
|
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
|
update_parameters_from_simulink (S);
plhs[0] = X;
prhs[0] = P;
mexCallMATLAB (1, plhs, 1, prhs, "<mtt_model_name>_state");
std::cerr << "Initial states;\t";
for (int i = 0; i < MTTNX; i++) {
ssGetContStates (S)[i] = states [i];
std::cerr << states [i] << ' ';
}
std::cerr << std::endl;
}
static void mdlOutputs(SimStruct *S, int_T tid)
{
static mxArray *plhs[1];
static mxArray *prhs[4];
update_states_from_simulink (S);
update_inputs_from_simulink (S);
update_simtime_from_simulink (S);
update_parameters_from_simulink (S);
plhs[0] = Y;
prhs[0] = X;
prhs[1] = U;
prhs[2] = T;
prhs[3] = P;
UNUSED_ARG(tid); // not used in single tasking mode
mexCallMATLAB (1, plhs, 4, prhs, "<mtt_model_name>_odeo");
std::cerr << "Outputs:\t";
for (int i = 0; i < MTTNY; i++) {
ssGetOutputPortRealSignal (S,i)[0] = outputs [i];
std::cerr << outputs [i] << ' ';
}
std::cerr << std::endl;
}
#define MDL_DERIVATIVES
static void mdlDerivatives(SimStruct *S)
{
static mxArray *plhs[1];
static mxArray *prhs[4];
update_states_from_simulink (S);
update_inputs_from_simulink (S);
update_simtime_from_simulink (S);
update_parameters_from_simulink (S);
plhs[0] = dX;
prhs[0] = X;
prhs[1] = U;
prhs[2] = T;
prhs[3] = P;
mexCallMATLAB (1, plhs, 4, prhs, "<mtt_model_name>_ode");
std::cerr << "Rates:\t\t";
for (int i = 0; i < MTTNX; i++) {
ssGetdX (S)[i] = rates [i];
std::cerr << rates [i] << ' ';
}
std::cerr << std::endl;
}
static void mdlTerminate(SimStruct *S)
{
UNUSED_ARG(S);
}
#ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
#include "simulink.c" /* MEX-file interface mechanism */
#else
#include "cg_sfun.h" /* Code generation registration function */
#endif
|
<
<
<
<
>
<
<
<
<
>
|
<
<
<
|
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
|
update_parameters_from_simulink (S);
plhs[0] = X;
prhs[0] = P;
mexCallMATLAB (1, plhs, 1, prhs, "<mtt_model_name>_state");
for (int i = 0; i < MTTNX; i++) {
ssGetContStates (S)[i] = states [i];
}
}
static void mdlOutputs(SimStruct *S, int_T tid)
{
static mxArray *plhs[1];
static mxArray *prhs[4];
update_states_from_simulink (S);
update_inputs_from_simulink (S);
update_simtime_from_simulink (S);
update_parameters_from_simulink (S);
prhs[0] = X;
prhs[1] = U;
prhs[2] = T;
prhs[3] = P;
UNUSED_ARG(tid); // not used in single tasking mode
mexCallMATLAB (1, plhs, 4, prhs, "<mtt_model_name>_odeo");
double *outputs = mxGetPr (plhs[0]);
for (int i = 0; i < MTTNY; i++) {
ssGetOutputPortRealSignal (S,i)[0] = outputs [i];
}
}
#define MDL_DERIVATIVES
static void mdlDerivatives(SimStruct *S)
{
static mxArray *plhs[1];
static mxArray *prhs[4];
update_states_from_simulink (S);
update_inputs_from_simulink (S);
update_simtime_from_simulink (S);
update_parameters_from_simulink (S);
prhs[0] = X;
prhs[1] = U;
prhs[2] = T;
prhs[3] = P;
mexCallMATLAB (1, plhs, 4, prhs, "<mtt_model_name>_ode");
double *rates= mxGetPr (plhs[0]);
for (int i = 0; i < MTTNX; i++) {
ssGetdX (S)[i] = rates [i];
}
}
static void mdlTerminate(SimStruct *S)
{
UNUSED_ARG(S);
}
#ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
#include "simulink.c" /* MEX-file interface mechanism */
#else
#include "cg_sfun.h" /* Code generation registration function */
#endif
|