如何使用Ompython覆盖模型系统中的数组?

发布于 2025-02-06 10:16:09 字数 1787 浏览 2 评论 0原文

我正在使用OpenModelica从事一个项目。我正在尝试多次模拟系统,并编写了Python脚本以自动化它。我还覆盖了模拟的每次迭代中的参数,但是如果我尝试覆盖一个数组,我会在控制台上获得此内容:

Traceback (most recent call last):
  File "pythonScripts/testing.py", line 164, in <module>
    startSimulation(5,2,1,4, noFault,flyZone[1])
  File "pythonScripts/testing.py", line 118, in startSimulation
    vars = omc.sendExpression("readSimulationResult(\"System_res.mat\",{time})")
  File "/home/francesco/.local/lib/python3.8/site-packages/OMPython/__init__.py", line 779, in sendExpression
    answer = OMTypedParser.parseString(result)
  File "/home/francesco/.local/lib/python3.8/site-packages/OMPython/OMTypedParser.py", line 120, in parseString
    res = omcGrammar.parseString(string)
  File "/home/francesco/.local/lib/python3.8/site-packages/pyparsing/core.py", line 1143, in parse_string
    raise exc.with_traceback(None)
pyparsing.exceptions.ParseException: Expected end of text, found '('  (at char 4), (line:1, col:5)

这是我如何做到的示例:

def startSimulation(n, intr, miss, statObs, fault, flyZone):
  with open("newValues.txt", 'wt') as f:
        f.write("const.N="+str(n)+"\n")
        f.write("const.nIntr="+str(intr)+"\n")
        f.write("const.nRocket="+str(miss)+"\n")
        f.write("const.nStatObs="+str(statObs)+"\n")
        f.write("fault.transMatrix[4,4]="+str(fault)+"\n")
        f.write("const.flyZone[3]="+flyZone+"\n")
        f.flush()
        os.fsync(f)
  os.system("./System -overrideFile=newValues.txt >> LogOverride")

noFault = "[1, 0, 0, 0; 1, 0, 0, 0; 1, 0, 0, 0; 1, 0, 0, 0]"
flyZone = ["{100,100,100}","{150,150,150}","{200,200,200}"]

startSimulation(5,2,1,4, noFault,flyZone[1])

有什么建议吗?

I'm working on a project, using openmodelica. I'm trying to simulate the system multiple times and i wrote a python script to automatize it. I also override parameters on each iteration of the simulation, but if i try to override an array i'll get this on console:

Traceback (most recent call last):
  File "pythonScripts/testing.py", line 164, in <module>
    startSimulation(5,2,1,4, noFault,flyZone[1])
  File "pythonScripts/testing.py", line 118, in startSimulation
    vars = omc.sendExpression("readSimulationResult(\"System_res.mat\",{time})")
  File "/home/francesco/.local/lib/python3.8/site-packages/OMPython/__init__.py", line 779, in sendExpression
    answer = OMTypedParser.parseString(result)
  File "/home/francesco/.local/lib/python3.8/site-packages/OMPython/OMTypedParser.py", line 120, in parseString
    res = omcGrammar.parseString(string)
  File "/home/francesco/.local/lib/python3.8/site-packages/pyparsing/core.py", line 1143, in parse_string
    raise exc.with_traceback(None)
pyparsing.exceptions.ParseException: Expected end of text, found '('  (at char 4), (line:1, col:5)

This is an example of how i did it:

def startSimulation(n, intr, miss, statObs, fault, flyZone):
  with open("newValues.txt", 'wt') as f:
        f.write("const.N="+str(n)+"\n")
        f.write("const.nIntr="+str(intr)+"\n")
        f.write("const.nRocket="+str(miss)+"\n")
        f.write("const.nStatObs="+str(statObs)+"\n")
        f.write("fault.transMatrix[4,4]="+str(fault)+"\n")
        f.write("const.flyZone[3]="+flyZone+"\n")
        f.flush()
        os.fsync(f)
  os.system("./System -overrideFile=newValues.txt >> LogOverride")

noFault = "[1, 0, 0, 0; 1, 0, 0, 0; 1, 0, 0, 0; 1, 0, 0, 0]"
flyZone = ["{100,100,100}","{150,150,150}","{200,200,200}"]

startSimulation(5,2,1,4, noFault,flyZone[1])

Any suggestion?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱的故事 2025-02-13 10:16:10

生成C代码时,OpenModelica将将数组扩展到标量,因此您需要在NewValues.txt中分别覆盖每个数组元素:

fault.transMatrix[1,1]=1
fault.transMatrix[1,2]=0
fault.transMatrix[1,3]=0
fault.transMatrix[1,4]=0
fault.transMatrix[2,1]=1
fault.transMatrix[2,2]=0
fault.transMatrix[2,3]=0
fault.transMatrix[2,4]=0
...

OpenModelica will expand arrays into scalars when the C code is generated so you need to override each of the array elements separately in newValues.txt:

fault.transMatrix[1,1]=1
fault.transMatrix[1,2]=0
fault.transMatrix[1,3]=0
fault.transMatrix[1,4]=0
fault.transMatrix[2,1]=1
fault.transMatrix[2,2]=0
fault.transMatrix[2,3]=0
fault.transMatrix[2,4]=0
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文