如何使用Python接口获得Dymola中模型的模拟时间?

2024-10-01 22:34:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Dymola的python_接口,希望模拟一组模型:

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

基本上,这是Dymola手册中给出的示例(添加了for循环)。现在我想得到模型的模拟时间并将其写入(csv-)文件。在

模拟时间也会写入日志文件,但有没有办法直接获取?模拟结果被写入一个.mat文件,这对我来说很好。在

谢谢你的帮助!在


Tags: 文件from模型importnoneformodelsas
1条回答
网友
1楼 · 发布于 2024-10-01 22:34:53

您可以使用标志OutputCPUtime=true将所需的CPU时间作为变量包含到模拟结果中。然后您可以使用simulateExtendedModel命令来获得这个变量的最终值。在

将以下内容添加到try部分,它将执行您想要的操作:

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]


作为一种替代方法,您可以首先转换模型,然后使用python测量simulate命令执行所需的时间,使用tic, toc functions analog in Python中描述的方法之一

然后可以如下所示:

^{pr2}$

相关问题 更多 >

    热门问题