city_retrofit/exports/formats/idf.py

369 lines
16 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 © 2022 Concordia CERC group
Project Coder Guille Guillermo.GutierrezMorote@concordia.ca
Code contributors: Pilar Monsalvete Alvarez de Uribarri pilar.monsalvete@concordia.ca
Soroush Samareh Abolhassani soroush.samarehabolhassani@mail.concordia.ca
"""
2022-03-16 13:21:51 -04:00
from pathlib import Path
2020-10-21 14:38:19 -04:00
from geomeppy import IDF
import helpers.constants as cte
2021-08-12 11:58:24 -04:00
class Idf:
2021-08-26 13:27:43 -04:00
"""
Export city to 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'
2022-03-16 13:21:51 -04:00
_COMPACT_SCHEDULE = 'SCHEDULE:COMPACT'
_FILE_SCHEDULE = 'SCHEDULE:FILE'
_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'
2022-03-16 13:21:51 -04:00
_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
cte.WALL: 'wall',
cte.GROUND: 'floor',
cte.ROOF: 'roof'
}
idf_usage = {
2021-08-11 10:29:54 -04:00
# todo: make an enum for all the usage types
cte.RESIDENTIAL: 'residential_building'
}
2022-03-16 13:21:51 -04:00
idf_type_limits = {
cte.ON_OFF: 'on/off',
cte.FRACTION: 'Fraction',
cte.ANY_NUMBER: 'Any Number',
cte.CONTINUOUS: 'Continuous',
cte.DISCRETE: 'Discrete'
}
idf_day_types = {
cte.MONDAY: 'Monday',
cte.TUESDAY: 'Tuesday',
cte.WEDNESDAY: 'Wednesday',
cte.THURSDAY: 'Thursday',
cte.FRIDAY: 'Friday',
cte.SATURDAY: 'Saturday',
cte.SUNDAY: 'Sunday',
cte.HOLIDAY: 'Holidays',
cte.WINTER_DESIGN_DAY: 'WinterDesignDay',
cte.SUMMER_DESIGN_DAY: 'SummerDesignDay'
}
idf_schedule_types = {
'compact': 'Compact',
cte.DAY: 'Day',
cte.WEEK: 'Week',
cte.YEAR: 'Year',
'file': 'File'
}
idf_schedule_data_type = {
'compact': 'Compact',
'hourly': 'Hourly',
'daily': 'Daily',
'interval': 'Interval',
'list': 'List',
2022-03-16 13:21:51 -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
2022-03-16 13:21:51 -04:00
self._output_path = str(output_path.resolve())
self._output_file = 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
2022-03-16 13:21:51 -04:00
def _matrix_to_list(points, lower_corner):
lower_x = lower_corner[0]
lower_y = lower_corner[1]
lower_z = lower_corner[2]
2021-08-12 11:58:24 -04:00
points_list = []
for point in points:
2022-03-16 13:21:51 -04:00
point_tuple = (point[0]-lower_x, point[1]-lower_y, point[2]-lower_z)
2021-08-12 11:58:24 -04:00
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_standard_compact_hourly_schedule(self, usage, schedules):
_kwargs = {'Name': f'{schedules[0].type} schedules {usage}',
'Schedule_Type_Limits_Name': self.idf_type_limits[schedules[0].data_type],
'Field_1': 'Through: 12/31'}
for j, schedule in enumerate(schedules):
_val = schedule.values
_new_field = ''
for day_type in schedule.day_types:
_new_field += f' {self.idf_day_types[day_type]}'
_kwargs[f'Field_{j * 25 + 2}'] = f'For:{_new_field}'
for i in range(0, len(_val)):
_kwargs[f'Field_{j * 25 + 3 + i}'] = f'Until: {i + 1:02d}:00,{_val[i]}'
self._idf.newidfobject(self._COMPACT_SCHEDULE, **_kwargs)
def _add_non_hourly_schedule(self, usage, schedules):
raise NotImplementedError
def _write_schedules_file(self, usage, schedule):
file_name = str((Path(self._output_path) / f'{schedule.type} schedules {usage}.dat').resolve())
with open(file_name, 'w') as file:
for value in schedule.values:
file.write(f'{str(value)},\n')
return file_name
def _add_file_schedule(self, usage, schedule, file_name):
_schedule = self._idf.newidfobject(self._FILE_SCHEDULE, Name=f'{schedule.type} schedules {usage}')
_schedule.Schedule_Type_Limits_Name = self.idf_type_limits[schedule.data_type]
_schedule.File_Name = file_name
_schedule.Column_Number = 1
_schedule.Rows_to_Skip_at_Top = 0
_schedule.Number_of_Hours_of_Data = 8760
_schedule.Column_Separator = 'Comma'
_schedule.Interpolate_to_Timestep = 'No'
_schedule.Minutes_per_Item = 60
def _add_schedules(self, usage, new_schedules, schedule_from_file=False):
if schedule_from_file:
new_schedule = new_schedules[0]
for schedule in self._idf.idfobjects[self._FILE_SCHEDULE]:
if schedule.Name == f'{new_schedule.type} schedules {usage}':
return
file_name = self._write_schedules_file(usage, new_schedule)
return self._add_file_schedule(usage, new_schedule, file_name)
else:
for schedule in self._idf.idfobjects[self._HOURLY_SCHEDULE]:
if schedule.Name == f'{new_schedules[0].type} schedules {usage}':
return
return self._add_standard_compact_hourly_schedule(usage, new_schedules)
2022-03-16 13:21:51 -04:00
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}
2021-08-11 10:29:54 -04:00
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
def _add_zone(self, thermal_zone):
2021-08-11 10:29:54 -04:00
for zone in self._idf.idfobjects['ZONE']:
if zone.Name == thermal_zone.id:
2021-08-11 10:29:54 -04:00
return
# todo: what do we need to define a zone in energy plus?
self._idf.newidfobject(self._ZONE, Name=thermal_zone.id, Volume=thermal_zone.volume)
self._add_heating_system(thermal_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.thermal_control.mean_heating_set_point,
Constant_Cooling_Setpoint=usage_zone.thermal_control.mean_cooling_set_point)
2020-10-23 15:49:04 -04:00
def _add_heating_system(self, thermal_zone):
for air_system in self._idf.idfobjects[self._IDEAL_LOAD_AIR_SYSTEM]:
if air_system.Zone_Name == thermal_zone.id:
return
thermostat = self._add_thermostat(thermal_zone)
2021-08-11 10:29:54 -04:00
self._idf.newidfobject(self._IDEAL_LOAD_AIR_SYSTEM,
Zone_Name=thermal_zone.id,
System_Availability_Schedule_Name=f'HVAC AVAIL SCHEDULES {thermal_zone.usage}',
Heating_Availability_Schedule_Name=f'HVAC AVAIL SCHEDULES {thermal_zone.usage}',
Cooling_Availability_Schedule_Name=f'HVAC AVAIL SCHEDULES {thermal_zone.usage}',
2021-08-11 10:29:54 -04:00
Template_Thermostat_Name=thermostat.Name)
def _add_occupancy(self, thermal_zone):
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 +
thermal_zone.occupancy.latent_internal_gain)
2021-08-26 13:27:43 -04:00
self._idf.newidfobject(self._PEOPLE,
Name=f'{thermal_zone.id}_occupancy',
Zone_or_ZoneList_Name=thermal_zone.id,
Number_of_People_Schedule_Name=f'Occupancy schedules {thermal_zone.usage}',
2021-08-26 13:27:43 -04:00
Number_of_People_Calculation_Method="People",
Number_of_People=number_of_people,
Fraction_Radiant=fraction_radiant,
Activity_Level_Schedule_Name=f'Occupancy schedules {thermal_zone.usage}'
2021-08-26 13:27:43 -04:00
)
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=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
)
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"
"""
2022-03-16 13:21:51 -04:00
2021-08-11 16:16:15 -04:00
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)
usage = thermal_zone.usage
# todo: infiltration can be written with two values (system on and system off) in E+? Or just as schedule?
# self._add_schedule(usage, "Infiltration")
self._add_schedules(usage, thermal_zone.lighting.schedules)
self._add_schedules(usage, thermal_zone.occupancy.occupancy_schedules, schedule_from_file=True)
self._add_schedules(usage, thermal_zone.thermal_control.hvac_availability_schedules)
self._add_zone(thermal_zone)
self._add_heating_system(thermal_zone)
# self._add_infiltration(usage_zone)
self._add_occupancy(thermal_zone)
2021-08-12 11:58:24 -04:00
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)
2022-03-16 13:21:51 -04:00
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",
)
2021-08-17 12:52:48 -04:00
self._idf.match()
2022-03-16 13:21:51 -04:00
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}_')
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.thermal_boundaries:
2021-08-11 15:11:00 -04:00
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):
2022-03-16 13:21:51 -04:00
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]
surface = self._idf.newidfobject(self._SURFACE, Name=f'{boundary.parent_surface.name}',
Surface_Type=idf_surface_type, Zone_Name=thermal_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)