2

I am using Dymola's python_interface and want to simulate a set of models:

import platform

from dymola.dymola_interface import DymolaInterface
from dymola.dymola_exception import DymolaException

osString = platform.system()
isWindows = osString.startswith("Win")

with open('ModelList.txt', 'r') as f:
    models = f.read().splitlines()

for model in models:
    dymola = None
    try:
        dymola = DymolaInterface("C:\\Program Files\\Dymola 2018 FD01\\bin64\\Dymola.exe")

        result = dymola.simulateModel(model)
        if not result:
            print("Simulation failed:")
            log = dymola.getLastErrorLog()
            print(log)
        else:
            print("OK")
    except DymolaException as ex:
        print(("Error: " + str(ex)))
    finally:
        if dymola is not None:
            dymola.close()
            dymola = None

Basically, this is the example given in the Dymola manual (with the added for-loop). Now I want to get the models' simulation time and write it to a (csv-)file.

The simulation time is also written to the log-file, but is there a way to get it directly? The results of the simulation are written to a .mat file which is fine for me.

Thank You for your help!

ggoer
  • 58
  • 7
  • Would setting the flag `OutputCPUtime=true` be sufficient? – matth May 02 '18 at 12:49
  • Do you mean the simulation end time or the required cpu time? – marco May 02 '18 at 13:27
  • If I set `OutputCPUtime=True` how can I store the time within a string or float? @marco I mean the required cpu time. The 'stopTime' is known. – ggoer May 02 '18 at 13:28
  • A comment to your code: I would put the model loop inside the try statement. Currently you are opening and closing dymola for every model in ModelList.txt, which gives a big and unnecessary overhead – marco May 02 '18 at 14:25
  • Yes, you are right, I changed this. – ggoer May 03 '18 at 07:45

1 Answers1

2

You can include the required CPU time as variable into the simulation result with the flag OutputCPUtime=true. Then you can use the simulateExtendedModel command to get the final value of this variable.

Add the following into your try section and it should do what you want:

dymola = DymolaInterface("C:\\Program Files\\Dymola 2018 FD01\\bin64\\Dymola.exe")
dymola.ExecuteCommand("OutputCPUtime=true")
result, values = dymola.simulateExtendedModel(model, finalNames= ['CPUtime'])
cpu_time = values[0]


As an alternative you can first translate your model and then measure in python how long the simulate command takes to execute, using one of the methods described in tic, toc functions analog in Python

This could then look like as follows:

    dymola = DymolaInterface("C:\\Program Files\\Dymola 2018 FD01\\bin64\\Dymola.exe")
    dymola.translateModel(model)
    t = time.time()
    result = dymola.simulateModel(model)
    elapsed = time.time() - t
marco
  • 5,944
  • 10
  • 22
  • Thank you,this just worked out. Still a question: This cpuTime is always a bit bigger than the 'CPU time for integration'. The latter only covers the simulation AFAIK, does the cputime contain more actions (like translation time)? – ggoer May 03 '18 at 07:50
  • 1
    You are right, the CPUtime apparently includes more than the simulation time. If you activate the flag in Dymola and plot the variable after a simulation, you see that the plot starts with an offset - I assume that this is the translation time. – marco May 03 '18 at 08:12