system_assignation/exports/formats/idf.py
2021-08-11 10:29:54 -04:00

282 lines
13 KiB
Python

"""
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
"""
from geomeppy import IDF
import os
import esoreader
from pathlib import Path
from city_model_structure.city import City
class Idf:
_THERMOSTAT = 'HVACTEMPLATE:THERMOSTAT'
_IDEAL_LOAD_AIR_SYSTEM = 'HVACTEMPLATE:ZONE:IDEALLOADSAIRSYSTEM'
_SURFACE = 'BUILDINGSURFACE:DETAILED'
_CONSTRUCTION = 'CONSTRUCTION'
_MATERIAL = 'MATERIAL'
_MATERIAL_NOMASS = 'MATERIAL:NOMASS'
_ROUGHNESS = "MediumRough"
_OCCUPANCY_HOURLY_SCHEDULE = "SCHEDULE:DAY:HOURLY"
_ZONE = "ZONE"
_LIGHTS = "LIGHTS"
idf_surfaces = {
# todo: make an enum for all the surface types
'Wall': 'wall',
'Ground': 'floor',
'Roof': 'roof'
}
idf_usage = {
# todo: make an enum for all the usage types
'residential': 'residential_building'
}
def __init__(self, city, idf_file_path, idd_file_path, epw_file_path):
self._city = city
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)
def _add_components(self, building):
for zone in building.usage_zones:
self._add_occupancy_schedule(zone)
self._add_heating_system(zone)
for zone in building.thermal_zones:
for boundary in zone.bounded:
self._add_construction(boundary)
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_occupancy_schedule(self, usage_zone):
for schedule in self._idf.idfobjects[self._OCCUPANCY_HOURLY_SCHEDULE]:
if schedule.Name == f'occupant schedules {usage_zone.usage}':
return
schedule_occupancy = self._idf.newidfobject(self._OCCUPANCY_HOURLY_SCHEDULE)
schedule_occupancy.Name = f'occupant schedules {usage_zone.usage}'
schedule_occupancy.Hour_1 = usage_zone.occupants.occupant_schedule[0]
schedule_occupancy.Hour_2 = usage_zone.occupants.occupant_schedule[1]
schedule_occupancy.Hour_3 = usage_zone.occupants.occupant_schedule[2]
schedule_occupancy.Hour_4 = usage_zone.occupants.occupant_schedule[3]
schedule_occupancy.Hour_5 = usage_zone.occupants.occupant_schedule[4]
schedule_occupancy.Hour_6 = usage_zone.occupants.occupant_schedule[5]
schedule_occupancy.Hour_7 = usage_zone.occupants.occupant_schedule[6]
schedule_occupancy.Hour_8 = usage_zone.occupants.occupant_schedule[7]
schedule_occupancy.Hour_9 = usage_zone.occupants.occupant_schedule[8]
schedule_occupancy.Hour_10 = usage_zone.occupants.occupant_schedule[9]
schedule_occupancy.Hour_11 = usage_zone.occupants.occupant_schedule[10]
schedule_occupancy.Hour_12 = usage_zone.occupants.occupant_schedule[11]
schedule_occupancy.Hour_13 = usage_zone.occupants.occupant_schedule[12]
schedule_occupancy.Hour_14 = usage_zone.occupants.occupant_schedule[13]
schedule_occupancy.Hour_15 = usage_zone.occupants.occupant_schedule[14]
schedule_occupancy.Hour_16 = usage_zone.occupants.occupant_schedule[15]
schedule_occupancy.Hour_17 = usage_zone.occupants.occupant_schedule[16]
schedule_occupancy.Hour_18 = usage_zone.occupants.occupant_schedule[17]
schedule_occupancy.Hour_19 = usage_zone.occupants.occupant_schedule[18]
schedule_occupancy.Hour_20 = usage_zone.occupants.occupant_schedule[19]
schedule_occupancy.Hour_21 = usage_zone.occupants.occupant_schedule[20]
schedule_occupancy.Hour_22 = usage_zone.occupants.occupant_schedule[21]
schedule_occupancy.Hour_23 = usage_zone.occupants.occupant_schedule[22]
schedule_occupancy.Hour_24 = usage_zone.occupants.occupant_schedule[23]
def _add_construction(self, thermal_boundary):
for construction in self._idf.idfobjects[self._CONSTRUCTION]:
if construction.Name == thermal_boundary.construction_name:
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):
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)
self._add_heating_system(usage_zone)
lighting = self._add_lighting(usage_zone)
lighting.Zone_or_ZoneList_Name= usage_zone.id,
def _add_thermostat(self, usage_zone):
thermostat_name = f'Thermostat {usage_zone.usage}'
for thermostat in self._idf.idfobjects[self._THERMOSTAT]:
if thermostat == thermostat_name:
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)
def _add_heating_system(self, usage_zone):
thermostat = self._add_thermostat(usage_zone)
# todo: doesn't the air system have name?
self._idf.newidfobject(self._IDEAL_LOAD_AIR_SYSTEM,
Zone_Name=usage_zone.id,
Template_Thermostat_Name=thermostat.Name)
def _add_lighting(self, usage_zone):
for lights in self._idf.idfobjects[self._LIGHTS]:
if lights.Name == f'{usage_zone.usage}_lighting':
return
self._idf.newidfobject(self._LIGHTS,
Name=f'{usage_zone.id}_lighting',
Schedule_Name="Lighting_Weekday", # todo: from where?
Design_Level_Calculation_Method='Watts/Area', # todo: to constant
Watts_per_Zone_Floor_Area=15, # todo: from usage library
Return_Air_Fraction=0, # todo: to constant/document it.
Fraction_Radiant=0.7, # todo: from usage library
Fraction_Visible=0.2, # todo: ????
Fraction_Replaceable=1, # todo: to constant/document it.
Return_Air_Fraction_Calculated_from_Plenum_Temperature='No' # todo: to constant/document it.
)
def add_occupancy(self):
for zone in self._idf.idfobjects["ZONE"]:
self._idf.newidfobject("PEOPLE",
Name=zone.Name + "_" + "schedules",
Zone_or_ZoneList_Name=zone.Name,
Number_of_People_Schedule_Name='occupant schedules',
Number_of_People_Calculation_Method="People",
Number_of_People=500,
Fraction_Radiant=0.3,
Activity_Level_Schedule_Name='occupant schedules'
)
def add_equipment(self):
for zone in self._idf.idfobjects["ZONE"]:
self._idf.newidfobject("ELECTRICEQUIPMENT",
Name=zone.Name + "_" + 'electricload',
Zone_or_ZoneList_Name=zone.Name,
Schedule_Name='ElectricalEquipment',
Design_Level_Calculation_Method='EquipmentLevel',
Design_Level=566000
)
def add_infiltration(self):
for zone in self._idf.idfobjects["ZONE"]:
self._idf.newidfobject("ZONEINFILTRATION:DESIGNFLOWRATE",
Name=zone.Name + "_" + "infiltration",
Zone_or_ZoneList_Name=zone.Name,
Schedule_Name='Infiltration_schedule',
Design_Flow_Rate_Calculation_Method='AirChanges/Hour',
Air_Changes_per_Hour=0.35,
Constant_Term_Coefficient=0.606,
Temperature_Term_Coefficient=3.6359996E-02,
Velocity_Term_Coefficient=0.1177165,
Velocity_Squared_Term_Coefficient=0.0000000E+00
)
def run(self, energy_plus_path, output_directory, window_ratio=0.35, display_render=False, output_prefix=None,
keep_file=None):
# todo: doesn't really make sense to run energy+ from here, should just return the path to files.
self._idf.set_default_constructions()
# todo: calculate window ratio in the workflow
self._idf.set_wwr(window_ratio, construction="Project External Window")
self._idf.translate_to_origin()
if display_render:
self._idf.view_model()
# Run
self._idf.newidfobject("OUTPUT:METER", Key_Name="Heating:DistrictHeating", Reporting_Frequency="hourly")
self._idf.newidfobject("OUTPUT:METER", Key_Name="Cooling:DistrictCooling", Reporting_Frequency="hourly")
idf_path = None
if keep_file is not None:
idf_path = (keep_file / 'in.idf').resolve()
self._idf.saveas(str(idf_path))
if idf_path is None:
idf_path = (Path(__file__).parent / 'in.idf').resolve()
# There is a bug in the IDF class, when called, it return an error, as a work around we call call energy+ directly
run_command = f"{energy_plus_path} --weather {self._epw_file_path} --output-directory {output_directory} --idd " \
f"{self._idd_file_path} --expandobjects --output-prefix {output_prefix} {idf_path}"
os.system(run_command)
if keep_file is None:
os.remove(idf_path)
return
@staticmethod
def read_eso(eso_file_path):
# todo: the heating and cooling values need to go into the city_model_structure
dd, data = esoreader.read(eso_file_path)
list_values = [v for v in data.values()]
heating = [(float(x)) / 3600000.0 for x in list_values[0]]
cooling = [(float(x)) / 3600000.0 for x in list_values[1]]
return heating, cooling
@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
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))
self._idf.intersect_match()
def _add_surfaces(self, building):
index = 0
for zone in building.thermal_zones:
zone_name = f'Building {building.name} usage zone {index}'
self._idf.newidfobject('ZONE', Name=zone_name)
for boundary in zone.bounded:
self._add_construction(boundary)
idf_surface = self.idf_surfaces[boundary.surface.type]
wall = self._idf.newidfobject(self._SURFACE, Name=f'{building.name}-{boundary.surface.name}',
Surface_Type=idf_surface, Zone_Name=zone_name,
Construction_Name=boundary.construction_name)
coordinates = self._matrix_to_list(boundary.surface.coordinates)
wall.setcoords(coordinates)
index += 1
self._add_heating_system(building)
self._idf.intersect_match()