energy_system_modelling_wor.../test.py

85 lines
4.8 KiB
Python

import csv
from pathlib import Path
from building_modelling.ep_run_enrich import energy_plus_workflow
from energy_system_modelling_package.energy_system_modelling_factories.energy_system_sizing_factory import \
EnergySystemsSizingFactory
from energy_system_modelling_package.energy_system_modelling_factories.hvac_dhw_systems_simulation_models.heat_pump_boiler_tes_heating import \
HeatPumpBoilerTesHeating
from hub.imports.geometry_factory import GeometryFactory
from hub.helpers.dictionaries import Dictionaries
from hub.imports.construction_factory import ConstructionFactory
from hub.imports.usage_factory import UsageFactory
from hub.imports.weather_factory import WeatherFactory
import hub.helpers.constants as cte
from building_modelling.geojson_creator import process_geojson
from energy_system_modelling_package import random_assignation
from hub.imports.energy_systems_factory import EnergySystemsFactory
# Specify the GeoJSON file path
input_files_path = (Path(__file__).parent / 'input_files')
input_files_path.mkdir(parents=True, exist_ok=True)
geojson_file = process_geojson(x=-73.5681295982132, y=45.49218262677643, diff=0.00006)
geojson_file_path = input_files_path / 'output_buildings.geojson'
output_path = (Path(__file__).parent / 'out_files').resolve()
output_path.mkdir(parents=True, exist_ok=True)
energy_plus_output_path = output_path / 'energy_plus_outputs'
energy_plus_output_path.mkdir(parents=True, exist_ok=True)
simulation_results_path = (Path(__file__).parent / 'out_files' / 'simulation_results').resolve()
simulation_results_path.mkdir(parents=True, exist_ok=True)
sra_output_path = output_path / 'sra_outputs'
sra_output_path.mkdir(parents=True, exist_ok=True)
cost_analysis_output_path = output_path / 'cost_analysis'
cost_analysis_output_path.mkdir(parents=True, exist_ok=True)
city = GeometryFactory(file_type='geojson',
path=geojson_file_path,
height_field='height',
year_of_construction_field='year_of_construction',
function_field='function',
function_to_hub=Dictionaries().montreal_function_to_hub_function).city
ConstructionFactory('nrcan', city).enrich()
UsageFactory('nrcan', city).enrich()
WeatherFactory('epw', city).enrich()
energy_plus_workflow(city, energy_plus_output_path)
random_assignation.call_random(city.buildings, random_assignation.residential_new_systems_percentage)
EnergySystemsFactory('montreal_future', city).enrich()
EnergySystemsSizingFactory('peak_load_sizing', city).enrich()
hp = city.buildings[0].energy_systems[1].generation_systems[1]
boiler = city.buildings[0].energy_systems[1].generation_systems[0]
tes = city.buildings[0].energy_systems[1].generation_systems[0].energy_storage_systems[0]
# Step 1: Calculate daily demands
daily_demands = [sum(city.buildings[0].heating_demand[cte.HOUR][i:i + 24]) for i in
range(0, len(city.buildings[0].heating_demand[cte.HOUR]), 24)]
# Step 2: Find the day with maximum demand
max_day_index = daily_demands.index(max(daily_demands))
# Step 3: Extract the hourly demands for the day before, the day with max demand, and the day after
# Ensure you don't go out of bounds if max_day_index is 0 or the last day
if max_day_index > 0 and max_day_index < len(daily_demands) - 1:
start_index = (max_day_index - 1) * 24
end_index = (max_day_index + 2) * 24
three_day_demands = city.buildings[0].heating_demand[cte.HOUR][start_index:end_index]
elif max_day_index == 0: # If max is the first day, just include day 1 and day 2
start_index = max_day_index * 24
end_index = (max_day_index + 2) * 24
three_day_demands = city.buildings[0].heating_demand[cte.HOUR][start_index:end_index]
else: # If max is the last day, just include the last two days
start_index = (max_day_index - 1) * 24
end_index = len(city.buildings[0].heating_demand[cte.HOUR])
three_day_demands = city.buildings[0].heating_demand[cte.HOUR][start_index:end_index]
results = HeatPumpBoilerTesHeating(hp=hp,
boiler=boiler,
tes=tes,
hourly_heating_demand_joules=three_day_demands,
heating_peak_load_watts=max(three_day_demands),
upper_limit_tes=55,
outdoor_temperature=city.buildings[0].external_temperature[cte.HOUR],
dt=900).simulation()
heating_eui = city.buildings[0].heating_demand[cte.YEAR][0] / (
city.buildings[0].thermal_zones_from_internal_zones[0].total_floor_area * 3.6e6)
file_name = f'energy_system_simulation_results_{city.buildings[0].name}.csv'
with open(simulation_results_path / file_name, 'w', newline='') as csvfile:
output_file = csv.writer(csvfile)
# Write header
output_file.writerow(results.keys())
# Write data
output_file.writerows(zip(*results.values()))