solution04-4.ipynb 16.3 KB
Newer Older
sp2668's avatar
sp2668 committed
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
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Simple electricity market examples\n",
    "\n",
    "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.\n",
    "\n",
    "Available as a Jupyter notebook at http://www.pypsa.org/examples/simple-electricity-market-examples.ipynb."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Preliminaries\n",
    "\n",
    "Here libraries are imported and data is defined."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
sp2668's avatar
sp2668 committed
26
   "metadata": {},
sp2668's avatar
sp2668 committed
27
28
29
30
31
32
33
34
   "outputs": [],
   "source": [
    "import pypsa, numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
sp2668's avatar
sp2668 committed
35
   "metadata": {},
sp2668's avatar
sp2668 committed
36
37
38
39
40
   "outputs": [],
   "source": [
    "#marginal costs in EUR/MWh\n",
    "marginal_costs = {\"Wind\" : 0,\n",
    "                  \"Hydro\" : 0,\n",
sp2668's avatar
sp2668 committed
41
    "                  \"Solar\" : 0,\n",
sp2668's avatar
sp2668 committed
42
43
44
45
    "                  \"Coal\" : 30,\n",
    "                  \"Gas\" : 60,\n",
    "                  \"Oil\" : 80}\n",
    "\n",
sp2668's avatar
sp2668 committed
46
47
48
49
50
51
52
53
    "#capital costs in EUR/kW\n",
    "capital_costs = {\"Wind\" : 10000,\n",
    "                 \"Hydro\" : 3000,\n",
    "                 \"Solar\" : 6000,\n",
    "                 \"Coal\" : 35000,\n",
    "                 \"Gas\" : 8000,\n",
    "                 \"Oil\" : 20000}\n",
    "\n",
sp2668's avatar
sp2668 committed
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
    "#power plant capacities (nominal powers in MW) in each country (not necessarily realistic)\n",
    "power_plant_p_nom = {\"South Africa\" : {\"Coal\" : 35000,\n",
    "                                       \"Wind\" : 3000,\n",
    "                                       \"Gas\" : 8000,\n",
    "                                       \"Oil\" : 2000\n",
    "                                      },\n",
    "                     \"Mozambique\" : {\"Hydro\" : 1200,\n",
    "                                    },\n",
    "                     \"Swaziland\" : {\"Hydro\" : 600,\n",
    "                                    },\n",
    "                    }\n",
    "\n",
    "#transmission capacities in MW (not necessarily realistic)\n",
    "transmission = {\"South Africa\" : {\"Mozambique\" : 500,\n",
    "                                  \"Swaziland\" : 250},\n",
    "                \"Mozambique\" : {\"Swaziland\" : 100}}\n",
    "\n",
    "#country electrical loads in MW (not necessarily realistic)\n",
    "loads = {\"South Africa\" : 42000,\n",
    "         \"Mozambique\" : 650,\n",
    "         \"Swaziland\" : 250}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
sp2668's avatar
sp2668 committed
81
    "## Three bidding zones connected by transmission, one period\n",
sp2668's avatar
sp2668 committed
82
    "\n",
sp2668's avatar
sp2668 committed
83
    "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."
sp2668's avatar
sp2668 committed
84
85
86
87
   ]
  },
  {
   "cell_type": "code",
sp2668's avatar
sp2668 committed
88
   "execution_count": 12,
sp2668's avatar
sp2668 committed
89
90
91
92
93
   "metadata": {},
   "outputs": [],
   "source": [
    "network = pypsa.Network()\n",
    "\n",
sp2668's avatar
sp2668 committed
94
    "countries = [\"Swaziland\", \"Mozambique\", \"South Africa\"]\n",
sp2668's avatar
sp2668 committed
95
    "\n",
sp2668's avatar
sp2668 committed
96
97
98
    "for country in countries:\n",
    "    \n",
    "    network.add(\"Bus\",country)\n",
sp2668's avatar
sp2668 committed
99
    "\n",
sp2668's avatar
sp2668 committed
100
101
102
103
104
105
    "    for tech in power_plant_p_nom[country]:\n",
    "        network.add(\"Generator\",\n",
    "                    \"{} {}\".format(country,tech),\n",
    "                    bus=country,\n",
    "                    p_nom=power_plant_p_nom[country][tech],\n",
    "                    marginal_cost=marginal_costs[tech])\n",
sp2668's avatar
sp2668 committed
106
    "\n",
sp2668's avatar
sp2668 committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
    "\n",
    "    network.add(\"Load\",\n",
    "                \"{} load\".format(country),\n",
    "                bus=country,\n",
    "                p_set=loads[country])\n",
    "    \n",
    "    #add transmission as controllable Link\n",
    "    if country not in transmission:\n",
    "        continue\n",
    "    \n",
    "    for other_country in countries:\n",
    "        if other_country not in transmission[country]:\n",
    "            continue\n",
    "        \n",
    "        #NB: Link is by default unidirectional, so have to set p_min_pu = -1\n",
    "        #to allow bidirectional (i.e. also negative) flow\n",
    "        network.add(\"Link\",\n",
    "                   \"{} - {} link\".format(country, other_country),\n",
    "                   bus0=country,\n",
    "                   bus1=other_country,\n",
    "                   p_nom=transmission[country][other_country],\n",
    "                   p_min_pu=-1)"
sp2668's avatar
sp2668 committed
129
130
131
132
   ]
  },
  {
   "cell_type": "code",
sp2668's avatar
sp2668 committed
133
   "execution_count": 13,
sp2668's avatar
sp2668 committed
134
135
136
137
138
139
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
sp2668's avatar
sp2668 committed
140
141
142
      "INFO:pypsa.pf:Slack bus for sub-network 0 is Swaziland\n",
      "INFO:pypsa.pf:Slack bus for sub-network 1 is Mozambique\n",
      "INFO:pypsa.pf:Slack bus for sub-network 2 is South Africa\n",
sp2668's avatar
sp2668 committed
143
144
      "INFO:pypsa.opf:Performed preliminary steps\n",
      "INFO:pypsa.opf:Building pyomo model using `angles` formulation\n",
sp2668's avatar
sp2668 committed
145
146
147
148
149
150
151
152
153
154
      "/home/ws/sp2668/software/anaconda3/lib/python3.6/site-packages/pypsa/components.py:758: FutureWarning:\n",
      "\n",
      "Sorting because non-concatenation axis is not aligned. A future version\n",
      "of pandas will change to not sort by default.\n",
      "\n",
      "To accept the future behavior, pass 'sort=True'.\n",
      "\n",
      "To retain the current behavior and silence the warning, pass sort=False\n",
      "\n",
      "\n",
sp2668's avatar
sp2668 committed
155
156
157
158
      "INFO:pypsa.opf:Solving model using glpk\n",
      "INFO:pypsa.opf:Optimization successful\n"
     ]
    },
