Open cyrosd opened 1 year ago
I try your fmu with python 3.8.8
and fmpy
module as well as exporting the PIDtest_noinput.mo
using OpenModelica v1.22.0-dev-39-g23fdc84c46 (64-bit)
resulting the expected results
import fmpy
import matplotlib.pyplot as plt
import pandas as pd
result = fmpy.simulate_fmu(PathtoFMU, stop_time=100, output_interval=0.001)
result_df = pd.DataFrame(result)
fig, ax = plt.subplots()
ax.plot(result_df['time'], result_df['pulse1'], result_df['time'], result_df['y'])
Are you sure you're using co-simulation? using fmpy.gui, I get this result
import pyfmi
import matplotlib.pyplot as plt
fmu = pyfmi.FMUModelCS2('X:/PIDtest/PIDtest_noinput.fmu')
opts = fmu.simulate_options()
opts['ncp'] = int(100/0.001)
result = fmu.simulate(final_time=100, options=opts)
ax.plot(result['time'],result['pulse1'], result3'time'],result['y'])
import matplotlib.pyplot as plt
import pyfmi
fmu = pyfmi.FMUModelCS2('X:/PIDtest/PIDtest_noinput.fmu')
fig, ax = plt.subplots()
result = fmu.simulate(final_time=100)
ax.plot(result['time'],result['pulse1'], result['time'],result['y'])
I dont understand how
IMHO: since euler is a fixed step size, one should take into account, how big/small the step should be.ncp
change the behavior of model.
Edit: yields the same problem with above example using pyfmi.
import fmpy, pandas
import matplotlib.pyplot as plt
fmu = fmpy.fmi2('X:/PIDtest/PIDtest_noinput.fmu')
res = fmpy.simulate_fmu('X:/PIDtest/PIDtest_noinput.fmu', solver='Euler', stop_time=100)
res2= fmpy.simulate_fmu('X:/PIDtest/PIDtest_noinput.fmu', solver='Euler', stop_time=100, output_interval=0.001)
Edit2:
using following Model in OM, where the model was "forced" to used Euler
also yield an erroneous result until the interval cahnged to 10ms:
@cyrosd by default the co-simulation model uses explicit Euler for integration. If the model has a time constant that is smaller than half the sampling time, it will become numerically unstable.
There are two ways out of this: either using a small enough time step, or using an implicit solver inside the CS FMU.
Description
My co-simulation FMU is not working as intended while the Model exchange part of the FMU works as intended.
The model is a simple PID with 2nd order transfer block activated by a 60 seconds period 50% pulse between 0 and 1 as an input. (Model in the additional files section)
What happens is that when the FMU is loaded in co-simulation (It is supposed to contain both Co-Simulation and Model Exchange which has been confirmed by the official FMU checker ) the output derivates exponentially, alternating between positives and negatives instead of staying in the [-1;2] range it usually stays within.
I have tested the model on 3 different FMI implementations with similar results and came to the conclusion the problem was with OpenModelica. An output csv generated by Daccosim demonstrates what happens in the files section.
Steps to reproduce
Open the file PIDtest_noinput.mo with OMEdit. (Available in the files section) Generate a FMU with File > Export > FMU
Load the FMU in a FMI-compatible software and run a simulation for 100 seconds
Expected behavior
Output generated natively by OpenModelica with a 100 seconds simulation: Output generated by Simulink with Model Exchange FMU with a 100 seconds simulation
Screenshots
Output generated by Simulink with Co-Simulation FMU with a 100 seconds simulation
Additional files
This archive contains:
Version and OS
OpenModelica v1.21.0 (64-bit) OMSimulator v2.1.1.post188-gaf996ad-mingw
OS Windows 10 Pro V. 22H2 64 bits