forked from PyPSA/PyPSA
-
Notifications
You must be signed in to change notification settings - Fork 1
/
simple-electricity-market-examples.py
331 lines (243 loc) · 9.97 KB
/
simple-electricity-market-examples.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
## Simple electricity market examples
#
#This Jupyter notebook is meant for teaching purposes. To use it, you need to install a Python environment with Jupyter notebooks, and the Python for Power System Analysis (PyPSA) library. See
#
#https://pypsa.org/doc/installation.html
#
#for tips on installation.
#
#It gradually builds up more and more complicated energy-only electricity markets in PyPSA, starting from a single bidding zone, going up to multiple bidding zones connected with transmission (NTCs) along with variable renewables and storage.
#
#Available as a Jupyter notebook at http://www.pypsa.org/examples/simple-electricity-market-examples.ipynb.
### Preliminaries
#
#Here libraries are imported and data is defined.
import pypsa, numpy as np
#marginal costs in EUR/MWh
marginal_costs = {"Wind" : 0,
"Hydro" : 0,
"Coal" : 30,
"Gas" : 60,
"Oil" : 80}
#power plant capacities (nominal powers in MW) in each country (not necessarily realistic)
power_plant_p_nom = {"South Africa" : {"Coal" : 35000,
"Wind" : 3000,
"Gas" : 8000,
"Oil" : 2000
},
"Mozambique" : {"Hydro" : 1200,
},
"Swaziland" : {"Hydro" : 600,
},
}
#transmission capacities in MW (not necessarily realistic)
transmission = {"South Africa" : {"Mozambique" : 500,
"Swaziland" : 250},
"Mozambique" : {"Swaziland" : 100}}
#country electrical loads in MW (not necessarily realistic)
loads = {"South Africa" : 42000,
"Mozambique" : 650,
"Swaziland" : 250}
### Single bidding zone with fixed load, one period
#
#In this example we consider a single market bidding zone, South Africa.
#
#The inelastic load has essentially infinite marginal utility (or higher than the marginal cost of any generator).
country = "South Africa"
network = pypsa.Network()
network.add("Bus",country)
for tech in power_plant_p_nom[country]:
network.add("Generator",
"{} {}".format(country,tech),
bus=country,
p_nom=power_plant_p_nom[country][tech],
marginal_cost=marginal_costs[tech])
network.add("Load",
"{} load".format(country),
bus=country,
p_set=loads[country])
#Run optimisation to determine market dispatch
network.lopf()
#print the load active power (P) consumption
network.loads_t.p
#print the generator active power (P) dispatch
network.generators_t.p
#print the clearing price (corresponding to gas)
network.buses_t.marginal_price
### Two bidding zones connected by transmission, one period
#
#In this example we have bidirectional transmission capacity between two bidding zones. The power transfer is treated as controllable (like an A/NTC (Available/Net Transfer Capacity) or HVDC line). Note that in the physical grid, power flows passively according to the network impedances.
network = pypsa.Network()
countries = ["Mozambique", "South Africa"]
for country in countries:
network.add("Bus",country)
for tech in power_plant_p_nom[country]:
network.add("Generator",
"{} {}".format(country,tech),
bus=country,
p_nom=power_plant_p_nom[country][tech],
marginal_cost=marginal_costs[tech])
network.add("Load",
"{} load".format(country),
bus=country,
p_set=loads[country])
#add transmission as controllable Link
if country not in transmission:
continue
for other_country in countries:
if other_country not in transmission[country]:
continue
#NB: Link is by default unidirectional, so have to set p_min_pu = -1
#to allow bidirectional (i.e. also negative) flow
network.add("Link",
"{} - {} link".format(country, other_country),
bus0=country,
bus1=other_country,
p_nom=transmission[country][other_country],
p_min_pu=-1)
network.lopf()
network.loads_t.p
network.generators_t.p
network.links_t.p0
#print the clearing price (corresponding to water in Mozambique and gas in SA)
network.buses_t.marginal_price
#link shadow prices
network.links_t.mu_lower
### Three bidding zones connected by transmission, one period
#
#In this example we have bidirectional transmission capacity between three bidding zones. The power transfer is treated as controllable (like an A/NTC (Available/Net Transfer Capacity) or HVDC line). Note that in the physical grid, power flows passively according to the network impedances.
network = pypsa.Network()
countries = ["Swaziland", "Mozambique", "South Africa"]
for country in countries:
network.add("Bus",country)
for tech in power_plant_p_nom[country]:
network.add("Generator",
"{} {}".format(country,tech),
bus=country,
p_nom=power_plant_p_nom[country][tech],
marginal_cost=marginal_costs[tech])
network.add("Load",
"{} load".format(country),
bus=country,
p_set=loads[country])
#add transmission as controllable Link
if country not in transmission:
continue
for other_country in countries:
if other_country not in transmission[country]:
continue
#NB: Link is by default unidirectional, so have to set p_min_pu = -1
#to allow bidirectional (i.e. also negative) flow
network.add("Link",
"{} - {} link".format(country, other_country),
bus0=country,
bus1=other_country,
p_nom=transmission[country][other_country],
p_min_pu=-1)
network.lopf()
network.loads_t.p
network.generators_t.p
network.links_t.p0
#print the clearing price (corresponding to hydro in S and M, and gas in SA)
network.buses_t.marginal_price
#link shadow prices
network.links_t.mu_lower
### Single bidding zone with price-sensitive industrial load, one period
#
#In this example we consider a single market bidding zone, South Africa.
#
#Now there is a large industrial load with a marginal utility which is low enough to interact with the generation marginal cost.
country = "South Africa"
network = pypsa.Network()
network.add("Bus",country)
for tech in power_plant_p_nom[country]:
network.add("Generator",
"{} {}".format(country,tech),
bus=country,
p_nom=power_plant_p_nom[country][tech],
marginal_cost=marginal_costs[tech])
#standard high marginal utility consumers
network.add("Load",
"{} load".format(country),
bus=country,
p_set=loads[country])
#add an industrial load as a dummy negative-dispatch generator with marginal utility of 70 EUR/MWh for 8000 MW
network.add("Generator",
"{} industrial load".format(country),
bus=country,
p_max_pu=0,
p_min_pu=-1,
p_nom=8000,
marginal_cost=70)
network.lopf()
network.loads_t.p
#NB only half of industrial load is served, because this maxes out
#Gas. Oil is too expensive with a marginal cost of 80 EUR/MWh
network.generators_t.p
network.buses_t.marginal_price
### Single bidding zone with fixed load, several periods
#
#In this example we consider a single market bidding zone, South Africa.
#
#We consider multiple time periods (labelled [0,1,2,3]) to represent variable wind generation.
country = "South Africa"
network = pypsa.Network()
#snapshots labelled by [0,1,2,3]
network.set_snapshots(range(4))
network.add("Bus",country)
#p_max_pu is variable for wind
for tech in power_plant_p_nom[country]:
network.add("Generator",
"{} {}".format(country,tech),
bus=country,
p_nom=power_plant_p_nom[country][tech],
marginal_cost=marginal_costs[tech],
p_max_pu=([0.3,0.6,0.4,0.5] if tech == "Wind" else 1),
)
#load which varies over the snapshots
network.add("Load",
"{} load".format(country),
bus=country,
p_set=loads[country] + np.array([0,1000,3000,4000]))
#specify that we consider all snapshots
network.lopf(network.snapshots)
network.loads_t.p
network.generators_t.p
network.buses_t.marginal_price
### Single bidding zone with fixed load and storage, several periods
#
#In this example we consider a single market bidding zone, South Africa.
#
#We consider multiple time periods (labelled [0,1,2,3]) to represent variable wind generation. Storage is allowed to do price arbitrage to reduce oil consumption.
country = "South Africa"
network = pypsa.Network()
#snapshots labelled by [0,1,2,3]
network.set_snapshots(range(4))
network.add("Bus",country)
#p_max_pu is variable for wind
for tech in power_plant_p_nom[country]:
network.add("Generator",
"{} {}".format(country,tech),
bus=country,
p_nom=power_plant_p_nom[country][tech],
marginal_cost=marginal_costs[tech],
p_max_pu=([0.3,0.6,0.4,0.5] if tech == "Wind" else 1),
)
#load which varies over the snapshots
network.add("Load",
"{} load".format(country),
bus=country,
p_set=loads[country] + np.array([0,1000,3000,4000]))
#storage unit to do price arbitrage
network.add("StorageUnit",
"{} pumped hydro".format(country),
bus=country,
p_nom=1000,
max_hours=6, #energy storage in terms of hours at full power
)
network.lopf(network.snapshots)
network.loads_t.p
network.generators_t.p
network.storage_units_t.p
network.storage_units_t.state_of_charge
network.buses_t.marginal_price