Skip to content

How to: Dymola Python Interface

marcusfuchs edited this page Jun 20, 2017 · 10 revisions

This page gives a quick overview of how to use the Python interface of Dymola. This assumes you're running Dymola 2018 and Python 3 on Windows.

Use Case

You want to automate Modelica simulations with Python, e.g. for parameter studies, optimization, unit testing, ...

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 2018\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 2018\Modelica\Library\python_interface\doc\index.html for some rendered html documentation of the available functions.

In addition, there is a section explaining the Dymola Python Interface in the Dymola User Manual Volume 2 (click Help > Documentation in Dymola for the Link) starting on page 289.

"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 2018\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 2018\Modelica\Library\python_interface\dymola.egg. Make sure to replace the D:\Program Files (x86)\Dymola 2018 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('D:\\',
                                'Program Files (x86)',
                                'Dymola 2018',
                                'Modelica',
                                'Library',
                                'python_interface',
                                'dymola.egg'))

That may not be the best option, though.

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 of your local AixLib clone
dir_aixlib = <path/to/your/AixLib/AixLib>

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

# Open AixLib
dymola.openModel(path=os.path.join(dir_aixlib, 'package.mo'))

# Translate any model you'd like to simulate
dymola.translateModel('AixLib.ThermalZones.ReducedOrder.Examples.ThermalZone')

# Simulate the model
output = dymola.simulateExtendedModel(
    problem='AixLib.ThermalZones.ReducedOrder.Examples.ThermalZone',
    startTime=0.0,
    stopTime=3.1536e+07,
    outputInterval=3600
    method="Dassl",
    tolerance=0.0001,
    resultFile=os.path.join(dir_result, 'demo_results'),
    finalNames=['thermalZone.TAir' ],
)

dymola.close()

After this, you can work with the results stored in output. 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.

Call to action

If

  • anything on this page is not clear to you,
  • you found an error on this page,
  • you have a suggestion for improvement,
  • you experience a problem with this approach in your work,
  • you have a better solution for using the Dymola Python Interface,
  • ...

Make sure to raise the issue on our Issue Tracker!

Clone this wiki locally