MATLAB crashes when running s-function

6 views (last 30 days)
Maliha
Maliha on 2 Nov 2014
Commented: Geoff Hayes on 2 Nov 2014
So I am having trouble with my codes for a superheater. The mex file compiles but when I run the s-function block in simulink it crashes. Later it says its a segmentation error. Will be really helpful if someone can help me identify problem. Thanks in advance.
#define S_FUNCTION_NAME see #define S_FUNCTION_LEVEL 2
#include "simstruc.h"
#include math.h #include stdio.h
static void mdlInitializeSizes(SimStruct *S) {
/* See sfuntmpl_doc.c for more details on the macros below */
ssSetNumSFcnParams(S, 1); /* Number of expected parameters */
if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
/* Return if number of expected != number of actual parameters */
return;
}
ssSetNumContStates(S, 12);
ssSetNumDiscStates(S, 0);
if (!ssSetNumInputPorts(S, 2)) return;
ssSetInputPortWidth(S, 0, DYNAMICALLY_SIZED);
ssSetInputPortWidth(S, 1, DYNAMICALLY_SIZED);
ssSetInputPortDirectFeedThrough(S, 0, 0);
ssSetInputPortDirectFeedThrough(S, 1, 0);
if (!ssSetNumOutputPorts(S, 2)) return;
ssSetOutputPortWidth(S, 0, DYNAMICALLY_SIZED);
ssSetOutputPortWidth(S, 1, DYNAMICALLY_SIZED);
ssSetNumSampleTimes(S, 1);
/* Specify the sim state compliance to be same as a built-in block */
ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
ssSetOptions(S, 0);
}
static void mdlInitializeSampleTimes(SimStruct *S) { ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME); ssSetOffsetTime(S, 0, 0.0); }
#define MDL_INITIALIZE_CONDITIONS /* Change to #undef to remove function */ #if defined(MDL_INITIALIZE_CONDITIONS)
static void mdlInitializeConditions(SimStruct *S)
{
real_T *x = ssGetContStates(S);
int_T lp;
int_T N = ssGetSFcnParam(S, 0);
for (lp=0;lp<3*N-1;lp++) {
*x++=0.0;
}
}
#endif /* MDL_INITIALIZE_CONDITIONS */
/* Function: mdlOutputs ======================================================= * Abstract: * In this function, you compute the outputs of your S-function * block. */
static void mdlOutputs(SimStruct *S, int_T tid) { int_T y[] = {ssGetOutputPortRealSignal(S,0),ssGetOutputPortRealSignal(S,1)};
int_T N = ssGetSFcnParam(S,0);
real_T *x = ssGetContStates(S);
UNUSED_ARG(tid);
y[0] = x[N-1] ;
y[1] = x[2*N-1];
}
#define MDL_DERIVATIVES /* Change to #undef to remove function / #if defined(MDL_DERIVATIVES) / Function: mdlDerivatives ================================================= * Abstract: * In this function, you compute the S-function block's derivatives. * The derivatives are placed in the derivative vector, ssGetdX(S). */ static void mdlDerivatives(SimStruct *S) { real_T *dx = ssGetdX(S); real_T *x = ssGetContStates(S); int_T N = ssGetSFcnParam(S, 0);
int_T T1_vst= 11;
int_T T2_vst= -7;
int_T tau1 = 50;
int_T tau2 = 100;
int_T taus1 = 200;
int_T taus2 = 300;
int_T L = 60;
int_T h, n;
int_T u1 = ssGetInputPortRealSignal(S,0);
int_T u2 = ssGetInputPortRealSignal(S,1);
h=L/N;
//slice 1 , n=0
dx[0] = 1/tau1*(x[2*N]-x[0])-u1/(2*h)*(x[1]-T1_vst);
dx[N] = 1/tau2*(x[2*N] - x[N]) - u2/(2*h)*(x[N+1] -T2_vst);
dx[2*N] = 1/taus1*(x[0] - x[2*N]) + 1/taus2*(x[N] -x[2*N]);
//slice 2 to N-1, n=1 to N-2
for (n = 1; n <= N-2; n++) {
dx[n]=1/tau1*(x[n+2*N]-x[n])-u1/(2*h)*(x[n+1]-x[n-1]);
dx[n+N]=1/tau2*(x[n+2*N]-x[n+N])-u2/(2*h)*(x[n+N+1]-x[n+N-1]);
dx[n+2*N]=1/taus1*(x[n]-x[n+2*N])+1/taus2*(x[n+N]-x[n+2*N]); }
//slice N, n=N-1
dx[N-1]=1/tau1*(x[3*N-1]-x[N-1])-u1/(2*h)*(x[N-3]-4*x[N-2]+3*x[N-1]);
dx[2*N-1]=1/tau2*(x[3*N-1]-x[2*N-1])-u2/(2*h)*(x[2*N-3]-4*x[2*N-2]+3*x[2*N-1]);
dx[3*N-1]=1/taus1*(x[N-1]-x[3*N-1])+1/taus2*(x[2*N-1]-x[3*N-1]);
}
#endif /* MDL_DERIVATIVES */
/* Function: mdlTerminate ===================================================== * Abstract: * In this function, you should perform any actions that are necessary * at the termination of a simulation. For example, if memory was * allocated in mdlStart, this is the place to free it. */ static void mdlTerminate(SimStruct *S) { UNUSED_ARG(S); }
/*======================================================* * See sfuntmpl_doc.c for the optional S-function methods ======================================================*/
/*=============================* * Required S-function trailer =============================*/
#ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? / #include "simulink.c" / MEX-file int_Terface mechanism / #else #include "cg_sfun.h" / Code generation registration function */ #endif
  1 Comment
Geoff Hayes
Geoff Hayes on 2 Nov 2014
Maliha - the error could be the result of accessing or modifying an array element that is outside the bounds of the array. I don't know enough about how the Simulink code is supposed to work, but if you look at the mdlInitializeSizes function, the number of continuous states is set as 12 via
ssSetNumContStates(S, 12);
Then in the mdlInitializeConditions function, those same (?) are retrieved with
real_T *x = ssGetContStates(S);
and some sort of function parameter
int_T N = ssGetSFcnParam(S, 0);
Then the sets all elements of x to zero via
for (lp=0;lp<3*N-1;lp++) {
*x++=0.0;
}
My question then is what is N and how does it relate to the 12, the number of continuous states? Because if x is an array of 12 continuous states, and N is is such that 3*N-1 is greater than 12, then it could be that the code is stepping beyond the bounds of the array.

Sign in to comment.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!