调整globalSeed参数后仿真结果未变化的技术求助
问题:遍历globalSeed参数仿真结果一致(OMEdit中正常)
我改写了论坛中的可用示例脚本,用来遍历globalSeed参数进行仿真,原本期望每次运行能得到不同结果,但实际所有仿真结果完全一致。不过在OMEdit中手动仿真时,每次修改globalSeed都能得到符合预期的不同结果。以下是仿真输出日志和所用脚本代码,求帮忙排查问题。
仿真输出日志
C:\Users\USUARIO\Documents\MyOModelica\MyModels\OMScripting\parsweep1>"C:\Program Files\OpenModelica1.19.2-64bit\bin\omc" parsweep1.mos true "" {"C:/Users/USUARIO/Documents/MyOModelica/MyModels/OMScripting/parsweep1/brisi","brisi_init.xml"} "Notification: Automatically loaded package Modelica 3.2.3 due to uses annotation. Notification: Automatically loaded package Complex 3.2.3 due to uses annotation. Notification: Automatically loaded package ModelicaServices 3.2.3 due to uses annotation. Notification: Automatically loaded package PNlib 2.2 due to usage. 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"). " "time,p1, " brisi.exe -override globalSeed=1 brisi.exe -override globalSeed=2 brisi.exe -override globalSeed=3 brisi.exe -override globalSeed=4 time,p1, 1,46, 2,46, 3,46, 4,46, "" true true
实验所用脚本
loadString(" class brisi PNlib.Components.PD p1(nOut = 1, startTokens = 50) annotation( Placement(visible = true, transformation(origin = {-54, 42}, extent = {{-10, -10}, {10, 10}}, rotation = 0))); PNlib.Components.TDS t1(localSeed = 1, nIn = 1) annotation( Placement(visible = true, transformation(origin = {6, 42}, extent = {{-10, -10}, {10, 10}}, rotation = 0))); inner PNlib.Components.Settings settings(globalSeed = 30020) annotation( Placement(visible = true, transformation(origin = {66, 76}, extent = {{-10, -10}, {10, 10}}, rotation = 0))); equation connect(p1.outTransition[1], t1.inPlaces[1]) annotation( Line(points = {{-44, 42}, {1, 42}}, thickness = 0.5)); end brisi; "); getErrorString(); buildModel(brisi, stopTime=6); getErrorString(); results := "time,p1, "; for globalSeed in {1, 2, 3, 4} loop str_a := String(globalSeed); getErrorString(); str_cmd := "brisi.exe -override globalSeed=" + str_a; print(str_cmd + " "); getErrorString(); system(str_cmd, "output.txt"); getErrorString(); res := readSimulationResult("brisi_res.mat", p1.t); getErrorString(); results := results + str_a + "," + String(res[size(res, 1), size(res, 2)]) + ", "; closeSimulationResultFile(); end for; print(results); getErrorString(); //oms_getInteger("globalSeed");getErrorString(); writeFile("results.csv", results); plot(p1, true, "results.csv");
问题排查与解决
问题核心在于每次仿真都会覆盖同名结果文件,导致读取的始终是首次仿真的数据:
- 运行
brisi.exe时,默认生成的结果文件都是brisi_res.mat,后续仿真会直接覆盖之前的文件。 - 受文件系统缓存或执行时序影响,
readSimulationResult()实际读取的是缓存中未被更新的旧文件内容,因此所有结果一致。
修正步骤:
- 为每个仿真指定唯一的结果文件名,通过
-resultFile参数修改运行命令:
str_cmd := "brisi.exe -override globalSeed=" + str_a + " -resultFile=brisi_res_" + str_a + ".mat";
- 读取结果时对应使用各自的唯一文件:
res := readSimulationResult("brisi_res_" + str_a + ".mat", p1.t);
修改后,每个globalSeed都会生成独立的结果文件,读取时不会混淆,就能得到和OMEdit中一致的不同仿真结果。
内容的提问来源于stack exchange,提问作者cubakija
相关产品推荐
相关产品推荐

