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 :
JavaScript
x
10
10
1
model Syslam_Q5
2
HePackage.Components.Hlam hlam(
3
UCfile=
4
"C:/Users/Pikachu/Docs/i_v2/H50.txt",
5
A_HS_mod1 = 0.0786,
6
CSize_flag=false,
7
A_HS_mod2 = 0.0914,
8
A_HS_mod3 = 0.0223,
9
A_HS_mod4 = 0.0245)
10
Python code :
JavaScript
1
13
13
1
from OMPython import OMCSessionZMQ
2
omc = OMCSessionZMQ()
3
cmds = [
4
'loadFile("HePackage.mo")',
5
#'removeElementModifiers(HePackage.Systems.Syslam_Q5, "component", false)',
6
'setElementModifierValue(HePackage.Systems.Syslam_Q5, HePackage.Components.Hlam, hlam.UCfile = C:/Users/Pikachu/Docs/i_v2/H100.txt)',
7
#'setParameterValue(HePackage.Systems.Syslam_Q5, hlam.UCfile, $Code(=C:/Users/Pikachu/Docs/i_v2/H100.txt))',
8
'saveModel("example_edit.mo", Example)',
9
]
10
for cmd in cmds:
11
answer = omc.sendExpression(cmd)
12
print(cmd, ':', answer)
13
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
:
JavaScript
1
32
32
1
package HePackage
2
3
package Systems
4
5
model Syslam_Q5
6
HePackage.Components.Hlam hlam(
7
UCfile=
8
"C:/Users/Pikachu/Docs/i_v2/H50.txt",
9
A_HS_mod1 = 0.0786,
10
CSize_flag=false,
11
A_HS_mod2 = 0.0914,
12
A_HS_mod3 = 0.0223,
13
A_HS_mod4 = 0.0245,
14
A_HS_mod5 = {5, 4, 3, 2, 1});
15
end Syslam_Q5;
16
17
end Systems;
18
19
package Components
20
model Hlam
21
parameter String UCfile = "";
22
parameter Real A_HS_mod1 = 0.0;
23
parameter Real A_HS_mod2 = 0.0;
24
parameter Real A_HS_mod3 = 0.0;
25
parameter Real A_HS_mod4 = 0.0;
26
parameter Boolean CSize_flag = false;
27
parameter Real A_HS_mod5[5] = {1, 2, 3, 4, 5};
28
end Hlam;
29
end Components;
30
31
end HePackage;
32
Then using this setModifiers.mos
script:
JavaScript
1
11
11
1
loadFile("HePackage.mo"); getErrorString();
2
list(HePackage.Systems.Syslam_Q5); getErrorString();
3
setElementModifierValue(HePackage.Systems.Syslam_Q5, hlam.UCfile, $Code(="C:/some/other/file.txt")); getErrorString();
4
setElementModifierValue(HePackage.Systems.Syslam_Q5, hlam.A_HS_mod1, $Code(=1.0)); getErrorString();
5
setElementModifierValue(HePackage.Systems.Syslam_Q5, hlam.CSize_flag, $Code(=true)); getErrorString();
6
setElementModifierValue(HePackage.Systems.Syslam_Q5, hlam.A_HS_mod2, $Code(=2.0)); getErrorString();
7
setElementModifierValue(HePackage.Systems.Syslam_Q5, hlam.A_HS_mod3, $Code(=3.0)); getErrorString();
8
setElementModifierValue(HePackage.Systems.Syslam_Q5, hlam.A_HS_mod4, $Code(=4.0)); getErrorString();
9
setElementModifierValue(HePackage.Systems.Syslam_Q5, hlam.A_HS_mod5, $Code(={1.0, 2.0, 5.0, 4.0, 3.0})); getErrorString();
10
list(HePackage.Systems.Syslam_Q5); getErrorString();
11
You get via running: omc setModifiers.mos
:
JavaScript
1
27
27
1
adrpo33@ida-0030 MINGW64 /c/Users/adrpo33/Downloads
2
# omc setModifiers.mos
3
true
4
""
5
"model Syslam_Q5
6
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});
7
end Syslam_Q5;"
8
""
9
Ok
10
""
11
Ok
12
""
13
Ok
14
""
15
Ok
16
""
17
Ok
18
""
19
Ok
20
""
21
Ok
22
""
23
"model Syslam_Q5
24
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});
25
end Syslam_Q5;"
26
""
27
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:
JavaScript
1
7
1
loadString("
2
model SomeParameterSet1
3
extends HePackage.Systems.Syslam_Q5(hlam(file="some/file", A_HS_mod1 = 1));
4
end SomeParameterSet1;");
5
simulate(SomeParameterSet1);
6
saveModel("SomeParameterSet1.mo", SomeParameterSet1);
7