The examples demonstrate how to call Python functions from Modelica.
model Exchange
extends Modelica.Icons.Example;
Real yR1[1] ;
Integer yI1[1] ;
Real yR2[2] ;
Integer yI2[2] ;
algorithm
yR1 :=
Buildings.Utilities.IO.Python27.Functions.exchange(
moduleName="testFunctions",
functionName="r1_r1",
dblWri={2.0},
intWri={0},
nDblWri=1,
nDblRea=1,
nIntWri=0,
nIntRea=0,
nStrWri=0,
strWri={""});
assert(
abs(4 - yR1[1]) < 1e-5, "Error in function r1_r1");
yR1 :=
Buildings.Utilities.IO.Python27.Functions.exchange(
moduleName="testFunctions",
functionName="r2_r1",
dblWri={2.0,3.0},
intWri={0},
nDblWri=2,
nDblRea=1,
nIntWri=0,
nIntRea=0,
nStrWri=0,
strWri={""});
assert(
abs(6 - yR1[1]) < 1e-5, "Error in function r2_r1");
yR2 :=
Buildings.Utilities.IO.Python27.Functions.exchange(
moduleName="testFunctions",
functionName="r1_r2",
dblWri={2.0},
intWri={0},
nDblWri=1,
nDblRea=2,
nIntWri=0,
nIntRea=0,
nStrWri=0,
strWri={""});
assert(
abs(yR2[1] - 2) +
abs(yR2[2] - 4) < 1E-5, "Error in function r1_r2");
(yR1,yI1) :=
Buildings.Utilities.IO.Python27.Functions.exchange(
moduleName="testFunctions",
functionName="i1_i1",
dblWri={0.0},
intWri={3},
nDblWri=0,
nDblRea=0,
nIntWri=1,
nIntRea=1,
nStrWri=0,
strWri={""});
assert((6 - yI1[1]) < 1e-5, "Error in function i1_i1");
(yR1,yI2) :=
Buildings.Utilities.IO.Python27.Functions.exchange(
moduleName="testFunctions",
functionName="i1_i2",
dblWri={0.0},
intWri={2},
nDblWri=0,
nDblRea=0,
nIntWri=1,
nIntRea=2,
nStrWri=0,
strWri={""});
assert(
abs(yI2[1] - 2) +
abs(yI2[2] - 4) < 1E-5, "Error in function i1_i2");
yR2 :=
Buildings.Utilities.IO.Python27.Functions.exchange(
moduleName="testFunctions",
functionName="r1i1_r2",
dblWri={0.3},
intWri={2},
nDblWri=1,
nDblRea=2,
nIntWri=1,
nIntRea=0,
nStrWri=0,
strWri={""});
assert(
abs(yR2[1] - 0.6) +
abs(yI2[2] - 4) < 1E-5, "Error in function r1i1_r2");
Modelica.Utilities.Files.removeFile(fileName="tmp-TestPythonInterface.txt");
Modelica.Utilities.Streams.print(string="1.23", fileName="tmp-TestPythonInterface.txt");
yR1 :=
Buildings.Utilities.IO.Python27.Functions.exchange(
moduleName="testFunctions",
functionName="s2_r1",
dblWri={0.0},
intWri={0},
nDblWri=0,
nDblRea=1,
nIntWri=0,
nIntRea=0,
nStrWri=2,
strWri={"tmp-TestPythonInterface","txt"});
assert(
abs(yR1[1] - 1.23) < 1E-5, "Error in function s2_r1");
end Exchange;