ggoer
ggoer

Reputation: 23

How can I get the simulation time of a model in Dymola using the Python interface?

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!

Upvotes: 2

Views: 1002

Answers (1)

marco
marco

Reputation: 6645

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

Upvotes: 2

Related Questions