This article is designed for Windows users that want to interface with Dymola from Python.
A Python Inteface to Dymola was first implemented in Dymola 2015. In this article Python 2.7 and Dymola 2017 are interfaced using Visual Studio 2010 with PTVS.
Dymola Interface
Dymola includes a Dymola interface that is stored in the Python package <Dymola 2017 installation directory>\Modelica\Library\python_interface\dymola.egg. The dymola.egg package contains the Python functions for interfacing with Dymola such as simulateModel, ExecuteCommand and many more:
Figure 1: Snippet of Python Dymola interface functions.
Full list can be found in <Dymola 2017 installation directory>\Modelica\Library\python_interface\genindex.html
Dymola does not include Python or a Python IDE so these need to be installed.
Installing Python
Section 6.7 of the Dymola User Guide Volume 2 contains simple instructions about how to install and test Python.
These instructions are easy to follow however make sure that you get Python 2.7. Also when you modify the PATH first create a backup of the path before modifying the path, and make sure that the path has been modified in the way you expect.
Run through the instructions in the Dymola Manual and make sure that DymolaExample.py simulates as expected.
Installing Python Tools for Visual Studio
Python does not include an Integrated Development Enviroment (IDE), fortunately there are many Python IDE’s:
Figure 2: Comparison chart of Python IDE tools.
From http://stackoverflow.com/questions/81584/what-ide-to-use-for-python
I chose Python Tools for Visual Studio (PTVS) which is a free plugin for Microsoft Visual Studio for creating, debugging and running Python projects from within Visual Studio. This interface has many features such as auto code completion and integrated Python debugging.
The installation of PTVS is described here:
Setting up Visual Studio for Python Development
I recommend running through the first 4 videos.
An example using PTVS
Create a new Python project using “From Existing Python code” as in Visual Studio Tutorial 2/6: Starting coding in Python using Visual Studio.
Select Python 64-bit 2.7 as the Python interpreter and leave the other settings as is.
Figure 3: Python Code Wizard for creating a new project.
Select Python 64-bit 2.7 as the Python interpreter.
Add the DymolaExample.py code by right clicking on the project name and selecting Add->Existing Item and browsing to <Dymola 2017 Installation directory>\Modelica\Libraries\python_interface\examples\DymolaExample.py.
Figure 4: Adding an Existing Item to the Python project.
In the VS project right click on the DymolaExample.py and select Set as Startup File
For the interface to work you need the Project to be able to utilize the Dymola.egg package, this is done by adding the dymola.egg package to Search Path (as done below):
Figure 5: Adding Zip Archive to Search Path.
<Dymola 2017 installation directory>\Modelica\Libraries\python_interface\dymola.egg is selected.
Now it should be possible to step through the Python code in the Visual Studio environment.
Figure 6: Stepping through the Python code in the Visual Studio environment.
Written by: Garron Fish – Chief Engineer