sp2668's avatar
sp2668 committed
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
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# ==========================================================\n",
      "# = Solver Results                                         =\n",
      "# ==========================================================\n",
      "# ----------------------------------------------------------\n",
      "#   Problem Information\n",
      "# ----------------------------------------------------------\n",
      "Problem: \n",
      "- Name: unknown\n",
      "  Lower bound: 1245000.0\n",
      "  Upper bound: 1245000.0\n",
      "  Number of objectives: 1\n",
      "  Number of constraints: 13\n",
      "  Number of variables: 13\n",
      "  Number of nonzeros: 22\n",
      "  Sense: minimize\n",
      "# ----------------------------------------------------------\n",
      "#   Solver Information\n",
      "# ----------------------------------------------------------\n",
      "Solver: \n",
      "- Status: ok\n",
      "  Termination condition: optimal\n",
      "  Statistics: \n",
      "    Branch and bound: \n",
      "      Number of bounded subproblems: 0\n",
      "      Number of created subproblems: 0\n",
      "  Error rc: 0\n",
      "  Time: 0.01071619987487793\n",
      "# ----------------------------------------------------------\n",
      "#   Solution Information\n",
      "# ----------------------------------------------------------\n",
      "Solution: \n",
      "- number of solutions: 0\n",
      "  number of solutions displayed: 0\n"
     ]
    },
