Buildings.Utilities.IO.Python27.Functions.Examples

Collection of models that illustrate model use and test models

Information

This package contains examples for the use of models that can be found in Buildings.Utilities.IO.Python27.Functions.

The examples demonstrate how to call Python functions from Modelica.

Extends from Modelica.Icons.ExamplesPackage (Icon for packages containing runnable examples).

Package Content

Name Description
Buildings.Utilities.IO.Python27.Functions.Examples.Exchange Exchange Test model for exchange function
Buildings.Utilities.IO.Python27.Functions.Examples.ExchangeWithPassPythonObject ExchangeWithPassPythonObject Test model for exchange function with memory

Buildings.Utilities.IO.Python27.Functions.Examples.Exchange Buildings.Utilities.IO.Python27.Functions.Examples.Exchange

Test model for exchange function

Information

This example calls various functions in the Python module testFunctions.py. It tests whether arguments and return values are passed correctly. The functions in testFunctions.py are very simple in order to test whether they compute correctly, and whether the data conversion between Modelica and Python is implemented correctly. Each call to Python is followed by an assert statement which terminates the simulation if the return value is different from the expected value.

Extends from Modelica.Icons.Example (Icon for runnable examples).

Parameters

TypeNameDefaultDescription
BooleanpassPythonObjectfalseSet to true if the Python function returns and receives an object, see User's Guide

Modelica definition

model Exchange "Test model for exchange function" extends Modelica.Icons.Example; parameter Boolean passPythonObject = false "Set to true if the Python function returns and receives an object, see User's Guide"; Real yR1[1] "Real function value"; Integer yI1[1] "Integer function value"; Real yR2[2] "Real function value"; Integer yI2[2] "Integer function value"; protected model M "Class that contains the Python object" Buildings.Utilities.IO.Python27.Functions.BaseClasses.PythonObject pytObj= Buildings.Utilities.IO.Python27.Functions.BaseClasses.PythonObject() "Instance of Python object"; end M; M[8] m "Array with instances of Python objects"; algorithm yR1 := Buildings.Utilities.IO.Python27.Functions.exchange( moduleName="testFunctions", functionName="r1_r1", pytObj=m[1].pytObj, passPythonObject=passPythonObject, 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", pytObj=m[2].pytObj, passPythonObject=passPythonObject, 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", pytObj=m[3].pytObj, passPythonObject=passPythonObject, 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"); // In the call below, yR1 is a dummy variable, as exchange returns (Real[1], Integer[1]) (yR1, yI1) := Buildings.Utilities.IO.Python27.Functions.exchange( moduleName="testFunctions", functionName="i1_i1", pytObj=m[4].pytObj, passPythonObject=passPythonObject, 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"); // In the call below, yR1 is a dummy variable, as exchange returns (Real[1], Integer[2]) (yR1, yI2) := Buildings.Utilities.IO.Python27.Functions.exchange( moduleName="testFunctions", functionName="i1_i2", pytObj=m[5].pytObj, passPythonObject=passPythonObject, 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", pytObj=m[6].pytObj, passPythonObject=passPythonObject, 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"); // From Modelica, write a number to a text file, and from Python, read the text file // and return the number. 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", pytObj=m[7].pytObj, passPythonObject=passPythonObject, 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"); (yR2, yI1) := Buildings.Utilities.IO.Python27.Functions.exchange( moduleName="testFunctions", functionName="r1i1_r2i1", pytObj=m[8].pytObj, passPythonObject=passPythonObject, dblWri={0.3}, intWri={2}, nDblWri=1, nDblRea=2, nIntWri=1, nIntRea=1, nStrWri=0, strWri={""}); assert(abs(yR2[1]-0.6) + abs(yR2[2]-4) < 1E-5, "Error in function r1i1_r2i1"); assert(abs(yI1[1]-3) == 0, "Error in function r1i1_r2i1"); end Exchange;

Buildings.Utilities.IO.Python27.Functions.Examples.ExchangeWithPassPythonObject Buildings.Utilities.IO.Python27.Functions.Examples.ExchangeWithPassPythonObject

Test model for exchange function with memory

Information

This example calls various functions in the Python module testFunctions.py. It tests whether arguments and return values are passed correctly. The functions in testFunctions.py are very simple in order to test whether they compute correctly, and whether the data conversion between Modelica and Python is implemented correctly. Each call to Python is followed by an assert statement which terminates the simulation if the return value is different from the expected value.

Extends from Modelica.Icons.Example (Icon for runnable examples).

Parameters

TypeNameDefaultDescription
BooleanpassPythonObjecttrueSet to true if the Python function returns and receives an object, see User's Guide

Modelica definition

model ExchangeWithPassPythonObject "Test model for exchange function with memory" extends Modelica.Icons.Example; parameter Boolean passPythonObject = true "Set to true if the Python function returns and receives an object, see User's Guide"; Buildings.Utilities.IO.Python27.Functions.BaseClasses.PythonObject pytObj= Buildings.Utilities.IO.Python27.Functions.BaseClasses.PythonObject(); Real yR1[1] "Real function value"; Real yR2[1] "Real function value"; equation yR1 = Buildings.Utilities.IO.Python27.Functions.exchange( moduleName="testFunctions", functionName="r1_r1PassPythonObject", dblWri={2.0}, intWri={0}, nDblWri=1, nDblRea=1, nIntWri=0, nIntRea=0, nStrWri=0, strWri={""}, pytObj=pytObj, passPythonObject=passPythonObject); assert(abs(3-yR1[1]) < 1e-5, "Error in function r1_r1PassPythonObject"); // Invoke the same function with the same Python object. // Hence, pytObj is reused. yR2 = Buildings.Utilities.IO.Python27.Functions.exchange( moduleName="testFunctions", functionName="r1_r1PassPythonObject", dblWri=yR1, intWri={0}, nDblWri=1, nDblRea=1, nIntWri=0, nIntRea=0, nStrWri=0, strWri={""}, pytObj=pytObj, passPythonObject=passPythonObject); assert(abs(16-yR2[1]) < 1e-5, "Error in function r1_r1PassPythonObject"); end ExchangeWithPassPythonObject;

Buildings.Utilities.IO.Python27.Functions.Examples.Exchange.M

Class that contains the Python object

Modelica definition

model M "Class that contains the Python object" Buildings.Utilities.IO.Python27.Functions.BaseClasses.PythonObject pytObj= Buildings.Utilities.IO.Python27.Functions.BaseClasses.PythonObject() "Instance of Python object"; end M;