Python 3 环境中的 PyFMI Ubuntu 18.04

PyFMI in Python 3 environment in Ubuntu 18.04

我的目标是能够 运行 OpenModelica 在 Ubuntu 18.04 中生成的 FMU,然后在 Python 3 环境中使用 PyFMI 运行 这些。

我在这里遵循 PyFMI 安装大纲 https://jmodelica.org/pyfmi/installation.html

到目前为止,我已经使用 Conda 设法安装了 Python3、Numpy、Scipy、lxml 和一些其他软件包,并使其与我的一些 Python 示例一起使用。但我会很感激一些详细的建议如何

  1. 安装 FMI 库 - 我不知道如何设置标志 fmil-home
  2. 安装 Assimulo

在那之后我想我们已经准备好从安装大纲开始了 “python setup.py 安装 —fmil-home=/path/to/fmil”

感谢一些基本建议!

我必须编译所有内容才能使其正常工作,因此 conda 可能是一个更简单的解决方案。这对我有用:

# 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

在这里,我总结了关于如何使用 OpenModelica 在 Xubuntu 18.04 上设置 PyFMI 的良好输入。 输入来自 Modelon 的 Christian Winther 和 LiU 的 Adrian Pop,我很高兴。

安装遵循 https://jmodelica.org/pyfmi/installation.html 并做了一些说明。

OpenModelica 安装在 Linux 的虚拟机上 https://openmodelica.org/download/virtual-machine 都是我理解的64位软件

使用conda安装比pip更方便如下图:

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

安装 Miniconda3,你会得到 Python 3.7 和一些软件包。很高兴通过

更新 conda
$conda update conda

PyFMI 的安装现在只需通过以下命令即可完成:

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

在此安装过程中,还安装了关键软件包,如:NumPy、Scipy、Lxml、Matplotlib。 根据上面提到的 PyFMIs 主页,安装 wxpython 可能也很有趣,但不是必需的。如果已安装,也应该使用 conda 完成。

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

a) 将 OpenModelica(或其他 Ubuntu 环境)生成的 FMU 与一些 Python 脚本 simu_FMU 放在文件夹 FMU_test 中 运行 FMU 并绘制结果。 转到文件夹 FMU_test。以下命令 运行s FMU 并绘制结果

$python3 simu_FMU.py

b)

可以安装一个与流行的 Jupyter notebook 的交互框架
$conda install ipython
$conda install jupyter

然后从文件夹 FMU_test

中执行以下命令启动笔记本
$jupyter notebook

然后打开网络浏览器,然后您可以 运行 来自单元格的 python 脚本,还可以直接与 FMU 交互并更改参数等。几个 python 命令可以在每个单元格中完成。单元格的结果显示在输出单元格中。 Jupyter notebook 专注于一种研究仿真模型的顺序方法。图中的所有模拟必须在一个单元格中执行。

c) IPython 的交互式框架也很有趣。通过这种方式,可以完成一种更具迭代性的方法来处理模拟。类似模拟,更改一些参数,再次模拟并在与之前相同的图表中绘制。

使用交互式 Python window,使用以下命令启动

$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 中的表示方式具有一定的灵活性,OpenModelica 生成的模型包含 json 类型的文本文件,并非所有供应商的 FMU 中都有,例如JModelica.org。并且读取此 json 文件需要区域设置进行设置才能在 IPython-window 中正确读取它。因此在 Jupyter notebook 环境中不需要,但至少没有负面影响。

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

OpenModelica 1.14.1及之后的开发版本编译的FMU可以使用程序mode.simulate()进行仿真。然而,与 model.get() 和 model.set() 的交互表现出不同的行为。这可能是由于对 FMU 标准的不同解释,甚至是实施中的错误。从事 OpenModelica 开发工作的人员了解并研究它。