Repository: RWTH-EBC/pyCity

Issue:
Make Building getter methods more robust
Currently the functions `get_space_heating_power_curve`, `get_electric_power_curve` and `get_dhw_power_curve` of the `Building` class return inconsistent results when used in edge cases.

When no loads are given in the apartments they return empty arrays.

Instead they should return arrays of propper length filled with zeros.

Comments:

Patch:
diff --git a/pycity_base/classes/building.py b/pycity_base/classes/building.py
index 39c4f19..1eb81ee 100644
--- a/pycity_base/classes/building.py
+++ b/pycity_base/classes/building.py
@@ -182,7 +182,11 @@ class Building(object):
         """
 
         #  Initialize array with zeros
-        space_heat_power = np.zeros(len(self.apartments[0].demand_space_heating.get_power(currentValues=currentValues)))
+        if currentValues:
+            timesteps = self.environment.timer.timesteps_horizon
+        else:
+            timesteps = self.environment.timer.timesteps_total
+        space_heat_power = np.zeros(timesteps)
 
         # Get power curves of each apartment
         for apartment in self.apartments:
@@ -209,8 +213,11 @@ class Building(object):
         """
 
         #  Initialize array with zeros
-        space_cooling_power = np.zeros(len(self.apartments[0].demand_space_cooling.
-                                           get_power(currentValues=currentValues)))
+        if currentValues:
+            timesteps = self.environment.timer.timesteps_horizon
+        else:
+            timesteps = self.environment.timer.timesteps_total
+        space_cooling_power = np.zeros(timesteps)
 
         # Get power curves of each apartment
         for apartment in self.apartments:
@@ -237,7 +244,11 @@ class Building(object):
         """
 
         #  Initialize array with zeros
-        el_power_curve = np.zeros(len(self.apartments[0].power_el.get_power(currentValues=currentValues)))
+        if currentValues:
+            timesteps = self.environment.timer.timesteps_horizon
+        else:
+            timesteps = self.environment.timer.timesteps_total
+        el_power_curve = np.zeros(timesteps)
 
         # Get power curves of each apartment
         for apartment in self.apartments:
@@ -264,9 +275,11 @@ class Building(object):
         """
 
         #  Initialize array with zeros
-        dhw_heat_power = \
-            np.zeros(len(self.apartments[0].demand_domestic_hot_water.
-                         get_power(currentValues=currentValues, returnTemperature=False)))
+        if currentValues:
+            timesteps = self.environment.timer.timesteps_horizon
+        else:
+            timesteps = self.environment.timer.timesteps_total
+        dhw_heat_power = np.zeros(timesteps)
 
         # Get power curves of each apartment
         for apartment in self.apartments:


Test patch:
diff --git a/pycity_base/test/test_building.py b/pycity_base/test/test_building.py
index 8031999..a79e3df 100644
--- a/pycity_base/test/test_building.py
+++ b/pycity_base/test/test_building.py
@@ -6,6 +6,8 @@ Building test.
 
 from __future__ import division
 
+import numpy as np
+
 import pycity_base.classes.demand.apartment as apart
 import pycity_base.classes.building as build
 import pycity_base.classes.supply.building_energy_system as bes
@@ -78,3 +80,19 @@ class TestBuilding():
         assert building.get_number_of_apartments() == 2
         assert building.get_number_of_occupants() == 2
         assert building.get_net_floor_area_of_building() == 50
+
+    def test_get_power_no_load(self, create_environment):
+        building = build.Building(environment=create_environment)
+
+        bes_unit = bes.BES(environment=create_environment)
+
+        building.addEntity(entity=bes_unit)
+
+        assert all(building.get_space_heating_power_curve(currentValues=False) == 0)
+        assert all(building.get_space_heating_power_curve(currentValues=True) == 0)
+        assert all(building.get_space_cooling_power_curve(currentValues=False) == 0)
+        assert all(building.get_space_cooling_power_curve(currentValues=True) == 0)
+        assert all(building.get_electric_power_curve(currentValues=False) == 0)
+        assert all(building.get_electric_power_curve(currentValues=True) == 0)
+        assert all(building.get_dhw_power_curve(currentValues=False) == 0)
+        assert all(building.get_dhw_power_curve(currentValues=True) == 0)