sp2668's avatar
sp2668 committed
198
199
200
201
202
203
    {
     "data": {
      "text/plain": [
       "('ok', 'optimal')"
      ]
     },
sp2668's avatar
sp2668 committed
204
     "execution_count": 13,
sp2668's avatar
sp2668 committed
205
206
207
208
209
210
211
212
213
214
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "network.lopf()"
   ]
  },
  {
   "cell_type": "code",
sp2668's avatar
sp2668 committed
215
   "execution_count": 14,
sp2668's avatar
sp2668 committed
216
217
218
219
220
221
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
sp2668's avatar
sp2668 committed
222
223
224
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
sp2668's avatar
sp2668 committed
225
226
227
228
229
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
sp2668's avatar
sp2668 committed
230
231
232
233
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
sp2668's avatar
sp2668 committed
234
235
236
237
238
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
sp2668's avatar
sp2668 committed
239
240
       "      <th>Swaziland load</th>\n",
       "      <th>Mozambique load</th>\n",
sp2668's avatar
sp2668 committed
241
242
243
244
245
246
       "      <th>South Africa load</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>now</th>\n",
sp2668's avatar
sp2668 committed
247
248
       "      <td>250.0</td>\n",
       "      <td>650.0</td>\n",
sp2668's avatar
sp2668 committed
249
250
251
252
253
254
255
       "      <td>42000.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
sp2668's avatar
sp2668 committed
256
257
       "     Swaziland load  Mozambique load  South Africa load\n",
       "now           250.0            650.0            42000.0"
sp2668's avatar
sp2668 committed
258
259
      ]
     },
sp2668's avatar
sp2668 committed
260
     "execution_count": 14,
sp2668's avatar
sp2668 committed
261
262
263
264
265
266
267
268
269
270
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "network.loads_t.p"
   ]
  },
  {
   "cell_type": "code",
sp2668's avatar
sp2668 committed
271
   "execution_count": 15,
sp2668's avatar
sp2668 committed
272
273
274
275
276
277
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
sp2668's avatar
sp2668 committed
278
279
280
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
sp2668's avatar
sp2668 committed
281
282
283
284
285
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
sp2668's avatar
sp2668 committed
286
287
288
289
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
sp2668's avatar
sp2668 committed
290
291
292
293
294
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
sp2668's avatar
sp2668 committed
295
296
297
       "      <th>Swaziland Hydro</th>\n",
       "      <th>Mozambique Hydro</th>\n",
       "      <th>South Africa Coal</th>\n",
sp2668's avatar
sp2668 committed
298
299
       "      <th>South Africa Wind</th>\n",
       "      <th>South Africa Gas</th>\n",
sp2668's avatar
sp2668 committed
300
       "      <th>South Africa Oil</th>\n",
sp2668's avatar
sp2668 committed
301
302
303
304
305
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>now</th>\n",
sp2668's avatar
sp2668 committed
306
307
       "      <td>600.0</td>\n",
       "      <td>1050.0</td>\n",
sp2668's avatar
sp2668 committed
308
       "      <td>35000.0</td>\n",
sp2668's avatar
sp2668 committed
309
310
311
       "      <td>3000.0</td>\n",
       "      <td>3250.0</td>\n",
       "      <td>0.0</td>\n",
sp2668's avatar
sp2668 committed
312
313
314
315
316
317
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
sp2668's avatar
sp2668 committed
318
319
320
321
322
       "     Swaziland Hydro  Mozambique Hydro  South Africa Coal  South Africa Wind  \\\n",
       "now            600.0            1050.0            35000.0             3000.0   \n",
       "\n",
       "     South Africa Gas  South Africa Oil  \n",
       "now            3250.0               0.0  "
sp2668's avatar
sp2668 committed
323
324
      ]
     },
sp2668's avatar
sp2668 committed
325
     "execution_count": 15,
sp2668's avatar
sp2668 committed
326
327
328
329
330
331
332
333
334
335
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "network.generators_t.p"
   ]
  },
  {
   "cell_type": "code",
sp2668's avatar
sp2668 committed
336
   "execution_count": 16,
sp2668's avatar
sp2668 committed
337
338
339
340
341
342
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
sp2668's avatar
sp2668 committed
343
344
345
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
sp2668's avatar
sp2668 committed
346
347
348
349
350
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
sp2668's avatar
sp2668 committed
351
352
353
354
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
sp2668's avatar
sp2668 committed
355
356
357
358
359
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
sp2668's avatar
sp2668 committed
360
361
362
       "      <th>Mozambique - Swaziland link</th>\n",
       "      <th>South Africa - Swaziland link</th>\n",
       "      <th>South Africa - Mozambique link</th>\n",
sp2668's avatar
sp2668 committed
363
364
365
366
367
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>now</th>\n",
sp2668's avatar
sp2668 committed
368
369
370
       "      <td>-100.0</td>\n",
       "      <td>-250.0</td>\n",
       "      <td>-500.0</td>\n",
sp2668's avatar
sp2668 committed
371
372
373
374
375
376
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
sp2668's avatar
sp2668 committed
377
378
379
380
381
       "     Mozambique - Swaziland link  South Africa - Swaziland link  \\\n",
       "now                       -100.0                         -250.0   \n",
       "\n",
       "     South Africa - Mozambique link  \n",
       "now                          -500.0  "
sp2668's avatar
sp2668 committed
382
383
      ]
     },
