Reading Dymola Results to Python without Dymola

There is a Python interface with Dymola shipped with every version of Dymola. Users can control Dymola with a wide variety of function calls that can load, create, simulate and read results from Dymola models. But when a limited number of licenses are available it can be very inefficient just using an instance of Dymola to read results when the .mat result file already exits.

Dymola result files are in a trajectory format, as described in a great post by Nate Horne. This means that you can use a common interface to read data files without the need of Dymola.

Python

Python Code

There is a free SDF library available through PIP that reads and parses the Dymola result file. This means that reading data directly into Python is very easy.

The SDF library is able to access data related to each variable. Once a .mat file has been loaded the path is used to find its data. Each path will have associated data with it, with the key data being accessible. After simulating “Modelica.Mechanics.MultiBody.Examples.Elementary.DoublePendulum” in Dymola:

import sdf
sdfData = sdf.load("C:\Dymola Working\DoublePendulum.mat")
wRevolute=sdfData["revolute1"]["w"]
print(max(wRevolute.data))
print(wRevolute.unit)
print(wRevolute.comment)

>>>5.2928314
>>>rad/s
>>>First derivative of angle phi (relative angular velocity)

Below is an example of a simple function that reads data from a specified simulation result and outputs the data as a list:

import sdf
def readDymolaData(resultFile,variablePath):
  sdfData = sdf.load(resultFile)
  splitPath=variablePath.split(".")
  dataGroup=sdfData[splitPath[0]]
  if len(splitPath)>1:
    for i in splitPath[1:]:
      dataGroup=dataGroup[i]
  return dataGroup.data

This can be used in the following way:

resultFile="C:\Dymola Working\DoublePendulum.mat"

revoluteSpeed="revolute1.w"
tauPath="revolute1.axis.tau"

wRevolute=readDymolaData(resultFile,revoluteSpeed)
tauRevolute=readDymolaData(resultFile,tauPath)

maxSpeed=max(wRevolute)
maxTorque=max(tauRevolute)

print(maxSpeed)
print(maxTorque)

>>>5.2928314
>>>0.836536

Size Limit

Unfortunately Dymola uses a .mat version 4 that has a limit on the size of file that can be used. Dymola can output .mat files that are larger than that limit for simulations that have a large amount of data. Dymola is able to read those data files, but the SDF library cannot. Therefore using the Python-Dymola Interface to gain data is the best way.

Written by: David Briant – 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

CONTACT US

Got a question? Just fill in this form and send it to us and we'll get back to you shortly.

Sending

© Copyright 2010-2023 Claytex Services Ltd All Rights Reserved

Log in with your credentials

Forgot your details?