Skip to content
This repository was archived by the owner on Mar 27, 2025. It is now read-only.

Commit 8572393

Browse files
post-discretize the projects in the hacking function to unify later code (#269)
* post-discretize the projects in the hacking function to unify later code * bigger steps for AC lenghts, smaller units for AC&DC * move discretzation * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 06b56b4 commit 8572393

File tree

1 file changed

+60
-40
lines changed

1 file changed

+60
-40
lines changed

workflow/scripts/export_ariadne_variables.py

Lines changed: 60 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3683,19 +3683,7 @@ def get_grid_investments(n, costs, region, length_factor=1.0):
36833683
nep_dc = dc_links.query(
36843684
"index.str.startswith('DC') or index=='TYNDP2020_1' or index=='TYNDP2020_2' or index=='TYNDP2020_23'"
36853685
).index
3686-
dc_expansion = dc_links.p_nom_opt.apply(
3687-
lambda x: get_discretized_value(
3688-
x,
3689-
post_discretization["link_unit_size"]["DC"],
3690-
post_discretization["link_threshold"]["DC"],
3691-
)
3692-
) - dc_links.p_nom_min.apply(
3693-
lambda x: get_discretized_value(
3694-
x,
3695-
post_discretization["link_unit_size"]["DC"],
3696-
post_discretization["link_threshold"]["DC"],
3697-
)
3698-
)
3686+
dc_expansion = dc_links.p_nom_opt - dc_links.p_nom_min
36993687

37003688
dc_investments = dc_expansion * dc_links.overnight_cost * 1e-9
37013689
# International dc_projects are only accounted with half the costs
@@ -3705,19 +3693,9 @@ def get_grid_investments(n, costs, region, length_factor=1.0):
37053693

37063694
ac_lines = n.lines[(n.lines.bus0 + n.lines.bus1).str.contains(region)]
37073695
nep_ac = ac_lines.query("build_year > 2000").index
3708-
ac_expansion = ac_lines.s_nom_opt.apply(
3709-
lambda x: get_discretized_value(
3710-
x,
3711-
post_discretization["line_unit_size"],
3712-
post_discretization["line_threshold"],
3713-
)
3714-
) - n.lines.loc[ac_lines.index].s_nom_min.apply(
3715-
lambda x: get_discretized_value(
3716-
x,
3717-
post_discretization["line_unit_size"],
3718-
post_discretization["line_threshold"],
3719-
)
3720-
)
3696+
# Assuming the lines are already post-discretized
3697+
ac_expansion = ac_lines.s_nom_opt - ac_lines.s_nom_min
3698+
37213699
ac_investments = ac_expansion * ac_lines.overnight_cost * 1e-9
37223700
# International ac_projects are only accounted with half the costs
37233701
ac_investments[
@@ -4458,14 +4436,16 @@ def get_grid_capacity(n, region, year):
44584436
)
44594437
var["Length Additions|Electricity|Transmission|DC"] = (
44604438
dc_links.eval("p_nom_opt - p_nom_min")
4461-
.floordiv(1995)
4439+
.floordiv(995)
4440+
.div(2)
44624441
.multiply(dc_links.length)
44634442
.sum()
44644443
)
44654444
var["Length Additions|Electricity|Transmission|DC|NEP"] = (
44664445
dc_links.loc[nep_dc]
44674446
.eval("p_nom_opt - p_nom_min")
4468-
.floordiv(1995)
4447+
.floordiv(995)
4448+
.div(2)
44694449
.multiply(dc_links.length)
44704450
.sum()
44714451
)
@@ -4483,14 +4463,18 @@ def get_grid_capacity(n, region, year):
44834463
)
44844464
var["Length Additions|Electricity|Transmission|AC"] = (
44854465
ac_lines.eval("s_nom_opt - s_nom_min")
4486-
.floordiv(1695)
4466+
.floordiv(845)
4467+
.div(2)
4468+
.div(3) # Steps of half a `line_unit`, 3 lines are a Trasse
44874469
.multiply(ac_lines.length)
44884470
.sum()
44894471
)
44904472
var["Length Additions|Electricity|Transmission|AC|NEP"] = (
44914473
ac_lines.loc[nep_ac]
44924474
.eval("s_nom_opt - s_nom_min")
4493-
.floordiv(1695)
4475+
.floordiv(845)
4476+
.div(2)
4477+
.div(3)
44944478
.multiply(ac_lines.length)
44954479
.sum()
44964480
)
@@ -4536,30 +4520,50 @@ def get_grid_capacity(n, region, year):
45364520

45374521

45384522
def hack_DC_projects(n, n_start, model_year, snakemake, costs):
4523+
45394524
logger.info(f"Hacking DC projects for year {model_year}")
4540-
logger.warning(f"Assuming all indices of DC projects start with 'DC' or 'TYNDP'")
4525+
4526+
logger.info(f"Assuming all indices of DC projects start with 'DC' or 'TYNDP'")
45414527
tprojs = n.links.loc[
45424528
(n.links.index.str.startswith("DC") | n.links.index.str.startswith("TYNDP"))
45434529
& ~n.links.reversed
45444530
].index
45454531

45464532
future_projects = tprojs[n.links.loc[tprojs, "build_year"] > model_year]
4533+
45474534
current_projects = tprojs[
45484535
(n.links.loc[tprojs, "build_year"] > (model_year - 5))
45494536
& (n.links.loc[tprojs, "build_year"] <= model_year)
45504537
]
45514538
past_projects = tprojs[n.links.loc[tprojs, "build_year"] <= (model_year - 5)]
45524539

4540+
logger.info("Post-Discretizing DC projects")
4541+
# The values in p_nom_opt may already be discretized, here we make sure that
4542+
# the same logic is applied to p_nom and p_nom_min
4543+
for attr in ["p_nom_opt", "p_nom", "p_nom_min"]:
4544+
n.links.loc[tprojs, attr] = n.links.loc[tprojs, attr].apply(
4545+
lambda x: get_discretized_value(
4546+
x,
4547+
snakemake.params.post_discretization["link_unit_size"]["DC"],
4548+
snakemake.params.post_discretization["link_threshold"]["DC"],
4549+
)
4550+
)
4551+
for proj in tprojs:
4552+
if not isclose(n.links.loc[proj, "p_nom"], n_start.links.loc[proj, "p_nom"]):
4553+
logger.warning(
4554+
f"Changed p_nom of {proj} from {n_start.links.loc[proj, 'p_nom']} to {n.links.loc[proj, 'p_nom']}"
4555+
)
4556+
45534557
# Future projects should not have any capacity
45544558
assert isclose(n.links.loc[future_projects, "p_nom_opt"], 0).all()
4559+
45554560
# Setting p_nom to 0 such that n.statistics does not compute negative expanded capex or capacity additions
45564561
# Setting p_nom_min to 0 for the grid_expansion calculation
45574562
# This is ONLY POSSIBLE IN POST-PROCESSING
45584563
# We pretend that the model expanded the grid endogenously
45594564
n.links.loc[future_projects, "p_nom"] = 0
45604565
n.links.loc[future_projects, "p_nom_min"] = 0
45614566

4562-
# Current projects should have their p_nom_opt bigger or equal to p_nom until the year 2030 (Startnetz that we force in)
45634567
if snakemake.params.NEP_year == 2021:
45644568
logger.warning("Switching DC projects to NEP23 costs post-optimization")
45654569
n.links.loc[current_projects, "overnight_cost"] = (
@@ -4574,11 +4578,11 @@ def hack_DC_projects(n, n_start, model_year, snakemake, costs):
45744578
)
45754579
+ costs[0].at["HVDC inverter pair", "investment"] / 1e-9
45764580
)
4577-
4581+
# Current projects should have their p_nom_opt bigger or equal to p_nom until the year 2030 (Startnetz that we force in)
45784582
if model_year <= 2030:
45794583
assert (
4580-
n.links.loc[current_projects, "p_nom"]
4581-
<= n.links.loc[current_projects, "p_nom_opt"]
4584+
n.links.loc[current_projects, "p_nom_opt"] + 0.1
4585+
>= n.links.loc[current_projects, "p_nom"]
45824586
).all()
45834587

45844588
n.links.loc[current_projects, "p_nom"] -= n_start.links.loc[
@@ -4607,9 +4611,25 @@ def hack_AC_projects(n, n_start, model_year, snakemake):
46074611
logger.info(f"Hacking AC projects for year {model_year}")
46084612

46094613
# All transmission projects have build_year > 0, this is implicit in the query
4610-
ac_projs = n.lines.query("@model_year - 5 < build_year <= @model_year").index
4614+
ac_projs = n.lines.query("build_year > 0").index
4615+
current_projects = n.lines.query(
4616+
"@model_year - 5 < build_year <= @model_year"
4617+
).index
4618+
4619+
logger.info("Post-Discretizing AC projects")
4620+
4621+
for attr in ["s_nom_opt", "s_nom", "s_nom_min"]:
4622+
# The values in s_nom_opt may already be discretized, here we make sure that
4623+
# the same logic is applied to s_nom and s_nom_min
4624+
n.lines.loc[ac_projs, attr] = n.lines.loc[ac_projs, attr].apply(
4625+
lambda x: get_discretized_value(
4626+
x,
4627+
snakemake.params.post_discretization["line_unit_size"],
4628+
snakemake.params.post_discretization["line_threshold"],
4629+
)
4630+
)
46114631

4612-
s_nom_start = n_start.lines.loc[ac_projs, "s_nom"].apply(
4632+
s_nom_start = n_start.lines.loc[current_projects, "s_nom"].apply(
46134633
lambda x: get_discretized_value(
46144634
x,
46154635
snakemake.params.post_discretization["line_unit_size"],
@@ -4619,14 +4639,14 @@ def hack_AC_projects(n, n_start, model_year, snakemake):
46194639

46204640
if snakemake.params.NEP_year == 2021:
46214641
logger.warning("Switching AC projects to NEP23 costs post-optimization")
4622-
n.lines.loc[ac_projs, "overnight_cost"] *= 772 / 472
4642+
n.lines.loc[current_projects, "overnight_cost"] *= 772 / 472
46234643

46244644
# Eventhough the lines are available to the model from the start,
46254645
# we pretend that the lines were in expanded in this year
46264646
# s_nom_start is used, because the model may expand lines
46274647
# endogenously before that or after that
4628-
n.lines.loc[ac_projs, "s_nom"] -= s_nom_start
4629-
n.lines.loc[ac_projs, "s_nom_min"] -= s_nom_start
4648+
n.lines.loc[current_projects, "s_nom"] -= s_nom_start
4649+
n.lines.loc[current_projects, "s_nom_min"] -= s_nom_start
46304650

46314651
return n
46324652

0 commit comments

Comments
 (0)