costs_workflow/main.py

139 lines
5.6 KiB
Python

"""
Costs Workflow
SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2022 Project Author Pilar Monsalvete Álvarez de Uribarri pilar.monsalvete@concordia.ca
"""
import glob
import os
from pathlib import Path
import sys
import pandas as pd
from hub.imports.construction_factory import ConstructionFactory
from hub.helpers.dictionaries import Dictionaries
from hub.hub_logger import logger
from hub.imports.geometry_factory import GeometryFactory
from hub.imports.usage_factory import UsageFactory
from hub.imports.weather_factory import WeatherFactory
from hub.catalog_factories.costs_catalog_factory import CostCatalogFactory
import hub.helpers.constants as cte
from monthly_energy_balance_engine import MonthlyEnergyBalanceEngine
from sra_engine import SraEngine
from life_cycle_costs import LifeCycleCosts
def _search_archetype(costs_catalog, building_function):
costs_archetypes = costs_catalog.entries('archetypes').archetypes
for building_archetype in costs_archetypes:
if str(building_function) == str(building_archetype.function):
return building_archetype
raise KeyError('archetype not found')
file_path = (Path(__file__).parent.parent/'costs_workflow'/'input_files'/'selected_building_2864.geojson')
climate_reference_city = 'Montreal'
weather_file = 'CAN_PQ_Montreal.Intl.AP.716270_CWEC.epw'
weather_format = 'epw'
construction_format = 'nrcan'
usage_format = 'nrcan'
attic_heated_case = 0
basement_heated_case = 1
tmp_folder = (Path(__file__).parent.parent/'monthly_energy_balance_workflow'/'tmp')
out_path = (Path(__file__).parent.parent / 'costs_workflow' / 'out_files')
files = glob.glob(f'{out_path}/*')
retrofitting_year_of_construction = 2015
for file in files:
if file != '.gitignore':
os.remove(file)
number_of_years = 30
consumer_price_index = 0.04
discount_rate = 0.03
peak_electricity_demand = 33
factor_pv = 0.5
factor_peak_lights = 0.07
retrofitting_scenarios = [0, 1, 2, 3]
life_cycle_results = pd.DataFrame()
for retrofitting_scenario in retrofitting_scenarios:
if retrofitting_scenario == 2 or retrofitting_scenario == 3:
heating_scop = 3
cooling_seer = 4.5
else:
heating_scop = 1
cooling_seer = 2.8
print('[simulation start]')
city = GeometryFactory('geojson',
path=file_path,
height_field='heightmax',
name_field='OBJECTID_12',
year_of_construction_field='ANNEE_CONS',
function_field='CODE_UTILI',
function_to_hub=Dictionaries().montreal_function_to_hub_function).city
print(f'city created from {file_path}')
city.climate_reference_city = climate_reference_city
city.climate_file = (tmp_folder / f'{climate_reference_city}.cli').resolve()
print(f'city created from {file_path}')
WeatherFactory(weather_format, city, file_name=weather_file).enrich()
print('enrich weather... done')
UsageFactory(usage_format, city).enrich()
print('enrich usage... done')
catalog = CostCatalogFactory('montreal_custom').catalog
print('costs catalog access... done')
if retrofitting_scenario == 0 or retrofitting_scenario == 2:
for building in city.buildings:
building.year_of_construction = retrofitting_year_of_construction
ConstructionFactory(construction_format, city).enrich()
print('enrich constructions... done')
# sra + monthly running
print('exporting:')
sra_file = (tmp_folder / f'{city.name}_sra.xml').resolve()
SraEngine(city, sra_file, tmp_folder, weather_file)
# Assign radiation to the city
print(' sra processed...')
for building in city.buildings:
building.attic_heated = attic_heated_case
building.basement_heated = basement_heated_case
MonthlyEnergyBalanceEngine(city, tmp_folder)
for building in city.buildings:
try:
function = Dictionaries().hub_function_to_montreal_custom_costs_function[building.function]
archetype = _search_archetype(catalog, function)
except KeyError:
logger.error(f'Building {building.name} has unknown costs archetype for building function: '
f'{building.function}\n')
sys.stderr.write(f'Building {building.name} has unknown costs archetype for building function: '
f'{building.function}\n')
continue
lcc = LifeCycleCosts(building, archetype, number_of_years, consumer_price_index,
discount_rate, retrofitting_scenario, heating_scop, cooling_seer,
peak_electricity_demand, factor_pv,factor_peak_lights)
total_capital_costs, yearly_capital_costs = lcc.calculate_capital_costs()
end_of_life_costs = lcc.calculate_end_of_life_costs()
total_operational_costs = lcc.calculate_total_operational_costs()
total_maintenance_costs = lcc.calculate_total_maintenance_costs()
life_cycle_costs = total_capital_costs + end_of_life_costs + total_operational_costs + total_maintenance_costs
life_cycle_results[f'Scenario {retrofitting_scenario}'] = [total_capital_costs, end_of_life_costs,
total_operational_costs, total_maintenance_costs,
life_cycle_costs]
life_cycle_results.index = ['total_capital_costs', 'end_of_life_costs', 'total_operational_costs',
'total_maintenance_costs', 'life_cycle_costs']
life_cycle_results.to_excel(Path(__file__).parent/'out_files'/'Results.xlsx', index=True)