system_assignation/exports/formats/idf.py

279 lines
13 KiB
Python
Raw Normal View History

"""
TestOccupancyFactory test and validate the city model structure schedules parameters
SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2020 Project Author Soroush Samareh Abolhassani - soroush.samarehabolhassani@mail.concordia.ca
"""
2020-10-21 14:38:19 -04:00
from geomeppy import IDF
2021-08-11 16:16:15 -04:00
from pathlib import Path
2021-08-12 11:58:24 -04:00
class Idf:
2020-10-27 13:19:50 -04:00
_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'
_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'
2020-10-27 13:19:50 -04:00
idf_surfaces = {
2021-08-11 10:29:54 -04:00
# todo: make an enum for all the surface types
'Wall': 'wall',
'Ground': 'floor',
'Roof': 'roof'
}
idf_usage = {
2021-08-11 10:29:54 -04:00
# todo: make an enum for all the usage types
'residential': 'residential_building'
}
2020-10-21 14:38:19 -04:00
2021-08-12 11:58:24 -04:00
def __init__(self, city, output_path, idf_file_path, idd_file_path, epw_file_path, export_type="Surfaces"):
2021-08-11 10:29:54 -04:00
self._city = city
2021-08-17 12:52:48 -04:00
self._output_path = str((output_path / f'{city.name}.idf').resolve())
2021-08-12 11:58:24 -04:00
self._export_type = export_type
2020-10-22 07:55:40 -04:00
self._idd_file_path = str(idd_file_path)
self._idf_file_path = str(idf_file_path)
self._epw_file_path = str(epw_file_path)
2020-10-26 13:47:10 -04:00
IDF.setiddname(self._idd_file_path)
2020-10-26 13:33:03 -04:00
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)
2021-08-11 16:16:15 -04:00
self._export()
2021-08-11 10:29:54 -04:00
2021-08-12 11:58:24 -04:00
@staticmethod
def _matrix_to_list(points):
points_list = []
for point in points:
point_tuple = (point[0], point[1], point[2])
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
2021-08-11 15:11:00 -04:00
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,
2021-08-11 10:29:54 -04:00
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,
2021-08-11 10:29:54 -04:00
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_schedule(self, usage_zone, schedule_type, limit_name='Any Number'):
2021-08-11 16:16:15 -04:00
for schedule in self._idf.idfobjects[self._HOURLY_SCHEDULE]:
2021-08-11 12:13:16 -04:00
if schedule.Name == f'{schedule_type} schedules {usage_zone.usage}':
2021-08-11 10:29:54 -04:00
return
2021-08-25 14:29:30 -04:00
if usage_zone.schedules is None or schedule_type not in usage_zone.schedules:
# there are no schedule for this type
return
schedule = self._idf.newidfobject(self._HOURLY_SCHEDULE, Name=f'{schedule_type} schedules {usage_zone.usage}')
schedule.Schedule_Type_Limits_Name = limit_name
2021-08-11 15:11:00 -04:00
schedule.Hour_1 = usage_zone.schedules[schedule_type]["WD"][0]
schedule.Hour_2 = usage_zone.schedules[schedule_type]["WD"][1]
schedule.Hour_3 = usage_zone.schedules[schedule_type]["WD"][2]
schedule.Hour_4 = usage_zone.schedules[schedule_type]["WD"][3]
schedule.Hour_5 = usage_zone.schedules[schedule_type]["WD"][4]
schedule.Hour_6 = usage_zone.schedules[schedule_type]["WD"][5]
schedule.Hour_7 = usage_zone.schedules[schedule_type]["WD"][6]
schedule.Hour_8 = usage_zone.schedules[schedule_type]["WD"][7]
schedule.Hour_9 = usage_zone.schedules[schedule_type]["WD"][8]
schedule.Hour_10 = usage_zone.schedules[schedule_type]["WD"][9]
schedule.Hour_11 = usage_zone.schedules[schedule_type]["WD"][10]
schedule.Hour_12 = usage_zone.schedules[schedule_type]["WD"][11]
schedule.Hour_13 = usage_zone.schedules[schedule_type]["WD"][12]
schedule.Hour_14 = usage_zone.schedules[schedule_type]["WD"][13]
schedule.Hour_15 = usage_zone.schedules[schedule_type]["WD"][14]
schedule.Hour_16 = usage_zone.schedules[schedule_type]["WD"][15]
schedule.Hour_17 = usage_zone.schedules[schedule_type]["WD"][16]
schedule.Hour_18 = usage_zone.schedules[schedule_type]["WD"][17]
schedule.Hour_19 = usage_zone.schedules[schedule_type]["WD"][18]
schedule.Hour_20 = usage_zone.schedules[schedule_type]["WD"][19]
schedule.Hour_21 = usage_zone.schedules[schedule_type]["WD"][20]
schedule.Hour_22 = usage_zone.schedules[schedule_type]["WD"][21]
schedule.Hour_23 = usage_zone.schedules[schedule_type]["WD"][22]
schedule.Hour_24 = usage_zone.schedules[schedule_type]["WD"][23]
def _add_construction(self, thermal_boundary):
for construction in self._idf.idfobjects[self._CONSTRUCTION]:
if construction.Name == thermal_boundary.construction_name:
return
2021-08-12 11:58:24 -04:00
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
2021-08-11 10:29:54 -04:00
# 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)
2020-11-11 19:39:51 -05:00
2021-08-11 10:29:54 -04:00
def _add_zone(self, usage_zone):
for zone in self._idf.idfobjects['ZONE']:
if zone.Name == usage_zone.id:
return
# todo: what does we need to define a zone in energy plus?
self._idf.newidfobject(self._ZONE, Name=usage_zone.id, Volume=usage_zone.volume)
2021-08-11 10:29:54 -04:00
self._add_heating_system(usage_zone)
2020-10-22 07:55:40 -04:00
2021-08-11 10:29:54 -04:00
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:
2021-08-11 10:29:54 -04:00
return thermostat
return self._idf.newidfobject(self._THERMOSTAT,
Name=thermostat_name,
Constant_Heating_Setpoint=usage_zone.heating_setpoint,
Constant_Cooling_Setpoint=usage_zone.cooling_setpoint)
2020-10-23 15:49:04 -04:00
2021-08-11 10:29:54 -04:00
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
2021-08-11 10:29:54 -04:00
thermostat = self._add_thermostat(usage_zone)
self._idf.newidfobject(self._IDEAL_LOAD_AIR_SYSTEM,
Zone_Name=usage_zone.id,
Cooling_Availability_Schedule_Name=f'Refrigeration schedules {usage_zone.usage}',
2021-08-11 10:29:54 -04:00
Template_Thermostat_Name=thermostat.Name)
2021-08-11 15:11:00 -04:00
def _add_occupancy(self, usage_zone):
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",
2021-08-11 15:11:00 -04:00
Number_of_People=500, # todo: get people from where?
Fraction_Radiant=0.3, # todo: howto get this from InternalGains
Activity_Level_Schedule_Name='occupant schedules'
)
2021-08-11 15:11:00 -04:00
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
)
2021-08-11 15:11:00 -04:00
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=0.35, # todo: change it from usage catalog
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
)
2021-08-12 11:58:24 -04:00
def _export(self):
2021-08-11 15:11:00 -04:00
"""
Export the idf file into the given path
export type = "Surfaces|Block"
"""
2021-08-11 16:16:15 -04:00
for building in self._city.buildings:
2021-08-11 15:11:00 -04:00
for usage_zone in building.usage_zones:
self._add_schedule(usage_zone, "Infiltration")
self._add_schedule(usage_zone, "Lights")
self._add_schedule(usage_zone, "Occupancy")
self._add_schedule(usage_zone, "Refrigeration", self._ON_OFF)
2021-08-11 15:11:00 -04:00
self._add_zone(usage_zone)
self._add_heating_system(usage_zone)
# self._add_infiltration(usage_zone)
# self._add_occupancy(usage_zone)
2021-08-12 11:58:24 -04:00
for thermal_zone in building.thermal_zones:
for thermal_boundary in thermal_zone.bounded:
self._add_construction(thermal_boundary)
if self._export_type == "Surfaces":
2021-08-11 12:13:16 -04:00
self._add_surfaces(building)
2021-08-11 12:24:57 -04:00
else:
2021-08-11 12:13:16 -04:00
self._add_block(building)
2021-08-17 12:52:48 -04:00
self._idf.match()
2021-08-11 15:38:07 -04:00
self._idf.saveas(str(self._output_path))
2021-08-11 12:13:16 -04:00
2021-08-11 10:29:54 -04:00
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))
2021-08-11 15:11:00 -04:00
for surface in self._idf.idfobjects[self._SURFACE]:
for thermal_zone in building.thermal_zones:
for boundary in thermal_zone.bounded:
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
2021-08-11 10:29:54 -04:00
self._idf.intersect_match()
def _add_surfaces(self, building):
2021-08-11 15:11:00 -04:00
for thermal_zone in building.thermal_zones:
for boundary in thermal_zone.bounded:
idf_surface_type = self.idf_surfaces[boundary.surface.type]
2021-08-11 15:11:00 -04:00
for usage_zone in thermal_zone.usage_zones:
2021-08-11 15:11:00 -04:00
surface = self._idf.newidfobject(self._SURFACE, Name=f'{boundary.surface.name}',
Surface_Type=idf_surface_type, Zone_Name=usage_zone.id,
2021-08-11 15:11:00 -04:00
Construction_Name=boundary.construction_name)
coordinates = self._matrix_to_list(boundary.surface.solid_polygon.coordinates)
2021-08-11 15:11:00 -04:00
surface.setcoords(coordinates)
2021-08-17 12:52:48 -04:00