This module provides an example that illustrates the use of the python to run a Modelica simulation.
The module buildingspy.simulate.Simulator that can be used to automate running simulations. For example, to run the model Buildings.Controls.Continuous.Examples.PIDHysteresis.mo with controller parameters con.eOn = 1 and con.eOn = 5, use the following commands:
from multiprocessing import Pool
import buildingspy.simulate.Simulator as si
# Function to set common parameters and to run the simulation
def simulateCase(s):
s.setStopTime(86400)
# Kill the process if it does not finish in 1 minute
s.setTimeOut(60)
s.showProgressBar(False)
s.printModelAndTime()
s.simulate()
# Main function
if __name__ == '__main__':
# Build list of cases to run
li = []
# First model
model = 'Buildings.Controls.Continuous.Examples.PIDHysteresis'
s = si.Simulator(model, 'dymola', 'case1')
s.addParameters({'con.eOn': 0.1})
li.append(s)
# second model
s = si.Simulator(model, 'dymola', 'case2')
s.addParameters({'con.eOn': 1})
li.append(s)
# Run all cases in parallel
po = Pool()
po.map(simulateCase, li)
This will run the two test cases and store the results in the directories case1 and case2.
This module provides an example that illustrates the use of the python to plot results from a Dymola simulation.
The file plotResult.py illustrates how to plot results from a Dymola output file. To run the example, proceed as follows:
Open a terminal or dos-shell.
Set the PYTHONPATH environment variables to the directory `bie/BuildingsPy/buildingspy`, such as
export PYTHONPATH=${PYTHONPATH}:../..where the directory ../.. contains the subdirectory buildingspy
Type
python plotResult.py
This will execute the script plotResult.py, which contains the following instructions:
from buildingspy.io.outputfile import Reader
import matplotlib.pyplot as plt
from pylab import figure
import os
# Change fonts to use LaTeX fonts
from matplotlib import rc
rc('text', usetex=True)
rc('font', family='serif')
# Read results
ofr1=Reader(os.path.join("case1", "PIDHysteresis.mat"), "dymola")
ofr2=Reader(os.path.join("case2", "PIDHysteresis.mat"), "dymola")
(time1, T1) = ofr1.values("cap.T")
(time1, y1) = ofr1.values("con.y")
(time2, T2) = ofr2.values("cap.T")
(time2, y2) = ofr2.values("con.y")
# Plot figure
fig = plt.figure()
ax = fig.add_subplot(211)
ax.plot(time1/3600, T1-273.15, 'r', label='$T_1$')
ax.plot(time2/3600, T2-273.15, 'b', label='$T_2$')
ax.set_xlabel('time [h]')
ax.set_ylabel('temperature [$^\circ$C]')
ax.set_xticks(range(25))
ax.set_xlim([0, 24])
ax.legend()
ax.grid(True)
ax = fig.add_subplot(212)
ax.plot(time1/3600, y1, 'r', label='$y_1$')
ax.plot(time2/3600, y2, 'b', label='$y_2$')
ax.set_xlabel('time [h]')
ax.set_ylabel('y [-]')
ax.set_xticks(range(25))
ax.set_xlim([0, 24])
ax.legend()
ax.grid(True)
# Save figure to file
plt.savefig('plot.pdf')
plt.savefig('plot.png')
plt.show()
The script generates the following plot: