- Index
- » Programming
- » Modelica Language
- » Connecting a huge number of ports
Connecting a huge number of ports
Connecting a huge number of ports
Hi there!
I am a moderate experienced Modelica (Dymola) User. So i hope you guys could help me.
I want to build up a model with a huge number of ports (>60). They are named like "portxn_1_1, portxn_2_1, portxn_3_1, portxn_1_2, portxn_1_2, ..." and so on.
The first thing is that i don't want to connect them in the diagram editor or by manually programing to connect them with other variables, like this "portxn_1_1.T = Txn[1,1] ; portxn_2_1.T = Txn[2,1] ; portxn_3_1.T = Txn[3,1] ; ..." . So i tried to connect them by a loop, which loops through the numbers "1_1" of the "portxn".
For example like this:
for i in 1:ni loop
portxn_"i"_1.T = Txn[i,1];
end for;
That's the first problem. At my point of view there is no opportunity to loop through a String in Modelica. My first idea was to program the loop in C and include it with as a external function.
Is this the right way? Does anybody have some ideas? Maybe a example or something?
Thanks!
Re: Connecting a huge number of ports
I guess what you want is meta-programming. Write some scripting that builds the model for you.
As far as I know Dymola has a library for this called ModelManangement but I'm not really sure it it can be used to add new connections to classes.
I think the easiest would be to just generate the model file from some other language like python.
You can also do this in OpenModelica with a bit of scripting: buildModel.mos
Code:
// you can also load this via loadFile("File.mo"); if you already have the skeleton
loadString("
model TestConn
connector C
Real T;
end C;
model M
C c_1;
C c_2;
C c_3;
C c_4;
end M;
M m;
Real Txn[4,1];
equation
end TestConn;
"); // load the model skeleton
n := 4;
// build the connect commands as strings
connectStrings := "";
for i in 1:n loop
connectStrings := connectStrings + "addConnection(m.c_"+String(i)+".T, Txn["+ String(i) + ",1], TestConn); getErrorString();\n";
end for;
// write the connect commands to a new .mos script
writeFile("addConnects.mos", connectStrings); getErrorString();
// run the script with the connect commands to be added to the model
runScript("addConnects.mos"); getErrorString();
str := list(TestConn); getErrorString();
"write the model file";
writeFile("TestConn.mo", str);
"--- read the model file ---";
readFile("TestConn.mo");
output of running this via omc:
Code:
adrpo33@ida-0030 MINGW64 /c/home/adrpo33/dev/OMTesting/
$ ~/dev/OpenModelica/build/bin/omc buildModel.mos
true
4
""
true
""
"Ok
\"\"
Ok
\"\"
Ok
\"\"
Ok
\"\"
"
""
"model TestConn
connector C
Real T;
end C;
model M
C c_1;
C c_2;
C c_3;
C c_4;
end M;
M m;
Real Txn[4, 1];
equation
connect(m.c_1.T, Txn[1, 1]);
connect(m.c_2.T, Txn[2, 1]);
connect(m.c_3.T, Txn[3, 1]);
connect(m.c_4.T, Txn[4, 1]);
end TestConn;"
""
"write the model file"
true
"--- read the model file ---"
"model TestConn
connector C
Real T;
end C;
model M
C c_1;
C c_2;
C c_3;
C c_4;
end M;
M m;
Real Txn[4, 1];
equation
connect(m.c_1.T, Txn[1, 1]);
connect(m.c_2.T, Txn[2, 1]);
connect(m.c_3.T, Txn[3, 1]);
connect(m.c_4.T, Txn[4, 1]);
end TestConn;"
- adrpo
- 885 Posts
Re: Connecting a huge number of ports
Thanks for your answer!
Ok, this seems to be a good method to solve this problem. But the next problem is, that i don't have all these functions ( loadString, loadFile, writeFile, readFile) in Dymola. Do i have to activate them in the options or something?
Re: Connecting a huge number of ports
Yes, these are mostly all OpenModelica specific, i think loadFile is available in Dymola as well.
What I show you was a prof of concept done using OpenModelica scripting, you need to find from Dymola's manual and documentation if this is possible to do and how.
- adrpo
- 885 Posts
Re: Connecting a huge number of ports
You could also use a Python script or whatever to generate this file. Then open it in Dymola.
- sjoelund.se
- 1700 Posts
Re: Connecting a huge number of ports
Ok, i think i solved this problem with your help
I've done it without saving. Just copy and paste the command output.
Next question. How do i assign one start value for each cell in a 3D Matrix with variable size? Like this
Code:
...
Integer ni=3;
Integer nj=3;
Integer nk=3;
Real Tc_start=50;
Real Tc[ni,nj,nk](start=Tc_start);
...
Thanks again!
Re: Connecting a huge number of ports
For that you don't need any weird stuff, you do:
Code:
parameter Integer ni=3;
parameter Integer nj=3;
parameter Integer nk=3;
parameter Real Tc_start=50;
Real Tc[ni,nj,nk](each start=Tc_start);
- adrpo
- 885 Posts
- Index
- » Programming
- » Modelica Language
- » Connecting a huge number of ports