我正在使用 Dymola 的 python_interface 并想模拟一组模型:
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 文件,这对我来说很好。
感谢您的帮助!
请您参考如下方法:
您可以使用标记 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]
作为替代方案,您可以先转换模型,然后使用 tic, toc functions analog in Python 中描述的方法之一在 Python 中测量模拟命令执行所需的时间。
这可能如下所示:
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
