Previously on the Claytex blog, we covered an introduction to the concept of using the Python language alongside Dymola/Modelica. As you might recall, one of the tasks that can be accomplished by doing so is to simulate FMUs exported from Dymola. As covered in the previous post, there’s plenty of benefits to doing so which leverage advantages in the Dymola development environment.
Using Python to simulate an FMU requires the development of a “wrapper” a wrapper is code which is used to call, manage and execute the FMU. To do this, specific Python commands are needed to interface with the FMU. One common Python extension which contains such tools is FMPy, which we will introduce and discuss in this post.

What is FMPy?
FMPy is a free to use, open-source Python library developed and maintained by Dassault Systèmes. It is designed to support the usage of FMUs in an environmentally agnostic fashion by leveraging Python. By utilising Python, one of the world’s most popular programming languages, FMU execution and management is opened to a wide audience.
It is a versatile, lightweight and user-friendly package which not only provides Python developers tools to manage and execute FMUs, but also to bridge the gap between FMI-based models and the wider Python stack. This opens the possibilities of what a Python developer can do with the FMU, enabling a wrapper to take on evolved forms and functions which extend beyond simple running of an FMU. FMPy can be installed locally using the popular pip method, a well known and simple method of installing and managing the installation of Python libraries.
What are FMPy’s key features?
FMPy, despite being a lightweight library it has a plethora of useful features and components. Some of the key ones can be summarized as below:
- Inbuilt optional GUI: FMPy comes with a minimal, but useful, GUI that makes it easy to explore, configure, and simulate FMUs without writing Python code. It can be useful for quick inspection and validation of an FMU before embedding it in a larger workflow, or simply to avoid having to code a GUI and Python wrapper manually. Inputs, outputs, parameters and states can be inspected, with parameters and initial conditions editable prior to simulation. Settings for solvers, such as start/end time, step size (if fixed step) and solver choice (if a model exchange FMU) can all be set. Finally, the GUI can be used to plot results variables, and results can be exported for further processing.
- FMI 1.0/2.0/3.0: All 3 FMI standard versions are supported, both in the GUI and the underlying Python code found in the library.
- FMU management commands (instantiation, termination, mode control): Control of the FMU within memory – its creation as a memory object, its termination and the mode of operation the FMU is in all can be controlled with commands found in the FMPy library.
- Model Exchange/Co-Simulation: Both methods of solver definition are supported with FMPy. The FMU method is specified when the FMU is instantiated in memory, enabling it to run in Co-Simulation mode or go onto accept an external solver when in Model Exchange mode.
- Common solvers (also found in Dymola): FMPy comes with commonly used solvers already built-in for Model Exchange, such as Forward Euler (fixed step size) and Cvode (variable step size). Both these solvers are found in Dymola which helps simplify validation between FMU performance in Dymola and any Python implementation using FMPy.
- Automated parsing of the ModelDescription.xml file: Parsing the modelDescription.xml file is done through a specific command, which automatically creates a specific input object containing all the pertinent data within it. This greatly simplifies many processes, where the model description object can be passed directly to various commands, simplifying the transfer of model information to various sub tasks and routines around your code.
- Input/output management: FMPy contains commands used to construct an object for the management of inputs into the FMU, which enables input events to be detected and tracked. Beyond this, there are tools to manually set, and retrieve the values of inputs and other parameters in the FMU. Simulation results can be collected manually using these commands or using a specific recorder object which can be built using FMPy.
- Event management tools: A suite of functions within the FMPy library give the developer the ability to manage the events which occur during simulation in a granular way. Input, state, step and time events each feature individual indicators, allowing for bespoke event handling strategies to be conceived depending on the event to be handled. Coupled with the ability to control the mode the FMU is in, the developer has full control over event handling with FMPy.
Final remarks
Altogether, FMPy offers a comprehensive, free-to-use, lightweight and easy to use solution to simulating FMUs with Python code. A comprehensive array of features gives developers ful control over the process of simulating an FMU in Python with the FMPy library.
Written by: Theodor Ensbury – Senior Project Engineer
Please get in touch if you have any questions or have got a topic in mind that you would like us to write about. You can submit your questions / topics via: Tech Blog Questions / Topic Suggestion