Skip to content

Commit

Permalink
merge dev
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosEpia committed Oct 23, 2023
2 parents 75cbf94 + 79ecf3e commit 8ad8894
Show file tree
Hide file tree
Showing 20 changed files with 2,190 additions and 838 deletions.
22 changes: 22 additions & 0 deletions .readthedocs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# .readthedocs.yaml
# Read the Docs configuration file
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details

# Required
version: 2

# Set the version of Python and other tools you might need
build:
os: ubuntu-22.04
tools:
python: "3.8"

# Build documentation in the docs/ directory with Sphinx
sphinx:
configuration: doc/conf.py

# We recommend specifying your dependencies to enable reproducible builds:
# https://docs.readthedocs.io/en/stable/guides/reproducible-builds.html
python:
install:
- requirements: requirements-doc.txt
353 changes: 199 additions & 154 deletions etrago/appl.py

Large diffs are not rendered by default.

17 changes: 12 additions & 5 deletions etrago/args.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,18 @@
},
"generator_noise": 789456,
"extra_functionality": {},
"network_clustering_ehv": false,
"delete_dispensable_ac_buses": true,
"network_clustering_ehv": {
"active": false,
"busmap": false,
},
"network_clustering": {
"active": true,
"method": "kmedoids-dijkstra",
"n_clusters_AC": 30,
"cluster_foreign_AC": false,
"n_clusters_gas": 30,
"method_gas": "kmedoids-dijkstra",
"n_clusters_gas": 17,
"cluster_foreign_gas": false,
"k_elec_busmap": false,
"k_gas_busmap": false,
Expand All @@ -68,10 +73,11 @@
"line_length_factor": 1,
"remove_stubs": false,
"use_reduced_coordinates": false,
"random_state":42,
"random_state": 42,
"n_init": 10,
"max_iter": 100,
"tol": 1e-06
"tol": 1e-6,
"CPU_cores": 4,
},
"sector_coupled_clustering": {
"active": true,
Expand All @@ -88,10 +94,11 @@
"snapshot_clustering": {
"active": false,
"method": "segmentation",
"extreme_periods": null,
"how": "daily",
"storage_constraints": "soc_constraints",
"n_clusters": 5,
"n_segments": 5
"n_segments": 5,
},
"skip_snapshots": 5,
"temporal_disaggregation": {
Expand Down
2 changes: 0 additions & 2 deletions etrago/cluster/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@
__copyright__ = "tba"
__license__ = "tba"
__author__ = "tba"


81 changes: 55 additions & 26 deletions etrago/cluster/electrical.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,14 @@
import pypsa.io as io

from etrago.cluster.spatial import (
busmap_from_psql,
find_buses_area,
busmap_ehv_clustering,
group_links,
kmean_clustering,
kmedoids_dijkstra_clustering,
strategies_generators,
strategies_one_ports,
)
from etrago.tools.utilities import *

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -117,6 +116,27 @@ def adjust_no_electric_network(etrago, busmap, cluster_met):
Maps old bus_ids to new bus_ids including all sectors.
"""

def find_de_closest(network, bus_ne):
ac_ehv = network.buses[
(network.buses.v_nom > 110)
& (network.buses.carrier == "AC")
& (network.buses.country == "DE")
]

bus_ne_x = network.buses.loc[bus_ne, "x"]
bus_ne_y = network.buses.loc[bus_ne, "y"]

ac_ehv["dist"] = ac_ehv.apply(
lambda x: ((x.x - bus_ne_x) ** 2 + (x.y - bus_ne_y) ** 2)
** (1 / 2),
axis=1,
)

new_ehv_bus = ac_ehv.dist.idxmin()

return new_ehv_bus

network = etrago.network
# network2 is supposed to contain all the not electrical or gas buses
# and links
Expand All @@ -142,7 +162,8 @@ def adjust_no_electric_network(etrago, busmap, cluster_met):
# are going to be clustered
busmap2 = {}

# Map crossborder AC buses in case that they were not part of the k-mean clustering
# Map crossborder AC buses in case that they were not part of the k-mean
# clustering
if (not etrago.args["network_clustering"]["cluster_foreign_AC"]) & (
cluster_met in ["kmeans", "kmedoids-dijkstra"]
):
Expand All @@ -154,6 +175,12 @@ def adjust_no_electric_network(etrago, busmap, cluster_met):
for bus_out in ac_buses_out.index:
busmap2[bus_out] = bus_out

foreign_hv = network.buses[
(network.buses.country != "DE")
& (network.buses.carrier == "AC")
& (network.buses.v_nom > 110)
].index

busmap3 = pd.DataFrame(columns=["elec_bus", "carrier", "cluster"])
for bus_ne in network2.buses.index:
carry = network2.buses.loc[bus_ne, "carrier"]
Expand All @@ -164,8 +191,14 @@ def adjust_no_electric_network(etrago, busmap, cluster_met):
& (network2.links["carrier"] == map_carrier[carry])
].copy()
df["elec"] = df["bus0"].isin(busmap.keys())
bus_hv = df[df["elec"] == True]["bus0"][0]
busmap3.at[bus_ne, "elec_bus"] = busmap[bus_hv]
bus_hv = df[df["elec"]]["bus0"][0]
bus_ehv = busmap[bus_hv]
if bus_ehv not in foreign_hv:
busmap3.at[bus_ne, "elec_bus"] = bus_ehv
else:
busmap3.at[bus_ne, "elec_bus"] = find_de_closest(
network, bus_ne
)
except:
no_elec_conex.append(bus_ne)
busmap3.at[bus_ne, "elec_bus"] = bus_ne
Expand Down Expand Up @@ -237,11 +270,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 All @@ -268,6 +296,8 @@ def cluster_on_extra_high_voltage(etrago, busmap, with_time=True):
# Dealing with links
links = network.links.copy()
dc_links = links[links["carrier"] == "DC"]
# Discard links connected to buses under 220 kV
dc_links = dc_links[dc_links.bus0.isin(buses.index)]
links = links[links["carrier"] != "DC"]

new_links = (
Expand Down Expand Up @@ -326,7 +356,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 @@ -359,10 +388,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"] == False)
& (buses_ac["with_load"] == False)
& (buses_ac["with_link"] == False)
& (buses_ac["with_gen"] == False)
(~buses_ac["with_line"])
& (~buses_ac["with_load"])
& (~buses_ac["with_link"])
& (~buses_ac["with_gen"])
].index

if len(delete_buses):
Expand Down Expand Up @@ -400,8 +429,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 @@ -413,14 +442,14 @@ 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"

delete_ehv_buses_no_lines(self.network)

busmap = busmap_from_psql(self)
busmap = busmap_ehv_clustering(self)

self.network, busmap = cluster_on_extra_high_voltage(
self, busmap, with_time=True
Expand Down Expand Up @@ -705,7 +734,7 @@ def preprocessing(etrago):
----------------------- WARNING ---------------------------
THE FOLLOWING BUSES HAVE NOT COUNTRY DATA:
{network.buses[network.buses.country.isna()].index.to_list()}.
{network.buses[network.buses.country.isna()].index.to_list()}.
THEY WILL BE ASSIGNED TO GERMANY, BUT IT IS POTENTIALLY A
SIGN OF A PROBLEM IN THE DATASET.
----------------------- WARNING ---------------------------
Expand Down Expand Up @@ -782,7 +811,7 @@ def postprocessing(
method = settings["method"]
num_clusters = settings["n_clusters_AC"]

if settings["k_elec_busmap"] == False:
if not settings["k_elec_busmap"]:
busmap.name = "cluster"
busmap_elec = pd.DataFrame(busmap.copy(), dtype="string")
busmap_elec.index.name = "bus"
Expand Down Expand Up @@ -834,7 +863,7 @@ def postprocessing(
)

# merge busmap for foreign buses with the German buses
if settings["cluster_foreign_AC"] == False:
if not settings["cluster_foreign_AC"]:
for bus in busmap_foreign.index:
busmap[bus] = busmap_foreign[bus]
if bus == busmap_foreign[bus]:
Expand Down Expand Up @@ -930,7 +959,7 @@ def calc_availability_factor(gen):
else:
try:
cf = fixed_capacity_fac[gen["carrier"]]
except:
except KeyError:
print(gen)
cf = 1
return cf
Expand Down Expand Up @@ -1026,7 +1055,7 @@ def run_spatial_clustering(self):
) = preprocessing(self)

if self.args["network_clustering"]["method"] == "kmeans":
if self.args["network_clustering"]["k_elec_busmap"] == False:
if not self.args["network_clustering"]["k_elec_busmap"]:
logger.info("Start k-means Clustering")

busmap = kmean_clustering(
Expand All @@ -1038,7 +1067,7 @@ def run_spatial_clustering(self):
medoid_idx = pd.Series(dtype=str)

elif self.args["network_clustering"]["method"] == "kmedoids-dijkstra":
if self.args["network_clustering"]["k_elec_busmap"] == False:
if not self.args["network_clustering"]["k_elec_busmap"]:
logger.info("Start k-medoids Dijkstra Clustering")

busmap, medoid_idx = kmedoids_dijkstra_clustering(
Expand All @@ -1058,7 +1087,7 @@ def run_spatial_clustering(self):
)
self.update_busmap(busmap)

if self.args["disaggregation"] != None:
if self.args["disaggregation"] is not None:
self.disaggregated_network = self.network.copy()
else:
self.disaggregated_network = self.network.copy(with_time=False)
Expand Down
8 changes: 6 additions & 2 deletions etrago/cluster/gas.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import os

if "READTHEDOCS" not in os.environ:
import logging

from pypsa import Network
from pypsa.networkclustering import (
aggregatebuses,
Expand All @@ -41,7 +43,8 @@
kmedoids_dijkstra_clustering,
sum_with_inf,
)
from etrago.tools.utilities import *

logger = logging.getLogger(__name__)

__copyright__ = (
"Flensburg University of Applied Sciences, "
Expand Down Expand Up @@ -1014,7 +1017,8 @@ def run_spatial_clustering_gas(self):
self.update_busmap(busmap)

logger.info(
"GAS Network clustered to {} DE-buses and {} foreign buses with {} algorithm.".format(
"""GAS Network clustered to {} DE-buses and {} foreign buses
with {} algorithm.""".format(
len(
self.network.buses.loc[
(self.network.buses.carrier == "CH4")
Expand Down
Loading

0 comments on commit 8ad8894

Please sign in to comment.