dynamic_building_simulation/main.py
Pilar fc9d7ef307 erased populate and created enrich in libs.helpers.
add new constants to helpers.constants
2021-06-10 10:48:30 -04:00

116 lines
4.7 KiB
Python

import sys
from insel.insel import Insel
from pathlib import Path
from helpers.enrich_city import EnrichCity
from simplified_radiosity_algorithm import SimplifiedRadiosityAlgorithm
from imports.weather_factory import WeatherFactory
from insel.templates.thermal_demand_dynamic_simulation import ThermalDemandDynamicSimulation as Templates
from helpers.simulation_parameters import SimulationParameters as Sp
import helpers.constants as cte
from imports.geometry_factory import GeometryFactory
from imports.geometry.helpers.geometry_helper import GeometryHelper
from city_model_structure.city import City
from city_model_structure.building import Building
from city_model_structure.attributes.polygon import Polygon
from city_model_structure.attributes.surface import Surface
name_gml = 'one_building_in_kelowna.gml'
function_format = 'hft'
construction_format = 'nrcan'
usage_format = 'hft'
schedules_format = 'comnet'
climate_reference_city = 'Summerland'
weather_file_name = 'CAN_BC_Summerland.717680_CWEC.epw'
example_path = Path(__file__).parent
full_path_gml = (example_path / 'tests' / 'tests_data' / name_gml).resolve()
outputs_path = (example_path / 'tests' / 'tests_outputs').resolve()
tmp_path = (example_path / 'tests' / 'tmp').resolve()
weather_path = (Path(__file__).parent.parent / 'libs' / 'data' / 'weather').resolve()
keep_files = False
pickle_created = False
pickle_file = 'tests/tests_data/one_building_in_kelowna.pickle'
# todo: simplification of the data model
if not pickle_created:
city = GeometryFactory('citygml', full_path_gml).city
# Building division by storey
for building in city.buildings:
building.average_storey_height = 1.5
building.storeys_above_ground = 2
storeys = building.storeys
if len(storeys) != len(building.thermal_zones):
# todo:assign one thermal zone per storey
for storey in storeys:
surfaces = []
for face in storey.faces:
# todo: review for obj with windows
points = []
for vertex_index in face:
points.append(storey.vertices[vertex_index])
solid_polygon = Polygon(points)
perimeter_polygon = solid_polygon
surface = Surface(solid_polygon, perimeter_polygon)
surfaces.append(surface)
weather_format = 'epw'
city.climate_reference_city = climate_reference_city
city.climate_file = (tmp_path / f'{climate_reference_city}.cli').resolve()
WeatherFactory(weather_format, city, base_path=weather_path, file_name=weather_file_name).enrich()
for building in city.buildings:
if cte.HOUR not in building.external_temperature:
print('No external temperature found')
sys.exit()
max_buildings_handled_by_sra = 500
for building in city.buildings:
for surface in building.surfaces:
surface.swr = 0.2
path = (example_path / 'tests').resolve()
sra = SimplifiedRadiosityAlgorithm(city, path, weather_file_name)
total_number_of_buildings = len(city.buildings)
if total_number_of_buildings > max_buildings_handled_by_sra:
radius = 80
for building in city.buildings:
new_city = city.region(building.centroid, radius)
sra_new = SimplifiedRadiosityAlgorithm(new_city, path, weather_file_name)
sra_new.call_sra(weather_format, keep_files=keep_files)
sra_new.set_irradiance_surfaces(city, building_name=building.name)
else:
sra.call_sra(weather_format, keep_files=keep_files)
sra.set_irradiance_surfaces(city, mode=1)
# print(city.buildings[0].surfaces[1].global_irradiance['hour'].sra)
for building in city.buildings:
if function_format == 'hft':
building.function = GeometryHelper.hft_to_function[building.function]
elif function_format == 'pluto':
building.function = GeometryHelper.pluto_to_function[building.function]
city = EnrichCity(city).enriched_city(construction_format, usage_format, schedules_format)
city.save(pickle_file)
else:
city = City.load(pickle_file)
# todo: when the enrichment step goes after SRA, check whether srw has changed
# after reading the construction library or not
quit()
# Demand calculation (one model per building)
for city_object in city.city_objects:
full_path_out = (outputs_path / city_object.name).resolve()
full_path_wea = (tmp_path / (city_object.name + '.weather')).resolve()
full_path_ig = (tmp_path / (city_object.name + '.ig')).resolve()
insel_file_name = city_object.name + '.insel'
try:
content = Templates.generate_thermal_dynamic_template(city_object, full_path_out, full_path_wea, full_path_ig, Sp)
insel = Insel(example_path, insel_file_name, content, mode=2, keep_files=keep_files).results
except:
print(sys.exc_info()[1])
print('Building ' + city_object.name + ' could not be processed')
continue