PyFMI在Ubuntu 18.04中的Python 3环境中。

huangapple go评论72阅读模式
英文:

PyFMI in Python 3 environment in Ubuntu 18.04

问题

  1. 安装FMI库 - 我不太明白如何设置标志"fmil-home"。
  2. 安装Assimulo。

感谢提供的基本建议!

英文:

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

得分: 2

这里我总结了如何在Xubuntu 18.04上使用OpenModelica设置PyFMI的良好输入。这些输入来自Modelon的Christian Winther和LiU的Adrian Pop,对此表示感激。

安装步骤可以在https://jmodelica.org/pyfmi/installation.html上找到,以下是一些澄清说明。

OpenModelica可以在Linux上的虚拟机中安装,可以从https://openmodelica.org/download/virtual-machine获取。据我所知,这都是64位软件。

与使用pip相比,使用conda进行安装更方便,具体步骤如下:

  1. 在此处下载Python 3的Miniconda:https://docs.conda.io/en/latest/miniconda.html

  2. 安装Miniconda3,这将安装Python 3.7和一些包。建议使用以下命令更新conda:

    $conda update conda
    
  3. 现在可以使用以下命令简单地安装PyFMI:

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

在此安装过程中,还会安装关键包,如NumPy、Scipy、Lxml、Matplotlib。根据上面提到的PyFMI主页,也可以安装wxpython,但不是必需的。如果要安装,也应该使用conda进行安装。

我们可以通过Python脚本以不同的方式与FMU进行交互。

a) 将从OpenModelica生成的FMU(或来自其他Ubuntu环境的FMU)放在一个名为FMU_test的文件夹中,同时还需放入一个名为simu_FMU的Python脚本,用于运行FMU并绘制结果。进入FMU_test文件夹,以下命令将运行FMU并绘制结果:

```
$python3 simu_FMU.py
```

b) 可以通过以下方式安装与流行的Jupyter笔记本的交互框架:

```
$conda install ipython
$conda install jupyter
```

然后,从FMU_test文件夹执行以下命令启动笔记本:

```
$jupyter notebook
```

浏览器将打开,您可以从单元格中运行Python脚本,并直接与FMU交互以及更改参数等。每个单元格中可以执行多个Python命令。单元格的结果将显示在输出单元格中。Jupyter笔记本专注于一种顺序方法,用于研究仿真模型。图中的所有仿真必须在一个单元格中执行。

c) 还可以安装与IPython的交互框架,以更迭地处理仿真。可以像这样使用交互式Python窗口,从以下命令开始:

```
$ipython --pylab
```

这需要设置一个文本文件应如何由“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")
```

FMU在FMU中的表示方式有一定的灵活性,由OpenModelica生成的FMU包含一个json类型的文本文件,不是所有供应商都在其FMU中具有该文件,例如JModelica.org就没有。读取这个json文件需要通过locale设置来正确在IPython窗口中读取它。因此,在Jupyter笔记本环境中不需要这个设置,但至少在那里不会产生负面影响。

在标准的(Windows)JModelica PyFMI安装中,使用c)的交互方式。到目前为止,在使用Ubuntu 18.04中使用JModelica 2.4编译的FMUs时,已测试的Python脚本的工作方式与在Xubuntu 18.04中完全相同。测试包括PyFMI model.simulate()和model.estimate()。

通过OpenModelica 1.14.1编译的FMUs以及后续的开发版本可以使用procedure mode.simulate()进行仿真。然而,使用model.get()和model.set()的交互显示出不同的行为。这可能是由于FMU标准的不同解释或甚至是实现中的错误所致。OpenModelica开发人员已经意识到了这一点,并正在进行调查。

英文:

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

得分: 1

我不需要翻译的代码部分,以下是翻译好的部分:

我不需要编译所有内容,以使其正常工作,所以conda可能是一个更简单的解决方案。这对我有用:

在下面的代码中将myUser更改为您的用户!

安装依赖项(可能需要更多,我可能已经安装了一些)

pip3 install numpy
pip3 install Cython

获取FMIL并构建它

git克隆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

现在您应该在以下位置拥有FMIL库:

/home/myUser/fmil

在安装PyFMI之前在终端导出它

export FMIL_HOME=/home/myUser/fmil

获取并安装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

获取并安装lapack和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

获取Assimulo

git克隆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

获取PyFMI

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

现在您应该已经为您的myUser安装了所有内容

您需要执行:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/myUser/sundials/lib/

在运行PyFMI之前,因为所有这些库都是为本地用户安装的

请注意,如果您希望,您可以将所有这些安装在系统级别,只需执行:

-DCMAKE_INSTALL_PREFIX=/usr/local和-DFMILIB_INSTALL_PREFIX=/usr/local

英文:

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

huangapple
  • 本文由 发表于 2020年1月4日 00:46:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582257.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定