{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Interoperation with ANDES" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One of the most interesting feature of AMS is its interoperation with dynamic simulator ANDES.\n", "\n", "Interoperation includes compatible case conversion and data exchange, thus it facilitates dispatch-dynamic co-simulation using AMS and ANDES." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "import andes\n", "import ams\n", "\n", "import datetime" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Last run time: 2024-02-24 17:29:32\n", "andes:1.9.0\n", "ams:0.8.5.post62.dev0+gf6ed683\n" ] } ], "source": [ "print(\"Last run time:\", datetime.datetime.now().strftime(\"%Y-%m-%d %H:%M:%S\"))\n", "\n", "print(f'andes:{andes.__version__}')\n", "print(f'ams:{ams.__version__}')" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "ams.config_logger(stream_level=20)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Dispatch" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Parsing input file \"/Users/jinningwang/Documents/work/ams/ams/cases/ieee14/ieee14_uced.xlsx\"...\n", "Input file parsed in 0.1225 seconds.\n", "Zero line rates detacted in rate_a, rate_b, rate_c, adjusted to 999.\n", "If expect a line outage, please set 'u' to 0.\n", "System set up in 0.0042 seconds.\n" ] } ], "source": [ "sp = ams.load(ams.get_case('ieee14/ieee14_uced.xlsx'),\n", " setup=True,\n", " no_output=True,)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Routine <RTED> initialized in 0.0171 seconds.\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.RTED.init()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "RTED solved as optimal in 0.0245 seconds, converged after 11 iterations using solver ECOS.\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.RTED.run(solver='ECOS')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Convert to ANDES" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The built-in ANDES interface can convert an AMS case to ANDES case in memory.\n", "\n", "The bridge between AMS and converted ANDES is the shared power flow devices, Bus, PQ, PV, Slack, Line, and Shunt." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Parsing additional file \"/Users/jinningwang/Documents/work/mambaforge/envs/ams/lib/python3.9/site-packages/andes/cases/ieee14/ieee14_full.xlsx\"...\n", "Following PFlow models in addfile will be overwritten: <Bus>, <PQ>, <PV>, <Slack>, <Shunt>, <Line>, <Area>\n", "Addfile parsed in 0.0519 seconds.\n", "System converted to ANDES in 0.1987 seconds.\n", "AMS system 0x10a441af0 is linked to the ANDES system 0x17f0720a0.\n" ] } ], "source": [ "sa = sp.to_andes(setup=True,\n", " addfile=andes.get_case('ieee14/ieee14_full.xlsx'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you wish to add devices to the converted ANDES system, set `setup=False` to skip the ANDES setup process.\n", "\n", "As indicated by the output information, in the conversion process, ANDES power flow devices will be overwritten by AMS ones, if exists.\n", "\n", "Upon a successful conversion, you are ready to enjoy full capability of ANDES." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "``help`` command can give a quick reference." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on method to_andes in module ams.system:\n", "\n", "to_andes(setup=True, addfile=None, **kwargs) method of ams.system.System instance\n", " Convert the AMS system to an ANDES system.\n", " \n", " A preferred dynamic system file to be added has following features:\n", " 1. The file contains both power flow and dynamic models.\n", " 2. The file can run in ANDES natively.\n", " 3. Power flow models are in the same shape as the AMS system.\n", " 4. Dynamic models, if any, are in the same shape as the AMS system.\n", " \n", " Parameters\n", " ----------\n", " setup : bool, optional\n", " Whether to call `setup()` after the conversion. Default is True.\n", " addfile : str, optional\n", " The additional file to be converted to ANDES dynamic mdoels.\n", " **kwargs : dict\n", " Keyword arguments to be passed to `andes.system.System`.\n", " \n", " Returns\n", " -------\n", " andes : andes.system.System\n", " The converted ANDES system.\n", " \n", " Examples\n", " --------\n", " >>> import ams\n", " >>> import andes\n", " >>> sp = ams.load(ams.get_case('ieee14/ieee14_rted.xlsx'), setup=True)\n", " >>> sa = sp.to_andes(setup=False,\n", " ... addfile=andes.get_case('ieee14/ieee14_wt3.xlsx'),\n", " ... overwrite=True, no_keep=True, no_output=True)\n", "\n" ] } ], "source": [ "help(sp.to_andes)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Interoperation with ANDES" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the interface class ``dyn``, the link table is stored in ``dyn.link``.\n", "\n", "It describes the mapping relationships between power flow devices and dynamic devices." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "<div>\n", "<style scoped>\n", " .dataframe tbody tr th:only-of-type {\n", " vertical-align: middle;\n", " }\n", "\n", " .dataframe tbody tr th {\n", " vertical-align: top;\n", " }\n", "\n", " .dataframe thead th {\n", " text-align: right;\n", " }\n", "</style>\n", "<table border=\"1\" class=\"dataframe\">\n", " <thead>\n", " <tr style=\"text-align: right;\">\n", " <th></th>\n", " <th>stg_idx</th>\n", " <th>bus_idx</th>\n", " <th>syg_idx</th>\n", " <th>gov_idx</th>\n", " <th>dg_idx</th>\n", " <th>rg_idx</th>\n", " <th>gammap</th>\n", " <th>gammaq</th>\n", " </tr>\n", " </thead>\n", " <tbody>\n", " <tr>\n", " <th>0</th>\n", " <td>Slack_1</td>\n", " <td>1</td>\n", " <td>GENROU_1</td>\n", " <td>TGOV1_1</td>\n", " <td>NaN</td>\n", " <td>NaN</td>\n", " <td>1.0</td>\n", " <td>1.0</td>\n", " </tr>\n", " <tr>\n", " <th>1</th>\n", " <td>PV_5</td>\n", " <td>8</td>\n", " <td>GENROU_5</td>\n", " <td>TGOV1_5</td>\n", " <td>NaN</td>\n", " <td>NaN</td>\n", " <td>1.0</td>\n", " <td>1.0</td>\n", " </tr>\n", " <tr>\n", " <th>2</th>\n", " <td>PV_4</td>\n", " <td>6</td>\n", " <td>GENROU_4</td>\n", " <td>TGOV1_4</td>\n", " <td>NaN</td>\n", " <td>NaN</td>\n", " <td>1.0</td>\n", " <td>1.0</td>\n", " </tr>\n", " <tr>\n", " <th>3</th>\n", " <td>PV_3</td>\n", " <td>3</td>\n", " <td>GENROU_3</td>\n", " <td>TGOV1_3</td>\n", " <td>NaN</td>\n", " <td>NaN</td>\n", " <td>1.0</td>\n", " <td>1.0</td>\n", " </tr>\n", " <tr>\n", " <th>4</th>\n", " <td>PV_2</td>\n", " <td>2</td>\n", " <td>GENROU_2</td>\n", " <td>TGOV1_2</td>\n", " <td>NaN</td>\n", " <td>NaN</td>\n", " <td>1.0</td>\n", " <td>1.0</td>\n", " </tr>\n", " </tbody>\n", "</table>\n", "</div>" ], "text/plain": [ " stg_idx bus_idx syg_idx gov_idx dg_idx rg_idx gammap gammaq\n", "0 Slack_1 1 GENROU_1 TGOV1_1 NaN NaN 1.0 1.0\n", "1 PV_5 8 GENROU_5 TGOV1_5 NaN NaN 1.0 1.0\n", "2 PV_4 6 GENROU_4 TGOV1_4 NaN NaN 1.0 1.0\n", "3 PV_3 3 GENROU_3 TGOV1_3 NaN NaN 1.0 1.0\n", "4 PV_2 2 GENROU_2 TGOV1_2 NaN NaN 1.0 1.0" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.dyn.link" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Send" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As there is a gap between DC-based dispatch and AC-based TDS, a conversion is required to ensure the TDS initialization." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Routine <ACOPF> initialized in 0.0042 seconds.\n", "ACOPF solved in 0.2784 seconds, converged after 12 iterations using solver PYPOWER-PIPS.\n", "Attribute <aBus> already exists in <RTED>.\n", "<RTED> is converted to AC.\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.RTED.dc2ac()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the RTED routine, there are two mapping dictionaries to define the data exchange, namely, `map1` for receiving data from ANDES and `map2` for sending data to ANDES." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "OrderedDict([('vBus', ('Bus', 'v0')),\n", " ('ug', ('StaticGen', 'u')),\n", " ('pg', ('StaticGen', 'p0'))])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.RTED.map2" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Send <RTED> results to ANDES <0x17f0720a0>...\n", "Send <vBus> to Bus.v0\n", "Send <ug> to StaticGen.u\n", "Send <pg> to StaticGen.p0\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.dyn.send(adsys=sa, routine='RTED')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Run ANDES" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sometimes, the ANDES TDS initialization may fail due to inapproriate limits.\n", "\n", "Here, we alleviate the `TGOV1` limit issue by enlarging the `Pmax` and `Pmin` to the same value." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sa.TGOV1.set(src='VMAX', attr='v', idx=sa.TGOV1.idx.v, value=100*np.ones(sa.TGOV1.n))\n", "sa.TGOV1.set(src='VMIN', attr='v', idx=sa.TGOV1.idx.v, value=np.zeros(sa.TGOV1.n))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Run power flow." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sa.PFlow.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Try to init TDS." ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "_ = sa.TDS.init()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Run TDS." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sa.TDS.config.no_tqdm = True # disable progress bar\n", "sa.TDS.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Receive" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "OrderedDict([('ug', ('StaticGen', 'u')), ('pg0', ('StaticGen', 'p'))])" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.RTED.map1" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Receive <ug> from SynGen.u\n", "Receive <pg0> from SynGen.Pe\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.dyn.receive(adsys=sa, routine='RTED')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The RTED parameter ``pg0``, is retrieved from ANDES as the corresponding generator output power." ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0.32260084, 0.01 , 0.02 , 0.01 , 1.97393997])" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.RTED.pg0.v" ] } ], "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.18" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "d2b3bf80176349caa68dc4a3c77bd06eaade8abc678330f7d1c813c53380e5d2" } } }, "nbformat": 4, "nbformat_minor": 2 }