Translating a matlab model with S-functions to a python model with C extensions, problem with mdlDerivatives
Show older comments
As the title says I'm trying to translate the files of a model of a wastewater treatment plant consisting from .m-files and c-mex s-functions into .py-files and .c-Extensions. For the most part it's working perfectly well, however I can't seem to get the mdlDerivatives block of the S-function to work properly. As I am very new to matlab this might just be a lack of understanding.
I read a bunch of posts and documentation and watched some videos, by my understanding of s-functions we have inputs u, outputs y, states x and derivatives of the states dx.
y = f(u, x), dx = f(u, x)
In my model I can easily save inputs and outputs over 1344 timesteps to the workspace, afaik saving states and derivatives requires deeper knowledge. By knowing u and y from simulating the original model I can however calculate x and dx as functions of u and y. After testing this works at least for the initial states from mdlInitializeConditions.
Below is an exerpt of one of the c-mex s-function I want to translate. I only kept the parts I deemed relevant and simplified most calculations. The entries of the arrays are mostly concentrations with the mass flow at index 14.
My expectation for the correlation of dx and x was x(t) = x(t-1) + dx(t-1), maybe with a factor "a" x(t) = x(t-1) + a*dx(t-1). Comparing the derivatives as well as the changes in state of two timesteps however the different entries of the arrays show no correlation to the calculated derivatives.
...
static void mdlInitializeSampleTimes(SimStruct *S)
{
ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
ssSetOffsetTime(S, 0, 0.0);
}
/* can replicate */
static void mdlInitializeConditions(double *x0, SimStruct *S)
{
int i;
x0[i] = mxGetPr(XINIT)[i]; /* Initial values from parameter */
}
/* can replicate */
static void mdlOutputs(double *y, double *x, double *u, SimStruct *S, int tid)
{
int i;
for (i = 0; i < 21; i++) {
y[i] = x[i]/x[14];
}
y[14] = x[14];
}
static void mdlUpdate(double *x, double *u, SimStruct *S, int tid)
{
}
/* cannot replicate */
static void mdlDerivatives(double *dx, double *x, double *u, SimStruct *S, int tid)
{
int i;
for (i = 0; i < 21; i++) {
dx[i] = (u[i]*u[14]-x[i]*x[14]);
}
dx[14] = u[14]-x[14]
}
static void mdlTerminate(SimStruct *S)
{
}
...
Answers (0)
Categories
Find more on Configure C/C++ S-Function Features in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!