Search code examples
modelicaopenmodelica

Same results after sweeping globalSeed parameter


I rewrited an example found in the Forum (which by the way works fine) in order to suit my case - swepping globaSeed parameter.

I expected that each simulation run will give a different result, but it wasn't so.

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`

When I simulate model using OMedit, I've got different results as it should be.

Below is the script I used for this experiment. Model is a simple one, made from the two components from PNLib library:

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,\n";
for globalSeed in {1, 2, 3, 4} loop
  str_a := String(globalSeed); getErrorString();
  str_cmd := "brisi.exe -override globalSeed=" + str_a;
  print(str_cmd + "\n"); 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)]) + ",\n";
  closeSimulationResultFile();
end for;
print(results); getErrorString();
//oms_getInteger("globalSeed");getErrorString();
writeFile("results.csv", results);
plot(p1, true, "results.csv");

Apreciate if someone could help me with this.


Solution

  • From the model it is seen thatglobalSeed is not a top-level parameter but inside settings class, so please change to settings.globalSeedin the code as below inside the for loop:

    str_cmd := "brisi.exe -override settings.globalSeed=" + str_a;

    The notifications logs is shown in below image:

    Notification logs after running script

    Plot command output:

    Output Plot

    Hope this works!