Skip to content

Commit

Permalink
Merge pull request #91 from intelligent-environments-lab/citylearn-ch…
Browse files Browse the repository at this point in the history
…allenge-2023-power-outage-env

Storage bug fixes
  • Loading branch information
kingsleynweye committed Oct 15, 2023
2 parents 23c8b61 + 6bccbe0 commit f84ff1e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
35 changes: 19 additions & 16 deletions citylearn/building.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import math
from typing import Any, List, Mapping, Tuple, Union
from gym import spaces
import numpy as np
Expand Down Expand Up @@ -942,20 +941,21 @@ def update_cooling_storage(self, action: float):
"""

energy = action*self.cooling_storage.capacity
demand = self.cooling_demand[self.time_step]
temperature = self.weather.outdoor_dry_bulb_temperature[self.time_step]

if energy > 0.0:
temperature = self.weather.outdoor_dry_bulb_temperature[self.time_step]
max_electric_power = self.downward_electrical_flexibility
max_output = self.cooling_device.get_max_output_power(temperature, heating=False, max_electric_power=max_electric_power)
energy = min(max_output, energy)
electricity_consumption = self.cooling_device.get_input_power(energy, temperature, heating=False)
self.cooling_device.update_electricity_consumption(electricity_consumption)

else:
demand = self.cooling_demand[self.time_step]
energy = max(-demand, energy)

self.cooling_storage.charge(energy)
charged_energy = max(self.cooling_storage.energy_balance[self.time_step], 0.0)
electricity_consumption = self.cooling_device.get_input_power(charged_energy, temperature, heating=False)
self.cooling_device.update_electricity_consumption(electricity_consumption)

def update_heating_demand(self, action: float):
"""Update space heating demand for current time step."""
Expand Down Expand Up @@ -989,22 +989,23 @@ def update_heating_storage(self, action: float):
"""

energy = action*self.heating_storage.capacity
temperature = self.weather.outdoor_dry_bulb_temperature[self.time_step]

if energy > 0.0:
temperature = self.weather.outdoor_dry_bulb_temperature[self.time_step]
max_electric_power = self.downward_electrical_flexibility
max_output = self.heating_device.get_max_output_power(temperature, heating=True, max_electric_power=max_electric_power)\
if isinstance(self.heating_device, HeatPump) else self.heating_device.get_max_output_power(max_electric_power=max_electric_power)
energy = min(max_output, energy)
electricity_consumption = self.heating_device.get_input_power(energy, temperature, heating=True)\
if isinstance(self.heating_device, HeatPump) else self.heating_device.get_input_power(energy)
self.heating_device.update_electricity_consumption(electricity_consumption)

else:
demand = self.heating_demand[self.time_step]
energy = max(-demand, energy)

self.heating_storage.charge(energy)
charged_energy = max(self.heating_storage.energy_balance[self.time_step], 0.0)
electricity_consumption = self.heating_device.get_input_power(charged_energy, temperature, heating=True)\
if isinstance(self.heating_device, HeatPump) else self.heating_device.get_input_power(charged_energy)
self.heating_device.update_electricity_consumption(electricity_consumption)

def update_energy_from_dhw_device(self):
r"""Update dhw device electricity consumption and energy tranfer for current time step's dhw demand."""
Expand Down Expand Up @@ -1033,22 +1034,23 @@ def update_dhw_storage(self, action: float):
"""

energy = action*self.dhw_storage.capacity
temperature = self.weather.outdoor_dry_bulb_temperature[self.time_step]

if energy > 0.0:
temperature = self.weather.outdoor_dry_bulb_temperature[self.time_step]
max_electric_power = self.downward_electrical_flexibility
max_output = self.dhw_device.get_max_output_power(temperature, heating=True, max_electric_power=max_electric_power)\
if isinstance(self.dhw_device, HeatPump) else self.dhw_device.get_max_output_power(max_electric_power=max_electric_power)
energy =min(max_output, energy)
electricity_consumption = self.dhw_device.get_input_power(energy, temperature, heating=True)\
if isinstance(self.dhw_device, HeatPump) else self.dhw_device.get_input_power(energy)
self.dhw_device.update_electricity_consumption(electricity_consumption)
energy = min(max_output, energy)

else:
demand = self.dhw_demand[self.time_step]
energy = max(-demand, energy)

self.dhw_storage.charge(energy)
charged_energy = max(self.dhw_storage.energy_balance[self.time_step], 0.0)
electricity_consumption = self.dhw_device.get_input_power(charged_energy, temperature, heating=True)\
if isinstance(self.dhw_device, HeatPump) else self.dhw_device.get_input_power(charged_energy)
self.dhw_device.update_electricity_consumption(electricity_consumption)

def update_non_shiftable_load(self):
r"""Update non shiftable loads electricity consumption for current time step non shiftable load."""
Expand Down Expand Up @@ -1590,9 +1592,10 @@ def reset_data_sets(self):
def update_variables(self):
"""Update cooling, heating, dhw and net electricity consumption as well as net electricity consumption cost and carbon emissions."""

# cooling electricity consumption
if self.time_step == 0:
temperature = self.weather.outdoor_dry_bulb_temperature[self.time_step]

# cooling electricity consumption
cooling_demand = self.__energy_from_cooling_device[self.time_step] + self.cooling_storage.energy_balance[self.time_step]
cooling_electricity_consumption = self.cooling_device.get_input_power(cooling_demand, temperature, heating=False)
self.cooling_device.update_electricity_consumption(cooling_electricity_consumption)
Expand Down
2 changes: 1 addition & 1 deletion citylearn/energy_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ def soc(self) -> np.ndarray:
def energy_init(self) -> float:
r"""Latest energy level after accounting for standby hourly lossses in [kWh]."""

return self.__soc[self.time_step - 1]*self.capacity*(1 - self.loss_coefficient)
return max(0.0, self.__soc[self.time_step - 1]*self.capacity*(1 - self.loss_coefficient))

@property
def energy_balance(self) -> np.ndarray:
Expand Down

0 comments on commit f84ff1e

Please sign in to comment.