如何使用 OMPython 或 Python CLI 界面编辑 modelica 模型的参数

Tam*_*n S 2 python command-line-interface modelica openmodelica

我想在Python CLI界面中编辑modelica模型参数,但不知道如何找到正确的方法来制作它。

Modelica 型号代码:

model Syslam_Q5
  HePackage.Components.Hlam hlam(
    UCfile=
        "C:/Users/Pikachu/Docs/i_v2/H50.txt",
         A_HS_mod1 = 0.0786,
         CSize_flag=false,
         A_HS_mod2 = 0.0914,
         A_HS_mod3 = 0.0223,
         A_HS_mod4 = 0.0245)
Run Code Online (Sandbox Code Playgroud)

Python代码:

from OMPython import OMCSessionZMQ
omc = OMCSessionZMQ()
cmds = [
    'loadFile("HePackage.mo")',   
    #'removeElementModifiers(HePackage.Systems.Syslam_Q5, "component", false)',
    'setElementModifierValue(HePackage.Systems.Syslam_Q5, HePackage.Components.Hlam, hlam.UCfile = C:/Users/Pikachu/Docs/i_v2/H100.txt)',
    #'setParameterValue(HePackage.Systems.Syslam_Q5, hlam.UCfile, $Code(=C:/Users/Pikachu/Docs/i_v2/H100.txt))',
    'saveModel("example_edit.mo", Example)',
    ]
for cmd in cmds:
    answer = omc.sendExpression(cmd)
    print(cmd, ':', answer)
Run Code Online (Sandbox Code Playgroud)

在一个文件夹中,我有大约 10 个文本文件,我想为所有文本文件运行 modelica 模型。如何使用 Python 接口做到这一点。谢谢

AnH*_*ann 5

有多种方法可以做到这一点。

使用设置方法

使用mod.setParameters(["radius=14","c=0.5"]),请参阅OMPython 上的用户指南

模拟时覆盖变量

您还可以覆盖变量,包括带有模拟标志-override-overrideFile的参数。

所以添加一个命令cmds

simulate(example_edit, simflags=\"-override=radius=14,c=0.5\")
Run Code Online (Sandbox Code Playgroud)

应该管用。

如果您有很多参数需要更改,请使用覆盖文件:

radius=14
c=0.5
Run Code Online (Sandbox Code Playgroud)