Differences From Artifact [fc7b63a944]:

To Artifact [dcace679aa]:


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

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
-
+







-
-
-
-
-
-
-
-


-
-
+
+


















+







+






+






+






+

















-
+








-
+





-
+



-
+





-
+

-
+

-
+













-
+







/* -*-c-*-
/* -*-c-*-	Put emacs into c-mode
 * <mtt_model_name>_sfun.c:
 * Matlab S-function simulation of <mtt_model_name>
 */

#define S_FUNCTION_NAME <mtt_model_name>_sfun
#define S_FUNCTION_LEVEL 2

#if (0)				/* DEBUG? */
#define PRINT_ENTER(f) fprintf (stderr, "Entered %s\n", f)
#define PRINT_LEAVE(f) fprintf (stderr, "Leaving %s\n", f)
#else
#define PRINT_ENTER(f)
#define PRINT_LEAVE(f)
#endif /* DEBUG? */

#include <stdio.h>
#include <stdlib.h>

#include "simstruc.h"
#include "simstruc.h"
#include "sfun_debug.h"
#include "<mtt_model_name>_def.h"

static double *mttdx;		/* pointer to rates */
static double *mttu;		/* pointer to inputs */
static double *mttpar;		/* pointer to parameters */
static double *mttx;		/* pointer to states */
static double *mtty;		/* pointer to outputs */
static double *mttyz;		/* pointer to residuals */
static double  mttt;		/* time */

static unsigned int i;		/* loop counter */

/* system equations */

static void
<mtt_model_name>_ae (void)
{
#include "<mtt_model_name>_ae.c"
  PRINT_LEAVE;
}

static void
<mtt_model_name>_numpar (void)
{
#include "<mtt_model_name>_sympar.h"
#include "<mtt_model_name>_numpar.c"
  PRINT_LEAVE;
}

static void
<mtt_model_name>_ode (void)
{
#include "<mtt_model_name>_ode.c"
  PRINT_LEAVE;
}

static void
<mtt_model_name>_odeo (void)
{
#include "<mtt_model_name>_odeo.c"
  PRINT_LEAVE;
}

static void
<mtt_model_name>_state (void)
{
#include "<mtt_model_name>_state.c"
  PRINT_LEAVE;
}

/* utility procedures */

static double *
array_of_double (size_t n)
{
  void *p = calloc (n, sizeof (double));
  if (! p) {
    fprintf (stderr, "*** Error: failed to allocate memory\n");
  }
  return (double *) p;
}

static void
initialise_arrays (void)
{
  PRINT_ENTER("initialise_arrays");
  PRINT_ENTER;

  mttdx		= array_of_double (MTTNX);
  mttpar	= array_of_double (MTTNPAR);
  mttu		= array_of_double (MTTNU + MTTNYZ);
  mttx		= array_of_double (MTTNX);
  mtty		= array_of_double (MTTNY);
  mttyz		= array_of_double (MTTNYZ);

  PRINT_LEAVE("initialise_arrays");
  PRINT_LEAVE;
}

static void
update_states_from_simulink (SimStruct *S)
{
  PRINT_ENTER("update_states_from_simulink");
  PRINT_ENTER;
  for (i = 0; i < MTTNX; i++) {
    mttx[i] = ssGetContStates (S)[i];
  }
  PRINT_LEAVE("update_states_from_simulink");
  PRINT_LEAVE;
}

static void
update_inputs_from_simulink (SimStruct *S)
{
  PRINT_ENTER("update_inputs_from_simulink");
  PRINT_ENTER;
  for (i = 0; i < MTTNU; i++) {
    mttu[i] = *ssGetInputPortRealSignalPtrs (S, i)[0];
    mttu[i] = *ssGetInputPortRealSignalPtrs (S, 0)[i];
  }
  PRINT_LEAVE("update_inputs_from_simulink");
  PRINT_LEAVE;
}

