The last two weeks weren’t so great: I was haunted by segmentation faults all the time when trying to run the OpenModelica generated model library. Therefore, I couldn’t achieve any new meaningful result worth blogging about.
The idea presented here in my previous posts of issuing different FMI2 calls for each phase (flag) of computational function processing was somehow causing crashes, even if the Scicos block and FMI2 model data pointers seemed ok, and I simply didn’t have a clue about why.
A New Approach
One thing that I’ve noted while trying different fixes for my problem was that FMI2 calls right after initialization (fmi2Intantiate and fmi2SetupExperiment) always went smoothly. So, in a more radical (or conservative, depending on how you see it) attempt to prevent crashes I’ve reestructured my code to perform model creation and termination at each computational function call:
and finally get some results (Yay !):
(Resulting plot for chaos model simulation)
Refinements
As expected, the process of creating and destroying the FMI2 component structure on every processing step is expensive, mostly, I suppose, due to allocation and deallocation of memory, which also violates the rule of just performing it for flags4 (initialization) and 5 (termination). I’ve verified that when seeing the simulation running in a much slower speed than with the model library generated by modelicac compiler.
Gladly, keeping the model stored in a static variable, calling fmi2Instantiate only on the first computation step, fmi2Reset on subsequential ones and fmi2FreeInstance only in the end, also works. That change, combined with disabling FMU logs and other text output functions, allows things to run almost as fast as with the older compiler.
As a result, the reestructured code for fmi2_wrapper.h looks like this:
Final Thoughts
Even if now it is possible to get the simulation to output something usable, segfaults still happen rather frequently, and the results are not exactly the same as with modelicac (which is more visible with other ModelicaXcos demos). From now until the end of GSoC Working Period, my main task will involve using debugging tools (like running Scilab with -debug command-line option) to investigate remaining crashes and trying to match outputs from OpenModelica models to modelicac ones as much as possible.
That’s it for now. Thanks one more time for sticking by. See you soon !