costs_workflow/unittests/test_costs_workflow.py
2023-01-28 18:25:57 -05:00

142 lines
5.7 KiB
Python

"""
TestCostsWorkflow test
SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2022 Concordia CERC group
Copyright © 2022 Project Coder Atiya atiya.atiya@mail.concordia.ca
"""
from pathlib import Path
from unittest import TestCase
import pandas as pd
import helpers.constants as cte
from helpers.monthly_values import MonthlyValues
from imports.geometry_factory import GeometryFactory
from imports.construction_factory import ConstructionFactory
from imports.usage_factory import UsageFactory
from imports.weather_factory import WeatherFactory
from peak_loads import PeakLoads
from costs_workflow.capital_cost import CapitalCost
from costs_workflow.life_cycle_costs import LifeCycleCosts
from catalog_factories.costs_catalog_factory import CostCatalogFactory
class TestPeakLoadsWorkflow(TestCase):
"""
TestPeakLoadsWorkflow class
"""
def setUp(self) -> None:
"""
Test setup
:return: None
"""
self._city = None
self._complete_city = None
self._example_path = (Path(__file__).parent / 'tests_data').resolve()
self._output_path = (Path(__file__).parent / 'tests_outputs').resolve()
def _get_citygml(self, file):
file_path = (self._example_path / file).resolve()
self._city = GeometryFactory('citygml', path=file_path).city
self.assertIsNotNone(self._city, 'city is none')
return self._city
@property
def _read_sra_file(self) -> []:
path = (self._example_path / "one_building_in_kelowna_sra_SW.out").resolve()
_results = pd.read_csv(path, sep='\s+', header=0)
id_building = ''
header_building = []
_radiation = []
for column in _results.columns.values:
if id_building != column.split(':')[1]:
id_building = column.split(':')[1]
if len(header_building) > 0:
_radiation.append(pd.concat([MonthlyValues().month_hour, _results[header_building]], axis=1))
header_building = [column]
else:
header_building.append(column)
_radiation.append(pd.concat([MonthlyValues().month_hour, _results[header_building]], axis=1))
return _radiation
def _set_irradiance_surfaces(self, city, irradiance_format):
"""
saves in building surfaces the correspondent irradiance at different time-scales depending on the mode
if building is None, it saves all buildings' surfaces in file, if building is specified, it saves only that
specific building values
:parameter city: city
:return: none
"""
for radiation in self._read_sra_file:
city_object_name = radiation.columns.values.tolist()[1].split(':')[1]
building = city.city_object(city_object_name)
for column in radiation.columns.values:
if column == cte.MONTH:
continue
header_id = column
surface_id = header_id.split(':')[2]
surface = building.surface_by_id(surface_id)
new_value = pd.DataFrame(radiation[[header_id]].to_numpy(), columns=[irradiance_format])
surface.global_irradiance[cte.HOUR] = new_value
def _enrich_city(self, city, weather_file, weather_format, irradiance_format, construction_format, usage_format):
WeatherFactory(weather_format, city, file_name=weather_file).enrich()
self._set_irradiance_surfaces(city, irradiance_format)
for building in city.buildings:
building.year_of_construction = 2006
if building.function is None:
building.function = cte.LARGE_OFFICE
ConstructionFactory(construction_format, city).enrich()
UsageFactory(usage_format, city).enrich()
def test_workflow(self):
outputs_path = (Path(__file__).parent / 'tests_outputs').resolve()
gml_file = 'one_building_in_kelowna.gml'
city = self._get_citygml(gml_file)
weather_file = 'CAN_PQ_Montreal.Intl.AP.716270_CWEC.epw'
weather_format = 'epw'
irradiance_format = 'sra'
construction_format = 'nrel'
usage_format = 'comnet'
number_of_years = 40
consumer_price_index = 0.1
discount_rate = 2.5
self._enrich_city(city, weather_file, weather_format, irradiance_format, construction_format, usage_format)
# PeakLoads(city, outputs_path, weather_format, irradiance_format)
municipality = "montreal"
catalog = CostCatalogFactory('montreal_catalog').catalog
content = catalog.entries()
for building in city.buildings:
building_volume = 0.0
building_area = 0.0
total_opaque_area = 0.0
total_transparent_area = 0.0
for internal_zone in building.internal_zones:
for thermal_zone in internal_zone.thermal_zones:
for thermal_boundary in thermal_zone.thermal_boundaries:
if thermal_boundary.opaque_area is not None:
total_opaque_area += thermal_boundary.opaque_area
if thermal_boundary.windows_areas is not None:
total_transparent_area += thermal_boundary.windows_areas
building_area += internal_zone.area
building_volume += internal_zone.volume
bulding_name, heating_load, cooling_load = PeakLoads(city, outputs_path, weather_format, irradiance_format)._results[0]
print(bulding_name, heating_load, cooling_load)
capital_costs_at_year_0 = CapitalCost.calculate_capital_cost(building_area, municipality, building_volume, total_opaque_area, total_transparent_area, content, heating_load, cooling_load, building.floor_area)
print("capital_costs_at_year_0 ", capital_costs_at_year_0)
# end_of_life_cost = 0.0
# items = []
# fuels = city.fuels
# concepts = []
# LifeCycleCosts(city, number_of_years, consumer_price_index, discount_rate, end_of_life_cost,
# capital_costs_at_year_0, items, fuels, concepts).calculate_capital_costs
# print(PeakLoads(city, outputs_path, weather_format, irradiance_format)._results[][1])