static void
update_inputs_from_solver (void)
{
  mxArray *MTT_MATLAB_P;
  mxArray *MTT_MATLAB_T;
  mxArray *MTT_MATLAB_U;
  mxArray *MTT_MATLAB_Ui;
  mxArray *MTT_MATLAB_X;

  double *p;

  PRINT_ENTER ("update_inputs_from_solver");
  PRINT_ENTER;

  /* starting value for solver - start with zero */
  MTT_MATLAB_Ui = mxCreateDoubleMatrix (MTTNYZ, 1, mxREAL);
  mxSetName (MTT_MATLAB_Ui, "MTT_Ui");
  p = mxGetPr (MTT_MATLAB_Ui);
  for (i = 0; i < MTTNYZ; i++) {
    p[i] = 0.0;
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
241
242
243
244
245

246
247
248
249
250
251
252
253
254

255
256
257
258
259

260
261
262
263
264
265
266
267
268
269
270
271




272
273

274
275
276

277
278
279
280
281
282

283
284
285
286
287
288
289
290
291
292
293
294
295
296
297

298
299
300
301
302

303
304
305
306
307
308
309
310
311
312

313
314
315
316
317
318
319
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
241
242

243
244
245
246
247
248
249
250
251

252
253
254
255
256

257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274

275
276
277

278
279
280
281
282
283

284
285
286
287
288
289
290
291
292
293
294
295
296
297
298

299
300
301
302
303

304
305
306
307
308
309
310
311
312
313

314
315
316
317
318
319
320
321







-
+





-
+

-
+






-
+
+


-
+






-
+
-
-
-
-
+
+
+
-
-
-
-
-
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+




-
+
+


+
-
+





-
+








-
+




-
+












+
+
+
+

-
+


-
+





-
+














-
+




-
+









-
+







  /* free memory */
  mxDestroyArray (MTT_MATLAB_P);
  mxDestroyArray (MTT_MATLAB_T);
  mxDestroyArray (MTT_MATLAB_U);
  mxDestroyArray (MTT_MATLAB_Ui);
  mxDestroyArray (MTT_MATLAB_X);

  PRINT_LEAVE ("update_inputs_from_solver");
  PRINT_LEAVE;
}

static void
update_simtime_from_simulink (SimStruct *S)
{
  PRINT_ENTER("update_simtime_from_simulink");
  PRINT_ENTER;
  mttt = ssGetT (S);
  PRINT_LEAVE("update_simtime_from_simulink");
  PRINT_LEAVE;
}

/* S-function methods */

static void mdlInitializeSizes(SimStruct *S)
{
  PRINT_ENTER("mdlInitializeSizes");
  PRINT_ENTER;

  ssSetNumSFcnParams(S, 0); 
  if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
    PRINT_LEAVE("mdlInitializeSizes");
    PRINT_LEAVE;
    return;
  }
  
  ssSetNumContStates(S, MTTNX);
  ssSetNumDiscStates(S, 0);
  
  if (!ssSetNumInputPorts(S, MTTNU)) return;
  if (!ssSetNumInputPorts(S, 1)) return;
    for (i = 0; i < MTTNU; i++) {
      ssSetInputPortWidth(S, i, 1);
      ssSetInputPortDirectFeedThrough(S, i, 1);
    }
  ssSetInputPortWidth(S, 0, MTTNU);
  ssSetInputPortDirectFeedThrough(S, 0, 1);

    
    if (!ssSetNumOutputPorts(S, MTTNY)) return;
    for (i = 0; i < MTTNY; i++) {
      ssSetOutputPortWidth(S, i, 1);
    }
  if (!ssSetNumOutputPorts(S, 2)) return;
  ssSetOutputPortWidth(S, 0, MTTNX);
  ssSetOutputPortWidth(S, 1, MTTNY);
    
    
    ssSetNumSampleTimes(S, 1);
    ssSetNumRWork(S, 0);
    ssSetNumIWork(S, 0);
    ssSetNumPWork(S, 0);
    ssSetNumModes(S, 0);
    ssSetNumNonsampledZCs(S, 0);
    
    ssSetOptions(S, SS_OPTION_EXCEPTION_FREE_CODE);
    
    initialise_arrays ();
    PRINT_LEAVE("mdlInitializeSizes");
  ssSetNumSampleTimes(S, 1);
  ssSetNumRWork(S, 0);
  ssSetNumIWork(S, 0);
  ssSetNumPWork(S, 0);
  ssSetNumModes(S, 0);
  ssSetNumNonsampledZCs(S, 0);
  
  ssSetOptions(S, SS_OPTION_EXCEPTION_FREE_CODE);
  
  initialise_arrays ();

  PRINT_LEAVE;
}

static void mdlInitializeSampleTimes(SimStruct *S)
{
  PRINT_ENTER("mdlInitializeSampleTimes");
  PRINT_ENTER;

  ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
  ssSetOffsetTime(S, 0, 0.0);

  PRINT_LEAVE("mdlInitializeSampleTimes");
  PRINT_LEAVE;
}

#define MDL_INITIALIZE_CONDITIONS
static void mdlInitializeConditions(SimStruct *S)
{
  PRINT_ENTER("mdlInitializeConditions");
  PRINT_ENTER;

  <mtt_model_name>_numpar ();
  <mtt_model_name>_state ();

  for (i = 0; i < MTTNX; i++) {
    ssGetContStates (S)[i] = mttx[i];
  }

  PRINT_LEAVE("leaving mdlInitializeConditions");
  PRINT_LEAVE;
}

static void mdlOutputs(SimStruct *S, int_T tid)
{
  PRINT_ENTER("entered mdlOutputs");
  PRINT_ENTER;

  update_states_from_simulink (S);
  update_inputs_from_simulink (S);
  if (MTTNYZ > 0) {
    update_inputs_from_solver ();
  }
  update_simtime_from_simulink (S);

  UNUSED_ARG(tid); /* not used in single tasking mode */

  <mtt_model_name>_odeo ();

  for (i = 0; i < MTTNX; i++) {
      ssGetOutputPortRealSignal (S, 0)[i] = mttx[i];
  }

  for (i = 0; i < MTTNY; i++) {
      ssGetOutputPortRealSignal (S,i)[0] = mtty[i];
      ssGetOutputPortRealSignal (S, 1)[i] = mtty[i];
  }

  PRINT_LEAVE("leaving mdlOutputs");
  PRINT_LEAVE;
}

#define MDL_DERIVATIVES
static void mdlDerivatives(SimStruct *S)
{
  PRINT_ENTER("entered mdlDerivatives\n");
  PRINT_ENTER;
    
  update_states_from_simulink (S);
  update_inputs_from_simulink (S);
  if (MTTNYZ > 0) {
    update_inputs_from_solver ();
  } 
  update_simtime_from_simulink (S);

  <mtt_model_name>_ode ();

  for (i = 0; i < MTTNX; i++) {
    ssGetdX (S)[i] = mttdx[i];
  }

  PRINT_LEAVE("leaving mdlDerivatives");
  PRINT_LEAVE;
}

static void mdlTerminate(SimStruct *S)
{
  PRINT_ENTER("entered mdlTerminate");
  PRINT_ENTER;
  UNUSED_ARG(S);

  free (mttdx);
  free (mttpar);
  free (mttu);
  free (mttx);
  free (mtty);
  free (mttyz);

  PRINT_LEAVE("leaving mdlTerminate");
  PRINT_LEAVE;
}

#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

MTT: Model Transformation Tools
GitHub | SourceHut | Sourceforge | Fossil RSS ]