sp2668's avatar
sp2668 committed
384
     "execution_count": 16,
sp2668's avatar
sp2668 committed
385
386
387
388
389
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
sp2668's avatar
sp2668 committed
390
    "network.links_t.p0"
sp2668's avatar
sp2668 committed
391
392
393
394
   ]
  },
  {
   "cell_type": "code",
sp2668's avatar
sp2668 committed
395
   "execution_count": 17,
sp2668's avatar
sp2668 committed
396
397
398
399
400
401
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
sp2668's avatar
sp2668 committed
402
403
404
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
sp2668's avatar
sp2668 committed
405
406
407
408
409
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
sp2668's avatar
sp2668 committed
410
411
412
413
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
sp2668's avatar
sp2668 committed
414
415
416
417
418
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
sp2668's avatar
sp2668 committed
419
420
421
       "      <th>Swaziland</th>\n",
       "      <th>Mozambique</th>\n",
       "      <th>South Africa</th>\n",
sp2668's avatar
sp2668 committed
422
423
424
425
426
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>now</th>\n",
sp2668's avatar
sp2668 committed
427
428
429
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>60.0</td>\n",
sp2668's avatar
sp2668 committed
430
431
432
433
434
435
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
sp2668's avatar
sp2668 committed
436
437
       "     Swaziland  Mozambique  South Africa\n",
       "now        0.0         0.0          60.0"
sp2668's avatar
sp2668 committed
438
439
      ]
     },
sp2668's avatar
sp2668 committed
440
     "execution_count": 17,
sp2668's avatar
sp2668 committed
441
442
443
444
445
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
sp2668's avatar
sp2668 committed
446
447
    "#print the clearing price (corresponding to hydro in S and M, and gas in SA)\n",
    "network.buses_t.marginal_price"
sp2668's avatar
sp2668 committed
448
449
450
451
   ]
  },
  {
   "cell_type": "code",
sp2668's avatar
sp2668 committed
452
   "execution_count": 18,
sp2668's avatar
sp2668 committed
453
454
455
456
457
458
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
sp2668's avatar
sp2668 committed
459
460
461
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
sp2668's avatar
sp2668 committed
462
463
464
465
466
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
sp2668's avatar
sp2668 committed
467
468
469
470
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
sp2668's avatar
sp2668 committed
471
472
473
474
475
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
sp2668's avatar
sp2668 committed
476
477
478
       "      <th>Mozambique - Swaziland link</th>\n",
       "      <th>South Africa - Swaziland link</th>\n",
       "      <th>South Africa - Mozambique link</th>\n",
sp2668's avatar
sp2668 committed
479
480
481
482
483
484
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>now</th>\n",
       "      <td>0.0</td>\n",
sp2668's avatar
sp2668 committed
485
486
       "      <td>60.0</td>\n",
       "      <td>60.0</td>\n",
sp2668's avatar
sp2668 committed
487
488
489
490
491
492
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
sp2668's avatar
sp2668 committed
493
494
       "     Mozambique - Swaziland link  South Africa - Swaziland link  \\\n",
       "now                          0.0                           60.0   \n",
sp2668's avatar
sp2668 committed
495
       "\n",
sp2668's avatar
sp2668 committed
496
497
       "     South Africa - Mozambique link  \n",
       "now                            60.0  "
sp2668's avatar
sp2668 committed
498
499
      ]
     },
sp2668's avatar
sp2668 committed
500
     "execution_count": 18,
sp2668's avatar
sp2668 committed
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#link shadow prices\n",
    "network.links_t.mu_lower"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}