adjust pipe retrofitting constraint to work with myopic

This commit is contained in:
lisazeyen 2022-01-07 16:59:14 +01:00
parent 5b30f0ece6
commit 9f8b54a3ce

View File

@ -33,14 +33,14 @@ def _add_land_use_constraint(n):
existing = n.generators.loc[n.generators.carrier==carrier,"p_nom"].groupby(n.generators.bus.map(n.buses.location)).sum()
existing.index += " " + carrier + "-" + snakemake.wildcards.planning_horizons
n.generators.loc[existing.index,"p_nom_max"] -= existing
n.generators.p_nom_max.clip(lower=0, inplace=True)
def _add_land_use_constraint_m(n):
# if generators clustering is lower than network clustering, land_use accounting is at generators clusters
planning_horizons = snakemake.config["scenario"]["planning_horizons"]
planning_horizons = snakemake.config["scenario"]["planning_horizons"]
grouping_years = snakemake.config["existing_capacities"]["grouping_years"]
current_horizon = snakemake.wildcards.planning_horizons
@ -48,9 +48,9 @@ def _add_land_use_constraint_m(n):
existing = n.generators.loc[n.generators.carrier==carrier,"p_nom"]
ind = list(set([i.split(sep=" ")[0] + ' ' + i.split(sep=" ")[1] for i in existing.index]))
previous_years = [
str(y) for y in
str(y) for y in
planning_horizons + grouping_years
if y < int(snakemake.wildcards.planning_horizons)
]
@ -59,13 +59,13 @@ def _add_land_use_constraint_m(n):
ind2 = [i for i in ind if i + " " + carrier + "-" + p_year in existing.index]
sel_current = [i + " " + carrier + "-" + current_horizon for i in ind2]
sel_p_year = [i + " " + carrier + "-" + p_year for i in ind2]
n.generators.loc[sel_current, "p_nom_max"] -= existing.loc[sel_p_year].rename(lambda x: x[:-4] + current_horizon)
n.generators.loc[sel_current, "p_nom_max"] -= existing.loc[sel_p_year].rename(lambda x: x[:-4] + current_horizon)
n.generators.p_nom_max.clip(lower=0, inplace=True)
def prepare_network(n, solve_opts=None):
if 'clip_p_max_pu' in solve_opts:
for df in (n.generators_t.p_max_pu, n.generators_t.p_min_pu, n.storage_units_t.inflow):
df.where(df>solve_opts['clip_p_max_pu'], other=0., inplace=True)
@ -189,36 +189,42 @@ def add_chp_constraints(n):
def add_pipe_retrofit_constraint(n):
"""Add constraint for retrofitting existing CH4 pipelines to H2 pipelines."""
gas_pipes_i = n.links[n.links.carrier=="gas pipeline"].index
h2_retrofitted_i = n.links[n.links.carrier=='H2 pipeline retrofitted'].index
gas_pipes_i = n.links[n.links.carrier=="gas pipeline"].query("p_nom_extendable").index
h2_retrofitted_i = n.links[n.links.carrier=='H2 pipeline retrofitted'].query("p_nom_extendable").index
h2_retrofitted_fixed_i = n.links[n.links.carrier=='H2 pipeline retrofitted'].index.difference(h2_retrofitted_i)
if h2_retrofitted_i.empty or gas_pipes_i.empty: return
link_p_nom = get_var(n, "Link", "p_nom")
pipe_capacity = n.links.loc[gas_pipes_i, 'p_nom']
CH4_per_H2 = 1 / n.config["sector"]["H2_retrofit_capacity_per_CH4"]
fr = "H2 pipeline retrofitted"
to = "gas pipeline"
pipe_capacity = n.links.loc[gas_pipes_i, 'p_nom'].rename(index=lambda x: x.split("-2")[0])
already_retrofitted = (n.links.loc[h2_retrofitted_fixed_i, 'p_nom']
.rename(index= lambda x: x.split("-2")[0]
.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))),
(1, link_p_nom.loc[gas_pipes_i])
)
define_constraints(n, lhs, "=", pipe_capacity, 'Link', 'pipe_retrofit')
lhs.rename(index=lambda x: x.split("-2")[0], inplace=True)
define_constraints(n, lhs, "=", remaining_capacity, 'Link', 'pipe_retrofit')
def add_co2_sequestration_limit(n, sns):
co2_stores = n.stores.loc[n.stores.carrier=='co2 stored'].index
if co2_stores.empty or ('Store', 'e') not in n.variables.index:
return
vars_final_co2_stored = get_var(n, 'Store', 'e').loc[sns[-1], co2_stores]
lhs = linexpr((1, vars_final_co2_stored)).sum()
limit = n.config["sector"].get("co2_sequestration_potential", 200) * 1e6
@ -226,7 +232,7 @@ def add_co2_sequestration_limit(n, sns):
if not "seq" in o: continue
limit = float(o[o.find("seq")+3:])
break
name = 'co2_sequestration_limit'
sense = "<="
@ -258,7 +264,7 @@ def solve_network(n, config, opts='', **kwargs):
if cf_solving.get('skip_iterations', False):
network_lopf(n, solver_name=solver_name, solver_options=solver_options,
extra_functionality=extra_functionality,
extra_functionality=extra_functionality,
keep_shadowprices=keep_shadowprices, **kwargs)
else:
ilopf(n, solver_name=solver_name, solver_options=solver_options,
@ -275,12 +281,13 @@ if __name__ == "__main__":
if 'snakemake' not in globals():
from helper import mock_snakemake
snakemake = mock_snakemake(
'solve_network',
'solve_network_myopic',
simpl='',
clusters=48,
opts="",
clusters="45",
lv=1.0,
sector_opts='Co2L0-168H-T-H-B-I-solar3-dist1',
planning_horizons=2050,
sector_opts='168H-T-H-B-I-A-solar+p3-dist1',
planning_horizons="2030",
)
logging.basicConfig(filename=snakemake.log.python,