Archived OpenModelica forums. Posting is disabled.

Alternative forums include GitHub discussions or StackOverflow (make sure to read the Stack Overflow rules; you need to have well-formed questions)


Forgot password? | Forgot username? | Register

Simulation of FMI models in Python

Simulation of FMI models in Python

Dear all,

I tried the FMI export from OpenModelica 1.8.1 beta with the Python packages PyFMI (www.pyfmi.org) and Assimulo (www.assimulo.org) to test scripting of simulations of OpenModelica models in Python. PyFMI is a general purpose package for loading and interacting with compiled models compliant with the Functional Mock-up Interface standard (FMI), and Assimulo is a Python simulation package that interfaces several state of the art integrators, including IDAS and CVODES from the SUNDIALS suite and Hairer's Radau codes. Also, sensitivity analysis is supported by Assimulo. Together, the packages offer Python scripting of simulations as a complement to FMI compliant modeling tools.

When I first tested the functionality, I used a nightly build (r10787), and with this version, simulation works, apart from a minor issue: the name of the dll file should according to the FMI spec use the <modelIdentifier> string from the XML file, which is not the case in the FMUs generated by OpenModelica.  We have added a temporary workaround in PyFMI (http://trac.jmodelica.org/ticket/1758) so that OpenModelica FMUs can be used with PyFMI.

In OpenModelica 1.8.1 beta, there seems to be a bug in that there is a parameter value that is set to zero in the FMU:

In [34]: m=FMUModel('Simple.fmu')
In [35]: res=m.simulate(final_time=10)
ERROR: Division by zero in partial equation: (firstorder1.k * sine1.y - firstorder1.y) / firstorder1.T because firstorder1.T == 0.

The model I tried is the following:

model Simple
  Modelica.Blocks.Sources.Sine sine1;
  Modelica.Blocks.Continuous.FirstOrder firstorder1;
equation
  connect(sine1.y,firstorder1.u);
end Simple;

Hope this helps!

Best regards
/Johan

Re: Simulation of FMI models in Python

Johan, I was unable to run pyfmi or assimulo. I installed the win32 binaries of both on win32 python 2.7 on 64-bit windows 7. Here are some errors:

Code:


>>> import pyfmi
>>> import pyfmi.examples.fmi_bouncing_ball
>>> pyfmi.examples.fmi_bouncing_ball.run_demo()
WARNING:root:Could not find Assimulo package. Check jmodelica.check_packages()
WARNING:root:Could not load Assimulo module. Check pyfmi.check_packages()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\apps\Python27\lib\site-packages\pyfmi\examples\fmi_bouncing_ball.py",
line 35, in run_demo
    res = model.simulate(final_time=2.)
  File "C:\apps\Python27\lib\site-packages\pyfmi\fmi.py", line 1496, in simulate options)
  File "C:\apps\Python27\lib\site-packages\pyfmi\common\core.py", line 196, in _exec_simulate_algorithm
    alg = algorithm(start_time, final_time, input, self, options)
  File "C:\apps\Python27\lib\site-packages\pyfmi\fmi_algorithm_drivers.py", line
180, in __init__
    'Could not find Assimulo package. Check jmodelica.check_packages()')
Exception: Could not find Assimulo package. Check jmodelica.check_packages()



>>> from pyfmi.simulation.assimulo_interface import FMIODE
WARNING:root:Could not find Assimulo package. Check jmodelica.check_packages()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\apps\Python27\lib\site-packages\pyfmi\simulation\assimulo_interface.py", line 94, in <module>
    class FMIODE(Explicit_Problem):
NameError: name 'Explicit_Problem' is not defined


>>> pyfmi.check_packages()

Performing pyfmi package check
==============================

Platform...................... win32

Python version:............... 2.7.2



Dependencies:

Package                        Version
-------                        -------
numpy......................... 1.6.1                          Ok

scipy......................... 0.10.0                         Ok

matplotlib.................... 1.1.0                          Ok

lxml.......................... 2.3.0                          Ok

assimulo...................... n/a                            Ok

wxPython...................... 2.8.12.1                       Ok

pyreadline.................... 1.7.1                          Ok

setuptools.................... 0.6c12                         Ok

Re: Simulation of FMI models in Python

Hello,

it seems that you did not install the Assimulo package, which is a dependency of PyFMI. Assimulo is available from www.assimulo.org - can you try again after installing Assimulo?

PyFMI provides the FM import functionality, whereas the Assimulo package provides the integrators.

Let me know how it goes.

Best regards
/Johan

Re: Simulation of FMI models in Python

I installed it, I think. I'll investigate and provide more details at the jmodelica.org forum.

Re: Simulation of FMI models in Python

Ok, sounds good.

Best
/Johan

There are 0 guests and 0 other users also viewing this topic
You are here: