-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
366 lines (260 loc) · 12 KB
/
main.py
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import pypsa
import pandas as pd
import os
import logging
logger = logging.getLogger(__name__)
# Suppress logging of the slack bus choices
pypsa.pf.logger.setLevel(logging.WARNING)
from solve_together import *
from additional_constraints import *
from ED_CM import *
import yaml
with open("config.yaml", "r") as f:
config = yaml.load(f, Loader=yaml.FullLoader)
# optional
import warnings
warnings.simplefilter(action='ignore', category=pd.errors.PerformanceWarning)
###############################################################################
def solve_network(n, config, h2buses_df):
def extra_functionality(n, snapshots):
add_battery_constraints(n)
country_res_constraints(n, config)
excess_constraints(n, h2buses_df, config)
sus = n.model.variables["StorageUnit-state_of_charge"]
min_soc = n.storage_units.max_hours * n.storage_units.p_nom * 0.01
max_soc = n.storage_units.max_hours * n.storage_units.p_nom * 0.99
n.model.add_constraints(sus >= min_soc, name="StorageUnit-minimum_soc")
n.model.add_constraints(sus <= max_soc, name="StorageUnit-maximum_soc")
formulation = config['solving']['options']['formulation']
solver_options = config['solving']['solver']
solver_name = solver_options['name']
n.optimize(
extra_functionality=extra_functionality,
formulation=formulation,
solver_name=solver_name,
solver_options=solver_options,
)
def solve_network_dispatch(n, config, h2buses_df):
def extra_functionality(n, snapshots):
excess_constraints(n, h2buses_df, config)
sus = n.model.variables["StorageUnit-state_of_charge"]
min_soc = n.storage_units.max_hours * n.storage_units.p_nom * 0.001
max_soc = n.storage_units.max_hours * n.storage_units.p_nom * 0.999
n.model.add_constraints(sus >= min_soc, name="StorageUnit-minimum_soc")
n.model.add_constraints(sus <= max_soc, name="StorageUnit-maximum_soc")
formulation = config['solving']['options']['formulation']
solver_options = config['solving']['solver']
solver_name = solver_options['name']
n.optimize(
extra_functionality=extra_functionality,
formulation=formulation,
solver_name=solver_name,
solver_options=solver_options,
)
def solve_economic_dispatch(m, config, h2buses_df):
def extra_functionality(m, snapshots):
m.model.constraints.remove("StorageUnit-fix-p_dispatch-lower")
m.model.constraints.remove("StorageUnit-fix-p_dispatch-upper")
m.model.constraints.remove("StorageUnit-fix-p_store-lower")
m.model.constraints.remove("StorageUnit-fix-p_store-upper")
excess_constraints(m, h2buses_df, config)
formulation = config['solving']['options']['formulation']
solver_options = config['solving']['solver']
solver_name = solver_options['name']
m.optimize(
extra_functionality=extra_functionality,
formulation=formulation,
solver_name=solver_name,
solver_options=solver_options,
)
def solve_congestion_management(n, config, h2buses_df):
def extra_functionality(n, snapshots):
n.model.constraints.remove("StorageUnit-fix-p_dispatch-lower")
n.model.constraints.remove("StorageUnit-fix-p_dispatch-upper")
n.model.constraints.remove("StorageUnit-fix-p_store-lower")
n.model.constraints.remove("StorageUnit-fix-p_store-upper")
excess_constraints(n, h2buses_df, config)
formulation = config['solving']['options']['formulation']
solver_options = config['solving']['solver']
solver_name = solver_options['name']
n.optimize(
extra_functionality=extra_functionality,
formulation=formulation,
solver_name=solver_name,
solver_options=solver_options,
)
def solve_congestion_management_custom(n, m, config, h2buses_df):
def extra_functionality(n, snapshots):
n.model.constraints.remove("StorageUnit-fix-p_dispatch-lower")
n.model.constraints.remove("StorageUnit-fix-p_dispatch-upper")
n.model.constraints.remove("StorageUnit-fix-p_store-lower")
n.model.constraints.remove("StorageUnit-fix-p_store-upper")
excess_constraints(n, h2buses_df, config)
# new objective function
weights = n.snapshot_weightings["generators"]
expr=[]
for g in n.generators[n.generators.index.str.contains("ramp up")].index:
expr.append(n.model['Generator-p'].sel(Generator=g)
* weights
* n.generators.loc[g,"marginal_cost"])
for g in n.generators[n.generators.index.str.contains("ramp down")].index:
expr.append(n.model['Generator-p'].sel(Generator=g)
* weights
* -1
* (m.buses_t.marginal_price.BZ - n.generators.loc[g,"marginal_cost"])
)
obj_fct = sum(expr).sum()
n.model.add_objective(obj_fct, overwrite=True)
formulation = config['solving']['options']['formulation']
solver_options = config['solving']['solver']
solver_name = solver_options['name']
n.optimize(
extra_functionality=extra_functionality,
formulation=formulation,
solver_name=solver_name,
solver_options=solver_options,
)
# import network -------------------------------------------------------
o = pypsa.Network(config['network_file'])
################################################
#o.set_snapshots(list(o.snapshots[0:72]))
################################################
# read electrolyser location and capacity file
elys_file = config['elys_path'] + config['scenario']['allocation'] + "_" + config['scenario']['operation_mode'] + "_elys_" + str(config['scenario']['buses']) + ".csv"
elys_df = pd.read_csv(elys_file, delimiter=";")
# create results directory
results_dir = "results/" + config['scenario']['allocation'] + "_" + config['scenario']['operation_mode'] \
+ "_" + str(config['scenario']['offtake_volume']) + "_" + str(config['scenario']['ely_cap']/1000) \
+ "GW/"
if not os.path.exists(results_dir):
os.makedirs(results_dir)
# network pre-modifications ----------------------------------
# add missing carrier and colors
bat_color = o.carriers.color.loc["battery"]
o.madd(
"Carrier",
["H2 electrolysis", "H2 fuel cell", "battery charger", "battery discharger"],
color=["#ff29d9", "#c251ae", bat_color, bat_color]
)
# Add mc to storage links to avoid USC ----------------------------------
o.links.loc[o.links.carrier != "DC", "marginal_cost"] = config["global"]["mc_usc"]
# Set network up ------------------------------------------------
shutdown_lineexp(o)
h2buses_df = prepare_elys(elys_df, config)
add_H2_demand(o, config)
add_CI_gen_bat(o, config)
add_elys(o, h2buses_df, config)
if config["global"]["dummies"]:
add_dummies(o, config)
# Oversize stores 2 %
o.stores.e_min_pu = 0.01
o.stores.e_max_pu = 0.99
# Remove sus with max_hours=0
o.mremove(
"StorageUnit",
o.storage_units[o.storage_units["max_hours"]==0].index
)
###############################################################################
# Build 2030 power system
logger.info("Solve o")
solve_network(o, config, h2buses_df)
# export moved below
print(o.model.constraints)
print("\n#################\n")
print("Power system 2030 - o.nc")
print("Number of variables: ",o.model.nvars)
print("Number of constraints: ",o.model.ncons)
print("Objective value o (Investment + Dispatch): ", o.objective / 1e6 )
print("\n#################\n")
###############################################################################
# ED + CM Preparation
# Fixing optimal capcities
o.optimize.fix_optimal_capacities()
o.export_to_netcdf(results_dir + "o.nc")
o2 = o.copy()
drop_empty_components(o2)
o.stores.e_min_pu = 0.001
o.stores.e_max_pu = 0.999
# solve initial dispatch only -----------------------------------------
logger.info("Solve o2 (dispatch only of o)")
solve_network_dispatch(o2, config, h2buses_df)
o2.export_to_netcdf(results_dir + "o2.nc")
print(o2.model.constraints)
print("\n#################\n")
print("Power system 2030 dispatch only - o2.nc")
print("Number of variables: ",o2.model.nvars)
print("Number of constraints: ",o2.model.ncons)
print("Objective value o2 (Nodal Dispatch): ", o2.objective / 1e6 )
print("\n#################\n")
###############################################
o2_temp = o2.copy()
# Free up oversized space
o2_temp.stores.e_min_pu = 0.0
o2_temp.stores.e_max_pu = 1.0
# Fix variables
stores_e_initial = o2.stores_t.e.iloc[-1,:]
storage_units_soc_initial = o2.storage_units_t.state_of_charge.iloc[-1,:]
# storage_units
o2_temp.storage_units["state_of_charge_initial"] = storage_units_soc_initial
o2_temp.storage_units.cyclic_state_of_charge = False
o2_temp.storage_units_t.p_dispatch_set = o2.storage_units_t.p_dispatch
o2_temp.storage_units_t.p_store_set = o2.storage_units_t.p_store
# stores and links: without CI H2 and bat
o2_temp.stores.e_cyclic = False
o2_temp.stores.e_initial = stores_e_initial
p0_links_pu = o2_temp.links_t.p0 / o2_temp.links.p_nom
o2_temp.links_t.p_min_pu = p0_links_pu - 0.000001
o2_temp.links_t.p_max_pu = p0_links_pu + 0.000001
o2_temp.links_t.p_min_pu.drop(columns=["T10","T18","T20"], inplace=True)
o2_temp.links_t.p_max_pu.drop(columns=["T10","T18","T20"], inplace=True)
###############################################
m = o2_temp.copy() # for market model
n = o2_temp.copy() # for redispatch model
n_custom = o2_temp.copy() # for redispatch model
###########################################
# ED
prepare_economic_dispatch(m)
logger.info("Solve m")
solve_economic_dispatch(m, config, h2buses_df)
m.export_to_netcdf(results_dir + "m.nc")
print(m.model.constraints)
print("\n#################\n")
print("ED - m.nc")
print("Number of variables: ",m.model.nvars)
print("Number of constraints: ",m.model.ncons)
print("Objective value m: ", m.objective / 1e6 )
print("\n#################\n")
###########################################
# CM
prepare_congestion_management(m, n)
logger.info("Solve n")
solve_congestion_management(n, config, h2buses_df)
n.export_to_netcdf(results_dir + "n.nc")
print(n.model.constraints)
print("\n#################\n")
print("CM - n.nc")
print("Number of variables: ",n.model.nvars)
print("Number of constraints: ",n.model.ncons)
print("Objective value n (should be same as o2): ", n.objective / 1e6 )
print("n-m (CM costs in Mio): ", (n.objective - m.objective) / 1e6 )
print("ramp up [TWh]: ", (n.generators_t.p.filter(like="ramp up").groupby(n.generators.carrier, axis=1).sum().sum())
.sum() / 1e6)
print("ramp down [TWh]: ", (n.generators_t.p.filter(like="ramp down").groupby(n.generators.carrier, axis=1).sum().sum())
.sum() / 1e6)
print("\n#################\n")
# CM custom objective function
prepare_congestion_management(m, n_custom)
logger.info("Solve n_custom")
solve_congestion_management_custom(n_custom, m, config, h2buses_df)
n_custom.export_to_netcdf(results_dir + "n_custom.nc")
print(n_custom.model.constraints)
print("\n#################\n")
print("CM CUSTOM - n_custom.nc")
print("Number of variables: ",n_custom.model.nvars)
print("Number of constraints: ",n_custom.model.ncons)
print("objective value (CM costs in Mio): ", n_custom.objective / 1e6 )
print("ramp up [TWh]: ", (n_custom.generators_t.p.filter(like="ramp up").groupby(n_custom.generators.carrier, axis=1).sum().sum())
.sum() / 1e6)
print("ramp down [TWh]: ", (n_custom.generators_t.p.filter(like="ramp down").groupby(n_custom.generators.carrier, axis=1).sum().sum())
.sum() / 1e6)
print("\n#################\n")