Merge remote-tracking branch 'origin/shared_surfaces_method' into shared_surfaces_method

# Conflicts:
#	hub/helpers/geometry_helper.py
#	hub/unittests/test_geometry_factory.py
This commit is contained in:
Guille Gutierrez 2023-03-10 14:05:53 -05:00
commit 591107f8cf
30 changed files with 9715 additions and 699 deletions

View File

@ -40,7 +40,7 @@ class NrelCatalog(Catalog):
_catalog_windows = []
windows = self._constructions['library']['windows']['window']
for window in windows:
frame_ratio = window['frame_ratio']['#text']
frame_ratio = float(window['frame_ratio']['#text'])
g_value = window['shgc']
overall_u_value = float(window['conductivity']['#text']) / float(window['thickness']['#text'])
name = window['@name']
@ -54,9 +54,9 @@ class NrelCatalog(Catalog):
for material in materials:
material_id = material['@id']
name = material['@name']
solar_absorptance = material['solar_absorptance']['#text']
thermal_absorptance = material['thermal_absorptance']['#text']
visible_absorptance = material['visible_absorptance']['#text']
solar_absorptance = float(material['solar_absorptance']['#text'])
thermal_absorptance = float(material['thermal_absorptance']['#text'])
visible_absorptance = float(material['visible_absorptance']['#text'])
no_mass = False
thermal_resistance = None,
conductivity = None,
@ -64,11 +64,11 @@ class NrelCatalog(Catalog):
specific_heat = None
if 'no_mass' in material and material['no_mass'] == 'true':
no_mass = True
thermal_resistance = material['thermal_resistance']['#text']
thermal_resistance = float(material['thermal_resistance']['#text'])
else:
conductivity = material['conductivity']['#text']
density = material['density']['#text']
specific_heat = material['specific_heat']['#text']
conductivity = float(material['conductivity']['#text'])
density = float(material['density']['#text'])
specific_heat = float(material['specific_heat']['#text'])
_material = Material(material_id,
name,
solar_absorptance,
@ -96,7 +96,7 @@ class NrelCatalog(Catalog):
material_id = layer['material'][0]
thickness = 0
if 'thickness' in layer:
thickness = layer['thickness']['#text']
thickness = float(layer['thickness']['#text'])
for material in self._catalog_materials:
if str(material_id) == str(material.id):
layers.append(Layer(layer_id, layer_name, material, thickness))
@ -114,18 +114,20 @@ class NrelCatalog(Catalog):
climate_zone = archetype['@climate_zone']
construction_period = \
ConstructionHelper().reference_standard_to_construction_period[archetype['@reference_standard']]
average_storey_height = archetype['average_storey_height']['#text']
thermal_capacity = str(float(archetype['thermal_capacity']['#text']) * 1000)
extra_loses_due_to_thermal_bridges = archetype['extra_loses_due_to_thermal_bridges']['#text']
indirect_heated_ratio = archetype['indirect_heated_ratio']['#text']
infiltration_rate_for_ventilation_system_off = archetype['infiltration_rate_for_ventilation_system_off']['#text']
infiltration_rate_for_ventilation_system_on = archetype['infiltration_rate_for_ventilation_system_on']['#text']
average_storey_height = float(archetype['average_storey_height']['#text'])
thermal_capacity = float(archetype['thermal_capacity']['#text']) * 1000
extra_loses_due_to_thermal_bridges = float(archetype['extra_loses_due_to_thermal_bridges']['#text'])
indirect_heated_ratio = float(archetype['indirect_heated_ratio']['#text'])
infiltration_rate_for_ventilation_system_off = \
float(archetype['infiltration_rate_for_ventilation_system_off']['#text'])
infiltration_rate_for_ventilation_system_on = \
float(archetype['infiltration_rate_for_ventilation_system_on']['#text'])
archetype_constructions = []
for archetype_construction in archetype['constructions']['construction']:
for construction in self._catalog_constructions:
if construction.id == archetype_construction['@id']:
window_ratio = archetype_construction['window_ratio']['#text']
window_ratio = float(archetype_construction['window_ratio']['#text'])
window_id = archetype_construction['window']
_construction = None
_window = None

View File

@ -32,7 +32,6 @@ class Surface:
self._area = None
self._lower_corner = None
self._upper_corner = None
self._shared_surfaces = []
self._global_irradiance = dict()
self._perimeter_polygon = perimeter_polygon
self._holes_polygons = holes_polygons
@ -42,6 +41,7 @@ class Surface:
self._inverse = None
self._associated_thermal_boundaries = []
self._vegetation = None
self._percentage_shared = None
@property
def name(self):
@ -283,12 +283,6 @@ class Surface:
self._inverse = Surface(new_solid_polygon, new_perimeter_polygon, new_holes_polygons, cte.VIRTUAL_INTERNAL)
return self._inverse
def shared_surfaces(self):
"""
Raises not implemented error
"""
raise NotImplementedError
def divide(self, z):
"""
Divides a surface at Z plane
@ -336,3 +330,19 @@ class Surface:
:param value: None or [ThermalBoundary]
"""
self._associated_thermal_boundaries = value
@property
def percentage_shared(self):
"""
Get percentage of the wall shared with other walls
:return: float
"""
return self._percentage_shared
@percentage_shared.setter
def percentage_shared(self, value):
"""
Set percentage of the wall shared with other walls
:param value: float
"""
self._percentage_shared = value

View File

@ -510,10 +510,14 @@ class ThermalZone:
_schedule.values = values[:day]
_schedules.append(_schedule)
_internal_gain.convective_fraction = _convective_fraction / _average_internal_gain
_internal_gain.radiative_fraction = _radiative_fraction / _average_internal_gain
_internal_gain.latent_fraction = _latent_fraction / _average_internal_gain
_internal_gain.average_internal_gain = _average_internal_gain
_internal_gain.convective_fraction = 0
_internal_gain.radiative_fraction = 0
_internal_gain.latent_fraction = 0
if _average_internal_gain != 0:
_internal_gain.convective_fraction = _convective_fraction / _average_internal_gain
_internal_gain.radiative_fraction = _radiative_fraction / _average_internal_gain
_internal_gain.latent_fraction = _latent_fraction / _average_internal_gain
_internal_gain.type = 'mean_value'
_internal_gain.schedules = _schedules
self._internal_gains = [_internal_gain]

View File

@ -91,9 +91,13 @@ class Usage:
+ self.occupancy.sensible_radiative_internal_gain
+ self.occupancy.latent_internal_gain)
_internal_gain.average_internal_gain = _total_heat_gain
_internal_gain.latent_fraction = self.occupancy.latent_internal_gain / _total_heat_gain
_internal_gain.radiative_fraction = self.occupancy.sensible_radiative_internal_gain / _total_heat_gain
_internal_gain.convective_fraction = self.occupancy.sensible_convective_internal_gain / _total_heat_gain
_internal_gain.latent_fraction = 0
_internal_gain.radiative_fraction = 0
_internal_gain.convective_fraction = 0
if _total_heat_gain != 0:
_internal_gain.latent_fraction = self.occupancy.latent_internal_gain / _total_heat_gain
_internal_gain.radiative_fraction = self.occupancy.sensible_radiative_internal_gain / _total_heat_gain
_internal_gain.convective_fraction = self.occupancy.sensible_convective_internal_gain / _total_heat_gain
_internal_gain.schedules = self.occupancy.occupancy_schedules
self._internal_gains = [_internal_gain]
if self.lighting is not None:

