{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Simulate" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This example gives a \"hello world\" example to use AMS." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Import and Setting the Verbosity Level" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "We first import the `ams` library." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import ams\n", "\n", "import datetime" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Last run time: 2024-01-24 09:37:17\n", "ams:0.8.1.post5+ge752692\n" ] } ], "source": [ "print(\"Last run time:\", datetime.datetime.now().strftime(\"%Y-%m-%d %H:%M:%S\"))\n", "\n", "print(f'ams:{ams.__version__}')" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "We can configure the verbosity level for logging (output messages) by passing a verbosity level (10-DEBUG, 20-INFO, 30-WARNING, 40-ERROR, 50-CRITICAL) to the `stream_level` argument of `ams.main.config_logger()`. Verbose level 10 is useful for getting debug output.\n", "\n", "The logging level can be altered by calling ``config_logger`` again with new ``stream_level`` and ``file_level``." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "ams.config_logger(stream_level=20)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Note that the above ``ams.config_logger()`` is a shorthand to ``ams.main.config_logger()``.\n", "\n", "If this step is omitted, the default `INFO` level (`stream_level=20`) will be used." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Run Simulations" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Load Case" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "AMS support multiple input file formats, including AMS ``.xlsx`` file, MATPOWER ``.m`` file, PYPOWER ``.py`` file, and PSS/E ``.raw`` file.\n", "\n", "Here we use the AMS ``.xlsx`` file as an example. The source file locates at ``$HOME/ams/ams/cases/ieee39/ieee39_uced.xlsx``." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Parsing input file \"/Users/jinningwang/Documents/work/ams/ams/cases/5bus/pjm5bus_uced.xlsx\"...\n", "Input file parsed in 0.1218 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.0021 seconds.\n" ] } ], "source": [ "sp = ams.load(ams.get_case('5bus/pjm5bus_uced.xlsx'),\n", " setup=True,\n", " no_output=True,)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Inspect Models and Routines" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "In AMS, ``model`` refers to the device model, and all models are registered to an OrderedDict ``models``." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "OrderedDict([('Summary', Summary (3 devices) at 0x105584d30),\n", " ('Bus', Bus (5 devices) at 0x1056110d0),\n", " ('PQ', PQ (3 devices) at 0x12f9b8df0),\n", " ('PV', PV (3 devices) at 0x12f9caa60),\n", " ('Slack', Slack (1 device) at 0x12f9e0910),\n", " ('Shunt', Shunt (0 devices) at 0x12f9eb3d0),\n", " ('Line', Line (7 devices) at 0x12f9eb880),\n", " ('PVD1', PVD1 (0 devices) at 0x12f9f4f70),\n", " ('ESD1', ESD1 (0 devices) at 0x12fa065b0),\n", " ('REGCA1', REGCA1 (0 devices) at 0x12fa06b20),\n", " ('REGCV1', REGCV1 (0 devices) at 0x12fa11160),\n", " ('REGCV2', REGCV2 (0 devices) at 0x12fa11940),\n", " ('Area', Area (3 devices) at 0x12fa11e80),\n", " ('Region', Region (2 devices) at 0x12fa1e640),\n", " ('SFR', SFR (2 devices) at 0x12fa1edf0),\n", " ('SR', SR (2 devices) at 0x12fa2e490),\n", " ('NSR', NSR (2 devices) at 0x12fa2e8b0),\n", " ('VSGR', VSGR (0 devices) at 0x12fa2ecd0),\n", " ('GCost', GCost (4 devices) at 0x12fa3a160),\n", " ('SFRCost', SFRCost (4 devices) at 0x12fa3a7f0),\n", " ('SRCost', SRCost (4 devices) at 0x12fa3ad90),\n", " ('NSRCost', NSRCost (4 devices) at 0x12fa471f0),\n", " ('VSGCost', VSGCost (0 devices) at 0x12fa47610),\n", " ('DCost', DCost (3 devices) at 0x12fa47910),\n", " ('TimeSlot', TimeSlot (0 devices) at 0x12fa47e80),\n", " ('EDTSlot', EDTSlot (24 devices) at 0x12fa50940),\n", " ('UCTSlot', UCTSlot (24 devices) at 0x12fa50d60)])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.models" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "One can inspect the detailed model data by converting it to a pandas DataFrame." ] }, { "cell_type": "code", "execution_count": 6, "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", "
idxunamebusVnp0q0vmaxvminownerctrl
uid
0PQ_11.0PQ 11230.03.00.98611.10.9None1.0
1PQ_21.0PQ 22230.03.00.98611.10.9None1.0
2PQ_31.0PQ 33230.04.01.31471.10.9None1.0
\n", "
" ], "text/plain": [ " idx u name bus Vn p0 q0 vmax vmin owner ctrl\n", "uid \n", "0 PQ_1 1.0 PQ 1 1 230.0 3.0 0.9861 1.1 0.9 None 1.0\n", "1 PQ_2 1.0 PQ 2 2 230.0 3.0 0.9861 1.1 0.9 None 1.0\n", "2 PQ_3 1.0 PQ 3 3 230.0 4.0 1.3147 1.1 0.9 None 1.0" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.PQ.as_df()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "In AMS, all supported routines are registered to an OrderedDict ``routines``." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "OrderedDict([('DCPF', DCPF at 0x12f9b8490),\n", " ('PFlow', PFlow at 0x12fa5fa00),\n", " ('CPF', CPF at 0x12fa73070),\n", " ('ACOPF', ACOPF at 0x12fa736a0),\n", " ('DCOPF', DCOPF at 0x12fa73fa0),\n", " ('ED', ED at 0x12fca9100),\n", " ('EDDG', EDDG at 0x12fcb40a0),\n", " ('EDES', EDES at 0x12fcc7ac0),\n", " ('RTED', RTED at 0x12fe09fd0),\n", " ('RTEDDG', RTEDDG at 0x12fe1c0d0),\n", " ('RTEDES', RTEDES at 0x12fe42760),\n", " ('RTEDVIS', RTEDVIS at 0x12fe684f0),\n", " ('UC', UC at 0x12fe7ac40),\n", " ('UCDG', UCDG at 0x160845430),\n", " ('UCES', UCES at 0x160866370),\n", " ('DOPF', DOPF at 0x16088dd60),\n", " ('DOPFVIS', DOPFVIS at 0x1608b0070)])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.routines" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Solve an Routine" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Before solving an routine, we need to initialize it first.\n", "Here Real-time Economic Dispatch (RTED) is used as an example." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Routine initialized in 0.0130 seconds.\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.RTED.init()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then, one can solve it by calling ``run()``.\n", "Here, argument `solver` can be passed to specify the solver to use, such as `solver='ECOS'`.\n", "\n", "Installed solvers can be listed by ``ams.shared.INSTALLED_SOLVERS``,\n", "and more detailes of solver can be found at [CVXPY-Choosing a solver](https://www.cvxpy.org/tutorial/advanced/index.html#choosing-a-solver)." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['CLARABEL',\n", " 'CVXOPT',\n", " 'ECOS',\n", " 'ECOS_BB',\n", " 'GLPK',\n", " 'GLPK_MI',\n", " 'GUROBI',\n", " 'MOSEK',\n", " 'OSQP',\n", " 'PIQP',\n", " 'PROXQP',\n", " 'SCIPY',\n", " 'SCS']" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ams.shared.INSTALLED_SOLVERS" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "RTED solved as optimal in 0.0173 seconds, converged after 9 iterations using solver ECOS.\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.RTED.run(solver='ECOS')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The solved results are stored in each variable itself.\n", "For example, the solved power generation of ten generators\n", "are stored in ``pg.v``." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2.1, 5.2, 0.7, 2. ])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.RTED.pg.v" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here, ``get_idx()`` can be used to get the index of a variable." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['PV_1', 'PV_3', 'PV_5', 'Slack_4']" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.RTED.pg.get_idx()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Part of the solved results can be accessed with given indices." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2.1, 5.2])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.RTED.get(src='pg', attr='v', idx=['PV_1', 'PV_3'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "All Vars are listed in an OrderedDict ``vars``." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "OrderedDict([('pg', Var: StaticGen.pg),\n", " ('aBus', Var: Bus.aBus),\n", " ('plf', Var: Line.plf),\n", " ('pru', Var: StaticGen.pru),\n", " ('prd', Var: StaticGen.prd)])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.RTED.vars" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The Objective value can be accessed with ``obj.v``." ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.19537500005072062" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.RTED.obj.v" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Similarly, all Constrs are listed in an OrderedDict ``constrs``,\n", "and the expression values can also be accessed." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "OrderedDict([('pglb', Constraint: pglb [ON]),\n", " ('pgub', Constraint: pgub [ON]),\n", " ('pb', Constraint: pb [ON]),\n", " ('plflb', Constraint: plflb [ON]),\n", " ('plfub', Constraint: plfub [ON]),\n", " ('alflb', Constraint: alflb [ON]),\n", " ('alfub', Constraint: alfub [ON]),\n", " ('rbu', Constraint: rbu [ON]),\n", " ('rbd', Constraint: rbd [ON]),\n", " ('rru', Constraint: rru [ON]),\n", " ('rrd', Constraint: rrd [ON]),\n", " ('rgu', Constraint: rgu [ON]),\n", " ('rgd', Constraint: rgd [ON])])" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.RTED.constrs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One can also inspect the Constr values." ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([-996.9, -993.8, -998.3, -997. ])" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.RTED.rgu.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 }