forked from open-energy-transition/pypsa-eur
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathprepare_project.py
More file actions
223 lines (187 loc) · 7.06 KB
/
Copy pathprepare_project.py
File metadata and controls
223 lines (187 loc) · 7.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# SPDX-FileCopyrightText: Contributors to Open-TYNDP <https://github.com/open-energy-transition/open-tyndp>
#
# SPDX-License-Identifier: MIT
"""
Prepare a single CBA project network based on the assigned method (TOOT/PINT).
TOOT removes the project from the reference network, PINT adds the project.
Handles multi-border projects, creates links when needed, and validates capacity changes.
"""
import logging
import pandas as pd
import pypsa
from scripts._helpers import configure_logging, set_scenario_config
from scripts.cba._helpers import get_link_attrs
logger = logging.getLogger(__name__)
def check_method(method: str) -> str:
"""
Normalize and validate a given CBA method name.
Raises
------
ValueError
If the normalized value is neither "pint" nor "toot".
"""
method = method.lower().strip()
if method not in ["pint", "toot"]:
raise ValueError(f"Method must be 'pint' or 'toot', got: {method}")
return method
def load_method(methods_fn: str, project_id: int, planning_horizon: int) -> str:
"""
Load the method for a specific project and planning horizon.
Parameters
----------
methods_fn : str
Path to the file defining the methods.
project_id : int
Project reference ID.
planning_horizon : int
Planning horizon.
Returns
-------
str
Method to be used to assess a project at a planning horizon.
"""
methods = pd.read_csv(methods_fn)
row = methods[
(methods["project_id"] == project_id)
& (methods["planning_horizon"] == planning_horizon)
]
if row.empty:
raise ValueError(
f"Missing CBA method for project {project_id} and horizon {planning_horizon}"
)
return check_method(row["method"].iloc[0])
def apply_toot(
n: pypsa.Network,
transmission_project: pd.DataFrame,
negative_toot_option: str,
) -> None:
for _, project in transmission_project.iterrows():
bus0 = project["bus0"]
bus1 = project["bus1"]
link_id = f"{bus0}-{bus1}-DC"
reverse_link_id = f"{bus1}-{bus0}-DC"
capacity = project["p_nom 0->1"]
capacity_reverse = project["p_nom 1->0"]
result_capacity = n.links.loc[link_id, "p_nom"] - capacity
result_capacity_reverse = (
n.links.loc[reverse_link_id, "p_nom"] - capacity_reverse
)
if result_capacity < 0 or result_capacity_reverse < 0:
logger.warning(
"Applying TOOT for project %s (%s) would create negative capacity: "
"%s %.0f -> %.0f MW after removing %.0f MW, "
"%s %.0f -> %.0f MW after removing %.0f MW (policy=%s).",
project["project_id"],
project["project_name"],
link_id,
n.links.loc[link_id, "p_nom"],
result_capacity,
capacity,
reverse_link_id,
n.links.loc[reverse_link_id, "p_nom"],
result_capacity_reverse,
capacity_reverse,
negative_toot_option,
)
if negative_toot_option == "break":
raise ValueError(
"Cannot remove more capacity than exists in the network."
)
if negative_toot_option == "zero":
result_capacity = max(result_capacity, 0)
result_capacity_reverse = max(result_capacity_reverse, 0)
else:
raise ValueError(
f"Unknown cba.negative_toot_option policy: {negative_toot_option}"
)
if result_capacity == 0:
n.remove("Link", link_id)
logger.debug("Removed link %s (capacity reached zero)", link_id)
else:
n.links.loc[link_id, "p_nom"] = result_capacity
if result_capacity_reverse == 0:
n.remove("Link", reverse_link_id)
logger.debug("Removed link %s (capacity reached zero)", reverse_link_id)
else:
n.links.loc[reverse_link_id, "p_nom"] = result_capacity_reverse
def apply_pint(
n: pypsa.Network,
transmission_project: pd.DataFrame,
hurdle_costs: float,
costs: pd.DataFrame,
) -> None:
for _, project in transmission_project.iterrows():
bus0 = project["bus0"]
bus1 = project["bus1"]
link_id = f"{bus0}-{bus1}-DC"
reverse_link_id = f"{bus1}-{bus0}-DC"
capacity = project["p_nom 0->1"]
capacity_reverse = project["p_nom 1->0"]
if link_id in n.links.index and reverse_link_id in n.links.index:
n.links.loc[link_id, "p_nom"] += capacity
n.links.loc[reverse_link_id, "p_nom"] += capacity_reverse
continue
attrs = get_link_attrs(project, costs)
for lid, b0, b1, cap in [
(link_id, bus0, bus1, capacity),
(reverse_link_id, bus1, bus0, capacity_reverse),
]:
n.add(
"Link",
lid,
bus0=b0,
bus1=b1,
carrier="DC",
p_nom=cap,
marginal_cost=hurdle_costs,
**attrs,
)
if __name__ == "__main__":
if "snakemake" not in globals():
from scripts._helpers import mock_snakemake
snakemake = mock_snakemake(
"prepare_project",
cba_project="t1",
planning_horizons="2030",
run="NT",
configfiles=["config/config.tyndp.yaml"],
)
configure_logging(snakemake)
set_scenario_config(snakemake)
cba_project = snakemake.wildcards.cba_project
planning_horizon = int(snakemake.wildcards.planning_horizons)
methods_fn = snakemake.input.methods
transmission_projects = pd.read_csv(snakemake.input.transmission_projects)
n = pypsa.Network(snakemake.input.network)
hurdle_costs = snakemake.params.hurdle_costs
costs = pd.read_csv(snakemake.input.costs, index_col=0)
project_id = int(cba_project[1:])
if planning_horizon not in [2030, 2040]:
logger.warning(
"CBA methods are only available for 2030 or 2040. Using 2040 for planning horizon %s.",
snakemake.wildcards.planning_horizons,
)
planning_horizon = 2040
method = load_method(methods_fn, project_id, planning_horizon)
negative_toot_capacity = snakemake.config["cba"].get(
"negative_toot_capacity", "zero"
)
transmission_project = transmission_projects[
transmission_projects["project_id"] == project_id
]
assert not transmission_project.empty, (
f"Transmission project {project_id} not found."
)
if method == "toot":
apply_toot(n, transmission_project, negative_toot_capacity)
elif method == "pint":
apply_pint(n, transmission_project, hurdle_costs, costs)
else:
raise ValueError(f"Unknown method {method} for project {project_id}")
logger.info(
"Saved %s project network for project %s (%s borders)",
method,
project_id,
len(transmission_project),
)
n.export_to_netcdf(snakemake.output.network)