Skip to content

how to use python dymola interface

mikkdm edited this page Aug 20, 2021 · 2 revisions

How to get started using Dymola's Python interface

Location and documentation of the source files

The Dymola Python Interface comes in the form of a few modules at \Dymola xxxx \Modelica\Library\python_interface\. The modules are bundled within the dymola.egg file. You can unzip this file to have a look inside. In addition, you can open Dymola xxxx\Modelica\Library\python_interface\doc\index.html for some rendered html documentation of the available functions.

where xxxx is the current version of Dymola (depending on the Dymola version we have)

In addition, there is a section explaining the Dymola Python Interface in the Dymola User Manual Volume 2.

"Installing" the Python package dymola

The Dymola Python Interface is supposed to be imported as a Python package named dymola. Unfortunately, this is not available via pip. The recommended way to use the package is to append the \Dymola xxxx\Modelica\Library\python_interface\dymola.egg file to your PYTHONPATH environment variable. You can do so from the Windows command line via set PYTHONPATH=%PYTHONPATH%;D:\Program Files (x86)\Dymola xxxx\Modelica\Library\python_interface\dymola.egg. Make sure to replace the D:\Program Files (x86)\Dymola xxxx part with your actual installation path. Alternatively, you can append the dymola.egg file to your PYTHONPATH within your Python script with

import os
import sys
sys.path.insert(0, os.path.join('C:\\',
                                'Program Files (x86)',
                                'Dymola 2018',
                                'Modelica',
                                'Library',
                                'python_interface',
                                'dymola.egg'))

That may not be the best option, though.

If you are using conda, you can add the dymola package via a .pth file. This follows the recommendation given at StackOverflow - Anaconda: Permanently include external packages (like in PYTHONPATH). Simply add a file with any name of your choice ending in .pth (e.g. dymola-python-interface.pth) to the site-packages folder in your conda environment (e.g. at D:\Python\Miniconda\envs\<my-environment>\Lib\site-packages\<dymola-python-interface>.pth). In this file, just include the path to your local dymola.egg file, e.g. c:\Program Files (x86)\Dymola xxxx\Modelica\Library\python_interface\dymola.egg. With your next activation of your environment, you should be able to use import dymola.

Usage

Now you can use the Interface to simulate models from Python. Here is a quick example to help you along:

# Import dymola package
from dymola.dymola_interface import DymolaInterface

# Start the interface
dymola = DymolaInterface()

# Location where to store the results
dir_result = <path/to/where/you/want/it>

# Set working directory
dymola.cd(dir_result)

# Run script
output = dymola.RunScript("model_to_run.so", silent=self.silent)

dymola.close()

After this, you can work with the results stored in output. The variable output returns a list of the format [True, []], where output[0] is True for a successful simulation. output[1] is a list returning the values of the variables you requested with the parameter finalNames when running dymola.simulateExtendedModel(). For more information see the Dymola User Manual Vol. 2 as mentioned above.