improve code style

This commit is contained in:
lisazeyen 2022-03-18 10:18:02 +01:00
parent 5502943301
commit 08cef37e7e
2 changed files with 30 additions and 25 deletions

View File

@ -223,6 +223,26 @@ def plot_map(network, components=["links", "stores", "storage_units", "generator
bbox_inches="tight"
)
def group_pipes(df, drop_direction=False):
"""Group pipes which connect same buses and return overall capacity.
"""
if drop_direction:
positive_order = df.bus0 < df.bus1
df_p = df[positive_order]
swap_buses = {"bus0": "bus1", "bus1": "bus0"}
df_n = df[~positive_order].rename(columns=swap_buses)
df = pd.concat([df_p, df_n])
# there are pipes for each investment period rename to AC buses name for plotting
df.index = df.apply(
lambda x: f"H2 pipeline {x.bus0.replace(' H2', '')} -> {x.bus1.replace(' H2', '')}",
axis=1
)
# group pipe lines connecting the same buses and rename them for plotting
pipe_capacity = df["p_nom_opt"].groupby(level=0).sum()
return pipe_capacity
def plot_h2_map(network):
@ -246,31 +266,15 @@ def plot_h2_map(network):
# make a fake MultiIndex so that area is correct for legend
bus_sizes.rename(index=lambda x: x.replace(" H2", ""), level=0, inplace=True)
# frop all links which are not H2 pipelines
n.links.drop(n.links.index[~n.links.carrier.str.contains("H2 pipeline")], inplace=True)
h2_new = n.links.loc[n.links.carrier=="H2 pipeline"]
h2_retro = n.links.loc[n.links.carrier=='H2 pipeline retrofitted']
# sum capacitiy for pipelines from different investment periods
h2_new = group_pipes(h2_new)
h2_retro = group_pipes(h2_retro, drop_direction=True).reindex(h2_new.index).fillna(0)
positive_order = h2_retro.bus0 < h2_retro.bus1
h2_retro_p = h2_retro[positive_order]
swap_buses = {"bus0": "bus1", "bus1": "bus0"}
h2_retro_n = h2_retro[~positive_order].rename(columns=swap_buses)
h2_retro = pd.concat([h2_retro_p, h2_retro_n])
h2_retro.index = h2_retro.apply(
lambda x: f"H2 pipeline {x.bus0.replace(' H2', '')} -> {x.bus1.replace(' H2', '')}",
axis=1
)
h2_new.index = h2_new.apply(
lambda x: f"H2 pipeline {x.bus0.replace(' H2', '')} -> {x.bus1.replace(' H2', '')}"
,axis=1)
h2_new = h2_new["p_nom_opt"].groupby(level=0).sum()
h2_retro = h2_retro["p_nom_opt"].groupby(level=0).sum()
h2_retro = h2_retro.groupby(level=0).sum().reindex(h2_new.index).fillna(0)
n.links.rename(index=lambda x: x.split("-2")[0], inplace=True)
n.links = n.links.groupby(level=0).first()

View File

@ -185,6 +185,8 @@ def add_chp_constraints(n):
define_constraints(n, lhs, "<=", 0, 'chplink', 'backpressure')
def basename(x):
return x.split("-2")[0]
def add_pipe_retrofit_constraint(n):
"""Add constraint for retrofitting existing CH4 pipelines to H2 pipelines."""
@ -201,10 +203,9 @@ def add_pipe_retrofit_constraint(n):
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])
pipe_capacity = n.links.loc[gas_pipes_i, 'p_nom'].rename(basename)
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())
.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(
@ -212,7 +213,7 @@ def add_pipe_retrofit_constraint(n):
(1, link_p_nom.loc[gas_pipes_i])
)
lhs.rename(index=lambda x: x.split("-2")[0], inplace=True)
lhs.rename(basename, inplace=True)
define_constraints(n, lhs, "=", remaining_capacity, 'Link', 'pipe_retrofit')
@ -284,7 +285,7 @@ if __name__ == "__main__":
'solve_network_myopic',
simpl='',
opts="",
clusters="45",
clusters="37",
lv=1.0,
sector_opts='168H-T-H-B-I-A-solar+p3-dist1',
planning_horizons="2030",