{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# CoSim - RTED" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "import andes\n", "import ams\n", "\n", "import pandas as pd\n", "\n", "import json\n", "\n", "import cvxpy as cp" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0.6.5.post111.dev0+g3952d43'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ams.__version__" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "andes.config_logger(stream_level=20)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "ams.config_logger(stream_level=10)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Working directory: \"/Users/jinningwang/Documents/work/ams/dev/demo\"\n", "Input format guessed as xlsx.\n", "Parsing input file \"/Users/jinningwang/Documents/work/ams/ams/cases/ieee14/ieee14_rted.xlsx\"...\n", "Input file parsed in 0.2085 seconds.\n", "Adjust bus index to start from 0.\n", "System set up in 0.0064 seconds.\n" ] } ], "source": [ "sp = ams.load(ams.get_case('ieee14/ieee14_rted.xlsx'),\n", " setup=True,\n", " )" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Setup model of RTED\n", "RTED data check passed.\n", "- Generating symbols for RTED\n", "Set constrs pb: sum(pd) - sum(pg)\n", "Set constrs pinj: Cg@(pn - pd) - pg\n", "Set constrs lub: PTDF @ (pn - pd) - rate_a\n", "Set constrs llb: - PTDF @ (pn - pd) - rate_a\n", "Set constrs rbu: gs @ pru - du\n", "Set constrs rbd: gs @ prd - dd\n", "Set constrs rru: pg + pru - pmax\n", "Set constrs rrd: -pg + prd - pmin\n", "Set constrs rgu: pg - pg0 - R10\n", "Set constrs rgd: -pg + pg0 - R10\n", "RTED model set up in 0.0129 seconds.\n", "RTED solved as optimal in 0.0097 seconds with exit code 0.\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.RTED.run()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Setup model for ACOPF\n", "ACOPF data check passed.\n", "- Generating symbols for ACOPF\n", "Set constrs pb: sum(pd) - sum(pg)\n", "ACOPF model set up in 0.0019 seconds.\n", "Adjust bus index to start from 0.\n", "PYPOWER Version 5.1.4, 27-June-2018\n", " -- AC Optimal Power Flow\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Python Interior Point Solver - PIPS, Version 1.0, 07-Feb-2011\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "ACOPF completed in 0.2657 seconds with exit code 0.\n", "RTED is converted to AC.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Converged!\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.RTED.dc2ac()" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "JSON file written to \"ieee14_rted.json\"\n", "System convert to ANDES in 0.0806 seconds, saved as \"ieee14_rted.json\".\n", "Working directory: \"/Users/jinningwang/Documents/work/ams/dev/demo\"\n", "> Loaded config from file \"/Users/jinningwang/.andes/andes.rc\"\n", "> Loaded generated Python code in \"/Users/jinningwang/.andes/pycode\".\n", "Parsing input file \"ieee14_rted.json\"...\n", "Input file parsed in 0.0019 seconds.\n", "Converted file is removed. Set \"no_keep=False\" to keep it.\n", "Addfile format guessed as xlsx.\n", "Parsing additional file \"/Users/jinningwang/Documents/work/mambaforge/envs/ams/lib/python3.9/site-packages/andes/cases/ieee14/ieee14_wt3.xlsx\"...\n", "Power flow models exist in the addfile. Only dynamic models will be used.\n", "Adjust IdxParam \n", "Adjust IdxParam \n", "Addfile parsed in 0.0697 seconds.\n", "System has not been linked to ANDES. Call ``dyn.link_andes(adsys=sa)`` after setup ANDES system.\n", "System internal structure set up in 0.0231 seconds.\n", "AMS system 0x103f662b0 is linked to the ANDES system 0x15ba71d90.\n" ] } ], "source": [ "sa = sp.to_andes(setup=False,\n", " addfile=andes.get_case('ieee14/ieee14_wt3.xlsx'),\n", " overwrite=True,\n", " keep=False,\n", " no_output=True,\n", " )\n", "\n", "sa.add(\"Toggle\", dict(model='SynGen', dev=\"GENROU_3\", t=11.0))\n", "\n", "sa.setup()\n", "\n", "sp.dyn.link_andes(sa)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "RTED results has been converted to AC.\n", "Sending RTED results to ANDES <0x15ba71d90>...\n", "Sending results to models...\n", "Var is mapped from .\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.dyn.send()" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "-> System connectivity check results:\n", " No islanded bus detected.\n", " System is interconnected.\n", " Each island has a slack bus correctly defined and enabled.\n", "\n", "-> Power flow calculation\n", " Numba: Off\n", " Sparse solver: KLU\n", " Solution method: NR method\n", "Power flow initialized in 0.0040 seconds.\n", "0: |F(x)| = 0.5605182162\n", "1: |F(x)| = 0.006202200463\n", "2: |F(x)| = 5.819382991e-06\n", "3: |F(x)| = 6.960640397e-12\n", "Converged in 4 iterations in 0.0043 seconds.\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sa.PFlow.run()" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "idx_tg = sa.TGOV1.idx.v\n", "vmax0 = sa.TGOV1.get(src='VMAX', attr='v', idx=idx_tg)\n", "sa.TGOV1.set(src='VMAX', attr='v', idx=idx_tg, value=10 * vmax0)\n", "vmin0 = sa.TGOV1.get(src='VMIN', attr='v', idx=idx_tg)\n", "sa.TGOV1.set(src='VMIN', attr='v', idx=idx_tg, value=np.zeros_like(vmin0))" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "sa.TDS.config.criteria = 0" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\n", "-> Time Domain Simulation Summary:\n", "Sparse Solver: KLU\n", "Simulation time: 0.0-10 s.\n", "Fixed step size: h=33.33 ms. Shrink if not converged.\n", "Initialization for dynamics completed in 0.0481 seconds.\n", "Initialization was successful.\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "5a042bb680fb445ca1b0be9b57c89e07", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/100 [00:00: Line.Line_1 status changed to 0 at t=1.0 sec.\n", ": Line.Line_1 status changed to 1 at t=1.1 sec.\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Simulation to t=10.00 sec completed in 0.8113 seconds.\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sa.TDS.config.tf = 10\n", "sa.TDS.run()" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "c971bde974334c21905767c2005632de", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/100 [00:00: SynGen.GENROU_3 status changed to 0 at t=11.0 sec.\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Simulation to t=15.00 sec completed in 0.7521 seconds.\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sa.TDS.config.tf = 15\n", "sa.TDS.run()" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "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", " \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", " \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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idxunamebusgencoicoi2SnVnfn...gammaqxdxqxd2xq1xq2Td10Td20Tq10Tq20
uid
0GENROU_11.0GENROU_11Slack_1NoneNone100.069.060.0...1.01.81.750.230.80.236.50.060.20.05
1GENROU_21.0GENROU_22PV_2NoneNone100.069.060.0...1.01.81.750.280.80.286.50.060.20.05
2GENROU_30.0GENROU_33PV_3NoneNone100.069.060.0...1.01.81.750.340.80.346.50.060.20.05
3GENROU_41.0GENROU_46PV_4NoneNone100.0138.060.0...1.01.81.750.280.80.286.50.060.20.05
\n", "

4 rows × 30 columns

\n", "
" ], "text/plain": [ " idx u name bus gen coi coi2 Sn Vn fn \\\n", "uid \n", "0 GENROU_1 1.0 GENROU_1 1 Slack_1 None None 100.0 69.0 60.0 \n", "1 GENROU_2 1.0 GENROU_2 2 PV_2 None None 100.0 69.0 60.0 \n", "2 GENROU_3 0.0 GENROU_3 3 PV_3 None None 100.0 69.0 60.0 \n", "3 GENROU_4 1.0 GENROU_4 6 PV_4 None None 100.0 138.0 60.0 \n", "\n", " ... gammaq xd xq xd2 xq1 xq2 Td10 Td20 Tq10 Tq20 \n", "uid ... \n", "0 ... 1.0 1.8 1.75 0.23 0.8 0.23 6.5 0.06 0.2 0.05 \n", "1 ... 1.0 1.8 1.75 0.28 0.8 0.28 6.5 0.06 0.2 0.05 \n", "2 ... 1.0 1.8 1.75 0.34 0.8 0.34 6.5 0.06 0.2 0.05 \n", "3 ... 1.0 1.8 1.75 0.28 0.8 0.28 6.5 0.06 0.2 0.05 \n", "\n", "[4 rows x 30 columns]" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sa.GENROU.as_df()" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Receiving ANDES <0x15ba71d90> results to RTED.\n", "Receiving results to RTED...\n", "Generator online status are received from dynamic generators.\n", "Generator output power are received from dynamic generators.\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.dyn.receive()" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "NumParam: GENROU.u, v=[1. 1. 0. 1.], vin=[1. 1. 1. 1.]" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sa.GENROU.u" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "NumParam: PV.u, v=[1. 0. 1. 1.], vin=[1. 1. 1. 1.]" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.PV.u" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "OrderedDict([('pg',\n", " Var: StaticGen.pg, a=[0 1 2 3 4], v=[0.4999834 0.11666705 0.12500038 0.12500038 1.42680921]),\n", " ('pn',\n", " Var: Bus.pn, a=[ 5 6 7 8 9 10 11 12 13 14 15 16 17 18], v=[ 0.5 0.717 1. -0. -0. 0.5185 -0. 0.3685 -0.\n", " -0. -0. -0. -0. -0. ]),\n", " ('pru',\n", " Var: StaticGen.pru, a=[19 20 21 22 23], v=[0. 0. 0.025 0.025 0.05 ]),\n", " ('prd',\n", " Var: StaticGen.prd, a=[24 25 26 27 28], v=[0.01666667 0.01666667 0.025 0.025 0.01666667]),\n", " ('vBus',\n", " Var: Bus.vBus, a=[14 15 16 17 18 19 20 21 22 23 24 25 26 27], v=[1.09999758 1.08288953 1.05405178 1.05693288 1.06380295 1.0605452\n", " 1.07106374 1.08355769 1.06790801 1.05927457 1.05654399 1.04971782\n", " 1.0482145 1.05911482])])" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.RTED.vars" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "RTED data check passed.\n", "- Generating symbols for RTED\n", "Set constrs pb: sum(pd) - sum(pg)\n", "Set constrs pinj: Cg@(pn - pd) - pg\n", "Set constrs lub: PTDF @ (pn - pd) - rate_a\n", "Set constrs llb: - PTDF @ (pn - pd) - rate_a\n", "Set constrs rbu: gs @ pru - du\n", "Set constrs rbd: gs @ prd - dd\n", "Set constrs rru: pg + pru - pmax\n", "Set constrs rrd: -pg + prd - pmin\n", "Set constrs rgu: pg - pg0 - R10\n", "Set constrs rgd: -pg + pg0 - R10\n", "RTED model set up in 0.0088 seconds.\n", "RTED failed as infeasible with exit code 1!\n" ] }, { "data": { "text/plain": [ "False" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.RTED.run()" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Var: StaticGen.pg, a=[0 1 2 3 4], v=None" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.RTED.pg" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "RTED is not solved optimally.\n" ] }, { "data": { "text/plain": [ "False" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.dyn.send()" ] } ], "metadata": { "kernelspec": { "display_name": "ams", "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.9.16" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "d2b3bf80176349caa68dc4a3c77bd06eaade8abc678330f7d1c813c53380e5d2" } } }, "nbformat": 4, "nbformat_minor": 2 }