energy_system_modelling_wor.../varin.py

146 lines
5.2 KiB
Python
Raw Permalink Normal View History

2024-10-21 07:56:48 -04:00
from scripts.district_heating_network.directory_manager import DirectoryManager
import subprocess
from building_modelling.ep_run_enrich import energy_plus_workflow
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
from hub.imports.results_factory import ResultFactory
from building_modelling.geojson_creator import process_geojson
import hub.helpers.constants as cte
from hub.exports.exports_factory import ExportsFactory
import matplotlib.pyplot as plt
import numpy as np
from scripts.district_heating_network.district_heating_network_creator import DistrictHeatingNetworkCreator
from scripts.district_heating_network.district_heating_factory import DistrictHeatingFactory
import json
# Manage File Path
base_path = "./"
dir_manager = DirectoryManager(base_path)
# Input files directory
input_files_path = dir_manager.create_directory('input_files')
geojson_file_path = input_files_path / 'Varin.geojson'
pipe_data_file = input_files_path / 'pipe_data.json'
# Output files directory
output_path = dir_manager.create_directory('out_files')
# Subdirectories for output files
energy_plus_output_path = dir_manager.create_directory('out_files/energy_plus_outputs')
simulation_results_path = dir_manager.create_directory('out_files/simulation_results')
sra_output_path = dir_manager.create_directory('out_files/sra_outputs')
cost_analysis_output_path = dir_manager.create_directory('out_files/cost_analysis')
# Create ciry and run energyplus workflow
city = GeometryFactory(file_type='geojson',
path=geojson_file_path,
height_field='maximum_roof_height',
year_of_construction_field='year_built',
function_field='building_type',
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)
ResultFactory('energy_plus_multiple_buildings', city, energy_plus_output_path).enrich()
# District Heating Network Creator
central_plant_locations = [(-73.6641097164314, 45.433637169890005)] # Add at least one location
roads_file = "input_files/roads.json"
dhn_creator = DistrictHeatingNetworkCreator(geojson_file_path, roads_file, central_plant_locations)
network_graph = dhn_creator.run()
# Pipe and pump sizing
with open(pipe_data_file, 'r') as f:
pipe_data = json.load(f)
factory = DistrictHeatingFactory(
city=city,
graph=network_graph,
supply_temperature=80 + 273, # in Kelvin
return_temperature=60 + 273, # in Kelvin
simultaneity_factor=0.9
)
factory.enrich()
factory.sizing()
factory.calculate_diameters_and_costs(pipe_data)
pipe_groups, total_cost = factory.analyze_costs()
# Save the pipe groups with total costs to a CSV file
factory.save_pipe_groups_to_csv('pipe_groups.csv')
import json
import matplotlib.pyplot as plt
import networkx as nx
from shapely.geometry import shape, Polygon, MultiPolygon
from shapely.validation import explain_validity
# Load the GeoJSON file
with open("input_files/Varin.geojson", "r") as file:
geojson_data = json.load(file)
# Initialize a figure
fig, ax = plt.subplots(figsize=(15, 10))
# Set aspect to equal
ax.set_aspect('equal')
# Set figure and axes backgrounds to transparent
fig.patch.set_facecolor('none')
ax.set_facecolor('none')
# Iterate over each feature in the GeoJSON
for feature in geojson_data['features']:
geom = shape(feature['geometry'])
# Check if the geometry is valid
if not geom.is_valid:
print(f"Invalid geometry: {explain_validity(geom)}")
continue # Skip invalid geometries
# Plot polygons
if isinstance(geom, Polygon):
x, y = geom.exterior.xy
ax.plot(x, y, color='black', linewidth=1) # Plot only the edges
elif isinstance(geom, MultiPolygon):
for poly in geom.geoms:
x, y = poly.exterior.xy
ax.plot(x, y, color='black', linewidth=1) # Plot only the edges
else:
print(f"Unhandled geometry type: {type(geom)}")
continue
# Prepare and plot the network graph
G = network_graph
# Get positions from node attributes
pos = nx.get_node_attributes(G, 'pos')
# Draw nodes, using different colors based on node type
node_types = nx.get_node_attributes(G, 'type')
colors = {'building': 'blue', 'generation': 'green', 'junction': 'red'}
node_colors = [colors.get(node_types.get(node, 'junction'), 'black') for node in G.nodes()]
nx.draw_networkx_nodes(G, pos, ax=ax, node_size=50, node_color=node_colors)
nx.draw_networkx_edges(G, pos, ax=ax, edge_color="red", width=1.2)
# Remove axes, ticks, and background
ax.axis("off")
# Adjust plot limits to the data
x_values = [coord[0] for coord in pos.values()]
y_values = [coord[1] for coord in pos.values()]
ax.set_xlim(min(x_values) - 0.001, max(x_values) + 0.001)
ax.set_ylim(min(y_values) - 0.001, max(y_values) + 0.001)
# Save the plot with transparency
plt.savefig("network_plot.png", bbox_inches='tight', pad_inches=0, transparent=True)
plt.show()