View File

@ -91,9 +91,13 @@ class UsageZone:
+ self.occupancy.sensible_radiative_internal_gain
+ self.occupancy.latent_internal_gain)
_internal_gain.average_internal_gain = _total_heat_gain
_internal_gain.latent_fraction = self.occupancy.latent_internal_gain / _total_heat_gain
_internal_gain.radiative_fraction = self.occupancy.sensible_radiative_internal_gain / _total_heat_gain
_internal_gain.convective_fraction = self.occupancy.sensible_convective_internal_gain / _total_heat_gain
_internal_gain.latent_fraction = 0
_internal_gain.radiative_fraction = 0
_internal_gain.convective_fraction = 0
if _total_heat_gain != 0:
_internal_gain.latent_fraction = self.occupancy.latent_internal_gain / _total_heat_gain
_internal_gain.radiative_fraction = self.occupancy.sensible_radiative_internal_gain / _total_heat_gain
_internal_gain.convective_fraction = self.occupancy.sensible_convective_internal_gain / _total_heat_gain
_internal_gain.schedules = self.occupancy.occupancy_schedules
self._internal_gains = [_internal_gain]
if self.lighting is not None:

View File

@ -29,6 +29,7 @@ from hub.helpers.geometry_helper import GeometryHelper
from hub.helpers.location import Location
from hub.city_model_structure.energy_system import EnergySystem
from hub.city_model_structure.lca_material import LcaMaterial
import pandas as pd
class City:
@ -447,8 +448,31 @@ class City:
def merge(self, city) -> City:
_merge_city = self.copy
selected_city_object = None
building = None
# set initial minimum radiation to a higher number
min_radiation = 1999999
for city_object in city.city_objects:
_merge_city.add_city_object(city_object)
if city_object.type == 'building':
building = city_object
building_radiation = 0
for surface in city_object.surfaces:
radiation = surface.global_irradiance
if 'year' not in radiation and 'month' not in radiation:
continue
elif "year" in radiation:
building_radiation += radiation["year"].iloc[0]
elif "month" in radiation and "year" not in radiation:
surface.global_irradiance["year"] = pd.DataFrame({radiation["month"].sum()})
building_radiation += radiation["year"].iloc[0]
if building_radiation < min_radiation:
min_radiation = building_radiation
selected_city_object = city_object
# merge the city object with the minimum radiation
if selected_city_object is not None:
_merge_city.add_city_object(selected_city_object)
else:
_merge_city.add_city_object(building)
return _merge_city
@property

View File

@ -130,10 +130,7 @@ class Idf:
self._idf.newidfobject(self._MATERIAL_NOMASS,
Name=layer.material.name,
Roughness=self._ROUGHNESS,
Thermal_Resistance=layer.material.thermal_resistance,
Thermal_Absorptance=layer.material.thermal_absorptance,
Solar_Absorptance=layer.material.solar_absorptance,
Visible_Absorptance=layer.material.visible_absorptance
Thermal_Resistance=layer.material.thermal_resistance
)
else:
self._idf.newidfobject(self._MATERIAL,
@ -323,9 +320,12 @@ class Idf:
def _add_occupancy(self, thermal_zone, zone_name):
number_of_people = thermal_zone.occupancy.occupancy_density * thermal_zone.total_floor_area
fraction_radiant = thermal_zone.occupancy.sensible_radiative_internal_gain / \
(thermal_zone.occupancy.sensible_radiative_internal_gain +
thermal_zone.occupancy.sensible_convective_internal_gain)
fraction_radiant = 0
total_sensible = thermal_zone.occupancy.sensible_radiative_internal_gain + \
thermal_zone.occupancy.sensible_convective_internal_gain
if total_sensible != 0:
fraction_radiant = thermal_zone.occupancy.sensible_radiative_internal_gain / total_sensible
self._idf.newidfobject(self._PEOPLE,
Name=f'{zone_name}_occupancy',
Zone_or_ZoneList_Name=zone_name,
@ -377,7 +377,6 @@ class Idf:
self._rename_building(self._city.name)
self._lod = self._city.level_of_detail.geometry
for building in self._city.buildings:
for internal_zone in building.internal_zones:
for thermal_zone in internal_zone.thermal_zones:
for thermal_boundary in thermal_zone.thermal_boundaries:

View File

@ -13,14 +13,14 @@ from hub.imports.weather.helpers.weather import Weather
import hub.helpers.constants as cte
_CONSTRUCTION_CODE = {
cte.WALL: '1',
cte.GROUND: '2',
cte.ROOF: '3',
cte.INTERIOR_WALL: '5',
cte.GROUND_WALL: '6',
cte.ATTIC_FLOOR: '7',
cte.INTERIOR_SLAB: '8'
}
cte.WALL: '1',
cte.GROUND: '2',
cte.ROOF: '3',
cte.INTERIOR_WALL: '5',
cte.GROUND_WALL: '6',
cte.ATTIC_FLOOR: '7',
cte.INTERIOR_SLAB: '8'
}
class InselMonthlyEnergyBalance(Insel):
@ -35,8 +35,13 @@ class InselMonthlyEnergyBalance(Insel):
self._insel_files_paths.append(building.name + '.insel')
file_name_out = building.name + '.out'
output_path = Path(self._path / file_name_out).resolve()
self._contents.append(self.generate_meb_template(building, output_path, self._radiation_calculation_method,
self._weather_format))
if building.internal_zones is not None:
for internal_zone in building.internal_zones:
if internal_zone.thermal_zones is None:
break
self._contents.append(
self.generate_meb_template(building, output_path, self._radiation_calculation_method,self._weather_format)
)
self._export()
def _export(self):
@ -86,7 +91,7 @@ class InselMonthlyEnergyBalance(Insel):
total_internal_gain = 0
for ig in usage.internal_gains:
total_internal_gain += float(ig.average_internal_gain) * \
(float(ig.convective_fraction) + float(ig.radiative_fraction))
(float(ig.convective_fraction) + float(ig.radiative_fraction))
parameters.append(f'{total_internal_gain} % BP(12) #2 Internal gains of zone {i + 1}')
parameters.append(f'{usage.thermal_control.mean_heating_set_point} % BP(13) #3 Heating setpoint temperature '
f'zone {i + 1} (degree Celsius)')
@ -112,7 +117,9 @@ class InselMonthlyEnergyBalance(Insel):
for thermal_boundary in thermal_zone.thermal_boundaries:
type_code = _CONSTRUCTION_CODE[thermal_boundary.type]
window_area = thermal_boundary.opaque_area * thermal_boundary.window_ratio / (1 - thermal_boundary.window_ratio)
window_area = 0
if thermal_boundary.window_ratio < 1:
window_area = thermal_boundary.opaque_area * thermal_boundary.window_ratio / (1 - thermal_boundary.window_ratio)
parameters.append(type_code)
if thermal_boundary.type != cte.GROUND:

View File

@ -8,6 +8,7 @@ from hub.persistence import City
from hub.persistence import Application
from hub.persistence import User
from hub.persistence import CityObject
from hub.persistence import SimulationResults
class DBFactory:
@ -20,6 +21,7 @@ class DBFactory:
self._application = Application(db_name=db_name, app_env=app_env, dotenv_path=dotenv_path)
self._user = User(db_name=db_name, app_env=app_env, dotenv_path=dotenv_path)
self._city_object = CityObject(db_name=db_name, app_env=app_env, dotenv_path=dotenv_path)
self._simulation_results = SimulationResults(db_name=db_name, dotenv_path=dotenv_path, app_env=app_env)
def get_city(self, city_id):
"""
@ -42,6 +44,34 @@ class DBFactory:
"""
return self._city.get_by_user(user_id)
def get_simulation_results_by_id(self, sim_id):
"""
Retrieve a single simulation results
:param sim_id: the id of the simulation results to get
"""
return self._simulation_results.get_simulation_results_by_id(sim_id)
def get_simulation_results_by_name(self, name):
"""
Retrieve a single simulation results
:param name: the name of the simulation results to get
"""
return self._simulation_results.get_simulation_results_by_name(name)
def get_simulation_results_by_city_id(self, city_id):
"""
Retrieve a single simulation results
:param city_id: the city id of the simulation results to get
"""
return self._simulation_results.get_simulation_results_by_city_id(city_id)
def get_simulation_results_by_city_object_id(self, city_object_id):
"""
Retrieve a single simulation results
:param city_object_id: the city object id of the simulation results to get
"""
return self._simulation_results.get_simulation_results_by_city_object_id(city_object_id)
def application_info(self, application_uuid):
return self._application.get_by_uuid(application_uuid)

View File

@ -6,6 +6,7 @@ Project CoderPeter Yefi peteryefi@gmail.com
"""
from hub.persistence import User
class UserFactory:
"""
UserFactory class
@ -14,17 +15,19 @@ class UserFactory:
def __init__(self, db_name, app_env, dotenv_path):
self._user_repo = User(db_name=db_name, app_env=app_env, dotenv_path=dotenv_path)
def login_user(self, email: str, password: str):
def login_user(self, name: str, password: str, application_id: int):
"""
Retrieve a single city from postgres
:param email: the email of the user
:param name: the email of the user
:param password: the password of the user
:param application_id: the id of the application accessing hub
"""
return self._user_repo.get_user_by_email_and_password(email, password)
return self._user_repo.get_by_name_application_and_password(name, password, application_id)
def get_user_by_email(self, email):
def get_by_name_and_application(self, name: str, application: int):
"""
Retrieve a single user
:param email: the email of the user to get
:param name: user name
:param application: application accessing hub
"""
return self._user_repo.get_by_email(email)
return self._user_repo.get_by_name_and_application(name, application)

File diff suppressed because it is too large Load Diff

View File

@ -5,7 +5,6 @@ Copyright © 2022 Concordia CERC group
Project Coder Guille Gutierrez guillermo.gutierrezmorote@concordia.ca
Code contributors: Pilar Monsalvete Alvarez de Uribarri pilar.monsalvete@concordia.ca
"""
import datetime
import math
import numpy as np
import requests
@ -64,7 +63,7 @@ class GeometryHelper:
return MapPoint(((city.upper_corner[0] - coordinate[0]) * 0.5), ((city.upper_corner[1] - coordinate[1]) * 0.5))
@staticmethod
def city_mapping(city, building_names=None):
def city_mapping(city, building_names=None, plot=False):
"""
Returns a shared_information dictionary like
@ -81,6 +80,7 @@ class GeometryHelper:
map_info = [[{} for _ in range(y + 1)] for _ in range(x + 1)]
for building_name in building_names:
building = city.city_object(building_name)
shared_information[building_name]: []
line = 0
for ground in building.grounds:
length = len(ground.perimeter_polygon.coordinates) - 1

View File

@ -6,7 +6,8 @@ log_dir = (Path(__file__).parent.parent / 'logs').resolve()
log_file = (log_dir / 'hub.log').resolve()
try:
if not os.path.isfile(log_file):
os.mkdir(log_dir)
if not os.path.exists:
os.mkdir(log_dir)
with open(log_file, 'x'):
pass
logger.basicConfig(filename=log_file, format="%(asctime)s:%(levelname)s:{%(pathname)s:%(funcName)s:%(lineno)d} "

View File

@ -38,7 +38,7 @@ class NrcanPhysicsParameters:
archetype = self._search_archetype(nrcan_catalog, function, building.year_of_construction, self._climate_zone)
except KeyError:
sys.stderr.write(f'Building {building.name} has unknown construction archetype for building function: '
f'{building.function} and building year of construction: {building.year_of_construction} '
f'{building.function}, building year of construction: {building.year_of_construction} '
f'and climate zone {self._climate_zone}\n')
return
# if building has no thermal zones defined from geometry, and the building will be divided in storeys,
@ -51,7 +51,7 @@ class NrcanPhysicsParameters:
for thermal_zone in internal_zone.thermal_zones:
thermal_zone.total_floor_area = thermal_zone.footprint_area
else:
number_of_storeys = int(float(building.eave_height) / float(building.average_storey_height))
number_of_storeys = int(building.eave_height / building.average_storey_height)
thermal_zone = building.internal_zones[0].thermal_zones[0]
thermal_zone.total_floor_area = thermal_zone.footprint_area * number_of_storeys
else:
@ -69,7 +69,7 @@ class NrcanPhysicsParameters:
nrcan_archetypes = nrcan_catalog.entries('archetypes')
for building_archetype in nrcan_archetypes:
construction_period_limits = building_archetype.construction_period.split('_')
if int(construction_period_limits[0]) <= int(year_of_construction) < int(construction_period_limits[1]):
if int(construction_period_limits[0]) <= year_of_construction < int(construction_period_limits[1]):
if (str(function) == str(building_archetype.function)) and \
(climate_zone == str(building_archetype.climate_zone)):
return building_archetype
@ -135,12 +135,12 @@ class NrcanPhysicsParameters:
# The agreement is that the layers are defined from outside to inside
external_layer = construction_archetype.layers[0]
external_surface = thermal_boundary.parent_surface
external_surface.short_wave_reflectance = 1 - float(external_layer.material.solar_absorptance)
external_surface.long_wave_emittance = 1 - float(external_layer.material.solar_absorptance)
external_surface.short_wave_reflectance = 1 - external_layer.material.solar_absorptance
external_surface.long_wave_emittance = 1 - external_layer.material.solar_absorptance
internal_layer = construction_archetype.layers[len(construction_archetype.layers) - 1]
internal_surface = thermal_boundary.internal_surface
internal_surface.short_wave_reflectance = 1 - float(internal_layer.material.solar_absorptance)
internal_surface.long_wave_emittance = 1 - float(internal_layer.material.solar_absorptance)
internal_surface.short_wave_reflectance = 1 - internal_layer.material.solar_absorptance
internal_surface.long_wave_emittance = 1 - internal_layer.material.solar_absorptance
for thermal_opening in thermal_boundary.thermal_openings:
if construction_archetype.window is not None:

View File

@ -58,7 +58,7 @@ class NrelPhysicsParameters:
for thermal_zone in internal_zone.thermal_zones:
thermal_zone.total_floor_area = thermal_zone.footprint_area
else:
number_of_storeys = int(float(building.eave_height) / float(building.average_storey_height))
number_of_storeys = int(building.eave_height / building.average_storey_height)
thermal_zone = building.internal_zones[0].thermal_zones[0]
thermal_zone.total_floor_area = thermal_zone.footprint_area * number_of_storeys
else:
@ -78,7 +78,7 @@ class NrelPhysicsParameters:
construction_period_limits = building_archetype.construction_period.split(' - ')
if construction_period_limits[1] == 'PRESENT':
construction_period_limits[1] = 3000
if int(construction_period_limits[0]) <= int(year_of_construction) < int(construction_period_limits[1]):
if int(construction_period_limits[0]) <= year_of_constructionF < int(construction_period_limits[1]):
if (str(function) == str(building_archetype.function)) and \
(climate_zone == str(building_archetype.climate_zone)):
return building_archetype
@ -130,12 +130,12 @@ class NrelPhysicsParameters:
# The agreement is that the layers are defined from outside to inside
external_layer = construction_archetype.layers[0]
external_surface = thermal_boundary.parent_surface
external_surface.short_wave_reflectance = 1 - float(external_layer.material.solar_absorptance)
external_surface.long_wave_emittance = 1 - float(external_layer.material.solar_absorptance)
external_surface.short_wave_reflectance = 1 - external_layer.material.solar_absorptance
external_surface.long_wave_emittance = 1 - external_layer.material.solar_absorptance
internal_layer = construction_archetype.layers[len(construction_archetype.layers) - 1]
internal_surface = thermal_boundary.internal_surface
internal_surface.short_wave_reflectance = 1 - float(internal_layer.material.solar_absorptance)
internal_surface.long_wave_emittance = 1 - float(internal_layer.material.solar_absorptance)
internal_surface.short_wave_reflectance = 1 - internal_layer.material.solar_absorptance
internal_surface.long_wave_emittance = 1 - internal_layer.material.solar_absorptance
for thermal_opening in thermal_boundary.thermal_openings:
if construction_archetype.window is not None:

View File

@ -7,6 +7,7 @@ Project CoderPeter Yefi peteryefi@gmail.com
from hub.city_model_structure.city import City
from hub.persistence import City as CityRepository
from hub.persistence import SimulationResults
from hub.persistence import Application
class DBFactory:
@ -17,6 +18,7 @@ class DBFactory:
def __init__(self, db_name, dotenv_path, app_env):
self._city_repository = CityRepository(db_name=db_name, dotenv_path=dotenv_path, app_env=app_env)
self._simulation_results = SimulationResults(db_name=db_name, dotenv_path=dotenv_path, app_env=app_env)
self._application = Application(db_name=db_name, dotenv_path=dotenv_path, app_env=app_env)
def persist_city(self, city: City, pickle_path, application_id: int, user_id: int):
"""
@ -36,6 +38,24 @@ class DBFactory:
"""
return self._city_repository.update(city_id, city)
def persist_application(self, name: str, description: str, application_uuid: str):
"""
Creates an application
:param name: name of application
:param description: the description of the application
:param application_uuid: the uuid of the application to be created
"""
return self._application.insert(name, description, application_uuid)
def update_application(self, name: str, description: str, application_uuid: str):
"""
Update an application
:param name: name of application
:param description: the description of the application
:param application_uuid: the uuid of the application to be created
"""
return self._application.update(application_uuid, name, description)
def delete_city(self, city_id):
"""
Deletes a single city from postgres
@ -43,6 +63,13 @@ class DBFactory:
"""
self._city_repository.delete(city_id)
def delete_application(self, application_uuid):
"""
Deletes a single application from postgres
:param application_uuid: the id of the application to get
"""
self._application.delete(application_uuid)
def add_simulation_results(self, name, values, city_id=None, city_object_id=None):
"""
Add simulation results to the city or to the city_object
@ -51,4 +78,4 @@ class DBFactory:
:param city_id: city id or None
:param city_object_id: city object id or None
"""
self._simulation_results.insert(name, values,city_id, city_object_id)
self._simulation_results.insert(name, values, city_id, city_object_id)

View File

@ -12,7 +12,8 @@ from pyproj import Transformer
from shapely.geometry import Polygon as ShapelyPolygon
import hub.helpers.constants as cte
from hub.imports.geometry.helpers.geometry_helper import GeometryHelper
from hub.helpers.geometry_helper import GeometryHelper
from hub.imports.geometry.helpers.geometry_helper import GeometryHelper as igh
from hub.city_model_structure.attributes.polygon import Polygon
from hub.city_model_structure.building import Building
from hub.city_model_structure.building_demand.surface import Surface
@ -62,7 +63,7 @@ class Geojson:
surfaces = []
buildings = []
for zone, surface_coordinates in enumerate(surfaces_coordinates):
points = GeometryHelper.points_from_string(GeometryHelper.remove_last_point_from_string(surface_coordinates))
points = igh.points_from_string(igh.remove_last_point_from_string(surface_coordinates))
polygon = Polygon(points)
surfaces.append(Surface(polygon, polygon, surface_type=cte.GROUND))
buildings.append(Building(f'{name}_zone_{zone}', surfaces, year_of_construction, function))
@ -106,6 +107,47 @@ class Geojson:
polygons.append(transformed_coordinates.lstrip(' '))
return polygons
@staticmethod
def _find_wall(line_1, line_2):
for i in range(0, 2):
point_1 = line_1[i]
point_2 = line_2[i]
distance = GeometryHelper.distance_between_points(point_1, point_2)
if distance > 1e-2:
return False
return True
def _store_shared_percentage_to_walls(self, city, city_mapped):
for building in city.buildings:
building_mapped = city_mapped[building.name]
for entry in building_mapped:
if building_mapped[entry]['shared_points'] <= 3:
continue
line = [building_mapped[entry]['line_start'], building_mapped[entry]['line_end']]
neighbour_line = [building_mapped[entry]['neighbour_line_start'], building_mapped[entry]['neighbour_line_end']]
neighbour_height = city.city_object(building_mapped[entry]['neighbour_name']).max_height
for wall in building.walls:
percentage = 0
ground_line = []
for point in wall.perimeter_polygon.coordinates:
if point[2] < 0.5:
ground_line.append(point)
# todo: erase when we have no triangulation
if len(ground_line) < 2:
continue
# todo: erase down to here
if self._find_wall(line, ground_line):
line_shared = (GeometryHelper.distance_between_points(line[0], line[1]) +
GeometryHelper.distance_between_points(neighbour_line[0], neighbour_line[1]) -
GeometryHelper.distance_between_points(line[1], neighbour_line[0]) -
GeometryHelper.distance_between_points(line[0], neighbour_line[1])) / 2
percentage = line_shared / GeometryHelper.distance_between_points(line[0], line[1])
percentage_height = neighbour_height / building.max_height
if percentage_height > 1:
percentage_height = 1
percentage = percentage * percentage_height
wall.percentage_shared = percentage
@property
def city(self) -> City:
"""

View File

@ -35,8 +35,8 @@ class InselMonthlyEnergyBalance:
demand[i] = '0'
heating.append(demand[0])
cooling.append(demand[1])
monthly_heating = pd.DataFrame(heating, columns=[cte.INSEL_MEB])
monthly_cooling = pd.DataFrame(cooling, columns=[cte.INSEL_MEB])
monthly_heating = pd.DataFrame(heating, columns=[cte.INSEL_MEB]).astype(float)
monthly_cooling = pd.DataFrame(cooling, columns=[cte.INSEL_MEB]).astype(float)
return monthly_heating, monthly_cooling
def enrich(self):
@ -46,8 +46,8 @@ class InselMonthlyEnergyBalance:
if insel_output_file_path.is_file():
building.heating[cte.MONTH], building.cooling[cte.MONTH] = self._demand(insel_output_file_path)
building.heating[cte.YEAR] = pd.DataFrame(
[building.heating[cte.MONTH][cte.INSEL_MEB].sum()], columns=[cte.INSEL_MEB]
[building.heating[cte.MONTH][cte.INSEL_MEB].astype(float).sum()], columns=[cte.INSEL_MEB]
)
building.cooling[cte.YEAR] = pd.DataFrame(
[building.cooling[cte.MONTH][cte.INSEL_MEB].sum()], columns=[cte.INSEL_MEB]
[building.cooling[cte.MONTH][cte.INSEL_MEB].astype(float).sum()], columns=[cte.INSEL_MEB]
)

View File

@ -189,10 +189,15 @@ class ComnetUsageParameters:
_schedule_values[v, day] += value * archetype.appliances.density
_sum += value * archetype.appliances.density * _number_of_days_per_type[day]
_latent_fraction = _latent_heat_gain / _total_heat_gain
_radiative_fraction = _radiative_heat_gain / _total_heat_gain
_convective_fraction = _convective_heat_gain / _total_heat_gain
_average_internal_gain = _sum / _total_heat_gain
_latent_fraction = 0
_radiative_fraction = 0
_convective_fraction = 0
_average_internal_gain = 0
if _total_heat_gain != 0:
_latent_fraction = _latent_heat_gain / _total_heat_gain
_radiative_fraction = _radiative_heat_gain / _total_heat_gain
_convective_fraction = _convective_heat_gain / _total_heat_gain
_average_internal_gain = _sum / _total_heat_gain
_schedules = []
for day in range(0, len(_DAYS)):

View File

@ -4,6 +4,7 @@ SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2022 Concordia CERC group
Project CoderPeter Yefi peteryefi@gmail.com
"""
from hub.persistence import User
from hub.persistence import UserRoles
@ -16,17 +17,17 @@ class UserFactory:
def __init__(self, db_name, app_env, dotenv_path):
self._user_repo = User(db_name=db_name, app_env=app_env, dotenv_path=dotenv_path)
def create_user(self, name: str, email: str, password: str, role: UserRoles):
def create_user(self, name: str, application_id: int, password: str, role: UserRoles):
"""
Creates a new user
:param name: the name of the user
:param email: the email of the user
:param application_id: the application id of the user
:param password: the password of the user
:param role: the role of the user
"""
return self._user_repo.insert(name, email, password, role)
return self._user_repo.insert(name, password, role, application_id)
def update_user(self, user_id: int, name: str, email: str, password: str, role: UserRoles):
def update_user(self, user_id: int, name: str, password: str, role: UserRoles):
"""
Creates a new user
:param user_id: the id of the user
@ -35,11 +36,11 @@ class UserFactory:
:param password: the password of the user
:param role: the role of the user
"""
return self._user_repo.update(user_id, name, email, password, role)
return self._user_repo.update(user_id, name, password, role)
def delete_user(self, user_id):
"""
Retrieve a single user
:param user_id: the id of the user to delete
"""
return self._user_repo.delete_user(user_id)
return self._user_repo.delete(user_id)

View File

@ -12,6 +12,7 @@ from sqlalchemy import DateTime
from sqlalchemy.dialects.postgresql import JSONB
from hub.persistence.configuration import Models
class SimulationResults(Models):
"""
A model representation of an application

View File

@ -42,6 +42,7 @@ class Application(Repository):
if application is None:
try:
application = Model(name=name, description=description, application_uuid=application_uuid)
self.session.add(application)
self.session.commit()
return application

View File

@ -6,7 +6,6 @@ Project Coder Peter Yefi peteryefi@gmail.com
"""
import datetime
import pickle
from typing import Union, Dict
from sqlalchemy import select
@ -60,8 +59,11 @@ class City(Repository):
for building in city.buildings:
object_usage = ''
for internal_zone in building.internal_zones:
for usage in internal_zone.usages:
object_usage = f'{object_usage}{usage.name}_{usage.percentage} '
if internal_zone is None or internal_zone.usages is None:
object_usage = 'Unknown'
else:
for usage in internal_zone.usages:
object_usage = f'{object_usage}{usage.name}_{usage.percentage} '
object_usage = object_usage.rstrip()
db_city_object = CityObject(db_city.id,
building.name,
@ -101,6 +103,7 @@ class City(Repository):
:return: a city
"""
try:
self.session.query(CityObject).filter(CityObject.city_id == city_id).delete()
self.session.query(Model).filter(Model.id == city_id).delete()
self.session.commit()
except SQLAlchemyError as err:

View File

@ -125,6 +125,50 @@ class SimulationResults(Repository):
except SQLAlchemyError as err:
logger.error(f'Error while fetching city by city_id: {err}')
def get_simulation_results_by_id(self, sim_id) -> [Model]:
"""
Fetch simulation results by id
:param sim_id: a simulation result identifier
:return: [Model] with the provided sim_id
"""
try:
return self.session.execute(select(Model).where(Model.id == sim_id)).first()
except SQLAlchemyError as err:
logger.error(f'Error while fetching simulation results by sim_id: {err}')
def get_simulation_results_by_name(self, name) -> [Model]:
"""
Fetch simulation results by name
:param name: the name of the simulation results
:return: [Model] with the provided name
"""
try:
return self.session.execute(select(Model).where(Model.name == name))
except SQLAlchemyError as err:
logger.error(f'Error while fetching simulation results by name: {err}')
def get_simulation_results_by_city_id(self, city_id) -> [Model]:
"""
Fetch simulation results by name
:param city_id: the id of the city
:return: [Model] with the provided city id
"""
try:
return self.session.execute(select(Model).where(Model.city_id == city_id))
except SQLAlchemyError as err:
logger.error(f'Error while fetching simulation results by name: {err}')
def get_simulation_results_by_city_object_id(self, city_object_id) -> [Model]:
"""
Fetch simulation results by name
:param city_object_id: the id of the city object
:return: [Model] with the provided city object id
"""
try:
return self.session.execute(select(Model).where(Model.city_object_id == city_object_id))
except SQLAlchemyError as err:
logger.error(f'Error while fetching simulation results by name: {err}')
def get_city_object(self, city_object_id) -> [CityObject]:
"""
Fetch a city object based city id

View File

@ -6,8 +6,12 @@ Project Coder Guille Gutierrez Guillermo.GutierrezMorote@concordia.ca
"""
from pathlib import Path
from unittest import TestCase
from hub.imports.geometry_factory import GeometryFactory
import platform
from hub.exports.exports_factory import ExportsFactory
import subprocess
from subprocess import SubprocessError, TimeoutExpired, CalledProcessError
from hub.imports.results_factory import ResultFactory
class TestCityMerge(TestCase):
@ -20,6 +24,16 @@ class TestCityMerge(TestCase):
:return: None
"""
self._example_path = (Path(__file__).parent / 'tests_data').resolve()
self._output_path = (Path(__file__).parent / 'tests_outputs').resolve()
self._weather_file = (self._example_path / 'CAN_PQ_Montreal.Intl.AP.716270_CWEC.epw').resolve()
self._climate_file = (self._example_path / 'New_York.cli').resolve()
self._executable = None
if platform.system() == 'Linux':
self._executable = 'citysim_sra'
if platform.system() == 'Darwin':
self._executable = 'citysim_sra'
elif platform.system() == 'Windows':
self._executable = 'shortwave_integer'
def _get_citygml(self, file):
file_path = (self._example_path / file).resolve()
@ -34,3 +48,23 @@ class TestCityMerge(TestCase):
self.assertEqual(len(city_1.city_objects), 1, 'Wrong amount of city_objects found in city_1')
self.assertEqual(len(city_2.city_objects), 1, 'Wrong amount of city_objects found in city_2')
self.assertEqual(len(city.city_objects), 2, 'Wrong amount of city_objects found in city')
def test_merge_with_radiation(self):
city_one = self._get_citygml('one_building_in_kelowna.gml')
city_two = self._get_citygml('pluto_building.gml')
city_two.name = 'New_York'
city_two.climate_file = self._climate_file
try:
ExportsFactory(export_type='sra', city=city_two, path=self._output_path, weather_file=self._weather_file,
target_buildings=city_two.buildings, weather_format='epw').export()
subprocess.run([self._executable, f'{str(self._output_path)}/{city_two.name}_sra.xml'], stdout=subprocess.DEVNULL)
except (SubprocessError, TimeoutExpired, CalledProcessError) as error:
raise Exception(error)
ResultFactory('sra', city_two, self._output_path).enrich()
merged_city = city_one.merge(city_two)
self.assertEqual(len(merged_city.buildings), 2)
self.assertEqual(merged_city.buildings[1].surfaces[0].global_irradiance['year'].iloc[0], 254.3453196347032)
self.assertEqual(merged_city.buildings[0].surfaces[0].global_irradiance, {})
self.assertEqual(merged_city.buildings[0].surfaces[2].global_irradiance, {})
self.assertEqual(city_one.buildings[0].surfaces[0].global_irradiance, merged_city.buildings[0].surfaces[0].global_irradiance)
self.assertEqual(city_two.buildings[0].surfaces[0].global_irradiance, merged_city.buildings[1].surfaces[0].global_irradiance)

View File

@ -269,7 +269,7 @@ class TestConstructionFactory(TestCase):
path=file_path,
height_field='citygml_me',
year_of_construction_field='ANNEE_CONS',
function_field='LIBELLE_UT',
function_field='CODE_UTILI',
function_to_hub=Dictionaries().montreal_function_to_hub_function).city
ConstructionFactory('nrcan', city).enrich()

View File

@ -9,12 +9,12 @@ from hub.imports.geometry_factory import GeometryFactory
from hub.imports.db_factory import DBFactory
from hub.imports.user_factory import UserFactory
from hub.exports.db_factory import DBFactory as ExportDBFactory
from hub.persistence.base_repo import BaseRepo
from hub.persistence.repository import Repository
from sqlalchemy import create_engine
from hub.persistence.models import City
from hub.persistence.models import City, Application, CityObject
from hub.persistence.models import User, UserRoles
from pickle import loads
from sqlalchemy.exc import ProgrammingError
import uuid
class TestDBFactory(TestCase):
@ -29,8 +29,8 @@ class TestDBFactory(TestCase):
:return: None
"""
# Create test database
repo = BaseRepo(db_name='test_db', app_env='TEST', dotenv_path='/usr/local/etc/hub/.env')
eng = create_engine(f'postgresql://{repo.config.get_db_user()}@/{repo.config.get_db_user()}')
repo = Repository(db_name='test_db', app_env='TEST', dotenv_path='/usr/local/etc/hub/.env')
eng = create_engine(f'postgresql://{repo.configuration.get_db_user()}@/{repo.configuration.get_db_user()}')
try:
# delete test database if it exists
@ -45,56 +45,58 @@ class TestDBFactory(TestCase):
cnn.execute('commit')
cnn.execute("CREATE DATABASE test_db")
cnn.close()
Application.__table__.create(bind=repo.engine, checkfirst=True)
User.__table__.create(bind=repo.engine, checkfirst=True)
City.__table__.create(bind=repo.engine, checkfirst=True)
CityObject.__table__.create(bind=repo.engine, checkfirst=True)
city_file = "tests_data/C40_Final.gml"
cls.city = GeometryFactory('citygml', city_file).city
cls._db_factory = DBFactory(db_name='test_db', app_env='TEST', dotenv_path='../.env')
cls._export_db_factory = ExportDBFactory(db_name='test_db', app_env='TEST', dotenv_path='../.env')
user_factory = UserFactory(db_name='test_db', app_env='TEST', dotenv_path='../.env')
cls._user = user_factory.create_user("Admin", "admin@hub.com", "Admin@123", UserRoles.Admin)
cls.unique_id = str(uuid.uuid4())
cls.application = cls._db_factory.persist_application("test", "test application", cls.unique_id)
cls._user = user_factory.create_user("Admin", cls.application.id, "Admin@123", UserRoles.Admin)
cls.pickle_path = 'tests_data/pickle_path.bz2'
def test_save_application(self):
self.assertEqual(self.application.name, "test")
self.assertEqual(self.application.description, "test application")
self.assertEqual(str(self.application.application_uuid), self.unique_id)
def test_save_city(self):
saved_city = self._db_factory.persist_city(self._user.id, self.city)
self.city.name = "Montréal"
saved_city = self._db_factory.persist_city(self.city, self.pickle_path, self.application.id, self._user.id)
self.assertEqual(saved_city.name, 'Montréal')
pickled_city = loads(saved_city.city)
self.assertEqual(len(pickled_city.buildings), 10)
self.assertEqual(pickled_city.buildings[0].floor_area, 1990.9913970530033)
self.assertEqual(saved_city.pickle_path, self.pickle_path)
self.assertEqual(saved_city.level_of_detail, self.city.level_of_detail.geometry)
self._db_factory.delete_city(saved_city.id)
def test_save_same_city_with_same_hub_version(self):
first_city = self._db_factory.persist_city(self._user.id, self.city)
second_city = self._db_factory.persist_city(self._user.id, self.city)
self.assertEqual(second_city['message'], f'Same version of {self.city.name} exist')
self.assertEqual(first_city.name, 'Montréal')
self.assertEqual(first_city.country_code, 'ca')
self._db_factory.delete_city(first_city.id)
def test_get_city_by_name(self):
city = self._db_factory.persist_city(self._user.id, self.city)
city = self._db_factory.persist_city(self.city, self.pickle_path, self.application.id, self._user.id)
retrieved_city = self._export_db_factory.get_city_by_name(city.name)
self.assertEqual(retrieved_city[0].lower_corner[0], 610610.7547462888)
self._db_factory.delete_city(city.id)
def test_get_city_by_user(self):
city = self._db_factory.persist_city(self._user.id, self.city)
retrieved_city = self._export_db_factory.get_city_by_user(self._user.id)
self.assertEqual(retrieved_city[0].application_id, 1)
self.assertEqual(retrieved_city[0].user_id, self._user.id)
self._db_factory.delete_city(city.id)
def test_get_city_by_user(self):
city = self._db_factory.persist_city(self.city, self.pickle_path, self.application.id, self._user.id)
retrieved_city = self._export_db_factory.get_city_by_user(self._user.id)
self.assertEqual(retrieved_city[0].pickle_path, self.pickle_path)
self._db_factory.delete_city(city.id)
def test_get_city_by_id(self):
city = self._db_factory.persist_city(self._user.id, self.city)
city = self._db_factory.persist_city(self.city, self.pickle_path, self.application.id, self._user.id)
retrieved_city = self._export_db_factory.get_city(city.id)
self.assertEqual(retrieved_city.upper_corner[0], 610818.6731258357)
self.assertEqual(retrieved_city.level_of_detail, self.city.level_of_detail.geometry)
self._db_factory.delete_city(city.id)
def test_get_update_city(self):
city = self._db_factory.persist_city(self._user.id, self.city)
self.city.longitude = 1.43589
self.city.latitude = -9.38928339
city = self._db_factory.persist_city(self.city, self.pickle_path, self.application.id, self._user.id)
self.city.name = "Ottawa"
self._db_factory.update_city(city.id, self.city)
updated_city = self._export_db_factory.get_city(city.id)
self.assertEqual(updated_city.longitude, 1.43589)
self.assertEqual(updated_city.latitude, -9.38928339)
self.assertEqual(updated_city.name, self.city.name)
self._db_factory.delete_city(city.id)

View File

@ -4,7 +4,7 @@ SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2022 Concordia CERC group
Project Coder Pilar Monsalvete Alvarez de Uribarri pilar.monsalvete@concordia.ca
"""
import datetime
from pathlib import Path
from unittest import TestCase
from hub.helpers.geometry_helper import GeometryHelper

File diff suppressed because it is too large Load Diff

Binary file not shown.