Skip to content

Commit

Permalink
Consistent function scope (#1537)
Browse files Browse the repository at this point in the history
* start removing snakemake from functions

* move snakemake objects out of prepare_sector_functions

* continue with add_existing_baseyear

* continue with prepare_perfect_foresight

* apply it to add_electricity

* continue with solve network

* add pylint check to reusable functions

* iron out follow up issues

* adjust pylint check
  • Loading branch information
FabianHofmann authored Feb 10, 2025
1 parent dfd75a2 commit 9717cb0
Show file tree
Hide file tree
Showing 9 changed files with 1,420 additions and 364 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ jobs:
conda env update -n pypsa-eur -f ${{ env.env_file }}
echo "Run conda list" && conda list
- name: Run pylint check on scripts
# check for undefined variables to reuse functions across scripts
run: |
pylint --disable=all --enable=E0601 --output-format=parseable scripts/add_* scripts/prepare_* scripts/solve_*
- name: Run snakemake test workflows
run: |
make test
Expand Down
1 change: 1 addition & 0 deletions envs/environment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ dependencies:
- ipython
- pre-commit
- ruff
- pylint

- pip:
- gurobipy
Expand Down
46 changes: 38 additions & 8 deletions scripts/add_brownfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,32 @@
idx = pd.IndexSlice


def add_brownfield(n, n_p, year):
def add_brownfield(
n,
n_p,
year,
h2_retrofit=False,
h2_retrofit_capacity_per_ch4=None,
capacity_threshold=None,
):
"""
Add brownfield capacity from previous network.
Parameters
----------
n : pypsa.Network
Network to add brownfield to
n_p : pypsa.Network
Previous network to get brownfield from
year : int
Planning year
h2_retrofit : bool
Whether to allow hydrogen pipeline retrofitting
h2_retrofit_capacity_per_ch4 : float
Ratio of hydrogen to methane capacity for pipeline retrofitting
capacity_threshold : float
Threshold for removing assets with low capacity
"""
logger.info(f"Preparing brownfield for the year {year}")

# electric transmission grid set optimised capacities of previous as minimum
Expand All @@ -49,11 +74,9 @@ def add_brownfield(n, n_p, year):
& c.df.index.str.contains("heat")
]

threshold = snakemake.params.threshold_capacity

if not chp_heat.empty:
threshold_chp_heat = (
threshold
capacity_threshold
* c.df.efficiency[chp_heat.str.replace("heat", "electric")].values
* c.df.p_nom_ratio[chp_heat.str.replace("heat", "electric")].values
/ c.df.efficiency[chp_heat].values
Expand All @@ -67,7 +90,7 @@ def add_brownfield(n, n_p, year):
c.name,
c.df.index[
(c.df[f"{attr}_nom_extendable"] & ~c.df.index.isin(chp_heat))
& (c.df[f"{attr}_nom_opt"] < threshold)
& (c.df[f"{attr}_nom_opt"] < capacity_threshold)
],
)

Expand All @@ -85,7 +108,7 @@ def add_brownfield(n, n_p, year):
n.import_series_from_dataframe(c.pnl[tattr], c.name, tattr)

# deal with gas network
if snakemake.params.H2_retrofit:
if h2_retrofit:
# subtract the already retrofitted from the maximum capacity
h2_retrofitted_fixed_i = n.links[
(n.links.carrier == "H2 pipeline retrofitted")
Expand Down Expand Up @@ -118,7 +141,7 @@ def add_brownfield(n, n_p, year):
pipe_capacity = n.links.loc[gas_pipes_i, "p_nom"]
fr = "H2 pipeline retrofitted"
to = "gas pipeline"
CH4_per_H2 = 1 / snakemake.params.H2_retrofit_capacity_per_CH4
CH4_per_H2 = 1 / h2_retrofit_capacity_per_ch4
already_retrofitted.index = already_retrofitted.index.str.replace(fr, to)
remaining_capacity = (
pipe_capacity
Expand Down Expand Up @@ -301,7 +324,14 @@ def update_heat_pump_efficiency(n: pypsa.Network, n_p: pypsa.Network, year: int)

update_heat_pump_efficiency(n, n_p, year)

add_brownfield(n, n_p, year)
add_brownfield(
n,
n_p,
year,
h2_retrofit=snakemake.params.H2_retrofit,
h2_retrofit_capacity_per_ch4=snakemake.params.H2_retrofit_capacity_per_CH4,
capacity_threshold=snakemake.params.threshold_capacity,
)

disable_grid_expansion_if_limit_hit(n)

Expand Down
Loading

0 comments on commit 9717cb0

Please sign in to comment.