Apartments2Grid notebook example¶
In this example, a rule based controller is used to control the model “Apartments2Thermal-v0”. At first we import the controller:
[1]:
from energym.examples.Controller import LabController
Next, we import Energym and create the simulation environment by specifying the model, a weather file and the number of simulation days.
[2]:
import energym
weather = "ESP_CT_Barcelona"
env = energym.make("Apartments2Grid-v0", weather=weather, simulation_days=30)
C:\Users\PSh\AppData\Local\Continuum\anaconda3\envs\Opti\lib\site-packages\pandas\core\indexing.py:494: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
self.obj[item] = s
[OK] fmi2Instantiate: The Resource location of FMU with instance name %s is %s.
[WARNING] fmi2Instantiate: Argument loggingOn is set to %d
. This is not supported. loggingOn will default to '0'.
[OK] The current working directory is %s
[OK] fmi2Instantiate: Path to fmuUnzipLocation is not %s.
[OK] fmi2Instantiate: Path to fmuUnzipLocation is not %s.
[OK] fmi2Instantiate: Path to fmuUnzipLocation is not %s.
[OK] fmi2Instantiate: Path to fmuUnzipLocation %s
[OK] fmi2Instantiate: Path to fmuResourceLocation %s
[OK] Command executes to copy content of resources folder: %s
[OK] fmi2Instantiate: Path to model description file is %s.
[OK] fmi2Instantiate: The FMU modelIdentifier is %s.
[OK] fmi2Instantiate: The FMU modelGUID is %s.
[OK] fmi2Instantiate: Slave %s is instantiated.
[OK] fmi2Instantiate: Instantiation of %s succeded.
[OK] fmi2EnterInitializationMode: The sockfd is %d.
[OK] fmi2EnterInitializationMode: The port number is %d.
[OK] fmi2EnterInitializationMode: This hostname is %s.
[OK] fmi2EnterInitializationMode: TCPServer Server waiting for clients on port: %d.
[OK] fmi2EnterInitializationMode: The number of input variables is %d.
[OK] fmi2EnterInitializationMode: The number of output variables is %d.
[OK] Get input file from resource folder %s.
[OK] Searching for following pattern %s
[OK] Read directory and search for *.idf, *.epw, or *.idd file.
[OK] Read directory and search for *.idf, *.epw, or *.idd file.
[OK] Read directory and search for *.idf, *.epw, or *.idd file.
[OK] Read directory and search for *.idf, *.epw, or *.idd file.
[OK] Read directory and search for *.idf, *.epw, or *.idd file.
[OK] Read directory and search for *.idf, *.epw, or *.idd file.
[OK] Found matching file %s.
[OK] done searching pattern %s
[OK] Get input file from resource folder %s.
[OK] Searching for following pattern %s
[OK] Read directory and search for *.idf, *.epw, or *.idd file.
[OK] Read directory and search for *.idf, *.epw, or *.idd file.
[OK] Read directory and search for *.idf, *.epw, or *.idd file.
[OK] Read directory and search for *.idf, *.epw, or *.idd file.
[OK] Found matching file %s.
[OK] done searching pattern %s
[OK] Get input file from resource folder %s.
[OK] Searching for following pattern %s
[OK] Read directory and search for *.idf, *.epw, or *.idd file.
[OK] Read directory and search for *.idf, *.epw, or *.idd file.
[OK] Read directory and search for *.idf, *.epw, or *.idd file.
[OK] Found matching file %s.
[OK] done searching pattern %s
[OK] This version uses the **energyplus** command line interface to call the EnergyPlus executable. **RunEPlus.bat** and **runenergyplus** , which were used in earlier versions, were deprecated as of August 2015.
[OK] fmi2EnterInitializationMode: The connection has been accepted.
[OK] fmi2EnterInitializationMode: Slave %s is initialized.
The control inputs can be inspected using the get_inputs_names()
method and to construct a controller, we pass the list of inputs and further parameters. This controller determines inputs to get close to the temperature setpoints and uses fixed setpoints during the night.
[3]:
inputs = env.get_inputs_names()
print(inputs)
controller = LabController(control_list=inputs, lower_tol=0.3, upper_tol=0.8, nighttime_setback=True, nighttime_start=18, nighttime_end=6, nighttime_temp=18)
['Bd_Ch_EV1Bat_sp', 'Bd_Ch_EV2Bat_sp', 'Bd_Pw_Bat_sp', 'P1_T_Thermostat_sp', 'P2_T_Thermostat_sp', 'P3_T_Thermostat_sp', 'P4_T_Thermostat_sp']
To run the simulation, a number of steps is specified (here 480 steps per day for 10 days) and the obtained control inputs are passed to the simulation model with the step()
method. To generate some plots later on, we save all the inputs and outputs in lists.
[4]:
steps = 480*10
out_list = []
outputs = env.get_output()
controls = []
hour = 0
for i in range(steps):
control = controller.get_control(outputs, 21, hour)
control['Bd_Ch_EV1Bat_sp'] = [0.0]
control['Bd_Ch_EV2Bat_sp'] = [0.0]
controls +=[ {p:control[p][0] for p in control} ]
outputs = env.step(control)
_,hour,_,_ = env.get_date()
out_list.append(outputs)
Since the inputs and outputs are given as dictionaries and are collected in lists, we can simply load them as a pandas.DataFrame.
[5]:
import pandas as pd
out_df = pd.DataFrame(out_list)
cmd_df = pd.DataFrame(controls)
To generate plots, we can directly get the data from the DataFrames, by using the key names. Displayed are the zone temperatures and the setpoints determined by the controller for zone 1, the external temperature, and the total power demand.
[6]:
import matplotlib.pyplot as plt
%matplotlib notebook
f, (ax1,ax2,ax3) = plt.subplots(3,figsize=(10,15))#
ax1.plot(out_df['Z01_T'], 'r')
ax1.plot(out_df['P1_T_Thermostat_sp_out'], 'b--')
ax1.set_ylabel('Temp')
ax1.set_xlabel('Steps')
ax2.plot(out_df['Ext_T'], 'r')
ax2.set_ylabel('Temp')
ax2.set_xlabel('Steps')
ax3.plot(out_df['Fa_Pw_All'], 'g')
ax3.set_ylabel('Power')
ax3.set_xlabel('Steps')
plt.subplots_adjust(hspace=0.4)
plt.show()
To end the simulation, the close()
method is called. It deletes files that were produced during the simulation and stores some information about the simulation in the runs folder.
[7]:
env.close()
[OK] fmi2Terminate: fmiFreeInstanceSlave must be called to free the FMU instance.
[OK] fmi2FreeInstance: The function fmi2FreeInstance of instance %s is executed.
[OK] freeInstanceResources: %s will be freed.
[ ]: