Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revise network creation #40

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions scripts/build_demand.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,9 @@ def calculate_load(
# Change column names to 'bus_' + the original column number
load_df.columns = ["bus_" + str(col) for col in load_df.columns]

# Remove the bus_9 column
load_df = load_df.drop("bus_9", axis=1)
# TODO It looks like load should be fetched to the central bus
## Remove the bus_9 column
# load_df = load_df.drop("bus_9", axis=1)

# Save the microgrid load to a CSV file with snapshots index
load_df.insert(0, "snapshots", n.snapshots)
Expand Down
42 changes: 33 additions & 9 deletions scripts/create_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import pandas as pd
import pypsa
from _helpers_dist import configure_logging, read_geojson, sets_path_to_root
from geopy import distance as geodistance
from scipy.spatial import Delaunay, distance
from shapely.geometry import Polygon

Expand Down Expand Up @@ -102,15 +103,38 @@ def create_microgrid_network(

line_type = line_type

# Add lines to the network between connected buses in the Delaunay triangulation
for i, j in edges:
bus0 = n.buses.index[i]
bus1 = n.buses.index[j]
line_name = f"{microgrid_id}_line_{i}_{j}"
x1, y1 = n.buses.x[i], n.buses.y[i]
x2, y2 = n.buses.x[j], n.buses.y[j]
length = ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5
n.add("Line", line_name, bus0=bus0, bus1=bus1, type=line_type, length=length)
line_name = [f"{microgrid_id}_line_{i}_{j}" for (i, j) in edges]

edges_list = [(i, j) for (i, j) in edges]
i_bus0 = [k[0] for k in edges_list]
i_bus1 = [k[1] for k in edges_list]
# keep order of buses to evaluate length of the lines
lines_df = pd.concat(
[
n.buses.loc[n.buses.index[i_bus0], ["x", "y"]].reset_index(),
n.buses.loc[n.buses.index[i_bus1], ["x", "y"]].reset_index(),
],
axis=1,
ignore_index=True,
).set_axis(["bus0", "x0", "y0", "bus1", "x1", "y1"], axis=1)

lines_df["length"] = lines_df.apply(
lambda x: geodistance.geodesic((x["x0"], x["y0"]), (x["x1"], x["y1"])).meters
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need geopy for this?
An alternative is to make lines a geodataframe and use metric_crs to calculate the length

/ 1e3,
axis=1,
)

n.madd(
"Line",
line_name,
bus0=lines_df["bus0"].values,
bus1=lines_df["bus1"].values,
type=line_type,
length=lines_df["length"],
# Default settings imply zero s_nom and non-extandable lines
s_nom=0.1, # quick fix
# s_nom_extendable=True # makes optimisation longer
)


def add_bus_at_center(n, number_microgrids, voltage_level, line_type):
Expand Down
Loading