solution02.ipynb 18 KB
 sp2668 committed Jun 28, 2018 1 2 3 4 { "cells": [ { "cell_type": "markdown",  sp2668 committed Jun 30, 2018 5 6 7 8 9  "metadata": { "slideshow": { "slide_type": "slide" } },  sp2668 committed Jun 28, 2018 10 11 12 13 14 15  "source": [ "# Energy System Modelling - Solutions to Tutorial II" ] }, { "cell_type": "markdown",  sp2668 committed Jun 30, 2018 16 17 18 19 20  "metadata": { "slideshow": { "slide_type": "skip" } },  sp2668 committed Jun 28, 2018 21 22 23 24 25 26 27  "source": [ "**Imports**" ] }, { "cell_type": "code", "execution_count": 1,  sp2668 committed Jun 30, 2018 28 29 30 31 32  "metadata": { "slideshow": { "slide_type": "skip" } },  sp2668 committed Jun 28, 2018 33 34 35 36 37 38 39 40 41 42 43  "outputs": [], "source": [ "import numpy as np\n", "import numpy.linalg\n", "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "%matplotlib inline" ] }, { "cell_type": "markdown",  sp2668 committed Jun 30, 2018 44 45 46 47 48  "metadata": { "slideshow": { "slide_type": "fragment" } },  sp2668 committed Jun 28, 2018 49 50  "source": [ "***\n",  sp2668 committed Jun 30, 2018 51  "## Problem II.1"  sp2668 committed Jun 28, 2018 52 53 54 55  ] }, { "cell_type": "markdown",  sp2668 committed Jun 30, 2018 56 57 58 59 60  "metadata": { "slideshow": { "slide_type": "slide" } },  sp2668 committed Jun 28, 2018 61 62 63 64 65 66 67 68 69  "source": [ "***\n", "**(a) Compile the nodes list and the edge list.**\n", "> **Remark:** While graph-theoretically both lists are unordered sets, let's agree on an ordering now which can serve as basis for the matrices in the following exercises: we sort everything in ascending numerical order, i.e.\\ node 1 before node 2 and edge (1,2) before (1,4) before (2,3)." ] }, { "cell_type": "code", "execution_count": 142,  sp2668 committed Jun 30, 2018 70 71 72 73 74  "metadata": { "slideshow": { "slide_type": "fragment" } },  sp2668 committed Jun 28, 2018 75 76 77 78 79 80 81 82 83 84  "outputs": [], "source": [ "nodes = [0, 1, 2, 3, 4, 5]\n", "edges = [(0, 1), (1, 2), (1, 4),\n", " (1, 5), (2, 3), (2, 4),\n", " (3, 4), (4, 5)]" ] }, { "cell_type": "markdown",  sp2668 committed Jun 30, 2018 85 86 87 88 89  "metadata": { "slideshow": { "slide_type": "slide" } },  sp2668 committed Jun 28, 2018 90 91 92 93 94 95 96 97  "source": [ "***\n", "**(b) Determine the order and the size of the network.**" ] }, { "cell_type": "code", "execution_count": 143,  sp2668 committed Jun 30, 2018 98 99 100 101 102  "metadata": { "slideshow": { "slide_type": "fragment" } },  sp2668 committed Jun 28, 2018 103 104 105 106 107 108 109 110 111  "outputs": [], "source": [ "N = len(nodes)\n", "E = len(edges)" ] }, { "cell_type": "code", "execution_count": 144,  sp2668 committed Jun 30, 2018 112 113 114 115 116  "metadata": { "slideshow": { "slide_type": "fragment" } },  sp2668 committed Jun 28, 2018 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132  "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Order: 6\n", "Size: 8\n" ] } ], "source": [ "print(\"Order: {}\\nSize: {}\".format(N, E))" ] }, { "cell_type": "markdown",  sp2668 committed Jun 30, 2018 133 134 135 136 137  "metadata": { "slideshow": { "slide_type": "slide" } },  sp2668 committed Jun 28, 2018 138 139 140 141 142 143 144 145  "source": [ "***\n", "**(c) Compute the adjacency matrix $A$ and check that it is symmetric.**" ] }, { "cell_type": "code", "execution_count": 145,  sp2668 committed Jun 30, 2018 146 147 148 149 150  "metadata": { "slideshow": { "slide_type": "fragment" } },  sp2668 committed Jun 28, 2018 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  "outputs": [ { "data": { "text/plain": [ "array([[0., 1., 0., 0., 0., 0.],\n", " [1., 0., 1., 0., 1., 1.],\n", " [0., 1., 0., 1., 1., 0.],\n", " [0., 0., 1., 0., 1., 0.],\n", " [0., 1., 1., 1., 0., 1.],\n", " [0., 1., 0., 0., 1., 0.]])" ] }, "execution_count": 145, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = np.zeros((N, N))\n", "\n", "for u, v in edges:\n", " A[u, v] += 1\n", " A[v, u] += 1\n", "\n", "A" ] }, { "cell_type": "code", "execution_count": 146,  sp2668 committed Jun 30, 2018 181 182 183 184 185  "metadata": { "slideshow": { "slide_type": "fragment" } },  sp2668 committed Jun 28, 2018 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203  "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 146, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(A == A.T).all()" ] }, { "cell_type": "markdown",  sp2668 committed Jun 30, 2018 204 205 206 207 208  "metadata": { "slideshow": { "slide_type": "slide" } },  sp2668 committed Jun 28, 2018 209 210 211 212 213 214 215 216  "source": [ "***\n", "**(d) Find the $k_n$ of each node $n$ and compute the average degree of the network.**" ] }, { "cell_type": "code", "execution_count": 147,  sp2668 committed Jun 30, 2018 217 218 219 220 221  "metadata": { "slideshow": { "slide_type": "fragment" } },  sp2668 committed Jun 28, 2018 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241  "outputs": [ { "data": { "text/plain": [ "array([1., 4., 3., 2., 4., 2.])" ] }, "execution_count": 147, "metadata": {}, "output_type": "execute_result" } ], "source": [ "k = A.sum(axis=1)\n", "k" ] }, { "cell_type": "code", "execution_count": 148,  sp2668 committed Jun 30, 2018 242 243 244 245 246  "metadata": { "slideshow": { "slide_type": "fragment" } },  sp2668 committed Jun 28, 2018 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264  "outputs": [ { "data": { "text/plain": [ "2.6666666666666665" ] }, "execution_count": 148, "metadata": {}, "output_type": "execute_result" } ], "source": [ "k.mean()" ] }, { "cell_type": "markdown",  sp2668 committed Jun 30, 2018 265 266 267 268 269  "metadata": { "slideshow": { "slide_type": "slide" } },  sp2668 committed Jun 28, 2018 270 271 272 273 274 275 276 277  "source": [ "***\n", "**(e) Determine the incidence matrix $K$ by assuming the links are always directed from smaller-numbered node to larger-numbered node, i.e.\\ from node 2 to node 3, instead of from 3 to 2.**" ] }, { "cell_type": "code", "execution_count": 150,  sp2668 committed Jun 30, 2018 278 279 280 281 282  "metadata": { "slideshow": { "slide_type": "fragment" } },  sp2668 committed Jun 28, 2018 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  "outputs": [ { "data": { "text/plain": [ "array([[ 1., 0., 0., 0., 0., 0., 0., 0.],\n", " [-1., 1., 1., 1., 0., 0., 0., 0.],\n", " [ 0., -1., 0., 0., 1., 1., 0., 0.],\n", " [ 0., 0., 0., 0., -1., 0., 1., 0.],\n", " [ 0., 0., -1., 0., 0., -1., -1., 1.],\n", " [ 0., 0., 0., -1., 0., 0., 0., -1.]])" ] }, "execution_count": 150, "metadata": {}, "output_type": "execute_result" } ], "source": [ "K = np.zeros((N,E))\n", "\n", "for i, (u, v) in enumerate(edges):\n", " K[u,i] = 1\n", " K[v,i] = -1\n", " \n", "K" ] }, { "cell_type": "markdown",  sp2668 committed Jun 30, 2018 312 313 314 315 316  "metadata": { "slideshow": { "slide_type": "slide" } },  sp2668 committed Jun 28, 2018 317 318 319 320 321 322 323 324  "source": [ "***\n", "**(f) Compute the Laplacian $L$ of the network using $k_n$ and $A$. Remember that the Laplacian can also be computed as $L=KK^T$ and check that the two definitions agree.**" ] }, { "cell_type": "code", "execution_count": 151,  sp2668 committed Jun 30, 2018 325 326 327 328 329  "metadata": { "slideshow": { "slide_type": "fragment" } },  sp2668 committed Jun 28, 2018 330 331 332 333 334 335 336 337  "outputs": [], "source": [ "D = np.diag(A.sum(axis=1))" ] }, { "cell_type": "code", "execution_count": 152,  sp2668 committed Jun 30, 2018 338 339 340 341 342  "metadata": { "slideshow": { "slide_type": "fragment" } },  sp2668 committed Jun 28, 2018 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367  "outputs": [ { "data": { "text/plain": [ "array([[ 1., -1., 0., 0., 0., 0.],\n", " [-1., 4., -1., 0., -1., -1.],\n", " [ 0., -1., 3., -1., -1., 0.],\n", " [ 0., 0., -1., 2., -1., 0.],\n", " [ 0., -1., -1., -1., 4., -1.],\n", " [ 0., -1., 0., 0., -1., 2.]])" ] }, "execution_count": 152, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L = D - A\n", "L" ] }, { "cell_type": "code", "execution_count": 153,  sp2668 committed Jun 30, 2018 368 369 370 371 372  "metadata": { "slideshow": { "slide_type": "fragment" } },  sp2668 committed Jun 28, 2018 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390  "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 153, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.array_equal(L, K.dot(K.T))" ] }, { "cell_type": "markdown",  sp2668 committed Jun 30, 2018 391 392 393 394 395  "metadata": { "slideshow": { "slide_type": "slide" } },  sp2668 committed Jun 28, 2018 396 397 398 399 400 401 402  "source": [ "***\n", "**(g) Find the diameter of the network by looking at the graph.**" ] }, { "cell_type": "markdown",  sp2668 committed Jun 30, 2018 403 404 405 406 407  "metadata": { "slideshow": { "slide_type": "fragment" } },  sp2668 committed Jun 28, 2018 408 409 410 411 412 413  "source": [ "By looking: Between nodes $0$ and $3$, f.ex. $0 \\to 1 \\to 2 \\to 3$" ] }, { "cell_type": "markdown",  sp2668 committed Jun 30, 2018 414 415 416 417 418  "metadata": { "slideshow": { "slide_type": "slide" } },  sp2668 committed Jun 28, 2018 419 420 421 422 423 424 425 426 427  "source": [ "***\n", "## Problem II.3\n", "\n", "If you map the nodes to 0=DK, 1=DE, 2=CH, 3=IT, 4=AT,5=CZ the network represents a small part of the European electricity network (albeit very simplified). On the [course homepage](https://nworbmot.org/courses/complex_renewable_energy_networks/), you can find the power imbalance time series for the six countries for January 2017 in hourly MW in the file imbalance.csv. They have been derived from physical flows as published by [ENTSO-E](https://transparency.entsoe.eu/transmission-domain/physicalFlow/show)" ] }, { "cell_type": "markdown",  sp2668 committed Jun 30, 2018 428 429 430 431 432  "metadata": { "slideshow": { "slide_type": "subslide" } },  sp2668 committed Jun 28, 2018 433 434 435 436 437 438 439 440  "source": [ "The linear power flow is given by\n", "$$p_i = \\sum_j \\tilde{L}_{i,j}\\theta_j \\qquad \\text{and} \\qquad f_l = \\frac{1}{x_l} \\sum_i K_{i,l}\\theta_i, \\qquad \\text{where} \\qquad \\tilde{L}_{i,j}= \\sum_l = K_{i,l}\\frac{1}{x_l} K_{j,l}$$\n", "is the weighted Laplacian. For simplicity, we assume identity reactance on all links $x_l = 1$." ] }, { "cell_type": "markdown",  sp2668 committed Jun 30, 2018 441 442 443 444 445  "metadata": { "slideshow": { "slide_type": "slide" } },  sp2668 committed Jun 28, 2018 446 447 448 449 450 451 452 453  "source": [ "***\n", "**Read data**" ] }, { "cell_type": "code", "execution_count": 154,  sp2668 committed Jun 30, 2018 454 455 456 457 458  "metadata": { "slideshow": { "slide_type": "fragment" } },  sp2668 committed Jun 28, 2018 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 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 562 563 564 565 566 567 568 569  "outputs": [ { "data": { "text/html": [ "
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", "" ], "text/plain": [ " DK DE CH IT AT CZ\n", "DateTime \n", "2017-01-01 00:00:00 -79.73 3486.25 -3403.37 -97.5 -2867.33 2961.68\n", "2017-01-01 01:00:00 -109.14 4248.17 -3829.59 -105.9 -3149.98 2946.44\n", "2017-01-01 02:00:00 -101.49 5314.56 -4131.66 -199.2 -3646.87 2764.66\n", "2017-01-01 03:00:00 -197.96 5450.71 -4009.61 -656.0 -3345.56 2758.42\n", "2017-01-01 04:00:00 -526.15 5695.72 -3924.43 -646.2 -3393.12 2794.18" ] }, "execution_count": 154, "metadata": {}, "output_type": "execute_result" } ], "source": [ "imbalance = pd.read_csv('imbalance.csv', index_col=0, parse_dates=True)\n", "imbalance.head()" ] }, { "cell_type": "markdown",  sp2668 committed Jun 30, 2018 570 571 572 573 574  "metadata": { "slideshow": { "slide_type": "skip" } },  sp2668 committed Jun 28, 2018 575 576 577 578 579 580 581 582  "source": [ "\\n", "p_u = \\sum_v L_{u,v} \\theta_v\n", "\" ] }, { "cell_type": "markdown",  sp2668 committed Jun 30, 2018 583 584 585 586 587  "metadata": { "slideshow": { "slide_type": "slide" } },  sp2668 committed Jun 28, 2018 588 589 590 591 592 593 594 595  "source": [ "***\n", "**(a) Compute the voltage angles $\\theta_j$ and flows $f_l$ for the first hour in the dataset with the convention of $\\theta_0 = 0$; i.e. the slack bus is at node 0.**\n", "> **Remark:** Linear equation systems are solved efficiently using numpy.linalg.solve." ] }, { "cell_type": "markdown",  sp2668 committed Jun 30, 2018 596 597 598 599 600  "metadata": { "slideshow": { "slide_type": "fragment" } },  sp2668 committed Jun 28, 2018 601 602 603 604 605 606 607  "source": [ "Calculate the *voltage angles* first:" ] }, { "cell_type": "code", "execution_count": 155,  sp2668 committed Jun 30, 2018 608 609 610 611 612  "metadata": { "slideshow": { "slide_type": "fragment" } },  sp2668 committed Jun 28, 2018 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631  "outputs": [ { "data": { "text/plain": [ "array([ 3486.25, -3403.37, -97.5 , -2867.33, 2961.68])" ] }, "execution_count": 155, "metadata": {}, "output_type": "execute_result" } ], "source": [ "imbalance.iloc[0].values[1:]" ] }, { "cell_type": "code", "execution_count": 156,  sp2668 committed Jun 30, 2018 632 633 634 635 636  "metadata": { "slideshow": { "slide_type": "fragment" } },  sp2668 committed Jun 28, 2018 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656  "outputs": [ { "data": { "text/plain": [ "array([ 0. , 79.73 , -2302.97857143, -1995.25809524,\n", " -1590.03761905, 725.68619048])" ] }, "execution_count": 156, "metadata": {}, "output_type": "execute_result" } ], "source": [ "theta = np.r_[0, np.linalg.solve(L[1:,1:], imbalance.iloc[0].values[1:])]\n", "theta" ] }, { "cell_type": "markdown",  sp2668 committed Jun 30, 2018 657 658 659 660 661  "metadata": { "slideshow": { "slide_type": "subslide" } },  sp2668 committed Jun 28, 2018 662 663 664 665 666 667 668  "source": [ "Then, calculate the *flows*:" ] }, { "cell_type": "code", "execution_count": 157,  sp2668 committed Jun 30, 2018 669 670 671 672 673  "metadata": { "slideshow": { "slide_type": "fragment" } },  sp2668 committed Jun 28, 2018 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693  "outputs": [ { "data": { "text/plain": [ "array([ -79.73 , 2382.70857143, 1669.76761905, -645.95619048,\n", " -307.72047619, -712.94095238, -405.22047619, -2315.72380952])" ] }, "execution_count": 157, "metadata": {}, "output_type": "execute_result" } ], "source": [ "flows = K.T.dot(theta)\n", "flows" ] }, { "cell_type": "markdown",  sp2668 committed Jun 30, 2018 694 695 696 697 698  "metadata": { "slideshow": { "slide_type": "slide" } },  sp2668 committed Jun 28, 2018 699 700 701 702 703 704 705 706  "source": [ "***\n", "**(b) Determine the average flow on each link for January 2017 and draw it as a directed network**" ] }, { "cell_type": "code", "execution_count": 158,  sp2668 committed Jun 30, 2018 707 708 709 710 711  "metadata": { "slideshow": { "slide_type": "fragment" } },  sp2668 committed Jun 28, 2018 712 713 714 715 716 717 718 719  "outputs": [], "source": [ "flows = K.T.dot(np.vstack([np.zeros((1, len(imbalance))), np.linalg.solve(L[1:,1:], imbalance.values[:,1:].T)]))" ] }, { "cell_type": "code", "execution_count": 159,  sp2668 committed Jun 30, 2018 720 721 722 723 724  "metadata": { "slideshow": { "slide_type": "fragment" } },  sp2668 committed Jun 28, 2018 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743  "outputs": [ { "data": { "text/plain": [ "(8, 744)" ] }, "execution_count": 159, "metadata": {}, "output_type": "execute_result" } ], "source": [ "flows.shape" ] }, { "cell_type": "code", "execution_count": 160,  sp2668 committed Jun 30, 2018 744 745 746 747 748  "metadata": { "slideshow": { "slide_type": "fragment" } },  sp2668 committed Jun 28, 2018 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768  "outputs": [ { "data": { "text/plain": [ "array([ 451.02294355, 2402.42223502, 2124.94871608, 426.46578277,\n", " 421.1171115 , -277.47351895, -698.59063044, -1698.48293331])" ] }, "execution_count": 160, "metadata": {}, "output_type": "execute_result" } ], "source": [ "avg_flows = flows.mean(axis=1)\n", "avg_flows" ] } ], "metadata": {  sp2668 committed Jun 30, 2018 769  "celltoolbar": "Slideshow",  sp2668 committed Jun 28, 2018 770  "kernelspec": {  sp2668 committed Jun 30, 2018 771  "display_name": "Python [default]",  sp2668 committed Jun 28, 2018 772 773 774 775 776 777 778 779 780 781 782 783 784  "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",  sp2668 committed Jun 30, 2018 785  "version": "3.6.5"  sp2668 committed Jun 28, 2018 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836  }, "nav_menu": {}, "toc": { "navigate_menu": true, "number_sections": true, "sideBar": false, "threshold": 6, "toc_cell": false, "toc_section_display": "block", "toc_window_display": false }, "toc_position": { "height": "47px", "left": "595px", "right": "20px", "top": "-14px", "width": "318px" }, "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": 2 }