monthly_energy_balance/monthly_energy_balance.py

94 lines
4.3 KiB
Python
Raw Normal View History

"""
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 helpers.constants as cte
from helpers.monthly_values import MonthlyValues
from exports.energy_building_exports_factory import EnergyBuildingsExportsFactory
from insel.monthly_demand_calculation import MonthlyDemandCalculation
class MonthlyEnergyBalance:
def __init__(self, city, path, attic_heated_case, basement_heated_case, weather_format):
self._city = city
print(path)
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._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], 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.internal_zones[0].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)