Changes to extract the monthly calculation as a class for the game

This commit is contained in:
Pilar 2021-04-28 15:41:42 -04:00
parent c254c42608
commit 81b46fb5c4
2 changed files with 91 additions and 50 deletions

92
main.py
View File

@ -9,16 +9,15 @@ from pathlib import Path
from argparse import ArgumentParser
import ast
import pandas as pd
import datetime
from helpers import monthly_values as mv
from populate import Populate
from insel.insel import Insel
from insel.templates.monthly_energy_balance import MonthlyEnergyBalance as templates
from simplified_radiosity_algorithm import SimplifiedRadiosityAlgorithm
from imports.geometry_factory import GeometryFactory
from imports.weather_factory import WeatherFactory
from city_model_structure.city import City
import datetime
from monthly_demand_calculation import MonthlyDemandCalculation
parser = ArgumentParser(description='Monthly energy balance workflow v0.1.')
required = parser.add_argument_group('required arguments')
@ -52,6 +51,12 @@ else:
file = Path(args.input_geometry_file).resolve()
pickle_file = Path(str(file).replace('.gml', '.pickle'))
city = GeometryFactory(args.geometry_type, file).city
print(len(city.buildings))
for building in city.buildings:
volume = building.volume
if str(volume) == 'inf':
sys.stderr.write(f'Building {building.name} has geometry errors. It has been removed from the city\n')
city.remove_city_object(building)
city.save(pickle_file)
print('begin_populating_time', datetime.datetime.now())
@ -95,64 +100,51 @@ if not weather_step_in_pickle:
else:
total_number_of_buildings = len(city.buildings)
if total_number_of_buildings > max_buildings_handled_by_sra:
radius = 100
radius = 80
for building in city.buildings:
new_city = city.region(building.location, radius)
sra_new = SimplifiedRadiosityAlgorithm(city, Path(args.project_folder).resolve(), args.weather_file_name)
new_city = city.region(building.centroid, radius)
sra_new = SimplifiedRadiosityAlgorithm(new_city, Path(args.project_folder).resolve(), args.weather_file_name)
sra_new.call_sra(keep_files=True)
sra_new.set_irradiance_surfaces(city, building_name=building.name)
sra_new.set_irradiance_surfaces(populated_city, building_name=building.name)
else:
sra.call_sra(keep_files=keep_files)
sra.set_irradiance_surfaces(populated_city)
pickle_file = Path(str(pickle_file).replace('.pickle', '_weather.pickle'))
populated_city.save(pickle_file)
print('begin_insel_time', datetime.datetime.now())
# Step 4: Demand calculation (one model per building)
print_results = None
file = 'city name: ' + city.name + '\n'
weather_format = 'epw'
print('begin_user_assignment_time', datetime.datetime.now())
# Step 4: Assign user defined parameters
for building in populated_city.buildings:
if building.name != 'BLD122177':
# todo: default values to be defined at each specific workflow!
building.heated = True
building.cooled = False
building.attic_heated = 1
building.basement_heated = 1
building.usage_zones[0].internal_gains[0].average_internal_gain = 10.45
full_path_out = Path(args.project_folder + '/outputs/' + building.name + '_insel.out').resolve()
full_path_out.parent.mkdir(parents=True, exist_ok=True)
insel_file_name = building.name + '.insel'
try:
key = 'epw'
content = templates.generate_meb_template(building, full_path_out, key)
insel = Insel(Path(args.project_folder).resolve(), insel_file_name, content, mode=2, keep_files=keep_files).run()
building.heating['month'], building.cooling['month'] = templates.demand(full_path_out)
new_building = building.heating['month'].rename(columns={'INSEL': building.name})
demand_year = 0
for value in building.heating['month']['INSEL']:
if value == 'NaN':
value = '0'
demand_year += float(value)
yearly_heating = pd.DataFrame([demand_year], columns=['INSEL'])
building.heating['year'] = yearly_heating
if print_results is None:
print_results = new_building
else:
print_results = pd.concat([print_results, new_building], axis='columns')
file += '\n'
file += 'name: ' + building.name + '\n'
file += 'year of construction: ' + building.year_of_construction + '\n'
file += 'function: ' + building.function + '\n'
file += 'floor area: ' + str(building.thermal_zones[0].floor_area) + '\n'
file += 'storeys: ' + str(building.storeys_above_ground) + '\n'
file += 'heated_volume: ' + str(building.volume) + '\n'
file += 'volume: ' + str(building.volume) + '\n'
building.heated = True
building.cooled = False
building.attic_heated = 2
building.basement_heated = 0
# for value in building.heating['month']['INSEL']:
except ValueError:
print(sys.exc_info()[1])
print('Building ' + building.name + ' could not be processed')
continue
print('begin_insel_time', datetime.datetime.now())
MonthlyDemandCalculation(city, args.project_folder, weather_format).monthly_demand()
print('begin_write_results_time', datetime.datetime.now())
print_results = None
file = 'city name: ' + city.name + '\n'
for building in populated_city.buildings:
insel_file_name = building.name + '.insel'
building_results = building.heating['month'].rename(columns={'INSEL': building.name})
if print_results is None:
print_results = building_results
else:
print_results = pd.concat([print_results, building_results], axis='columns')
file += '\n'
file += 'name: ' + building.name + '\n'
file += 'year of construction: ' + building.year_of_construction + '\n'
file += 'function: ' + building.function + '\n'
file += 'floor area: ' + str(building.thermal_zones[0].floor_area) + '\n'
file += 'storeys: ' + str(building.storeys_above_ground) + '\n'
file += 'heated_volume: ' + str(building.volume) + '\n'
file += 'volume: ' + str(building.volume) + '\n'
full_path_results = Path(args.project_folder + '/outputs/heating_demand.csv').resolve()
print_results.to_csv(full_path_results)

View File

@ -0,0 +1,49 @@
"""
Monthly demand calculation using the monthly energy balance methodology based on the norm...
SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2020 Project Author Pilar Monsalvete Alvarez de Uribarri pilar.monsalvete@concordia.ca
"""
from pathlib import Path
import pandas as pd
import sys
from insel.templates.monthly_energy_balance import MonthlyEnergyBalance as templates
from insel.insel import Insel
class MonthlyDemandCalculation:
def __init__(self, city, main_path, weather_format):
self._city = city
self._main_path = main_path
self._weather_format = weather_format
def monthly_demand(self):
for building in self._city.buildings:
full_path_out = Path(self._main_path + '/outputs/' + building.name + '_insel.out').resolve()
full_path_out.parent.mkdir(parents=True, exist_ok=True)
insel_file_name = building.name + '.insel'
try:
content = templates.generate_meb_template(building, full_path_out, self._weather_format)
insel = Insel(Path(self._main_path).resolve(), insel_file_name, content, mode=2, keep_files=True).run()
building.heating['month'], building.cooling['month'] = templates.demand(full_path_out)
heating_year = 0
for value in building.heating['month']['INSEL']:
if value == 'NaN':
value = '0'
heating_year += float(value)
yearly_heating = pd.DataFrame([heating_year], columns=['INSEL'])
building.heating['year'] = yearly_heating
cooling_year = 0
for value in building.cooling['month']['INSEL']:
if value == 'NaN':
value = '0'
cooling_year += float(value)
yearly_cooling = pd.DataFrame([cooling_year], columns=['INSEL'])
building.cooling['year'] = yearly_cooling
except ValueError:
print(sys.exc_info()[1])
print('Building ' + building.name + ' could not be processed')
continue