【问题标题】:Translate model before Simulating it in Modelica using Python在使用 Python 在 Modelica 中模拟模型之前翻译模型
【发布时间】:2017-12-05 18:22:48
【问题描述】:

我正在尝试使用 Python 运行 Dymola,每次我更改参数的值时,都会通过许多“if elseif”使用不同的方程来激活和停用相应的方程。

我正在使用此代码:

def simulationModel(model, startTime, stopTime, resultFile, initialNames, initialValues, finalNames):

output = dymola.simulateExtendedModel(model,
                                      startTime=startTime,
                                      stopTime=stopTime,
                                      numberOfIntervals=500,
                                      outputInterval=0.0,
                                      method="Dassl",
                                      tolerance=0.0001,
                                      fixedstepsize=0.0,
                                      resultFile=resultFile,
                                      initialNames=initialNames,
                                      initialValues=initialValues,
                                      finalNames=finalNames,
                                      autoLoad=False)

status = output[0]

if not status:
    print("Simulation failed.")
    log = dymola.getLastError()
    print(log)
    return

result = output[1]
return result

问题是,当我为参数定义新值时,脚本会在模型中更改它们,但它一直使用我上次在 Dymola 上直接运行程序时使用的相同方程组,这不是案子, 该程序没有考虑到参数会改变要使用的方程组。

有什么建议吗? 问候

【问题讨论】:

  • 也许这些参数会被评估?还是 Dymola 将它们视为结构变量?另见stackoverflow.com/questions/35907544/…stackoverflow.com/questions/32739428/…
  • 您也应该尝试直接在 Dymola GUI 中翻译模型,然后检查是否可以在不再次翻译的情况下更改模拟之间的参数。
  • 我无法在不翻译的情况下更改参数,因为参数的每次更改都会导致所研究的方程组发生更改。我尝试在 Dymola 中设置:参数 Real eq1=0 annotation(Evaluate=false);参数 Real eq2=1 注释(Evaluate=false);但这不起作用,我得到错误:Errors or failure to expand vector or matrix expressions.
  • 所以这与 Python 无关,它是一个纯 Modelica 问题。某些更改只需要新的翻译。
  • 您能否给我一些建议,告诉我如何使新的翻译成为可能?

标签: python-2.7 dymola


【解决方案1】:

我是这样做的:

dymola = DymolaInterface() 

## This sends the string right to Dymola, in this case to open a Model.
dymola.ExecuteCommand('DymolaCommands.SimulatorAPI.openModel("Path\To\your\file.mo", changeDirectory=false);')

## Now Translate the Model for every time you want to assign certain variables
ModelTranslate = "Dymola.Path.To.Your.Model(Dymola.Path.to.Variable=1000)"
dymola.translateModel(ModelTranslate)
output = dymola.simulateExtendedModel("", 0.0, 2678400, 0, 0.0, "Dassl", 0.0001, 0.0, Result, [], [], ["Path.To.Output" ], True)

但是,我只使用 Python 3 的接口,我依稀记得它在用户手册中说只与 Python 3 兼容。但是你应该在其他编程环境下的用户手册 2 中找到。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-04
    相关资源
    最近更新 更多