【问题标题】:Dymola with python interface - plotting the results to a text file (.txt) or something similar带有 python 接口的 Dymola - 将结果绘制到文本文件 (.txt) 或类似文件中
【发布时间】:2018-02-05 14:51:19
【问题描述】:

我正在从 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

【问题讨论】:

    标签: python output modelica dymola


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 感谢您的帮助,但我认为您误解了我的问题。我想通过 python 开始模拟并将结果不仅存储在绘图中,还存储在表格或类似的东西中,以便将结果与其他结果进行比较。是否有可能同时进行模拟模型+分析结果?
    • 而且我没有找到必须“放置”帖子“Modelica - Dymola Python 接口:将输出格式设置为文本”中提到的代码的地方。
    • 通过调用 simulateModel,您可以在 Dymola 工作目录中以 .mat 文件的形式获得模拟结果。 “Modelica - Dymola Python 接口:将输出格式设置为文本”一文解释了如何以 .txt 文件而不是 .mat 格式获取模拟结果。您可以在调用 simulateModel 之前将代码添加到任何位置
    • 在“try:”上方添加代码并将 dymola 定义为 Dymolainterface() 时,模拟运行没有错误,但我找不到结果 .txt 文件。在“try”下添加代码时(在模拟模型之前),我收到以下错误:“C:\Python27>python.exe DymolaExample.py 文件“DymolaExample.py”,第 8 行 dymola.experimentSetupOutput(textual=True) ^ IndentationError:意外缩进”
    • 现在可以了!非常感谢!问题是我使用 Tab 而不是 4 个空格。
    猜你喜欢
    • 2015-09-20
    • 2018-07-13
    • 1970-01-01
    • 1970-01-01
    • 2018-12-18
    • 1970-01-01
    • 2019-05-13
    • 1970-01-01
    • 2019-03-04
    相关资源
    最近更新 更多