How do I tackle the artificial algebraic loops that Simulink detects?

3 views (last 30 days)
This is a bit of a convoluted problem that deals with a slightly open ended question. I realize that StackOverflow frowns on this practice, but as you will see, it is hard for me to break it down into smaller parts.
I have C++ code that is called using a CMEX S-Function (compiled using the legacy_code). I hand modified the wrapper file to make the S-Fn continuous in the minor time step. This is important because, when it was fixed in the minor step, I would see a staircase-like change in the function outputs instead of a smooth line. Since making it continuous, this problem has been solved.
Unfortunately, I later discovered that the Memory blocks between the input signals and the S-Function (that I was using to solve the algebraic loops) are fixed in the minor time step.
So instead, I referred to this Mathworks documentation. They claim that the PWork vector can be used to keep values persistent in memory between successive invocations of the S-Function. I was able to use this vector to do this: 1. Store values of current time step for next step 2. In the next step, use the old values 3. After the calculations, update old values to current values 4. Repeat
In essence, I replicated the Memory block functionality internally. I thought I could now remove the Memory blocks and replace them with the Initial Condition block. Unfortunately, on doing this, the warning for algebraic loop appears. Now, my model sometimes solves and sometimes doesn't.
The two cases are shown below schematically:
S-Function and S-Function2 are the CMEX objects of interest.
I think perhaps the CMEX function is dense to the algebraic loop diagnostics code, and Simulink is unable to realize that there actually isn't a real algebraic loop but only an artificial one?
My question is: Is there something that I can do to tell Simulink that there actually ISN'T an algebraic loop? Is there some kind of best practice for what to do in such cases?

Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by