PyFMI in Python 3 environment in Ubuntu 18.04

非 Y 不嫁゛ 提交于 2020-02-26 02:37:28

问题


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!


回答1:


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.




回答2:


I had to compile everything to make it work so conda might be an easier solution. This worked for me:

# change myUser to your user in the code below!
# install the dependencies (maybe you need more, I might have installed some already)
pip3 install numpy
pip3 install Cython
# get FMIL and build it
git clone https://github.com/modelon-community/fmi-library
cd fmi-library
mkdir build-fmil
cd build-fmil
cmake -DFMILIB_INSTALL_PREFIX=/home/myUser/fmil ..
make install test
# now you should have the FMIL library in:
# /home/myUser/fmil
# export that to terminal before installing PyFMI
export FMIL_HOME=/home/myUser/fmil

# get and install sundials
wget https://computing.llnl.gov/projects/sundials/download/sundials-3.0.0.tar.gz
tar -xf sundials-3.0.0.tar.gz
cd sundials-3.0.0
mkdir build
cd build
cmake -DCMAKE_INSTALL_PREFIX=/home/myUser/sundials ..
make install

# get and install lapack and blas
https://github.com/Reference-LAPACK/lapack/archive/v3.9.0.tar.gz
tar -xf v3.9.0.tar.gz
cd lapack-3.9.0/
mkdir build
cmake -DCMAKE_INSTALL_PREFIX=/home/myUser/lapack ..
make install

# get Assimulo
git clone https://github.com/modelon-community/Assimulo
cd Assimulo/
sudo python3 setup.py install --sundials-home=/home/myUser/sundials --blas-home=/home/myUser/lapack/lib --lapack-home=/home/myUser/lapack

# get PyFMI
git clone https://github.com/modelon-community/PyFMI/
cd PyFMI
sudo python3 setup.py install --fmil-home=/home/myUser/fmil

# now you should have everything installed for your myUser
# you need to do:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/myUser/sundials/lib/
# before running PyFMI as all these libraries are installed for the local user
# note that you can install all these at the system level if you want, just do:
# -DCMAKE_INSTALL_PREFIX=/usr/local and -DFMILIB_INSTALL_PREFIX=/usr/local


来源:https://stackoverflow.com/questions/59582257/pyfmi-in-python-3-environment-in-ubuntu-18-04

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!