Why I'm obtaining NaN in the output of a simple FMU exported from Simulink and tested in Python 3?

4 views (last 30 days)
Hi all,
I'm new to the FMI standard and FMUs. I'm trying to export a FMU of a simple Simulink model, the Ohm's law in my case, to test the funcionality of the FMI standard in Python.
This is my Simulink model:
In the previous model I'm defining 2 inputs (Voltage and Resistance), a division and 1 output (Current). Then, I export the FMU by clicking on Save As > Export Model To > Standalone FMU. Note I'm configuring fixed-step size in the model settings. I've also tested exporting the FMU from the Command Window with the following line:
exportToFMU2CS('OhmLaw')
The FMU is exported correctly, but when I go to test it in Python 3 with a Jupyter notebook, I'm not obtaining the expected output. As shown below, for the input values V=10 and R=2, the simulation returns I=NaN. I'm pasting also the minimal code:
from pyfmi import load_fmu
model = load_fmu('OhmLaw.fmu')
model.set('Voltage', 10)
model.set('Resistance', 2)
res = model.simulate(final_time=1, input=(), options={'ncp': 1})
model.get('Current')
I've tested with different data types in all blocks (auto, int32 and double), but I'm still not able to achieve the correct result.
I've tried to check for a similar problem in forums, but I haven't found anything similar. Does anyone know how to solve this issue? I'm using MATLAB R2022a in Windows 10.
Thank you,
Jon

Answers (1)

Infinite_king
Infinite_king on 11 Dec 2023
Hi Jon Aztiria,
I understand that you are converting the Simulink model to FMU format to simulate the model in python and getting unexpected output values when the model is simulated in python.
To resolve the issue, follow the below steps and see if it resolves the issue,
  1. Ensure that there are no errors or warnings during the export process from Simulink. Make sure that the export process completes without any issues.
  2. Check the solver settings in your Simulink model and make sure that they are compatible with the solver settings used in your Python simulation. For example, if you are using a fixed-step solver in Simulink, make sure you set the same step size in your Python simulation.
  3. Double-check that the names of your inputs and outputs in Python match the names used in your Simulink model.
  4. Once the FMU is generated, verify the FMU using the following link to see if SIMULINK generated FMU correctly, https://fmu-check.herokuapp.com/
  5. Make sure if you have all the dependencies installed to use ‘pyFMI’ library. You can find the required libraries in the following link, https://pypi.org/project/PyFMI/#:~:text=org/forum.-,Requirements,-%3A
  6. Experiment with different simulation options. For example, try different values for the final_time parameter in the simulate function. Also, you may want to increase the number of communication points (ncp) to get more data points.
  7. Simulate with adjusted options, for example refer the below code snippet,
res = model.simulate(final_time=1, input=(), options={'ncp': 500, 'CVode_options': {'atol': 1e-8, 'rtol': 1e-8}})
model.get('Current')
For more information, please refer the following resources,
  1. https://www.mathworks.com/help/slcompiler/ref/exporttofmu2cs.html
  2. https://fmi-standard.org/
  3. https://pypi.org/project/PyFMI/
Hope this is helpful.

Categories

Find more on Create Standalone FMU in Help Center and File Exchange

Tags

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!