From b27227f3ed610bbecf734e18b308345e313825e3 Mon Sep 17 00:00:00 2001 From: Amos Schledorn Date: Mon, 30 Sep 2024 17:44:47 +0200 Subject: [PATCH] style: simplify update_heat_pump_efficiency functions --- scripts/add_brownfield.py | 11 +++++++---- scripts/prepare_perfect_foresight.py | 24 ++++++++++++++---------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/scripts/add_brownfield.py b/scripts/add_brownfield.py index bc4a48e77..0f3cb318c 100644 --- a/scripts/add_brownfield.py +++ b/scripts/add_brownfield.py @@ -241,10 +241,13 @@ def update_heat_pump_efficiency(n: pypsa.Network, n_p: pypsa.Network, year: int) This function updates the efficiency in place and does not return a value. """ - for link_name_p in n_p.links.index: - if "heat pump" in link_name_p: - link_name = link_name_p.replace(link_name_p[-4:], str(year)) - n_p.links_t["efficiency"][link_name_p] = n.links_t["efficiency"][link_name] + # get names of heat pumps in previous iteration + heat_pump_idx_previous_iteration = n_p.links.index[n_p.links.index.str.contains("heat pump")] + # construct names of same-technology heat pumps in the current iteration + corresponding_idx_this_iteration = heat_pump_idx_previous_iteration.str[:-4] + str(year) + # update efficiency of heat pumps in previous iteration in-place to efficiency in this iteration + n_p.links_t["efficiency"].loc[:, heat_pump_idx_previous_iteration] = n.links_t["efficiency"].loc[:, corresponding_idx_this_iteration].values + if __name__ == "__main__": diff --git a/scripts/prepare_perfect_foresight.py b/scripts/prepare_perfect_foresight.py index 77f1062f5..50992d4c6 100644 --- a/scripts/prepare_perfect_foresight.py +++ b/scripts/prepare_perfect_foresight.py @@ -508,16 +508,20 @@ def update_heat_pump_efficiency(n: pypsa.Network, years: List[int]): This function updates the efficiency in place and does not return a value. """ - for link_name in n.links.index: - if "heat pump" in link_name: - heat_pump_name = link_name[:-4] - for year in years: - correct_efficiency = n.links_t["efficiency"].loc[ - (year, slice(None)), heat_pump_name + str(year) - ] - n.links_t["efficiency"].loc[ - (year, slice(None)), link_name - ] = correct_efficiency + # get names of all heat pumps + heat_pump_idx = n.links.index[n.links.index.str.contains("heat pump")] + for year in years: + # for each heat pump type, correct efficiency is the efficiency of that technology built in + correct_efficiency = n.links_t["efficiency"].loc[ + (year, slice(None)), heat_pump_idx.str[:-4] + str(year) + ] + # in , set the efficiency of all heat pumps to the correct efficiency + n.links_t["efficiency"].loc[ + (year, slice(None)), heat_pump_idx + ] = correct_efficiency.values + + + if __name__ == "__main__":