Skip to main content
 首页 » 编程设计

python之带有 python 界面的 Dymola之将结果绘制到文本文件 (.txt) 或类似的文件中

2025年01月19日9shangdawei

我正在从 dymola python 界面运行这个示例(见下文),如何将输出绘制/存储在表/文本文件或类似文件中?

from dymola.dymola_interface import DymolaInterface 
from dymola.dymola_exception import DymolaException 
 
dymola = None 
try: 
    # Instantiate the Dymola interface and start Dymola 
    dymola = DymolaInterface() 
 
    # Call a function in Dymola and check its return value 
    result = dymola.simulateModel("Modelica.Mechanics.Rotational.Examples.CoupledClutches") 
    if not result: 
        print("Simulation failed. Below is the translation log.") 
        log = dymola.getLastError() 
        print(log) 
        exit(1) 
 
    dymola.plot(["J1.w", "J2.w", "J3.w", "J4.w"]) 
    dymola.ExportPlotAsImage("C:/temp/plot.png") 
    print("OK") 
except DymolaException as ex: 
    print("Error: " + str(ex)) 
finally: 
    if dymola is not None: 
        dymola.close() 
        dymola = None 

请您参考如下方法:

在 Dymola 手册第 2 卷“第 6.7 节 Dymola 的 Python 接口(interface)”(发布的示例似乎来自该部分)中,您可以找到与多线程相关的示例。在此示例中,您可以找到一种直接创建绘图的方法。

可以在https://www.j-raedler.de/projects/dymat/ 找到一个不错的工具。 .您可以使用它来读取默认结果文件(这是存储在工作目录中的文件 #ModelName#.mat),并在 Python 中高效地进一步处理它。

要从默认的 .mat 文件切换到基于文本的文件,您可以在此处找到命令:Modelica - Dymola Python interface: Set output format to textual

还有其他方法,但这作为第一步应该没问题......