Buildings.Utilities.IO.Python27.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.

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

Package Content

Name Description
Buildings.Utilities.IO.Python27.Examples.KalmanFilter KalmanFilter Kalman filter implemented in Python and called from Modelica
Buildings.Utilities.IO.Python27.Examples.SimpleRoom SimpleRoom Simple room model implemented in Python that outputs the temperature and the energy

Buildings.Utilities.IO.Python27.Examples.KalmanFilter Buildings.Utilities.IO.Python27.Examples.KalmanFilter

Kalman filter implemented in Python and called from Modelica

Buildings.Utilities.IO.Python27.Examples.KalmanFilter

Information

This example demonstrates the implementation of a Kalman filter in Python. The model generates a uniform random number, which is computed in the Python file KalmanFilter.py by the function random(seed). This random number is added to a sine wave and then sent to the function filter(u) in the above Python file. The function filter(u) implements a Kalman filter that estimates and returns the state. The function saves its temporary variables to a file called tmp-kalman.pkl.

When simulating this model, the figure below will be generated which shows the sine wave, the sine wave plus noise, which is input to the Kalman filter, and the estimated state which is the output of the Kalman filter.

image

Implementation

The code is based on http://www.scipy.org/Cookbook/KalmanFiltering.

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

Parameters

TypeNameDefaultDescription
TimesamplePeriod0.001Sample period of component [s]

Modelica definition

model KalmanFilter "Kalman filter implemented in Python and called from Modelica" extends Modelica.Icons.Example; Real_Real ran( nDblWri=1, nDblRea=1, functionName="random", moduleName="KalmanFilter", samplePeriod=samplePeriod) "Generate a random number in Python"; Modelica.Blocks.Sources.Clock clock; Real_Real kalFil( moduleName="KalmanFilter", functionName="filter", nDblWri=1, nDblRea=1, samplePeriod=samplePeriod) "Kalman filter in Python"; parameter Modelica.SIunits.Time samplePeriod=0.001 "Sample period of component"; Modelica.Blocks.Sources.Sine sine(freqHz=1) "Sine wave"; Modelica.Blocks.Math.Add add; equation // Delete the temporary file generated by the Python file // at the start and end of the simulation. when {initial(), terminal()} then Modelica.Utilities.Files.removeFile("tmp-kalman.pkl"); end when; connect(clock.y, ran.uR[1]); connect(add.y, kalFil.uR[1]); connect(ran.yR[1], add.u2); connect(sine.y, add.u1); end KalmanFilter;

Buildings.Utilities.IO.Python27.Examples.SimpleRoom Buildings.Utilities.IO.Python27.Examples.SimpleRoom

Simple room model implemented in Python that outputs the temperature and the energy

Buildings.Utilities.IO.Python27.Examples.SimpleRoom

Information

This example demonstrates the implementation of a simple room model that is implemented in the Python module Resources/Python-Sources/SimpleRoom.py. The Python model computes a first-order response to the room model. It returns the current room air temperature and the energy consumed during the simulation. The Python module also passes an object from one call to the next. This object contains the past room temperature, energy and time when the function was called, stored as a Python dictionary. This illustrates how Python data structures can be passed between function calls, thereby enabling for example to call some (memory-intensive) machine learning program, implemented in Python, from a Modelica block that may then pass the output of this program to a controller.

Note that this example is for demonstration only. An implementation in Modelica would be much simpler and computationally more efficient.

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

Modelica definition

model SimpleRoom "Simple room model implemented in Python that outputs the temperature and the energy" extends Modelica.Icons.Example; Modelica.Blocks.Sources.Clock clock; Real_Real pyt( moduleName="SimpleRoom", functionName="doStep", nDblRea=2, nDblWri=4, samplePeriod=60, passPythonObject=true) "Python interface"; Modelica.Blocks.Sources.Constant T0(k=293.15) "Initial temperature"; Modelica.Blocks.Routing.Multiplex4 mul "Multiplex"; Controls.OBC.CDL.Logical.OnOffController onOffCon( bandwidth=2, u(unit="K", displayUnit="degC")) "On/off controller"; Controls.OBC.CDL.Conversions.BooleanToReal onOff(realTrue=1.2) "On off control"; Modelica.Blocks.Sources.Sine TAmb( amplitude=5, offset=283.15, y(unit="K", displayUnit="degC"), freqHz=1/86400, phase=-1.5707963267949) "Ambient temperature"; equation connect(mul.u1[1], T0.y); connect(clock.y, mul.u4[1]); connect(mul.y, pyt.uR); connect(pyt.yR[1], onOffCon.u); connect(onOffCon.reference, T0.y); connect(onOffCon.y, onOff.u); connect(onOff.y, mul.u3[1]); connect(TAmb.y, mul.u2[1]); end SimpleRoom;