1

I want to simulate an OpenModelica model from within Python using an FMU. When the model is simulated in OMEdit, the results are correct. When the corresponding FMU is simulated using FMPy, the results are not correct. I am trying to find out if I make a mistake or if this is bigger problem.

As an illustration consider a common OpenModelica example (BouncingBall.zip). The graphs below show the correct result when simulated in OpenModelica, whereas the height of the bouncing ball reaches below zero when using the FMU with FMPy. The code for OMSHell to reproduce the first graph and to generate the FMU:

loadFile("BouncingBall.mo")
simulate(BouncingBall, method="cvode", stopTime=3, numberOfIntervals=60)
plot(h)

setCommandLineOptions("--fmiFlags=s:cvode")
buildModelFMU(BouncingBall, version="2.0", fmuType="cs", fileNamePrefix="BouncingBall_cs")

Following code block shows how FMPy was used for the simulation. The same settings were applied when doing the simulation in OpenModelica.

from fmpy import simulate_fmu
from fmpy.util import plot_result
    
result = simulate_fmu(
    filename='BouncingBall.fmu',
    validate=False,
    start_time=0,
    stop_time=3,
    solver='CVode',
    step_size=0.001,
    output_interval=5e-2,
    output=['h'])
        
plot_result(result=result)

Simulation in OMEdit Simulation in FMPy using FMU

I used OpenModelica v1.21.0-dev.beta.2 on Windows.

By choosing a larger number of samples or a smaller output interval, the error becomes smaller. This seems to indicate that the CVode solver is not able to vary the stepsize by itself.

One SO question (link) reports about failing simulations when using the CVode solver with an FMU. The same failing simulations were mentioned in an issue report (link). This particular problem seems fixed since vs1.20 of OpenModelica. Indeed my simulations, using the same model, do not give an error message. However the graphs in the issue report clearly also show incorrect results (height of the bouncing ball less than zero).

Is the problem of incorrect results still related to the issue of failing simulations, or am I making another mistake?

  • 1
    Can you share the FMU? I assume it is a model-exchange FMU? (you can check this in the modelDescription.xml). If the FMU supports both model-exchange and co-simulation variant, please call fmu_simulate for simulating it as a model-exchange FMU by passing the argument fmi_type='ModelExchange' ,see https://github.com/CATIA-Systems/FMPy/blob/ca079372ef743266e208c2ec603b52466807a0e7/fmpy/simulation.py#L595 – Christian Bertsch Apr 18 '23 at 20:41
  • Thank you! I have added the co-simulation FMU as a link in the text, along with a way to generate it. I have done as you suggested and specified fmi_type='ModelExchange'. Also I have generated a model-exchange FMU. Both ways seem to give correct results. My immediate problem is solved now. Do you have some background on why this works? Is this behavior intended or not? – Twan Bearda Apr 20 '23 at 13:21

0 Answers0