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