{ "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 scheduling-dynamics 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" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "ams.config_logger(stream_level=20)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Scheduling" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Parsing input file \"/Users/jinningwang/work/miniconda3/envs/amsre/lib/python3.12/site-packages/ams/cases/ieee14/ieee14_uced.xlsx\"...\n", "Input file parsed in 0.0675 seconds.\n", "System set up in 0.0013 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": 4, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Building system matrices\n", "Parsing OModel for \n", "Evaluating OModel for \n", "Finalizing OModel for \n", " initialized in 0.0129 seconds.\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.RTED.init()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ " solved as optimal in 0.0119 seconds, converged in 10 iterations with CLARABEL.\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.RTED.run(solver='CLARABEL')" ] }, { "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": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Generating code for 1 models on 12 processes.\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Parsing additional file \"/Users/jinningwang/work/miniconda3/envs/amsre/lib/python3.12/site-packages/andes/cases/ieee14/ieee14_full.xlsx\"...\n", "Following PFlow models in addfile will be overwritten: , , , , , , \n", "Addfile parsed in 0.0336 seconds.\n", "System converted to ANDES in 0.2209 seconds.\n", "AMS system 0x3382b5340 is linked to the ANDES system 0x33bcfb260.\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": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on method to_andes in module ams.system:\n", "\n", "to_andes(addfile=None, setup=False, no_output=False, default_config=True, verify=False, tol=0.001, **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", " This function is wrapped as the ``System`` class method ``to_andes()``.\n", " Using the file conversion ``to_andes()`` will automatically\n", " link the AMS system instance to the converted ANDES system instance\n", " in the AMS system attribute ``dyn``.\n", "\n", " It should be noted that detailed dynamic simualtion requires extra\n", " dynamic models to be added to the ANDES system, which can be passed\n", " through the ``addfile`` argument.\n", "\n", " Parameters\n", " ----------\n", " system : System\n", " The AMS system to be converted to ANDES format.\n", " addfile : str, optional\n", " The additional file to be converted to ANDES dynamic mdoels.\n", " setup : bool, optional\n", " Whether to call `setup()` after the conversion. Default is True.\n", " no_output : bool, optional\n", " To ANDES system.\n", " default_config : bool, optional\n", " To ANDES system.\n", " verify : bool\n", " If True, the converted ANDES system will be verified with the source\n", " AMS system using AC power flow.\n", " tol : float\n", " The tolerance of error.\n", "\n", " Returns\n", " -------\n", " adsys : 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_uced.xlsx'), setup=True)\n", " >>> sa = sp.to_andes(addfile=andes.get_case('ieee14/ieee14_full.xlsx'),\n", " ... setup=False, overwrite=True, no_output=True)\n", "\n", " Notes\n", " -----\n", " 1. Power flow models in the addfile will be skipped and only dynamic models will be used.\n", " 2. The addfile format is guessed based on the file extension. Currently only ``xlsx`` is supported.\n", " 3. Index in the addfile is automatically adjusted when necessary.\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": 8, "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", "
stg_idxbus_idxsyg_idxgov_idxdg_idxrg_idxgammapgammaq
0Slack_11GENROU_1TGOV1_1NaNNaN1.01.0
1PV_58GENROU_5TGOV1_5NaNNaN1.01.0
2PV_46GENROU_4TGOV1_4NaNNaN1.01.0
3PV_33GENROU_3TGOV1_3NaNNaN1.01.0
4PV_22GENROU_2TGOV1_2NaNNaN1.01.0
\n", "
" ], "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": 8, "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": 9, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Parsing OModel for \n", "Evaluating OModel for \n", "Finalizing OModel for \n", " initialized in 0.0083 seconds.\n", " solved in 0.1152 seconds, converged in 12 iterations with PYPOWER-PIPS.\n", " converted to AC.\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 9, "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": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "OrderedDict([('vBus', ('Bus', 'v0')),\n", " ('ug', ('StaticGen', 'u')),\n", " ('pg', ('StaticGen', 'p0'))])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.RTED.map2" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Send results to ANDES <0x33bcfb260>...\n", "*Send to StaticGen.v0\n", "Send to Bus.v0\n", "Send to StaticGen.u\n", "Send to StaticGen.p0\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 11, "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": 12, "metadata": {}, "outputs": [], "source": [ "sa.TGOV1.alter(src='VMAX', idx=sa.TGOV1.idx.v, value=100*np.ones(sa.TGOV1.n))\n", "sa.TGOV1.alter(src='VMIN', idx=sa.TGOV1.idx.v, value=np.zeros(sa.TGOV1.n))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Run power flow." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sa.PFlow.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Try to init TDS." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "_ = sa.TDS.init()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Run TDS." ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 15, "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": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "OrderedDict([('ug', ('StaticGen', 'u')), ('pg0', ('StaticGen', 'p'))])" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.RTED.map1" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Receive from SynGen.u\n", "Receive from SynGen.Pe\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 17, "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": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0.48417982, 0.01000094, 0.02000094, 0.01000095, 1.79503641])" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.RTED.pg0.v" ] } ], "metadata": { "kernelspec": { "display_name": "amsre", "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.12.0" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }