-
Notifications
You must be signed in to change notification settings - Fork 331
enh(add_existing_baseyear): Pull out valid_grouping_years algorithm from heat #1570
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
""" | ||
|
||
import logging | ||
from collections.abc import Sequence | ||
from types import SimpleNamespace | ||
|
||
import country_converter as coco | ||
|
@@ -464,6 +465,25 @@ def get_efficiency( | |
return efficiency | ||
|
||
|
||
def valid_grouping_years( | ||
grouping_years: Sequence[int] | pd.Index, | ||
baseyear: int, | ||
max_lifetime: float | None = None, | ||
) -> pd.Index: | ||
grouping_years = pd.Index(sorted(grouping_years)).astype(int) | ||
|
||
valid = grouping_years <= baseyear | ||
FabianHofmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if max_lifetime is not None: | ||
valid &= grouping_years + max_lifetime >= baseyear | ||
if not valid.all(): | ||
logger.warning( | ||
"Only grouping years between [baseyear - max. lifetime, baseyear] are used." | ||
f"Dropping {grouping_years[~valid]}." | ||
) | ||
|
||
return grouping_years[valid] | ||
|
||
|
||
def add_heating_capacities_installed_before_baseyear( | ||
n: pypsa.Network, | ||
costs: pd.DataFrame, | ||
|
@@ -490,7 +510,7 @@ def add_heating_capacities_installed_before_baseyear( | |
Technology costs | ||
baseyear : int | ||
Base year for analysis | ||
grouping_years : list | ||
grouping_years : pd.Index | ||
Intervals to group capacities | ||
heat_pump_cop : xr.DataArray | ||
Heat pump coefficients of performance | ||
|
@@ -532,32 +552,14 @@ def add_heating_capacities_installed_before_baseyear( | |
else: | ||
nodes_elec = nodes | ||
|
||
too_large_grouping_years = [ | ||
gy for gy in grouping_years if gy >= int(baseyear) | ||
] | ||
if too_large_grouping_years: | ||
logger.warning( | ||
f"Grouping years >= baseyear are ignored. Dropping {too_large_grouping_years}." | ||
) | ||
valid_grouping_years = pd.Series( | ||
[ | ||
int(grouping_year) | ||
for grouping_year in grouping_years | ||
if int(grouping_year) + default_lifetime > int(baseyear) | ||
and int(grouping_year) < int(baseyear) | ||
] | ||
) | ||
|
||
assert valid_grouping_years.is_monotonic_increasing | ||
|
||
# get number of years of each interval | ||
_years = valid_grouping_years.diff() | ||
# Fill NA from .diff() with value for the first interval | ||
_years[0] = valid_grouping_years[0] - baseyear + default_lifetime | ||
# Installation is assumed to be linear for the past | ||
ratios = _years / _years.sum() | ||
# get number of years of each interval | ||
_years = pd.Index([grouping_years[0] - baseyear + default_lifetime]).append( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am a bit confused about this expression here, this can be a negative number or? If grouping_year[0] == 1990 and baseyear == 2020 and lifetime == 20 for example. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I must admit, i did not think about this. It is a literal port of Amos':
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, sorry, Amos, i was wrongly blaming you. The origin story of that expression is complicated. @lisazeyen came up with:
ie. (baseyear - last of grouping_years) which sounds quite sensible (for an interval, unless the last grouping year is the same as the baseyear). and then @lindnemi changed it to:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It sounds like there was some back and forth, i'll not try to understand this fully now, maybe its best to have a short discussion. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry for the late reply @coroa! I think I only made some small refactoring changes here. I'll be OOO for a week but happy to discuss this further then, if helpful. |
||
grouping_years.diff() | ||
) | ||
# Installation is assumed to be linear for the past | ||
ratios = _years / np.sum(_years) | ||
|
||
for ratio, grouping_year in zip(ratios, valid_grouping_years): | ||
for ratio, grouping_year in zip(ratios, grouping_years): | ||
# Add heat pumps | ||
for heat_source in heat_pump_source_types[heat_system.system_type.value]: | ||
costs_name = heat_system.heat_pump_costs_name(heat_source) | ||
|
@@ -732,8 +734,10 @@ def add_heating_capacities_installed_before_baseyear( | |
Nyears, | ||
) | ||
|
||
grouping_years_power = snakemake.params.existing_capacities["grouping_years_power"] | ||
grouping_years_heat = snakemake.params.existing_capacities["grouping_years_heat"] | ||
grouping_years_power = valid_grouping_years( | ||
snakemake.params.existing_capacities["grouping_years_power"], | ||
baseyear, | ||
) | ||
add_power_capacities_installed_before_baseyear( | ||
n=n, | ||
costs=costs, | ||
|
@@ -751,6 +755,11 @@ def add_heating_capacities_installed_before_baseyear( | |
year = int(snakemake.params["energy_totals_year"]) | ||
heating_efficiencies = pd.read_csv(fn, index_col=[1, 0]).loc[year] | ||
|
||
grouping_years_heat = valid_grouping_years( | ||
snakemake.params.existing_capacities["grouping_years_heat"], | ||
baseyear, | ||
snakemake.params.existing_capacities["default_heating_lifetime"], | ||
) | ||
add_heating_capacities_installed_before_baseyear( | ||
n=n, | ||
costs=costs, | ||
|
Uh oh!
There was an error while loading. Please reload this page.