hub/helpers/idf_helper.py
2020-11-11 19:39:51 -05:00

146 lines
6.2 KiB
Python

"""
TestOccupancyFactory test and validate the city model structure occupancy 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
import helpers
class IdfHelper:
_THERMOSTAT = 'HVACTEMPLATE:THERMOSTAT'
_IDEAL_LOAD_AIR_SYSTEM = 'HVACTEMPLATE:ZONE:IDEALLOADSAIRSYSTEM'
_SURFACE = 'BUILDINGSURFACE:DETAILED'
idf_surfaces = {
'Wall': 'wall',
'Ground': 'floor',
'Roof': 'roof'
}
idf_usage = {
'residential': 'residential_building'
}
def __init__(self, idf_file_path, idd_file_path, epw_file_path):
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.epw = self._epw_file_path
def add_material(self, layer):
materials = self._idf.newidfobject("MATERIAL".upper())
materials.Name = layer.material.name
materials.Roughness = helpers.roughness
materials.Thickness = layer.thickness
materials.Conductivity = layer.material.conductivity
materials.Density = layer.material.density
materials.Specific_Heat = layer.material.specific_heat
materials.Thermal_Absorptance = layer.material.thermal_absorptance
materials.Solar_Absorptance = layer.material.solar_absorptance
materials.Visible_Absorptance = layer.material.visible_absorptance
def add_construction(self, thermal_boundary):
for boundary in thermal_boundary:
for layer in boundary:
if len(layer) == 2:
self._idf.newidfobject("CONSTRUCTION", Name=boundary.construction_name,
Outside_Layer=layer[0].material.name, Layer_2=layer[1].material.name)
elif len(layer) == 3:
self._idf.newidfobject("CONSTRUCTION", Name=boundary.construction_name,
Outside_Layer=layer[0].material.name, Layer_2=layer[1].material.name, Layer_3=layer[2].material.name)
elif len(layer) == 4:
self._idf.newidfobject("CONSTRUCTION", Name=boundary.construction_name,
Outside_Layer=layer[0].material.name, Layer_2=layer[1].material.name, Layer_3=layer[2].material.name, Layer_4=layer[3].material.name)
else:
print("Could not find the true construction")
def add_heating_system(self, building):
for usage_zone in building.usage_zones:
thermostat_name = f'Thermostat {building.name}'
# todo: this will fail for more than one usage zone
static_thermostat = self._idf.newidfobject(self._THERMOSTAT,
Name=thermostat_name,
Constant_Heating_Setpoint=usage_zone.heating_setpoint,
Constant_Cooling_Setpoint=usage_zone.cooling_setpoint,
)
for zone in self._idf.idfobjects['ZONE']:
if zone.Name.find(building.name) != -1:
self._idf.newidfobject(self._IDEAL_LOAD_AIR_SYSTEM,
Zone_Name=zone.Name,
Template_Thermostat_Name=static_thermostat.Name,)
@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 = IdfHelper._matrix_to_2d_list(building.foot_print.points)
self._idf.add_block(name=building.name, coordinates=_points, height=building.max_height,
num_stories=int(building.storeys_above_ground))
self.add_heating_system(building)
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 surface in zone.surfaces:
idf_surface = self.idf_surfaces[surface.type]
wall = self._idf.newidfobject(self._SURFACE, Name=f'{building.name}-{surface.name}', Surface_Type=idf_surface,
Zone_Name=zone_name)
coordinates = IdfHelper._matrix_to_list(surface.points)
wall.setcoords(coordinates)
index += 1
self.add_heating_system(building)
self._idf.intersect_match()
def run(self, output_directory, window_ratio=0.35, display_render=False, output_prefix=None, keep_file=None):
self._idf.set_default_constructions()
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"energyplus --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):
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