Skip to content

Commit

Permalink
added pipeline to plot capacities
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasFrankenQ committed Jul 21, 2023
1 parent 05a5399 commit 8567e7c
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 23 deletions.
25 changes: 15 additions & 10 deletions config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ scenario:
# LW: Leading the Way,
# ST: System Transformation,
# CT: Consumer Transformation,
planning_horizons: # investment years for myopic and perfect; for overnight, year of cost assumptions can be different and is defined under 'costs'
- 2035
# for example, set to
# - 2020
# - 2030
# - 2040
# - 2050
# for myopic foresight
dt_event:
- '1' # selects file for turndown timing and magnitude in data/turndown_events/
opts: # only relevant for PyPSA-Eur
Expand All @@ -58,14 +66,6 @@ scenario:
# planning_horizons), be:beta decay; ex:exponential decay
# cb40ex0 distributes a carbon budget of 40 GtCO2 following an exponential
# decay with initial growth rate 0
planning_horizons: # investment years for myopic and perfect; for overnight, year of cost assumptions can be different and is defined under 'costs'
- 2035
# for example, set to
# - 2020
# - 2030
# - 2040
# - 2050
# for myopic foresight

# here is a list of all possible countries:
# countries: ['AL', 'AT', 'BA', 'BE', 'BG', 'CH', 'CZ', 'DE', 'DK', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HU', 'IE', 'IT', 'LT', 'LU', 'LV', 'ME', 'MK', 'NL', 'NO', 'PL', 'PT', 'RO', 'RS', 'SE', 'SI', 'SK']
Expand Down Expand Up @@ -730,8 +730,10 @@ plotting:
eu_node_location:
x: -5.5
y: 46.
costs_max: 1000
costs_threshold: 1
costs_max: 50
capacities_max: 1000
costs_threshold: 0
capacities_threshold: 0
energy_max: 20000
energy_min: -20000
energy_threshold: 50.
Expand Down Expand Up @@ -874,6 +876,7 @@ plotting:
electricity distribution grid: '#97ad8c'
# electricity demand
Electric load: '#110d63'
load: '#110d63'
electric demand: '#110d63'
electricity: '#110d63'
industry electricity: '#2d2a66'
Expand Down Expand Up @@ -931,8 +934,10 @@ plotting:
H2 pipeline: '#f081dc'
H2 pipeline retrofitted: '#ba99b5'
H2 Fuel Cell: '#c251ae'
H2 fuel cell: '#c251ae'
H2 turbine: '#991f83'
H2 Electrolysis: '#ff29d9'
H2 electrolysis: '#ff29d9'
# ammonia
NH3: '#46caf0'
ammonia: '#46caf0'
Expand Down
13 changes: 8 additions & 5 deletions rules/postprocess.smk
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ rule make_summary:
costs="data/costs_{}.csv".format(config["costs"]["year"])
if config["foresight"] == "overnight"
else "data/costs_{}.csv".format(config["scenario"]["planning_horizons"][0]),
plots=expand(
RESULTS
+ "maps/elec_s{simpl}_{gb_regions}_ec_l{ll}_{opts}_-costs-all_{fes_scenario}_{planning_horizons}.pdf",
**config["scenario"]
),
# plots=expand(
# RESULTS
# + "maps/elec_s{simpl}_{gb_regions}_ec_l{ll}_{opts}_-costs-all_{fes_scenario}_{planning_horizons}.pdf",
# **config["scenario"]
# ),
output:
nodal_costs=RESULTS + "csvs/nodal_costs.csv",
nodal_capacities=RESULTS + "csvs/nodal_capacities.csv",
Expand Down Expand Up @@ -117,13 +117,16 @@ rule plot_summary:
RDIR=RDIR,
input:
costs=RESULTS + "csvs/costs.csv",
nodal_costs=RESULTS + "csvs/nodal_costs.csv",
energy=RESULTS + "csvs/energy.csv",
balances=RESULTS + "csvs/supply_energy.csv",
nodal_capacities=RESULTS + "csvs/nodal_capacities.csv",
# eurostat=input_eurostat,
output:
costs=RESULTS + "graphs/costs.pdf",
energy=RESULTS + "graphs/energy.pdf",
balances=RESULTS + "graphs/balances-energy.pdf",
capacities=RESULTS + "graphs/capacities.pdf",
threads: 2
resources:
mem_mb=10000,
Expand Down
29 changes: 28 additions & 1 deletion scripts/make_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,35 @@ def calculate_nodal_capacities(n, label, nodal_capacities):
index = pd.MultiIndex.from_tuples(
[(c.list_name,) + t for t in nodal_capacities_c.index.to_list()]
)

extendables = (
c.df.loc[getattr(
c.df, opt_name.get(c.name, "p") + "_nom_extendable"
)]
.groupby(["location", "carrier"])[
opt_name.get(c.name, "p") + "_nom_opt"
].sum()
)

