Python is a versatile language that can be used as both a scripting and general programming code. Modelica, on the other hand, is a more niche product leveraged (often via Dymola) to solve complex multi physics engineering problems. The two languages are often deployed together to good effect. Using a Modelica tool such as Dymola in conjunction with Python is a niche within the already niche Modelica community; as a result, information regarding the two together is somewhat sparse. The aim of this blog post is to provide an introduction to some of the ways the two can be used together.

Why use Python with Dymola and Modelica?
One of Dymola’s strengths lies with its symbolic manipulation process, enabling complex multi physics domain models to be reduced to smaller, simpler and crucially easier to solve systems. The ability to export binary code of Dymola models is a key feature of the software. With the right license option, a user is able to export a wide range of binary forms, from FMUs and executables (.exes) all the way to full raw C code of translated Dymola models. Many organisations leverage these features specifically, as they have the ability to customize how the models are deployed within their organization or with customers and other partners.
In this sense Dymola is more of an Integrated Development Environment (IDE) than strictly an end-user-focused tool. As a result it can however be a little clunky to use directly, owing to it’s position as a specialised tool supporting developers and engineers who want to work outside it. Processes and functionality which otherwise could be considered outside the scope of the Dymola and the Modelica language can be achieved or enhanced through use of supplementary languages like Python.
Common Dymola/Python tasks
There are a few reasons why a developer would like to utilize both Python and Dymola in their work. Automation of manual Dymola processes such as batch running, FMU export, model compilation and translation can be configured internally through Modelica scripting. However, as Modelica’s primary role is to be an object-oriented modelling language, which means support for such addendum scripting actions is somewhat limited. It is easier to create and maintain automation scripts using a language like Python to write code, as well as offering a direct interface to a potential environment for post processing. In addition, running Dymola models from Python code offers developers one avenue to manage their own custom deployments.
Another example is when it is desired to interface the Dymola model with external code, such as optimizer algorthims not available natively in Dymola through the GUI.
How to pair Dymola and Python?
There are two ways of coupling Dymola and Python:
- Using Dymola’s inbuilt Python API
- Exporting an FMU from Dymola to Python
As has been featured in the Claytex tech blog in the past, the Python/Dymola interface shipped with Dymola is a quick and simple way to undertake actions within Dymola from Python code. Common tasks such as loading, translating and simulating models; obtaining variable values for setting parameter data; saving results of exporting FMUs and more can all be undertaken directly through the Python package supplied in Dymola’s installation directory. The linked Claytex blog post has plenty of information on the multitude of actions available, which go well beyond this summary. What must be remembered with this approach is that it is controlling Dymola itself from the Python code; local Dymola installation and a valid Dymola license is required, with the associated implications.
Exporting an FMU to work with external Python code is a common methodology of working with simulations outside of their native environments, and certainly not one limited to Dymola and/or Python. The code used to simulate the Dymola model FMU outside of Dymola is known as a ‘wrapper’. Deploying a Dymola model as an FMU within a Python wrapper can offer various benefits and is handled within a Python extension or module. The most common of these is FMPy, with PyFMI another option, albeit not yet supporting FMI 3.0. Such extensions come with a suite of commands for controlling various aspects of the FMU’s functions.

Which option to use, and when?
Both approaches offer distinct advantages and disadvantages. Viability of the method rests with the task in hand. Dymola’s Python interface is the simplest to use, with most suitability towards automating relatively routine tasks, like batch running, mass model export or translation. Really any of the tasks that can be undertaken with the commands found in the Dymola Commands library can be undertaken from the interface. Many of the examples of such utilities using Dymola commands can be found in previous editions of the Claytex Tech Blog can be undertaken in Python using the Dymola Python interface.
Creating a Python wrapper to work with a Dymola FMU can be considered a more advanced task. Choosing between Co-Simulation and Model Exchange, the two methods of deploying a FMU, requires consideration of the use case. If there is little need to consider the solver and its role with the model, then Co-Simulation is suitable by nature as the native solver will travel with the FMU to the new simulation environment. A Dymola FMU exported from a machine with the requisite Binary Code Export license option can function on any machine regardless of Dymola license, installation or lack thereof. This makes it a powerful option when wanting to deploy Dymola models across organisations and different machines in cases where the model just needs to run with little thought given to the solver type, and so on. By nature, Model Exchange is the more involved FMU method when compared to Co-Simulation, due to the need for the FMU to interface with a solver supplied externally. Interactions between the external solver and the FMU must be manually managed in the wrapper, including handling events which occur during simulation. Such topics are in depth and complex, often requiring experience or specialist developers. Despite this, it gives the developers full options in terms of numerically managing the model’s simulation, which can be important in some advanced applications where aspects such as discontinuous derivatives (amongst many other nuances) pose a challenge.
Closing Remarks
Combining Dymola with Python offers a well understood way of realizing the benefits Dymola can bring to any simulation activities. Various approaches to achieve this exist, and the choice can be tailored to the specific needs. In following entries on the Claytex blog post, we’ll explore the various options in greater detail.
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