From cfdec7e56d651a62e7ba01ae7dc3aa51d47873e1 Mon Sep 17 00:00:00 2001 From: lisazeyen Date: Tue, 12 Apr 2022 09:56:58 +0200 Subject: [PATCH] simplify pipe retrofitting constraint --- scripts/add_brownfield.py | 16 ++++++++++++++++ scripts/solve_network.py | 6 +----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/scripts/add_brownfield.py b/scripts/add_brownfield.py index 068ca255..23ef1352 100644 --- a/scripts/add_brownfield.py +++ b/scripts/add_brownfield.py @@ -12,6 +12,7 @@ import numpy as np from add_existing_baseyear import add_build_year_to_new_assets from helper import override_component_attrs +from solve_network import basename def add_brownfield(n, n_p, year): @@ -79,8 +80,23 @@ def add_brownfield(n, n_p, year): # deal with gas network pipe_carrier = ['gas pipeline'] if snakemake.config["sector"]['H2_retrofit']: + # drop capacities of previous year to avoid duplicating to_drop = n.links.carrier.isin(pipe_carrier) & (n.links.build_year!=year) n.mremove("Link", n.links.loc[to_drop].index) + + # subtract the already retrofitted from today's gas grid capacity + h2_retrofitted_fixed_i = n.links[(n.links.carrier=='H2 pipeline retrofitted') & (n.links.build_year!=year)].index + gas_pipes_i = n.links[n.links.carrier.isin(pipe_carrier)].index + CH4_per_H2 = 1 / snakemake.config["sector"]["H2_retrofit_capacity_per_CH4"] + fr = "H2 pipeline retrofitted" + to = "gas pipeline" + # today's pipe capacity + pipe_capacity = n.links.loc[gas_pipes_i, 'p_nom'] + # already retrofitted capacity from gas -> H2 + already_retrofitted = (n.links.loc[h2_retrofitted_fixed_i, 'p_nom'] + .rename(lambda x: basename(x).replace(fr, to)).groupby(level=0).sum()) + remaining_capacity = pipe_capacity - CH4_per_H2 * already_retrofitted.reindex(index=pipe_capacity.index).fillna(0) + n.links.loc[gas_pipes_i, "p_nom"] = remaining_capacity else: new_pipes = n.links.carrier.isin(pipe_carrier) & (n.links.build_year==year) n.links.loc[new_pipes, "p_nom"] = 0. diff --git a/scripts/solve_network.py b/scripts/solve_network.py index 7d2045ed..4f6cc2c4 100644 --- a/scripts/solve_network.py +++ b/scripts/solve_network.py @@ -193,7 +193,6 @@ def add_pipe_retrofit_constraint(n): gas_pipes_i = n.links.query("carrier == 'gas pipeline' and p_nom_extendable").index h2_retrofitted_i = n.links.query("carrier == 'H2 pipeline retrofitted' and p_nom_extendable").index - h2_retrofitted_fixed_i = n.links.query("carrier == 'H2 pipeline retrofitted' and not p_nom_extendable").index if h2_retrofitted_i.empty or gas_pipes_i.empty: return @@ -204,9 +203,6 @@ def add_pipe_retrofit_constraint(n): to = "gas pipeline" pipe_capacity = n.links.loc[gas_pipes_i, 'p_nom'].rename(basename) - already_retrofitted = (n.links.loc[h2_retrofitted_fixed_i, 'p_nom'] - .rename(lambda x: basename(x).replace(fr, to)).groupby(level=0).sum()) - remaining_capacity = pipe_capacity - CH4_per_H2 * already_retrofitted.reindex(index=pipe_capacity.index).fillna(0) lhs = linexpr( (CH4_per_H2, link_p_nom.loc[h2_retrofitted_i].rename(index=lambda x: x.replace(fr, to))), @@ -214,7 +210,7 @@ def add_pipe_retrofit_constraint(n): ) lhs.rename(basename, inplace=True) - define_constraints(n, lhs, "=", remaining_capacity, 'Link', 'pipe_retrofit') + define_constraints(n, lhs, "=", pipe_capacity, 'Link', 'pipe_retrofit') def add_co2_sequestration_limit(n, sns):