""" Monthly energy balance using Insel workflow SPDX - License - Identifier: LGPL - 3.0 - or -later Copyright © 2022 Concordia CERC group Project Coder Pilar Monsalvete Alvarez de Uribarri pilar.monsalvete@concordia.ca """ from pathlib import Path import pandas as pd import hub.helpers.constants as cte from helpers.monthly_values import MonthlyValues from hub.exports.energy_building_exports_factory import EnergyBuildingsExportsFactory from insel.monthly_demand_calculation import MonthlyDemandCalculation _DAYS_A_MONTH = {cte.MONDAY: [5, 4, 4, 5, 4, 4, 5, 4, 4, 5, 4, 5], cte.TUESDAY: [5, 4, 4, 4, 5, 4, 5, 4, 4, 5, 4, 4], cte.WEDNESDAY: [5, 4, 4, 4, 5, 4, 4, 5, 4, 5, 4, 4], cte.THURSDAY: [4, 4, 5, 4, 5, 4, 4, 5, 4, 4, 5, 4], cte.FRIDAY: [4, 4, 5, 4, 4, 5, 4, 5, 4, 4, 5, 4], cte.SATURDAY: [4, 4, 5, 4, 4, 5, 4, 4, 5, 4, 4, 5], cte.SUNDAY: [4, 4, 4, 5, 4, 4, 5, 4, 5, 4, 4, 5], cte.HOLIDAY: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]} class MonthlyEnergyBalance: def __init__(self, city, path, attic_heated_case, basement_heated_case, weather_format): self._city = city self._path = path self._weather_format = weather_format for building in self._city.buildings: building.attic_heated = attic_heated_case building.basement_heated = basement_heated_case self._sanity_check() self._workflow() def _sanity_check(self): levels_of_detail = self._city.level_of_detail if levels_of_detail.geometry is None: raise Exception(f'Level of detail of geometry not assigned') if levels_of_detail.geometry < 1: raise Exception(f'Level of detail of geometry = {levels_of_detail.geometry}. Required minimum level 1') if levels_of_detail.construction is None: raise Exception(f'Level of detail of construction not assigned') if levels_of_detail.construction < 1: raise Exception(f'Level of detail of construction = {levels_of_detail.construction}. Required minimum level 1') if levels_of_detail.usage is None: raise Exception(f'Level of detail of usage not assigned') if levels_of_detail.usage < 1: raise Exception(f'Level of detail of usage = {levels_of_detail.usage}. Required minimum level 1') for building in self._city.buildings: if cte.HOUR not in building.external_temperature: raise Exception(f'Building {building.name} does not have external temperature assigned') for surface in building.surfaces: if surface.type != cte.GROUND: if cte.HOUR not in surface.global_irradiance: raise Exception(f'Building {building.name} does not have global irradiance on surfaces assigned') def _workflow(self): for building in self._city.buildings: if cte.MONTH not in building.external_temperature: building.external_temperature[cte.MONTH] = MonthlyValues(). \ get_mean_values(building.external_temperature[cte.HOUR][[self._weather_format]]) for surface in building.surfaces: if surface.type != cte.GROUND: if cte.MONTH not in surface.global_irradiance: surface.global_irradiance[cte.MONTH] = MonthlyValues().get_mean_values(surface.global_irradiance[cte.HOUR]) tmp_path = (Path(__file__).parent / 'tmp').resolve() EnergyBuildingsExportsFactory('insel_monthly_energy_balance', self._city, tmp_path).export() insel = MonthlyDemandCalculation(self._city, tmp_path, self._weather_format) insel.run() insel.results() self._dhw_demand() self._electrical_demand() self._print_results() def _print_results(self): print_results = None file = 'city name: ' + self._city.name + '\n' for building in self._city.buildings: heating_results = building.heating[cte.MONTH].rename(columns={'INSEL': f'{building.name} heating Wh'}) cooling_results = building.cooling[cte.MONTH].rename(columns={'INSEL': f'{building.name} cooling Wh'}) if print_results is None: print_results = heating_results else: print_results = pd.concat([print_results, heating_results], axis='columns') print_results = pd.concat([print_results, cooling_results, building.lighting_electrical_demand, building.appliances_electrical_demand, building.domestic_hot_water_heat_demand], axis='columns') file += '\n' file += 'name: ' + building.name + '\n' file += 'year of construction: ' + str(building.year_of_construction) + '\n' file += 'function: ' + building.function + '\n' file += 'floor area: ' + str(building.floor_area) + '\n' file += 'storeys: ' + str(building.storeys_above_ground) + '\n' file += 'heated_volume: ' + str(0.85 * building.volume) + '\n' file += 'volume: ' + str(building.volume) + '\n' full_path_results = Path(self._path / 'demand.csv').resolve() print_results.to_csv(full_path_results) full_path_metadata = Path(self._path / 'metadata.csv').resolve() with open(full_path_metadata, 'w') as metadata_file: metadata_file.write(file) def _dhw_demand(self): for building in self._city.buildings: domestic_hot_water_demand = [] if building.internal_zones[0].thermal_zones is None: domestic_hot_water_demand = [0] * 12 else: thermal_zone = building.internal_zones[0].thermal_zones[0] area = thermal_zone.total_floor_area cold_water = building.cold_water_temperature[cte.MONTH]['epw'] for month in range(0, 12): total_dhw_demand = 0 for schedule in thermal_zone.domestic_hot_water.schedules: total_day = 0 for value in schedule.values: total_day += value for day_type in schedule.day_types: print('day_type', month, day_type) print(_DAYS_A_MONTH[day_type][month]) demand = thermal_zone.domestic_hot_water.peak_flow * cte.WATER_DENSITY * cte.WATER_HEAT_CAPACITY \ * (thermal_zone.domestic_hot_water.service_temperature - cold_water[month]) total_dhw_demand += total_day * _DAYS_A_MONTH[day_type][month] * demand print('demand', total_day, demand, cold_water[month]) print(area) domestic_hot_water_demand.append(total_dhw_demand * area) building.domestic_hot_water_heat_demand = \ pd.DataFrame(domestic_hot_water_demand, columns=[f'{building.name} domestic hot water demand Wh']) def _electrical_demand(self): for building in self._city.buildings: lighting_demand = [] appliances_demand = [] if building.internal_zones[0].thermal_zones is None: lighting_demand = [0] * 12 appliances_demand = [0] * 12 else: thermal_zone = building.internal_zones[0].thermal_zones[0] area = thermal_zone.total_floor_area for month in range(0, 12): total_lighting = 0 for schedule in thermal_zone.lighting.schedules: total_day = 0 for value in schedule.values: total_day += value for day_type in schedule.day_types: total_lighting += total_day * _DAYS_A_MONTH[day_type][month] * thermal_zone.lighting.density lighting_demand.append(total_lighting * area) total_appliances = 0 for schedule in thermal_zone.appliances.schedules: total_day = 0 for value in schedule.values: total_day += value for day_type in schedule.day_types: total_appliances += total_day * _DAYS_A_MONTH[day_type][month] * thermal_zone.appliances.density appliances_demand.append(total_appliances * area) building.lighting_electrical_demand = pd.DataFrame(lighting_demand, columns=[f'{building.name} lighting electrical demand Wh']) building.appliances_electrical_demand = pd.DataFrame(appliances_demand, columns=[f'{building.name} appliances electrical demand Wh'])