roll back slbuild genrated model by one step

I have generated a Simscape model to c excutable and code using slbuild and it has produced a main code rt_malloc_main in C which I am editing. I will eventually build that executable with gcc (and where needed g++) and use in Ubuntu 22.04.
For various reasons I need to roll back the model, i.e. reject all the progress made during the last call to rt_OneStep(S). This is a large model with many simscapep multibody library blocks, but also simulink blocks, and some matlab function blocks which use global variables through the persistent command.
What I ideally want would look like this:
save model S
advance one step: rt_OneStep(S)
if condition is not achieved completely roll back everything to how they where before rt_OneStep was called, or in the very least time and states so that a new call to rt_OneStep would give the exact same results: restore(S)
I currently have some ad-hoc lines of code to do save and restore and various efforts have also led me to use the rt_terminate (S) and rt_Init(S) to avoid bad memory allocation problems, and then use restore(S) in the hope that I also change my time and all states in the various blocks.
Is there a formal way to go back one step and cancel the effects of rt_OneStep(S) without starting the simulation from t=0?
The reason behind this roll back is that I am coupling this code with another code that needs the roll-back

3 comentarios

sneha
sneha el 14 de Nov. de 2025
Hello,
there is no formal MathWorks-supported API to roll back a Simscape model one step in generated C code, because SimState and operating-point features are not supported for code generation with Simscape; the only reliable approach is to manually save and restore all simulation state memory before calling rt_OneStep(S), including continuous states (X), discrete work vectors (DW), mode vectors, zero-crossing signals, solver workspaces, timing fields, and Simscape internal runtime structures.
relevant MathWorks documentation:
E_C
E_C el 17 de Nov. de 2025
Thanks for teh suggestion I believe I am restoring almost all of the above but I am not sure what is the name to use for saving and restoring Simscape internal runtime structures
e.g. for continuoustates I can for example access them as: S->contStates (S is my model)
E_C
E_C el 18 de Nov. de 2025
And perhaps if there is some guidance of what elements of DW (dwork) need to be saved and restored or how to configure slbuild to group them in a conveninent way to do so that would be very useful as a shallow copy of those doesn't seem to work (memcpy)

Iniciar sesión para comentar.

Respuestas (0)

Preguntada:

E_C
el 10 de Nov. de 2025

Comentada:

E_C
el 18 de Nov. de 2025

Community Treasure Hunt

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

Start Hunting!

Translated by