Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
475 views
in Technique[技术] by (71.8m points)

openmodelica - PyFMI in Python 3 environment in Ubuntu 18.04

My goal is to be able to run FMUs produced by OpenModelica in Ubuntu 18.04 and then run these with PyFMI in Python 3 environment.

I follow the outline for PyFMI installation here https://jmodelica.org/pyfmi/installation.html.

So far I have using Conda managed to install Python3, Numpy, Scipy, lxml and some other packages and made it work with some of my Python examples. But I would appreciate some detailed advice how to

  1. Install FMI Library - and I do not get how to setup the flag fmil-home
  2. Install Assimulo

After that I guess we are ready to do from the installation outline “python setup.py install —fmil-home=/path/to/fmil"

Appreciate some basic advice!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here I summarize the good input I have got on how to setup PyFMI on Xubuntu 18.04 with OpenModelica. The input has come from Christian Winther at Modelon and Adrian Pop at LiU and glad for that.

The installation follows https://jmodelica.org/pyfmi/installation.html with some clarifications.

The OpenModelica is installed in Linux on a VM you get here https://openmodelica.org/download/virtual-machine It is all 64-bit software what I understand.

It is more convenient to use conda for installation than pip as shown below:

Download Miniconda for Python 3 here https://docs.conda.io/en/latest/miniconda.html

Install Miniconda3 and with that you get Python 3.7 and some packages. Good to update conda by

$conda update conda

Installation of PyFMI is now simply done by the following commands:

$conda config --add channels conda-forge
$conda install pyfmi

During this installation key packages like: NumPy, Scipy, Lxml, Matplotlib are also installed. According to PyFMIs homepage mentioned above it could be of interest to also have wxpython installed but not necessary. If installed it should be done with conda as well.

We can interact with the FMU through Python script in different ways.

a) Put FMU generated from OpenModelica (or from some other Ubuntu environment) in a folder FMU_test together with some Python script simu_FMU that run the FMU and plot the results. Go to the folder FMU_test. The following command runs the FMU and plot the results

$python3 simu_FMU.py

b) An interactive framework with the popular Jupyter notebook can be installed by

$conda install ipython
$conda install jupyter

Then to start up the notebook do the following command from the folder FMU_test

$jupyter notebook

And the web-browser opens up you can then run the python scripts from a cell and also directly interact with the FMU and change parameters etc. Several python commands can be done in each cell. The results of the cell is presented in an output cell. The Jupyter notebook focus on a sort of sequential approach to investigating a simulation model. All simulations in a diagram must be executed in one cell.

c) An interactive framework with IPython would also be interesting to have. In this way a more iterative approach to work with simulations could be done. Something like simulate, change some parameters, simulate again AND plot in the same diagram as before.

Using the interactive Python window, starting up with the following command

$ipython --pylab

requires setting of how a text-file should be read by the command “locale"

$import numpy as np
$import matplotlib.pyplot as pli
$from pyfmi import load_fmu

$import locale
$locale.setlocale(locale.LC_ALL, ‘en_US.UTF-8’)

$model = load_fmu(”FMU_example.fmu”)

There is a certain flexibility of how a model is represented in the FMU and those produced by OpenModelica contain a text-file of json-type that not all vendors have in their FMUs, and for instance not JModelica.org. And reading this json-file requires the setting made by locale to read it correctly in the IPython-window. Thus NOT needed in the Jupyter notebook environment, but has at least no negative effect there.

In the standard (Windows) JModelica installation of PyFMI the interaction using c) is used. The Python scripts tested so far work exactly the same way in Xubuntu 18.04 when using FMUs compiled by JModelica 2.4 in Ubuntu 18.04. Tests include both PyFMI model.simulate() and model.estimate().

The FMUs compiled by OpenModelica 1.14.1 and also later development versions can be used for simulation using the procedure mode.simulate(). However the interaction with model.get() and model.set() show different behaviour. This may be due to different interpretation of FMU-standard or even errors in the implementation. The people working with development of OpenModelica are aware and investigate it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...