**DICTIONARY CONTENTS CHANGE ORDER! WHY ?**
我分配字典的内容更改顺序! 我分配Fc = {0.25,0.4,0.1,0.001,0.0491,0.06},然后在Python 3.6.7 Shell中我得到Fc ='{0.25,0.4,0.1,0.06,0.0491,0.001}'。为什么会这样呢? 我的目的是使用setComponentModifierValue命令更改模拟的修改器值。
我仅找到一个变量b = 0.5的示例omc.sendExpression(“ setComponentModifierValue(structure_test_final_no_payload,Fc,$ Code(=” + str(b)+“))”)),但是当有人想要更改数组。 n
在OpenModelica中,Fc定义为: 参数Real Fc [6] = {0.55,1.2,0.5,0.05,0.0491,0.08}
和的结果 omc.sendExpression(“ getComponentModifierValue(structure_test_final_no_payload,Fc)”) 是 '{0.55,1.2,0.5,0.05,0.0491,0.08}'
代替'{0.25,0.4,0.1,0.001,0.0491,0.06}'
我得到'{0.25,0.4,0.1,0.06,0.0491,0.001}'
答案 0 :(得分:0)
在Python中不要使用花括号{...},这些花括号没有顺序。请改用列表。
from OMPython import OMCSessionZMQ
omc = OMCSessionZMQ()
x = omc.sendExpression("cd()")
print("Got:" + str(x) + "\n");
loadFile = "loadFile(\"" + str(x) + "/m.mo\")"
print("loadFile: " + loadFile + "\n")
x = omc.sendExpression(loadFile)
print("Got:" + str(x) + "\n")
x = omc.sendExpression("getErrorString()")
print("Got:" + str(x) + "\n")
x = omc.sendExpression("getComponentModifierValue(structure_test_final_no_payload, Fc)")
print("Got:" + str(x) + "\n")
Fc= [0.25, 0.4, 0.1, 0.001, 0.0491, 0.06]
print("Fc: " + str(Fc) + "\n")
strFc = "{" + ",".join(map(str, Fc)) + "}"
print("strFc: " + strFc + "\n")
x = omc.sendExpression("setComponentModifierValue(structure_test_final_no_payload, Fc, $Code(="+strFc+"))")
print("Got:" + str(x) + "\n")
x = omc.sendExpression("getErrorString()")
print("Got:" + str(x) + "\n")
x = omc.sendExpression("getComponentModifierValue(structure_test_final_no_payload, Fc)")
print("Got:" + str(x) + "\n")
x = omc.sendExpression("list(structure_test_final_no_payload)")
print("Got:" + str(x) + "\n")
运行此命令的结果是:
$ /e/bin/python64/python test.py
2019-01-22 22:56:23,647 - OMPython - INFO - OMC Server is up and running at file:///c:/users/adrpo33/appdata/local/temp/openmodelica.port.558379ad46714b13bcd0c6aeb065c0a4 pid=22996
Got:C:/home/adrpo33/dev/OMTesting/python
loadFile: loadFile("C:/home/adrpo33/dev/OMTesting/python/m.mo")
Got:True
Got:
Got:{0.55, 1.2, 0.5, 0.05, 0.0491, 0.08}
Fc: [0.25, 0.4, 0.1, 0.001, 0.0491, 0.06]
strFc: {0.25,0.4,0.1,0.001,0.0491,0.06}
Got:Ok
Got:
Got:{0.25, 0.4, 0.1, 0.001, 0.0491, 0.06}
Got:model structure_test_final_no_payload
parameter Real Fc[6] = {0.25, 0.4, 0.1, 0.001, 0.0491, 0.06};
end structure_test_final_no_payload;
文件m.mo包含:
model structure_test_final_no_payload
parameter Real Fc[6] = {0.55, 1.2, 0.5, 0.05, 0.0491, 0.08};
end structure_test_final_no_payload;