import datetime import psycopg2 from pymongo import MongoClient from co2_emission.co2_emission import Co2Emission from costs.cost import Cost from building import Building from city_object import CityObject from hub.catalog_factories.energy_systems_catalog_factory import EnergySystemsCatalogFactory def calculate_building(building_info, building_results, scenario_id): energy_systems_catalog = EnergySystemsCatalogFactory('montreal_custom').catalog archetype = energy_systems_catalog.get_entry(building_info.system_name) mockup_building = Building(building_info, building_results, archetype) life_cycle = Cost(mockup_building, retrofit_scenario=scenario_id).life_cycle operational_co2 = Co2Emission(mockup_building).operational_co2 global_capital_costs = life_cycle[f'Scenario {scenario_id}']['global_capital_costs'] global_operational_costs = life_cycle[f'Scenario {scenario_id}']['global_operational_costs'] global_capital_incomes = life_cycle[f'Scenario {scenario_id}']['global_capital_incomes'] global_maintenance_costs = life_cycle[f'Scenario {scenario_id}']['global_maintenance_costs'] building_results['total_heating_area'] = building_info.total_heating_area building_results['year_of_construction'] = building_info.year_of_construction building_results['function'] = building_info.function building_results['costs'] = { 'total_capital_costs_skin': life_cycle[f'Scenario {scenario_id}']['total_capital_costs_skin'], 'total_capital_costs_systems': life_cycle[f'Scenario {scenario_id}']['total_capital_costs_systems'], 'end_of_life_costs': life_cycle[f'Scenario {scenario_id}']['end_of_life_costs'], 'total_operational_costs': life_cycle[f'Scenario {scenario_id}']['total_operational_costs'], 'total_maintenance_costs': life_cycle[f'Scenario {scenario_id}']['total_maintenance_costs'], 'operational_incomes': life_cycle[f'Scenario {scenario_id}']['operational_incomes'], 'capital_incomes': life_cycle[f'Scenario {scenario_id}']['capital_incomes'], 'global_capital_costs': { 'B2010_opaque_walls': global_capital_costs['B2010_opaque_walls'].tolist(), 'B2020_transparent': global_capital_costs['B2020_transparent'].tolist(), 'B3010_opaque_roof': global_capital_costs['B3010_opaque_roof'].tolist(), 'B10_superstructure': global_capital_costs['B10_superstructure'].tolist(), 'D3020_heat_generating_systems': global_capital_costs['D3020_heat_generating_systems'].tolist(), 'D3030_cooling_generation_systems': global_capital_costs['D3030_cooling_generation_systems'].tolist(), 'D3080_other_hvac_ahu': global_capital_costs['D3080_other_hvac_ahu'].tolist(), 'D5020_lighting_and_branch_wiring': global_capital_costs['D5020_lighting_and_branch_wiring'].tolist(), 'D301010_photovoltaic_system': global_capital_costs['D301010_photovoltaic_system'].tolist(), }, 'global_end_of_life_costs': life_cycle[f'Scenario {scenario_id}']['global_end_of_life_costs'][ 'End_of_life_costs'].tolist(), 'global_operational_costs': { 'fixed_costs_electricity_peak': global_operational_costs['Fixed_costs_electricity_peak'].tolist(), 'fixed_costs_electricity_monthly': global_operational_costs['Fixed_costs_electricity_monthly'].tolist(), 'variable_costs_electricity': global_operational_costs['Variable_costs_electricity'].tolist(), 'fixed_costs_gas': global_operational_costs['Fixed_costs_gas'].tolist(), 'variable_costs_gas': global_operational_costs['Variable_costs_gas'].tolist() }, 'global_maintenance_costs': { 'heating_maintenance': global_maintenance_costs['Heating_maintenance'].tolist(), 'cooling_maintenance': global_maintenance_costs['Cooling_maintenance'].tolist(), 'pv_maintenance': global_maintenance_costs['PV_maintenance'].tolist(), }, 'global_operational_incomes': life_cycle[f'Scenario {scenario_id}']['global_operational_incomes'][ 'Incomes electricity'].tolist(), 'global_capital_incomes': { 'subsidies_construction': global_capital_incomes['Subsidies construction'].tolist(), 'subsidies_hvac': global_capital_incomes['Subsidies HVAC'].tolist(), 'subsidies_pv': global_capital_incomes['Subsidies PV'].tolist() } } building_results['operational_co2'] = operational_co2 start = datetime.datetime.now() scenario_ids = {'current status': 0, 'skin retrofit': 1, 'system retrofit and pv': 2, 'skin and system retrofit with pv': 3 } connection = psycopg2.connect(database='montreal_retrofit', user='retrofit', password='C3rcIT$vc#', host='127.0.0.1', port=5432) cursor = connection.cursor() client = MongoClient('mongodb://localhost:27017/') montreal_retrofit_db = client.montreal_retrofit meb_collection = montreal_retrofit_db.me cursor.execute("SELECT id, scenario FROM city;") cities = cursor.fetchall() for city in cities: cursor.execute( f'SELECT DISTINCT id, name, aliases, year_of_construction, function, usage, volume, area, total_heating_area, ' f'wall_area, windows_area, roof_area, total_pv_area, system_name FROM city_object WHERE city_id = {city[0]}') buildings = cursor.fetchall() for building in buildings: cursor.execute(f'SELECT values FROM simulation_results WHERE city_object_id = {building[0]};') values = cursor.fetchall() for value in values: building_data = {'city_id': city[0], 'name': building[1], 'alias': building[2].split(',')[0][1:], 'year_of_construction': building[3], 'function': building[4], 'usage': building[5], 'volume': building[6], 'area': building[7], 'total_heating_area': building[8], 'wall_area': building[9], 'windows_area': building[10], 'roof_area': building[11], 'total_pv_area': building[12], 'system_name': building[13], 'scenario': city[1]} city_object = CityObject(building_data) results = values[0][0] try: calculate_building(city_object, results, scenario_ids[city[1]]) building_data['insel meb'] = results['insel meb'] building_data['costs'] = results['costs'] building_data['operational_co2'] = results['operational_co2'] try: montreal_retrofit_db[f'meb_{city[1].replace(" ", "_")}'].insert_one(building_data) except: pass except KeyError: pass print(f'Completed in: {datetime.datetime.now() - start}')