'Translating a matlab model with S-functions to a python model with C extensions, problem with mdlDerivatives

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

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-1) + a*dx(t-1). However after comparing differences between states of different timesteps and the derivatives there seems to be no correlation at all.

All I need for my translated model to work is to calculate my state values for the next timestep, but with the formula of x(t) = x(t-1) + dx(t-1) I just can't seem to get the same results as the original model.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source