PythonFMU ECMS 20203
PythonFMU ECMS 20203
PythonFMU ECMS 20203
net/publication/341798839
CITATIONS READS
10 1,143
3 authors, including:
Some of the authors of this publication are also working on these related projects:
A Flexible and Common Control Architecture for Rolls-Royce Marine Cranes and Robotic Arms (2012-2013) View project
RFFMIDT project(2019): Dynamic Motion Planning Based on Trajectory Prediction View project
All content following this page was uploaded by Lars Ivar Hatledal on 22 July 2020.
INTRODUCTION
The Functional Mock-up Interface (FMI) [Blochwitz
et al., 2012] is a tool independent standard managed
by the Modelica Association that supports both Model
Exchange (ME) and Co-Simulation (CS) of dynamic
models. A key goal of FMI is to improve the exchange
of simulation models between suppliers and original
equipment manufacturers (OEMs). The current ma-
jor version of the standard is 2.0, which was released in
2014. A minor revision, 2.0.1, was released in 2019.
Fig. 1: Possible use of PythonFMU in realizing complex
An FMU is a model that implements the FMI stan-
cyber-physical systems using FMI based co-simulation.
dard and is distributed as a zip-file with the extension
.fmu. This archive contains:
Python [Van Rossum et al., 2007] is one of the most
• An XML-file that contains meta-data about the
popular programming language today [O’Grady, 2020].
model, named modelDescription.xml.
The major reasons for that are the ease of learning the
*Corresponding author. E-mail: [email protected] language, the huge spectra of libraries covering fields
such as video game, machine learning, web server or ME and provides utilities for packaging the model as
scientific computing and the recent explosion of data an FMU, whereas CPPFMU only provides helper func-
science in which Python plays a central role. Of partic- tions to aid in development.
ular importance for scientific computing is the creation
JavaFMI [Galtier et al., 2017] is a set of compo-
of the Numpy[Oliphant, 2007] library that bridge the
nents for working with the FMI standard using Java,
gap between efficient code in C or Fortran languages
developed by SIANI institute (Las Palmas University)
and the ease of a scripting language. That library is
and funded by the European Institute for Energy Re-
now at the heart of all major scientific Python libraries
search (EIFER). It support both import and export
from Pandas for data analysis to Scipy for classical alge-
of FMUs. For export, it support FMI 2.0 for Co-
bra operators or Scikit-learn for machine learning anal-
simulation. Generated FMUs runs both on Linux and
ysis.
Windows. JavaFMI has been actively maintained since
This paper introduces PythonFMU, an easy to use its inception in 2013 and is licensed under the LGPLv3.
Python based framework that allows plain Python code
to be exported as FMI compatible co-simulation FMUs. FMI4j [Hatledal et al., 2018] is a MIT licensed soft-
Figure. 1 shows how PythonFMU could potentially be ware package for dealing with Functional Mock-up
used to implement complex cyber-physical systems that Units (FMUs) on the JVM. It support both import
are aggregates of models from different simulation do- and export of FMUs. For export, it support FMI 2.0
mains. for Co-simulation. FMI4j is written in Kotlin, which
The paper is organized as follows. Firstly some re- is 100% interoperable with Java. On the native side,
lated works are given. After which PythonFMU is in- FMI4j makes use of CPPFMU to implement the FMI
troduced. Then some benchmark results are presented. functions. FMUs generated using FMI4j can run on
Finally some concluding remarks and notes on future both Linux and Windows.
works are provided. While both JavaFMI and FMI4j allows FMUs to be
created using the Java language, they differ quite a
RELATED WORK bit in their implementation and usage. JavaFMI uses
A number of open-source software frameworks for ex- message-passing to bridge Java and the underlying C
porting FMUs from source-code have been developed in functions defined by FMI, while FMI4j relies on the
the recent years. While many more tools are capable of Java Native Interface (JNI) for this. Consequentially,
exporting FMUs like 20Sim, OpenModelica, MATLAB FMI4j generates much faster executables. Another key
and SimulationX, this paper is more focused on frame- difference is how users define their model. JavaFMI is
works that allows the generation of FMUs from plain imperative, e.g meta-data is defined using API func-
source-code. Each of these are described in more detail tions. FMI4j on the other hand is declarative, with
below. meta-data defined using annotations.
CPPFMU [SINTEF Ocean, 2017] is a set of inter- Evidently, some open-source software for generating
faces and helper functions for writing FMI-compliant FMUs from source code already exists. See Table. I
model/slave code in C++ using high-level features for a summary. However, only the ones targeting the
such as exceptions and automatic memory manage- JVM can be said to be easy to use as these manages
ment, rather than having to implement the low-level C everything related to the creation of an FMU. Still, the
functions specified by FMI. However, while CPPFMU JVM may not be a natural choice for many for imple-
makes implementing and compiling the shared library menting models and the barrier for using these tools are
required by an FMU, it does not handle generating high for non Java developers. CPPFMU and FMUSDK
the modelDescription.xml nor packaging of the FMU. both eases the process within the realm of C/C++, but
CPPFMU was developed as part of the R&D project still requires significant know-how in order to produce a
Virtual Prototyping of Maritime Systems and Opera- ready to use FMU. Furthermore, these tools only covers
tions (ViProMa) Hassani et al. [2016], and is currently a small subset of available programming languages.
maintained by SINTEF Ocean.
FMUSDK [QTronic, 2017] is a free, BSD licensed,
software development kit (SDK) provided by QTronic TABLE I: Open-source framework for exporting source-
to demonstrate basic use of FMUs for ME and for CS code as FMUs.
as defined by FMI version 1.0 and 2.0. The software is
written in C++, but models are to be implemented in Target Target FMI
Tool
C. The first version of FMU SDK was released already language platform version
in 2010, with the latest version, 2.0.6, being released in JavaFMI JVM Win, Linux 2.0
2018. FMI4j JVM Win, Linux 2.0
Like CPPFMU, FMUSDK does not auto-generate CPPFMU C++ Wina , Linuxa 1.0 & 2.0
the modelDescription.xml. The main difference be- Wina , Linuxa ,
FMUSDK C 1.0 & 2.0
tween these tools is that CPPFMU provides a more OSXa
high level and structured API in C++, whereas a
Binaries are only built for the current platform.
FMUSDK requires source-code to be written in quite
low-level C. On the other hand, FMUSDK supports
PYTHONFMU ing. 2 shows how to build an FMU from Python source
that implements the PythonFMU API using the accom-
PythonFMU is a MIT licensed framework that en-
panying CLI. Additional options may be specified, such
ables the packaging of Python 3.x code as co-simulation
as documentation and associated project files. The
FMUs, currently maintained in collaboration between
FMU built by PythonFMU contains pre-built binaries
NTNU and Safran Tech. The library required by users
for Windows and Linux 64-bit. This lowers the thresh-
to implement their own FMI co-simulation slaves as
old for using it tremendously compared to many ex-
well as the Command Line Interface (CLI) required
porting tools as a C++ compiler does not have to be
to build the actual FMU is easily retrieved using ei-
installed and the user does not have to figure out how
ther the pip or conda package managers. Unlike some
to cross-compile.
FMU exporters, FMUs built with PythonFMU runs
Like FMI4j, PythonFMU makes use of CPPFMU
out of the box on both Windows and Linux 64-bit sys-
for implementing the C functions required by the FMI
tems. PythonFMU has been implemented using the
standard. This shows a clear utility for CPPFMU as
limited Python API, which makes it compatible with
an enabler for higher-level applications to support the
any Python 3.x version. However, PythonFMU does
export of FMI compatible co-simulation models.
not bundle a Python distribution, which means that a
compatible Python distribution must be present on the
target system for the FMU to work. The same is true Listing 2: Building an FMU from Python source using
for any imported 3rd party libraries. Consequentially, the PythonFMU CLI.
if the slave makes use of e.g. the numpy package for sci- pythonfmu−b u i l d e r −f PythonSlave . py
entific computing, this library must already be present
on the target system. To remedy this, PythonFMU al-
lows users to specify any dependency it should have on
3rd party libraries. This information is bundled with RESULTS
the FMU as a standard requirements.txt for use with In the following some performance metrics for
one of Python’s package managers. Thus allowing end- PythonFMU is given.
users to easily figure out what kind of libraries that Table. II show the performance of PythonFMU com-
must be installed for it to run on a particular machine. pared to other similar tools. The FMUs used all imple-
ments the same model. The model does no computa-
tion during stepping, but defines a single real, integer,
Listing 1: Writing FMI 2.0 compatible slaves in Python boolean and string variable. These variables are read
using PythonFMU. by the importing tool after each iteration. 100.000 iter-
from pythonfmu im po rt ∗ ations were run. That makes for a total of 400.000 calls
through the FMI API. The benchmark was performed
c l a s s PythonSlave ( Fmi2Slave ) :
on a computer running Windows 10 fitted with an Intel
a u t h o r = ” John Doe” i7-8700k processor.
d e s c r i p t i o n = ”A s i m p l e d e s c r i p t i o n ”