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?