From 45aa714bdcdee1e08e1cefb514da4d4858afe02a Mon Sep 17 00:00:00 2001 From: Thomas Gilon Date: Fri, 12 Apr 2024 16:10:00 +0200 Subject: [PATCH] Add calculate_nodal_supply_energy in make summary --- doc/release_notes.rst | 2 ++ rules/postprocess.smk | 1 + scripts/make_summary.py | 71 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+) diff --git a/doc/release_notes.rst b/doc/release_notes.rst index d42b149f..d5532ddb 100644 --- a/doc/release_notes.rst +++ b/doc/release_notes.rst @@ -181,6 +181,8 @@ Upcoming Release * Fix custom busmap read in `cluster_network`. +* Add `nodal_supply_energy` to `make_summary`. + PyPSA-Eur 0.10.0 (19th February 2024) ===================================== diff --git a/rules/postprocess.smk b/rules/postprocess.smk index e7df2e66..b2d53253 100644 --- a/rules/postprocess.smk +++ b/rules/postprocess.smk @@ -199,6 +199,7 @@ rule make_summary: energy=RESULTS + "csvs/energy.csv", supply=RESULTS + "csvs/supply.csv", supply_energy=RESULTS + "csvs/supply_energy.csv", + nodal_supply_energy=RESULTS + "csvs/nodal_supply_energy.csv", prices=RESULTS + "csvs/prices.csv", weighted_prices=RESULTS + "csvs/weighted_prices.csv", market_values=RESULTS + "csvs/market_values.csv", diff --git a/scripts/make_summary.py b/scripts/make_summary.py index 8c2a1aea..dc1aaac1 100644 --- a/scripts/make_summary.py +++ b/scripts/make_summary.py @@ -413,6 +413,76 @@ def calculate_supply_energy(n, label, supply_energy): return supply_energy +def calculate_nodal_supply_energy(n, label, nodal_supply_energy): + """ + Calculate the total energy supply/consumption of each component at the buses + aggregated by carrier and node. + """ + + bus_carriers = n.buses.carrier.unique() + + for i in bus_carriers: + bus_map = n.buses.carrier == i + bus_map.at[""] = False + + for c in n.iterate_components(n.one_port_components): + items = c.df.index[c.df.bus.map(bus_map).fillna(False)] + + if len(items) == 0: + continue + + s = ( + pd.concat([ + ( + c.pnl.p[items] + .multiply(n.snapshot_weightings.generators, axis=0) + .sum() + .multiply(c.df.loc[items, "sign"]) + ), + c.df.loc[items][["bus", "carrier"]] + ], axis=1) + .groupby(by=["bus", "carrier"]) + .sum()[0] + ) + s = pd.concat([s], keys=[c.list_name]) + s = pd.concat([s], keys=[i]) + + nodal_supply_energy = nodal_supply_energy.reindex(s.index.union(nodal_supply_energy.index)) + nodal_supply_energy.loc[s.index, label] = s + + for c in n.iterate_components(n.branch_components): + for end in [col[3:] for col in c.df.columns if col[:3] == "bus"]: + items = c.df.index[c.df["bus" + str(end)].map(bus_map).fillna(False)] + + if (len(items) == 0) or c.pnl["p" + end].empty: + continue + + s = ( + pd.concat([ + ( + (-1) * c.pnl["p" + end][items] + .multiply(n.snapshot_weightings.generators, axis=0) + .sum() + ), + c.df.loc[items][["bus0", "carrier"]] + ], axis=1) + .groupby(by=["bus0", "carrier"]) + .sum()[0] + ) + + s.index = s.index.map(lambda x: (x[0], x[1] + end)) + s = pd.concat([s], keys=[c.list_name]) + s = pd.concat([s], keys=[i]) + + nodal_supply_energy = nodal_supply_energy.reindex( + s.index.union(nodal_supply_energy.index) + ) + + nodal_supply_energy.loc[s.index, label] = s + + return nodal_supply_energy + + def calculate_metrics(n, label, metrics): metrics_list = [ "line_volume", @@ -637,6 +707,7 @@ def make_summaries(networks_dict): "energy", "supply", "supply_energy", + "nodal_supply_energy", "prices", "weighted_prices", "price_statistics",