non_extendables = (
c.df.loc[~getattr(
c.df, opt_name.get(c.name, "p") + "_nom_extendable"
)]
.groupby(["location", "carrier"])[
opt_name.get(c.name, "p") + "_nom"
].sum()
)

nodal_capacities_c = pd.concat((extendables, non_extendables))

index = pd.MultiIndex.from_tuples(
[(c.list_name,) + t for t in nodal_capacities_c.index.to_list()]
)

nodal_capacities = nodal_capacities.reindex(index.union(nodal_capacities.index))
nodal_capacities.loc[index, label] = nodal_capacities_c.values

nodal_capacities.drop("load", level=2, inplace=True)
return nodal_capacities


Expand Down Expand Up @@ -654,7 +680,8 @@ def make_summaries(networks_dict):
]

columns = pd.MultiIndex.from_tuples(
networks_dict.keys(), names=["cluster", "ll", "opt", "planning_horizon"]
networks_dict.keys(),
names=["gb_regions", "ll", "opt", "fes_scenario", "planning_horizon"]
)

df = {}
Expand Down
85 changes: 78 additions & 7 deletions scripts/plot_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,27 +131,36 @@ def rename_techs(label):


def plot_costs():
cost_df = pd.read_csv(
snakemake.input.costs, index_col=list(range(3)), header=list(range(n_header))
nodal_cost_df = pd.read_csv(
snakemake.input.nodal_costs, index_col=list(range(4)), header=list(range(n_header))
)
idx = pd.IndexSlice

gb_buses = nodal_cost_df.index.get_level_values(2).unique()
gb_buses = gb_buses[gb_buses.fillna("nan").str.contains("GB")]

df = cost_df.groupby(cost_df.index.get_level_values(2)).sum()
df = nodal_cost_df.loc[idx[:, :, gb_buses, :]]

df = (
df
.groupby(df.index.get_level_values(3))
.sum()
.droplevel([0,1,2], axis=1)
.drop("load")
)
# convert to billions
df = df / 1e9

df = df.groupby(df.index.map(rename_techs)).sum()

to_drop = df.index[df.max(axis=1) < snakemake.config["plotting"]["costs_threshold"]]

logger.info(
f"Dropping technology with costs below {snakemake.config['plotting']['costs_threshold']} EUR billion per year"
)
logger.debug(df.loc[to_drop])

df = df.drop(to_drop)

logger.info(f"Total system cost of {round(df.sum()[0])} EUR billion per year")
logger.info(f"Average total system cost of {round(df.sum().mean())} EUR billion per year")

new_index = preferred_order.intersection(df.index).append(
df.index.difference(preferred_order)
Expand Down Expand Up @@ -188,6 +197,66 @@ def plot_costs():
fig.savefig(snakemake.output.costs, bbox_inches="tight")


def plot_capacities():
nodal_caps_df = pd.read_csv(
snakemake.input.nodal_capacities, index_col=list(range(3)), header=list(range(n_header))
)
idx = pd.IndexSlice

gb_buses = nodal_caps_df.index.get_level_values(1).unique()
gb_buses = gb_buses[gb_buses.fillna("nan").str.contains("GB")]

df = nodal_caps_df.loc[idx[:, gb_buses, :]]
df.drop("stores", level=0, inplace=True)

df = (
df
.groupby(df.index.get_level_values(2))
.sum()
.droplevel([0,1,2], axis=1)
)
# convert to GW
df = df / 1e3

df = df.groupby(df.index.map(rename_techs)).sum()

logger.warning("Dropping of techs not implemented for plot_capacities()")

new_index = preferred_order.intersection(df.index).append(
df.index.difference(preferred_order)
)

new_columns = df.sum().sort_values().index

fig, ax = plt.subplots(figsize=(12, 8))

df.loc[new_index, new_columns].T.plot(
kind="bar",
ax=ax,
stacked=True,
color=[snakemake.config["plotting"]["tech_colors"][i] for i in new_index],
)

handles, labels = ax.get_legend_handles_labels()

handles.reverse()
labels.reverse()

ax.set_ylim([0, snakemake.config["plotting"]["capacities_max"]])

ax.set_ylabel("Installed Capacity [GW]")

ax.set_xlabel("")

ax.grid(axis="x")

ax.legend(
handles, labels, ncol=1, loc="upper left", bbox_to_anchor=[1, 1], frameon=False
)

fig.savefig(snakemake.output.capacities, bbox_inches="tight")


def plot_energy():
energy_df = pd.read_csv(
snakemake.input.energy, index_col=list(range(2)), header=list(range(n_header))
Expand Down Expand Up @@ -547,7 +616,9 @@ def plot_carbon_budget_distribution(input_eurostat):

logging.basicConfig(level=snakemake.config["logging"]["level"])

n_header = 4
n_header = 5

plot_capacities()

plot_costs()

Expand Down

0 comments on commit 8567e7c

Please sign in to comment.