city_retrofit/factories/physics_feeders/nrel_interface.py

144 lines
8.4 KiB
Python
Raw Normal View History

2021-01-05 15:01:36 -05:00
"""
Nrel-based interface, it reads format defined within the CERC team based on NREL structure
and enriches the city with archetypes and materials
SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2020 Project Author Pilar Monsalvete pilar_monsalvete@yahoo.es
"""
import xmltodict
from factories.physics_feeders.data_classes.nrel_building_achetype import NrelBuildingArchetype
from factories.physics_feeders.data_classes.nrel_thermal_boundary_archetype import NrelThermalBoundaryArchetype
from factories.physics_feeders.data_classes.nrel_thermal_opening_archetype import NrelThermalOpeningArchetype
from factories.physics_feeders.data_classes.nrel_layer_archetype import NrelLayerArchetype
class NrelInterface:
"""
NrelInterface abstract class
"""
def __init__(self, base_path, constructions_file='us_constructions.xml',
archetypes_file='us_archetypes.xml'):
# load construction Library, CERC-NREL format
path = str(base_path / constructions_file)
with open(path) as xml:
self._library = xmltodict.parse(xml.read(), force_list='layer')
# load archetypes Library, CERC-NREL format
path = str(base_path / archetypes_file)
with open(path) as xml:
self._archetypes = xmltodict.parse(xml.read(), force_list='layer')
# todo: I don't need this here
# it is assumed that all buildings have the same archetypes' keys
keys = []
archetype = self._archetypes['archetypes']['archetype'][0]
for key, value in archetype.items():
if key[0] == '@':
keys.append(key)
building_archetypes = []
for archetype in self._archetypes['archetypes']['archetype']:
archetype_keys = []
for key, value in archetype.items():
if key[0] == '@':
archetype_keys.append([key, value])
average_storey_height = archetype['average_storey_height']['#text']
storeys_above_ground = archetype['number_of_storeys']['#text']
effective_thermal_capacity = archetype['thermal_capacity']['#text']
additional_thermal_bridge_u_value = archetype['extra_loses_due_to_thermal_bridges']['#text']
indirectly_heated_area_ratio = archetype['indirect_heated_ratio']['#text']
infiltration_rate_system_off = archetype['infiltration_rate_for_ventilation_system_off']['#text']
infiltration_rate_system_on = archetype['infiltration_rate_for_ventilation_system_on']['#text']
thermal_boundary_archetypes = []
for construction in archetype['constructions']['construction']:
construction_type = construction['@type']
construction_id = construction['@id']
c_lib = self._search_construction_type('construction', construction_id)
construction_name = c_lib['@name']
layers = []
if 'layers' in c_lib:
for current_layer in c_lib['layers']['layer']:
material_lib = self._search_construction_type('material', current_layer['material'])
name = material_lib['@name']
solar_absorptance = material_lib['solar_absorptance']['#text']
thermal_absorptance = material_lib['thermal_absorptance']['#text']
visible_absorptance = material_lib['visible_absorptance']['#text']
no_mass = 'no_mass' in material_lib
if no_mass:
thermal_resistance = material_lib['thermal_resistance']['#text']
layer = NrelLayerArchetype(name, solar_absorptance, thermal_absorptance, visible_absorptance,
no_mass=no_mass, thermal_resistance=thermal_resistance)
else:
thickness = current_layer['thickness']['#text']
conductivity = material_lib['conductivity']['#text']
specific_heat = material_lib['specific_heat']['#text']
density = material_lib['density']['#text']
layer = NrelLayerArchetype(name, solar_absorptance, thermal_absorptance, visible_absorptance,
thickness = thickness, conductivity = conductivity,
specific_heat = specific_heat, density = density)
layers.append(layer)
thermal_openings = []
window_ratio = 0
if 'window' in construction and construction['window'] is not None:
window_ratio = construction['window_ratio']['#text']
w_lib = self._search_construction_type('window', construction['window'])
frame_ratio = w_lib['frame_ratio']['#text']
if 'conductivity' in w_lib:
conductivity = w_lib['conductivity']['#text']
thickness = w_lib['thickness']['#text']
g_value = w_lib['solar_transmittance_at_normal_incidence']['#text']
back_side_solar_transmittance_at_normal_incidence = \
w_lib['back_side_solar_transmittance_at_normal_incidence']['#text']
front_side_solar_transmittance_at_normal_incidence = \
w_lib['front_side_solar_transmittance_at_normal_incidence']['#text']
opening = NrelThermalOpeningArchetype(conductivity=conductivity, frame_ratio=frame_ratio, g_value=g_value,
thickness=thickness,
back_side_solar_transmittance_at_normal_incidence=
back_side_solar_transmittance_at_normal_incidence,
front_side_solar_transmittance_at_normal_incidence=
front_side_solar_transmittance_at_normal_incidence)
else:
overall_u_value = w_lib['overall_u_value']['#text']
g_value = w_lib['g_value']
opening = NrelThermalOpeningArchetype(frame_ratio=frame_ratio, g_value=g_value,
overall_u_value=overall_u_value)
thermal_openings.append(opening)
if 'outside_thermal_absorptance' in c_lib:
outside_solar_absorptance = c_lib['outside_solar_absorptance']['#text']
outside_thermal_absorptance = c_lib['outside_thermal_absorptance']['#text']
outside_visible_absorptance = c_lib['outside_visible_absorptance']['#text']
thermal_boundary_archetype = NrelThermalBoundaryArchetype(construction_type, window_ratio, construction_name,
layers, thermal_openings, outside_solar_absorptance,
outside_thermal_absorptance,
outside_visible_absorptance)
else:
if 'overall_u_value' in c_lib:
overall_u_value = c_lib['overall_u_value']['#text']
outside_solar_absorptance = c_lib['outside_solar_absorptance']['#text']
thermal_boundary_archetype = NrelThermalBoundaryArchetype(construction_type, window_ratio,
construction_name, layers, thermal_openings,
outside_solar_absorptance=
outside_solar_absorptance,
overall_u_value=overall_u_value)
else:
thermal_boundary_archetype = NrelThermalBoundaryArchetype(construction_type, window_ratio,
construction_name, layers, thermal_openings)
thermal_boundary_archetypes.append(thermal_boundary_archetype)
building_archetype = NrelBuildingArchetype(archetype_keys, average_storey_height, storeys_above_ground,
effective_thermal_capacity, additional_thermal_bridge_u_value,
indirectly_heated_area_ratio, infiltration_rate_system_off,
infiltration_rate_system_on, thermal_boundary_archetypes)
building_archetypes.append(building_archetype)
print(building_archetypes[0].archetype_keys)
def _search_construction_type(self, construction_type, construction_id):
for c_lib in self._library['library'][construction_type + 's'][construction_type]:
if construction_id == c_lib['@id']:
return c_lib
raise Exception('Archetype definition contains elements that does not exist in the library')