我用OMPython覆盖变量,但模拟以默认值开始



我试图在openmodelica中制作的系统上工作,现在我想多次模拟它。在每次迭代中,我必须改变一些值,我使用OMPython来做到这一点。在这部分代码中,我重写了感兴趣的参数:

#Overwrite parameters
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")
for i in range(len(fault)):
for j in range(len(fault[i])):
f.write("fault.transMatrix["+str(i+1)+","+str(j+1)+"]="+str(fault[i][j])+"n")
for i in range(3):
f.write("const.flyZone["+str(i+1)+"]="+str(flyZone)+"n")
f.flush()
os.fsync(f)

os.system("./System -overrideFile=newValues.txt >> LogOverride.txt")

现在,如果我尝试从.mat文件中获取覆盖的所有值,例如,使用以下方法:

print(str(omc.sendExpression("val(const.N," + str(stopTime) + ", "System_res.mat")")))

它们被更改,但模拟以默认值开始。

例如,如果我设置

const。N = 7

,在。om文件中是

const。N = 5

模拟计算N为5而不是7。

有关更多信息,请访问github存储库:https://github.com/BigMautone/Drones.git

python脚本位于pythonScripts/testing.py中,大部分参数更改位于constant.mo

编辑1:为了了解更多的上下文,我解释了系统的目的以及为什么我看不到脚本所做的更改。该系统必须模拟一群无人机,并实现不同的寻路和避障算法。记录类K内部有系统中使用的所有参数,如N参数,定义蜂群中无人机的数量。因为我想用不同的参数多次模拟系统,所以我必须改变它。所以,如果K.N的默认值是5,我在python脚本中用7来改变它,我希望看到7个无人机做一些事情。相反,系统是用默认值模拟的!

我在这里添加了一部分代码,在那里我看不到overrideFile选项所做的更改。

"""
Here i load all the file and model needed...
"""
omc.sendExpression("buildModel(System, stopTime=180)")
omc.sendExpression("getErrorString()")
#This is the function i made for run the simulation multiple times
def startSimulation(n, intr, miss, statObs, fault, flyZone):
#Overwrite parameters
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")
for i in range(len(fault)):
for j in range(len(fault[i])):
f.write("fault.transMatrix["+str(i+1)+","+str(j+1)+"]="+str(fault[i][j])+"n")
for i in range(3):
f.write("const.flyZone["+str(i+1)+"]="+str(flyZone)+"n")
f.flush()
os.fsync(f)
os.system("./System -overrideFile=newValues.txt >> LogOverride.txt")
os.system("rm -f newValues.txt")      # .... to be on the safe side

#Down there i extract some values...
for j in range(1,n+1):
arrivalTime = omc.sendExpression("val(sucMo.arrivalTime[" + str(j) + "]," + str(stopTime) + ", "System_res.mat")")
droneArrived = omc.sendExpression("val(sucMo.arrived[" + str(j) + "]," + str(stopTime) + ", "System_res.mat")")
droneInfo[j] = (droneArrived, arrivalTime) 

#Here i call the function and try to execute it
startSimulation(7,1,1,1,noFault,100)

如果我打印droneInfo字典,它显然会有7个键,但如果默认值N等于5,那么我将得到这样的输出:

{1:(1.0, 8.0), 2:(1.0, 5.0), 3:(1.0, 8.0), 4:(1.0, 9.0), 5:(1.0, 12.0), 6:(‘南’,‘南’),7:("南"、"南")}

0.0
编辑2:我做了一些修改。我还没有实例化K类,它在数组长度和for循环中经常使用。现在我已经在所有其他模型中这样做了,问题仍然存在。同时,现在所有K类的值都有isValueChangeble标志为false

我试着复制这个。让这个小脚本读取末尾的值:

adrpo33@ida-0030 MINGW64 /c/home/adrpo33/dev/modelica/Drones
# cat val.mos
val(const.N, 9, "System_res.mat"); getErrorString();

然后运行。金属氧化物半导体脚本:

adrpo33@ida-0030 MINGW64 /c/home/adrpo33/dev/modelica/Drones
# omc run.mos
record SimulationResult
resultFile = "C:/home/adrpo33/dev/modelica/Drones/System_res.mat",
simulationOptions = "startTime = 0.0, stopTime = 30.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'System', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
messages = "LOG_SUCCESS       | info    | The initialization finished successfully without homotopy method.
[C:/home/adrpo33/dev/modelica/Drones/Monitors/MonitorSuccess.mo:52:28-52:116:writable]
stdout            | info    | Simulation call terminate() at time 9.000000
|                 | |       | Message : Tutti i droni hanno raggiunto la destinazione oppure hanno avuto collisioni
LOG_SUCCESS       | info    | The simulation finished successfully.
",
timeFrontend = 0.3274193,
timeBackend = 1.4592339,
timeSimCode = 0.5092729,
timeTemplates = 0.1533226,
timeCompile = 24.1450323,
timeSimulation = 3.3621366,
timeTotal = 29.9572641
end SimulationResult;
"Warning: The initial conditions are not fully specified. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->Show additional information from the initialization process, in OMNotebook call setCommandLineOptions("-d=initialization").
"

取出值:

adrpo33@ida-0030 MINGW64 /c/home/adrpo33/dev/modelica/Drones
# omc val.mos
5.0
""

重写值:

adrpo33@ida-0030 MINGW64 /c/home/adrpo33/dev/modelica/Drones
# ./System.exe -override const.N=7
LOG_SUCCESS       | info    | The initialization finished successfully without homotopy method.
[C:/home/adrpo33/dev/modelica/Drones/Monitors/MonitorSuccess.mo:52:28-52:116:writable]
stdout            | info    | Simulation call terminate() at time 9.000000
|                 | |       | Message : Tutti i droni hanno raggiunto la destinazione oppure hanno avuto collisioni
LOG_SUCCESS       | info    | The simulation finished successfully.

取出值:

adrpo33@ida-0030 MINGW64 /c/home/adrpo33/dev/modelica/Drones
# omc val.mos
7.0
""

所以我不能真正复制你的行为。当我更改某些内容时,.mat文件会反映更改。

最新更新