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

Fixes/#660 fix ehv clustering #676

Merged
merged 17 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 16 additions & 2 deletions etrago/appl.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@
"extra_functionality": {}, # Choose function name or {}
# Spatial Complexity:
"delete_dispensable_ac_buses": True, # bool. Find and delete expendable buses
"network_clustering_ehv": False, # clustering of HV buses to EHV buses
"network_clustering_ehv": {
"active": False, # choose if clustering of HV buses to EHV buses is activated
"busmap": False, # False or path to stored busmap
},
"network_clustering": {
"active": True, # choose if clustering is activated
"method": "kmedoids-dijkstra", # choose clustering method: kmeans or kmedoids-dijkstra
Expand Down Expand Up @@ -390,11 +393,22 @@ def run_etrago(args, json_path):
connected lines are merged. This reduces the spatial complexity without
losing any accuracy.
Default: True.
network_clustering_ehv : bool
network_clustering_ehv : dict
Choose if you want to apply an extra high voltage clustering to the
electrical network.
The provided dictionary can have the following entries:

* "active" : bool
Choose if you want to cluster the full HV/EHV dataset down to only the
EHV buses. In that case, all HV buses are assigned to their closest EHV
substation, taking into account the shortest distance on power lines.
Default: False.
* "busmap" : str
Choose if an stored busmap can be used to make the process quicker, or
a new busmap must be calculated. False or path to the busmap in csv
format should be given.
Default: False

network_clustering : dict
Choose if you want to apply a clustering of all network buses and
specify settings.
Expand Down
5 changes: 4 additions & 1 deletion etrago/args.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@
"generator_noise": 789456,
"extra_functionality": {},
"delete_dispensable_ac_buses": true,
"network_clustering_ehv": false,
"network_clustering_ehv": {
"active": false,
"busmap": false,
},
"network_clustering": {
"active": true,
"method": "kmedoids-dijkstra",
Expand Down
20 changes: 7 additions & 13 deletions etrago/cluster/electrical.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,6 @@ def cluster_on_extra_high_voltage(etrago, busmap, with_time=True):
etrago, busmap, cluster_met="ehv"
)

pd.DataFrame(busmap.items(), columns=["bus0", "bus1"]).to_csv(
"ehv_elecgrid_busmap_result.csv",
index=False,
)

buses = aggregatebuses(
network,
busmap,
Expand Down Expand Up @@ -381,7 +376,6 @@ def cluster_on_extra_high_voltage(etrago, busmap, with_time=True):
io.import_series_from_dataframe(network_c, df, one_port, attr)

network_c.links, network_c.links_t = group_links(network_c)

network_c.determine_network_topology()

return (network_c, busmap)
Expand Down Expand Up @@ -414,10 +408,10 @@ def delete_ehv_buses_no_lines(network):
buses_ac["with_gen"] = buses_ac.index.isin(network.generators.bus)

delete_buses = buses_ac[
(buses_ac["with_line"] is False)
& (buses_ac["with_load"] is False)
& (buses_ac["with_link"] is False)
& (buses_ac["with_gen"] is False)
(buses_ac["with_line"] == False)
& (buses_ac["with_load"] == False)
& (buses_ac["with_link"] == False)
& (buses_ac["with_gen"] == False)
].index

if len(delete_buses):
Expand Down Expand Up @@ -455,8 +449,8 @@ def ehv_clustering(self):
"""
Cluster the network based on Extra High Voltage (EHV) grid.

If `network_clustering_ehv` argument is True, the function clusters the
network based on the EHV grid.
If 'active' in the `network_clustering_ehv` argument is True, the function
clusters the network based on the EHV grid.

Parameters
----------
Expand All @@ -468,7 +462,7 @@ def ehv_clustering(self):
None
"""

if self.args["network_clustering_ehv"]:
if self.args["network_clustering_ehv"]["active"]:
logger.info("Start ehv clustering")

self.network.generators.control = "PV"
Expand Down
84 changes: 19 additions & 65 deletions etrago/cluster/spatial.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ def shortest_path(paths, graph):
return df


def busmap_by_shortest_path(etrago, scn_name, fromlvl, tolvl, cpu_cores=4):
def busmap_by_shortest_path(etrago, fromlvl, tolvl, cpu_cores=4):
"""
Creates a busmap for the EHV-Clustering between voltage levels based
on dijkstra shortest path. The result is automatically written to the
Expand All @@ -379,8 +379,6 @@ def busmap_by_shortest_path(etrago, scn_name, fromlvl, tolvl, cpu_cores=4):
Container for all network components.
session : sqlalchemy.orm.session.Session object
Establishes interactions with the database.
scn_name : str
Name of the scenario.
fromlvl : list
List of voltage-levels to cluster.
tolvl : list
Expand All @@ -393,8 +391,6 @@ def busmap_by_shortest_path(etrago, scn_name, fromlvl, tolvl, cpu_cores=4):
None
"""

# cpu_cores = mp.cpu_count()

# data preperation
s_buses = buses_grid_linked(etrago.network, fromlvl)
lines = connected_grid_lines(etrago.network, s_buses)
Expand Down Expand Up @@ -476,25 +472,11 @@ def busmap_by_shortest_path(etrago, scn_name, fromlvl, tolvl, cpu_cores=4):
df = pd.concat([df, tofill], ignore_index=True, axis=0)
df.drop_duplicates(inplace=True)

# prepare data for export

df["scn_name"] = scn_name
df["version"] = etrago.args["gridversion"]

if not df.version.any():
df.version = "testcase"

df.rename(columns={"source": "bus0", "target": "bus1"}, inplace=True)
df.set_index(["scn_name", "bus0", "bus1"], inplace=True)

df.to_sql(
"egon_etrago_hv_busmap",
con=etrago.engine,
schema="grid",
if_exists="append",
)
busmap = pd.Series(df.bus1.values, index=df.bus0).to_dict()

return
return busmap


def busmap_from_psql(etrago):
Copy link
Contributor

Choose a reason for hiding this comment

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

The description and the name of the function do not represent the content anymore

Expand All @@ -517,54 +499,26 @@ def busmap_from_psql(etrago):
busmap : dict
Maps old bus_ids to new bus_ids.
"""
scn_name = (
etrago.args["scn_name"]
if etrago.args["scn_extension"] is None
else etrago.args["scn_name"]
+ "_ext_"
+ "_".join(etrago.args["scn_extension"])
)

from saio.grid import egon_etrago_hv_busmap

filter_version = etrago.args["gridversion"]

if not filter_version:
filter_version = "testcase"

def fetch():
query = (
etrago.session.query(
egon_etrago_hv_busmap.bus0, egon_etrago_hv_busmap.bus1
)
.filter(egon_etrago_hv_busmap.scn_name == scn_name)
.filter(egon_etrago_hv_busmap.version == filter_version)
if etrago.args["network_clustering_ehv"]["busmap"] is False:
cpu_cores = etrago.args["network_clustering"]["CPU_cores"]
if cpu_cores == "max":
cpu_cores = mp.cpu_count()
else:
cpu_cores = int(cpu_cores)

busmap = busmap_by_shortest_path(
etrago,
fromlvl=[110],
tolvl=[220, 380, 400, 450],
cpu_cores=cpu_cores,
)

return dict(query.all())

busmap = fetch()

if busmap:
print(
"Existing busmap will be deleted and a new one will be calculated."
pd.DataFrame(busmap.items(), columns=["bus0", "bus1"]).to_csv(
"ehv_elecgrid_busmap_result.csv",
index=False,
)
etrago.engine.execute("""DELETE FROM grid.egon_etrago_hv_busmap""")

cpu_cores = etrago.args["network_clustering"]["CPU_cores"]
if cpu_cores == "max":
cpu_cores = mp.cpu_count()
else:
cpu_cores = int(cpu_cores)

busmap_by_shortest_path(
etrago,
scn_name,
fromlvl=[110],
tolvl=[220, 380, 400, 450],
cpu_cores=cpu_cores,
)
busmap = fetch()
busmap = pd.read_csv(etrago.args["network_clustering_ehv"]["busmap"])

return busmap

Expand Down
2 changes: 1 addition & 1 deletion etrago/tools/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -1878,7 +1878,7 @@ def get_clustering_data(self, path):

"""

if (self.args["network_clustering_ehv"]) | (
if (self.args["network_clustering_ehv"]["active"]) | (
self.args["network_clustering"]["active"]
):
path_clus = os.path.join(path, "clustering")
Expand Down
Loading