Skip to content
Advertisement

How to edit parameters of modelica model with OMPython or Python CLI interface

I want to edit modelica model parameters in Python CLI interface, But don’t know how to find the correct method to make it.

Modelica model code :

model Syslam_Q5
  HePackage.Components.Hlam hlam(
    UCfile=
        "C:/Users/Pikachu/Docs/i_v2/H50.txt",
         A_HS_mod1 = 0.0786,
         CSize_flag=false,
         A_HS_mod2 = 0.0914,
         A_HS_mod3 = 0.0223,
         A_HS_mod4 = 0.0245)

Python code :

from OMPython import OMCSessionZMQ
omc = OMCSessionZMQ()
cmds = [
    'loadFile("HePackage.mo")',   
    #'removeElementModifiers(HePackage.Systems.Syslam_Q5, "component", false)',
    'setElementModifierValue(HePackage.Systems.Syslam_Q5, HePackage.Components.Hlam, hlam.UCfile = C:/Users/Pikachu/Docs/i_v2/H100.txt)',
    #'setParameterValue(HePackage.Systems.Syslam_Q5, hlam.UCfile, $Code(=C:/Users/Pikachu/Docs/i_v2/H100.txt))',
    'saveModel("example_edit.mo", Example)',
    ]
for cmd in cmds:
    answer = omc.sendExpression(cmd)
    print(cmd, ':', answer)

In a folder i have around 10 text files, I want to run the modelica model for all the text files. How to do that with Python interface. Thanks

Advertisement

Answer

I made this package for testing in HePackage.mo:

package HePackage

package Systems

model Syslam_Q5
  HePackage.Components.Hlam hlam(
    UCfile=
        "C:/Users/Pikachu/Docs/i_v2/H50.txt",
         A_HS_mod1 = 0.0786,
         CSize_flag=false,
         A_HS_mod2 = 0.0914,
         A_HS_mod3 = 0.0223,
         A_HS_mod4 = 0.0245,
         A_HS_mod5 = {5, 4, 3, 2, 1});
end Syslam_Q5;

end Systems;

package Components
 model Hlam
   parameter String UCfile = "";
   parameter Real A_HS_mod1 = 0.0;
   parameter Real A_HS_mod2 = 0.0;
   parameter Real A_HS_mod3 = 0.0;
   parameter Real A_HS_mod4 = 0.0;
   parameter Boolean CSize_flag = false;
   parameter Real A_HS_mod5[5] = {1, 2, 3, 4, 5};
 end Hlam; 
end Components;

end HePackage;

Then using this setModifiers.mos script:

loadFile("HePackage.mo"); getErrorString();
list(HePackage.Systems.Syslam_Q5); getErrorString();
setElementModifierValue(HePackage.Systems.Syslam_Q5, hlam.UCfile, $Code(="C:/some/other/file.txt")); getErrorString();
setElementModifierValue(HePackage.Systems.Syslam_Q5, hlam.A_HS_mod1, $Code(=1.0)); getErrorString();
setElementModifierValue(HePackage.Systems.Syslam_Q5, hlam.CSize_flag, $Code(=true)); getErrorString();
setElementModifierValue(HePackage.Systems.Syslam_Q5, hlam.A_HS_mod2, $Code(=2.0)); getErrorString();
setElementModifierValue(HePackage.Systems.Syslam_Q5, hlam.A_HS_mod3, $Code(=3.0)); getErrorString();
setElementModifierValue(HePackage.Systems.Syslam_Q5, hlam.A_HS_mod4, $Code(=4.0)); getErrorString();
setElementModifierValue(HePackage.Systems.Syslam_Q5, hlam.A_HS_mod5, $Code(={1.0, 2.0, 5.0, 4.0, 3.0})); getErrorString();
list(HePackage.Systems.Syslam_Q5); getErrorString();

You get via running: omc setModifiers.mos:

adrpo33@ida-0030 MINGW64 /c/Users/adrpo33/Downloads
# omc setModifiers.mos
true
""
"model Syslam_Q5
  HePackage.Components.Hlam hlam(UCfile = "C:/Users/Pikachu/Docs/i_v2/H50.txt", A_HS_mod1 = 0.0786, CSize_flag = false, A_HS_mod2 = 0.0914, A_HS_mod3 = 0.0223, A_HS_mod4 = 0.0245, A_HS_mod5 = {5, 4, 3, 2, 1});
end Syslam_Q5;"
""
Ok
""
Ok
""
Ok
""
Ok
""
Ok
""
Ok
""
Ok
""
"model Syslam_Q5
  HePackage.Components.Hlam hlam(UCfile = "C:/some/other/file.txt", A_HS_mod1 = 1.0, CSize_flag = true, A_HS_mod2 = 2.0, A_HS_mod3 = 3.0, A_HS_mod4 = 4.0, A_HS_mod5 = {1.0, 2.0, 5.0, 4.0, 3.0});
end Syslam_Q5;"
""

I used a .mos script but you can use these commands via OMPython.

I don’t know what you need to do, it might be easier to just build another model on the fly at the top level, something like:

loadString("
model SomeParameterSet1
  extends HePackage.Systems.Syslam_Q5(hlam(file="some/file", A_HS_mod1 = 1));
end SomeParameterSet1;");
simulate(SomeParameterSet1);
saveModel("SomeParameterSet1.mo", SomeParameterSet1);
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement