368 lines
16 KiB
Python
368 lines
16 KiB
Python
"""
|
|
TestOccupancyFactory test and validate the city model structure schedules parameters
|
|
SPDX - License - Identifier: LGPL - 3.0 - or -later
|
|
Copyright © 2020 Project Author Guille Gutierrez guillermo.gutierrezmorote@concordia.ca
|
|
Contributors Soroush Samareh Abolhassani - soroush.samarehabolhassani@mail.concordia.ca
|
|
Pilar Monsalvete Alvarez de Uribarri pilar.monsalvete@concordia.ca
|
|
"""
|
|
|
|
from geomeppy import IDF
|
|
import helpers.constants as cte
|
|
|
|
|
|
class Idf:
|
|
"""
|
|
Export city to IDF
|
|
"""
|
|
_THERMOSTAT = 'HVACTEMPLATE:THERMOSTAT'
|
|
_IDEAL_LOAD_AIR_SYSTEM = 'HVACTEMPLATE:ZONE:IDEALLOADSAIRSYSTEM'
|
|
_SURFACE = 'BUILDINGSURFACE:DETAILED'
|
|
_CONSTRUCTION = 'CONSTRUCTION'
|
|
_MATERIAL = 'MATERIAL'
|
|
_MATERIAL_NOMASS = 'MATERIAL:NOMASS'
|
|
_ROUGHNESS = 'MediumRough'
|
|
_HOURLY_SCHEDULE = 'SCHEDULE:DAY:HOURLY'
|
|
_COMPACT_SCHEDULE = 'SCHEDULE:COMPACT'
|
|
_ZONE = 'ZONE'
|
|
_LIGHTS = 'LIGHTS'
|
|
_PEOPLE = 'PEOPLE'
|
|
_ELECTRIC_EQUIPMENT = 'ELECTRICEQUIPMENT'
|
|
_INFILTRATION = 'ZONEINFILTRATION:DESIGNFLOWRATE'
|
|
_BUILDING_SURFACE = 'BuildingSurfaceDetailed'
|
|
_SCHEDULE_LIMIT = 'SCHEDULETYPELIMITS'
|
|
_ON_OFF = 'On/Off'
|
|
_FRACTION = 'Fraction'
|
|
_ANY_NUMBER = 'Any Number'
|
|
_CONTINUOUS = 'Continuous'
|
|
_DISCRETE = 'Discrete'
|
|
|
|
idf_surfaces = {
|
|
# todo: make an enum for all the surface types
|
|
cte.WALL: 'wall',
|
|
cte.GROUND: 'floor',
|
|
cte.ROOF: 'roof'
|
|
}
|
|
idf_usage = {
|
|
# todo: make an enum for all the usage types
|
|
cte.RESIDENTIAL: 'residential_building'
|
|
}
|
|
|
|
idf_type_limits = {
|
|
cte.ON_OFF: 'on/off',
|
|
cte.FRACTION: 'Fraction',
|
|
cte.ANY_NUMBER: 'Any Number',
|
|
'continuous': 'Continuous',
|
|
'discrete': 'Discrete'
|
|
}
|
|
|
|
def __init__(self, city, output_path, idf_file_path, idd_file_path, epw_file_path, export_type="Surfaces"):
|
|
self._city = city
|
|
self._output_path = str(output_path.resolve())
|
|
self._output_file = str((output_path / f'{city.name}.idf').resolve())
|
|
self._export_type = export_type
|
|
self._idd_file_path = str(idd_file_path)
|
|
self._idf_file_path = str(idf_file_path)
|
|
self._epw_file_path = str(epw_file_path)
|
|
IDF.setiddname(self._idd_file_path)
|
|
self._idf = IDF(self._idf_file_path, self._epw_file_path)
|
|
self._idf.newidfobject(self._SCHEDULE_LIMIT, Name=self._ANY_NUMBER)
|
|
self._idf.newidfobject(self._SCHEDULE_LIMIT, Name=self._FRACTION, Lower_Limit_Value=0.0, Upper_Limit_Value=1.0,
|
|
Numeric_Type=self._CONTINUOUS)
|
|
self._idf.newidfobject(self._SCHEDULE_LIMIT, Name=self._ON_OFF, Lower_Limit_Value=0, Upper_Limit_Value=1,
|
|
Numeric_Type=self._DISCRETE)
|
|
self._export()
|
|
|
|
@staticmethod
|
|
def _matrix_to_list(points, lower_corner):
|
|
lower_x = lower_corner[0]
|
|
lower_y = lower_corner[1]
|
|
lower_z = lower_corner[2]
|
|
points_list = []
|
|
for point in points:
|
|
point_tuple = (point[0]-lower_x, point[1]-lower_y, point[2]-lower_z)
|
|
points_list.append(point_tuple)
|
|
return points_list
|
|
|
|
@staticmethod
|
|
def _matrix_to_2d_list(points):
|
|
points_list = []
|
|
for point in points:
|
|
point_tuple = (point[0], point[1])
|
|
points_list.append(point_tuple)
|
|
return points_list
|
|
|
|
def _add_material(self, layer):
|
|
for material in self._idf.idfobjects[self._MATERIAL]:
|
|
if material.Name == layer.material.name:
|
|
return
|
|
for material in self._idf.idfobjects[self._MATERIAL_NOMASS]:
|
|
if material.Name == layer.material.name:
|
|
return
|
|
if layer.material.no_mass:
|
|
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
|
|
)
|
|
else:
|
|
self._idf.newidfobject(self._MATERIAL,
|
|
Name=layer.material.name,
|
|
Roughness=self._ROUGHNESS,
|
|
Thickness=layer.thickness,
|
|
Conductivity=layer.material.conductivity,
|
|
Density=layer.material.density,
|
|
Specific_Heat=layer.material.specific_heat,
|
|
Thermal_Absorptance=layer.material.thermal_absorptance,
|
|
Solar_Absorptance=layer.material.solar_absorptance,
|
|
Visible_Absorptance=layer.material.visible_absorptance
|
|
)
|
|
|
|
def _add_daily_schedule(self, usage, schedule):
|
|
_schedule = self._idf.newidfobject(self._COMPACT_SCHEDULE, Name=f'{schedule.type} schedules {usage}')
|
|
_val = schedule.values
|
|
_schedule.Schedule_Type_Limits_Name = self.idf_type_limits[schedule.data_type.lower()]
|
|
_schedule.Field_1 = "Through: 12/31"
|
|
_schedule.Field_2 = "For: AllDays"
|
|
_schedule.Field_3 = "Until: 01:00"
|
|
_schedule.Field_4 = _val[0]
|
|
_schedule.Field_5 = "Until: 02:00"
|
|
_schedule.Field_6 = _val[1]
|
|
_schedule.Field_7 = "Until: 03:00"
|
|
_schedule.Field_8 = _val[2]
|
|
_schedule.Field_9 = "Until: 04:00"
|
|
_schedule.Field_10 = _val[3]
|
|
_schedule.Field_11 = "Until: 05:00"
|
|
_schedule.Field_12 = _val[4]
|
|
_schedule.Field_13 = "Until: 06:00"
|
|
_schedule.Field_14 = _val[5]
|
|
_schedule.Field_15 = "Until: 07:00"
|
|
_schedule.Field_16 = _val[6]
|
|
_schedule.Field_17 = "Until: 08:00"
|
|
_schedule.Field_18 = _val[7]
|
|
_schedule.Field_19 = "Until: 09:00"
|
|
_schedule.Field_20 = _val[8]
|
|
_schedule.Field_21 = "Until: 10:00"
|
|
_schedule.Field_22 = _val[9]
|
|
_schedule.Field_23 = "Until: 11:00"
|
|
_schedule.Field_24 = _val[10]
|
|
_schedule.Field_25 = "Until: 12:00"
|
|
_schedule.Field_26 = _val[11]
|
|
_schedule.Field_27 = "Until: 13:00"
|
|
_schedule.Field_28 = _val[12]
|
|
_schedule.Field_29 = "Until: 14:00"
|
|
_schedule.Field_30 = _val[13]
|
|
_schedule.Field_31 = "Until: 15:00"
|
|
_schedule.Field_32 = _val[14]
|
|
_schedule.Field_33 = "Until: 16:00"
|
|
_schedule.Field_34 = _val[15]
|
|
_schedule.Field_35 = "Until: 17:00"
|
|
_schedule.Field_36 = _val[16]
|
|
_schedule.Field_37 = "Until: 18:00"
|
|
_schedule.Field_38 = _val[17]
|
|
_schedule.Field_39 = "Until: 19:00"
|
|
_schedule.Field_40 = _val[18]
|
|
_schedule.Field_41 = "Until: 20:00"
|
|
_schedule.Field_42 = _val[19]
|
|
_schedule.Field_43 = "Until: 21:00"
|
|
_schedule.Field_44 = _val[20]
|
|
_schedule.Field_45 = "Until: 22:00"
|
|
_schedule.Field_46 = _val[21]
|
|
_schedule.Field_47 = "Until: 23:00"
|
|
_schedule.Field_48 = _val[22]
|
|
_schedule.Field_49 = "Until: 24:00"
|
|
_schedule.Field_50 = _val[23]
|
|
|
|
def _add_schedule(self, usage, new_schedule):
|
|
for schedule in self._idf.idfobjects[self._HOURLY_SCHEDULE]:
|
|
if schedule.Name == f'{new_schedule.type} schedules {usage}':
|
|
return
|
|
if new_schedule.time_range == "day":
|
|
return self._add_daily_schedule(usage, new_schedule)
|
|
return
|
|
|
|
def _add_construction(self, thermal_boundary):
|
|
for construction in self._idf.idfobjects[self._CONSTRUCTION]:
|
|
if construction.Name == thermal_boundary.construction_name:
|
|
return
|
|
|
|
if thermal_boundary.layers is None:
|
|
for material in self._idf.idfobjects[self._MATERIAL]:
|
|
if material.Name == "DefaultMaterial":
|
|
return
|
|
self._idf.set_default_constructions()
|
|
return
|
|
for layer in thermal_boundary.layers:
|
|
self._add_material(layer)
|
|
layers = thermal_boundary.layers
|
|
# The constructions should have at least one layer
|
|
_kwargs = {"Name": thermal_boundary.construction_name, "Outside_Layer": layers[0].material.name}
|
|
for i in range(1, len(layers) - 1):
|
|
_kwargs[f'Layer_{i + 1}'] = layers[1].material.name
|
|
self._idf.newidfobject(self._CONSTRUCTION, **_kwargs)
|
|
|
|
def _add_zone(self, usage_zone, thermal_zone_volume):
|
|
for zone in self._idf.idfobjects['ZONE']:
|
|
if zone.Name == usage_zone.id:
|
|
return
|
|
# todo: what do we need to define a zone in energy plus?
|
|
self._idf.newidfobject(self._ZONE, Name=usage_zone.id, Volume=thermal_zone_volume * usage_zone.percentage)
|
|
self._add_heating_system(usage_zone)
|
|
|
|
def _add_thermostat(self, usage_zone):
|
|
thermostat_name = f'Thermostat {usage_zone.usage}'
|
|
for thermostat in self._idf.idfobjects[self._THERMOSTAT]:
|
|
if thermostat.Name == thermostat_name:
|
|
return thermostat
|
|
return self._idf.newidfobject(self._THERMOSTAT,
|
|
Name=thermostat_name,
|
|
Constant_Heating_Setpoint=usage_zone.thermal_control.mean_heating_set_point,
|
|
Constant_Cooling_Setpoint=usage_zone.thermal_control.mean_cooling_set_point)
|
|
|
|
def _add_heating_system(self, usage_zone):
|
|
for air_system in self._idf.idfobjects[self._IDEAL_LOAD_AIR_SYSTEM]:
|
|
if air_system.Zone_Name == usage_zone.id:
|
|
return
|
|
thermostat = self._add_thermostat(usage_zone)
|
|
self._idf.newidfobject(self._IDEAL_LOAD_AIR_SYSTEM,
|
|
Zone_Name=usage_zone.id,
|
|
System_Availability_Schedule_Name=f'HVAC AVAIL SCHEDULES {usage_zone.usage}',
|
|
Heating_Availability_Schedule_Name=f'HVAC AVAIL SCHEDULES {usage_zone.usage}',
|
|
Cooling_Availability_Schedule_Name=f'HVAC AVAIL SCHEDULES {usage_zone.usage}',
|
|
Template_Thermostat_Name=thermostat.Name)
|
|
|
|
def _add_occupancy(self, usage_zone, area):
|
|
number_of_people = area * usage_zone.occupancy.occupancy_density
|
|
fraction_radiant = usage_zone.occupancy.sensible_radiative_internal_gain / \
|
|
(usage_zone.occupancy.sensible_radiative_internal_gain +
|
|
usage_zone.occupancy.sensible_convective_internal_gain +
|
|
usage_zone.occupancy.latent_internal_gain)
|
|
self._idf.newidfobject(self._PEOPLE,
|
|
Name=f'{usage_zone.id}_occupancy',
|
|
Zone_or_ZoneList_Name=usage_zone.id,
|
|
Number_of_People_Schedule_Name=f'Occupancy schedules {usage_zone.usage}',
|
|
Number_of_People_Calculation_Method="People",
|
|
Number_of_People=number_of_people,
|
|
Fraction_Radiant=fraction_radiant,
|
|
Activity_Level_Schedule_Name=f'Occupancy schedules {usage_zone.usage}'
|
|
)
|
|
|
|
def _add_equipment(self, usage_zone):
|
|
self._idf.newidfobject(self._ELECTRIC_EQUIPMENT,
|
|
Name=f'{usage_zone.id}_electricload',
|
|
Zone_or_ZoneList_Name=usage_zone.id,
|
|
Schedule_Name=f'Electrical schedules {usage_zone.usage}', # todo: add electrical schedules
|
|
Design_Level_Calculation_Method='EquipmentLevel',
|
|
Design_Level=566000 # todo: change it from usage catalog
|
|
)
|
|
|
|
def _add_infiltration(self, usage_zone):
|
|
for zone in self._idf.idfobjects["ZONE"]:
|
|
if zone.Name == f'{usage_zone.id}_infiltration':
|
|
return
|
|
self._idf.newidfobject(self._INFILTRATION,
|
|
Name=f'{usage_zone.id}_infiltration',
|
|
Zone_or_ZoneList_Name=usage_zone.id,
|
|
Schedule_Name=f'Infiltration schedules {usage_zone.usage}',
|
|
Design_Flow_Rate_Calculation_Method='AirChanges/Hour',
|
|
Air_Changes_per_Hour=usage_zone.mechanical_air_change,
|
|
Constant_Term_Coefficient=0.606, # todo: change it from usage catalog
|
|
Temperature_Term_Coefficient=3.6359996E-02, # todo: change it from usage catalog
|
|
Velocity_Term_Coefficient=0.1177165, # todo: change it from usage catalog
|
|
Velocity_Squared_Term_Coefficient=0.0000000E+00 # todo: change it from usage catalog
|
|
)
|
|
|
|
def _export(self):
|
|
"""
|
|
Export the idf file into the given path
|
|
export type = "Surfaces|Block"
|
|
"""
|
|
|
|
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:
|
|
self._add_construction(thermal_boundary)
|
|
for usage_zone in thermal_zone.usage_zones:
|
|
usage = usage_zone.usage
|
|
usage_zone_area = thermal_zone.floor_area * usage_zone.percentage
|
|
|
|
# todo: infiltration can be written with two values (system on and system off) in E+? Or just as schedule?
|
|
# self._add_schedule(usage, "Infiltration")
|
|
for schedule in usage_zone.lighting.schedules:
|
|
for day_type in schedule.day_types:
|
|
if day_type == cte.MONDAY:
|
|
self._add_schedule(usage, schedule)
|
|
for schedule in usage_zone.occupancy.occupancy_schedules:
|
|
for day_type in schedule.day_types:
|
|
if day_type == cte.MONDAY:
|
|
self._add_schedule(usage, usage_zone.occupancy.occupancy_schedules)
|
|
for schedule in usage_zone.thermal_control.hvac_availability_schedules:
|
|
for day_type in schedule.day_types:
|
|
if day_type == cte.MONDAY:
|
|
self._add_schedule(usage, schedule)
|
|
|
|
self._add_zone(usage_zone, thermal_zone.volume)
|
|
self._add_heating_system(usage_zone)
|
|
# self._add_infiltration(usage_zone)
|
|
self._add_occupancy(usage_zone, usage_zone_area)
|
|
|
|
if self._export_type == "Surfaces":
|
|
self._add_surfaces(building)
|
|
else:
|
|
self._add_block(building)
|
|
self._idf.newidfobject(
|
|
"OUTPUT:VARIABLE",
|
|
Variable_Name="Zone Ideal Loads Supply Air Total Heating Energy",
|
|
Reporting_Frequency="Hourly",
|
|
)
|
|
self._idf.newidfobject(
|
|
"OUTPUT:VARIABLE",
|
|
Variable_Name="Zone Ideal Loads Supply Air Total Cooling Energy",
|
|
Reporting_Frequency="Hourly",
|
|
)
|
|
self._idf.match()
|
|
self._idf.intersect_match()
|
|
self._idf.saveas(str(self._output_file))
|
|
return self._idf
|
|
|
|
def run(self):
|
|
"""
|
|
Start the energy plus simulation
|
|
"""
|
|
self._idf.run(expandobjects=True, readvars=True, output_directory=self._output_path,
|
|
output_prefix=f'{self._city.name}_')
|
|
|
|
def _add_block(self, building):
|
|
_points = self._matrix_to_2d_list(building.foot_print.coordinates)
|
|
self._idf.add_block(name=building.name, coordinates=_points, height=building.max_height,
|
|
num_stories=int(building.storeys_above_ground))
|
|
|
|
for surface in self._idf.idfobjects[self._SURFACE]:
|
|
for thermal_zone in building.thermal_zones:
|
|
for boundary in thermal_zone.thermal_boundaries:
|
|
if surface.Type == self.idf_surfaces[boundary.surface.type]:
|
|
surface.Construction_Name = boundary.construction_name
|
|
break
|
|
for usage_zone in thermal_zone.usage_zones:
|
|
surface.Zone_Name = usage_zone.id
|
|
break
|
|
break
|
|
self._idf.intersect_match()
|
|
|
|
def _add_surfaces(self, building):
|
|
|
|
for internal_zone in building.internal_zones:
|
|
for thermal_zone in internal_zone.thermal_zones:
|
|
for boundary in thermal_zone.thermal_boundaries:
|
|
idf_surface_type = self.idf_surfaces[boundary.parent_surface.type]
|
|
for usage_zone in thermal_zone.usage_zones:
|
|
surface = self._idf.newidfobject(self._SURFACE, Name=f'{boundary.parent_surface.name}',
|
|
Surface_Type=idf_surface_type, Zone_Name=usage_zone.id,
|
|
Construction_Name=boundary.construction_name)
|
|
coordinates = self._matrix_to_list(boundary.parent_surface.solid_polygon.coordinates,
|
|
self._city.lower_corner)
|
|
surface.setcoords(coordinates)
|