{ "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" ] }, { "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": 2, "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 supports 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": 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/5bus/pjm5bus_uced.xlsx\"...\n", "Input file parsed in 0.1254 seconds.\n", "Zero line rates detacted in rate_b, rate_c, adjusted to 999.\n", "System set up in 0.0016 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-level models, and they are registered to an OrderedDict ``models``." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "OrderedDict([('Summary', Summary (3 devices) at 0x1044cecf0),\n", " ('Bus', Bus (5 devices) at 0x103f4e840),\n", " ('PQ', PQ (3 devices) at 0x336e2ab10),\n", " ('Slack', Slack (1 device) at 0x336e2b620),\n", " ('PV', PV (3 devices) at 0x336e2bfe0),\n", " ('Shunt', Shunt (0 devices) at 0x336e54aa0),\n", " ('Line', Line (7 devices) at 0x336e54e90),\n", " ('Jumper', Jumper (0 devices) at 0x336e55a00),\n", " ('PVD1', PVD1 (0 devices) at 0x336e28890),\n", " ('ESD1', ESD1 (0 devices) at 0x336e56690),\n", " ('EV1', EV1 (0 devices) at 0x336e56ba0),\n", " ('EV2', EV2 (0 devices) at 0x336e571a0),\n", " ('REGCA1', REGCA1 (0 devices) at 0x336e575c0),\n", " ('REGCV1', REGCV1 (0 devices) at 0x336e57d10),\n", " ('REGCV2', REGCV2 (0 devices) at 0x336e883e0),\n", " ('Area', Area (3 devices) at 0x336e88950),\n", " ('Zone', Zone (2 devices) at 0x336c8bd40),\n", " ('SFR', SFR (2 devices) at 0x336e2ae40),\n", " ('SR', SR (2 devices) at 0x336e89b50),\n", " ('NSR', NSR (2 devices) at 0x336e89f40),\n", " ('VSGR', VSGR (0 devices) at 0x336e8a300),\n", " ('GCost', GCost (4 devices) at 0x336e8a6f0),\n", " ('SFRCost', SFRCost (4 devices) at 0x336e8b0b0),\n", " ('SRCost', SRCost (4 devices) at 0x336e8b620),\n", " ('NSRCost', NSRCost (4 devices) at 0x336e8b9e0),\n", " ('VSGCost', VSGCost (0 devices) at 0x336e8bdd0),\n", " ('DCost', DCost (3 devices) at 0x336ec00e0),\n", " ('TimeSlot', TimeSlot (0 devices) at 0x336e57710),\n", " ('EDTSlot', EDTSlot (24 devices) at 0x336ec0c50),\n", " ('UCTSlot', UCTSlot (24 devices) at 0x336ec1040)])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.models" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "We can inspect the detailed model data in the form of DataFrame." ] }, { "cell_type": "code", "execution_count": 5, "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 1Bus_2230.03.00.98611.10.9None1.0
1PQ_21.0PQ 2Bus_3230.03.00.98611.10.9None1.0
2PQ_31.0PQ 3Bus_4230.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 Bus_2 230.0 3.0 0.9861 1.1 0.9 None 1.0\n", "1 PQ_2 1.0 PQ 2 Bus_3 230.0 3.0 0.9861 1.1 0.9 None 1.0\n", "2 PQ_3 1.0 PQ 3 Bus_4 230.0 4.0 1.3147 1.1 0.9 None 1.0" ] }, "execution_count": 5, "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": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "OrderedDict([('DCPF', DCPF at 0x336e2a3f0),\n", " ('PFlow', PFlow at 0x3351be300),\n", " ('CPF', CPF at 0x336ed9c70),\n", " ('ACOPF', ACOPF at 0x336f7cb60),\n", " ('DCOPF', DCOPF at 0x336f7da00),\n", " ('DCOPF2', DCOPF2 at 0x336f7ef30),\n", " ('ED', ED at 0x336fcc320),\n", " ('EDDG', EDDG at 0x336fcfaa0),\n", " ('EDES', EDES at 0x337001760),\n", " ('RTED', RTED at 0x337003ec0),\n", " ('RTEDDG', RTEDDG at 0x3370454f0),\n", " ('RTEDES', RTEDES at 0x337046c30),\n", " ('RTEDVIS', RTEDVIS at 0x337084c50),\n", " ('UC', UC at 0x336eda0f0),\n", " ('UCDG', UCDG at 0x339c36540),\n", " ('UCES', UCES at 0x339d52ed0),\n", " ('DOPF', DOPF at 0x339d81a00),\n", " ('DOPFVIS', DOPFVIS at 0x339d83230),\n", " ('PFlow0', PFlow0 at 0x339d83f20),\n", " ('DCPF0', DCPF0 at 0x339dc0e00)])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.routines" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Solve a Routine" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Before solving a routine, we need to initialize it first.\n", "Here Real-time Economic Dispatch (RTED) is used as an example." ] }, { "cell_type": "code", "execution_count": 7, "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.0105 seconds.\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 7, "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": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['CLARABEL',\n", " 'ECOS',\n", " 'ECOS_BB',\n", " 'GUROBI',\n", " 'HIGHS',\n", " 'MOSEK',\n", " 'OSQP',\n", " 'PIQP',\n", " 'SCIP',\n", " 'SCIPY',\n", " 'SCS']" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ams.shared.installed_solvers" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ " solved as optimal in 0.0139 seconds, converged in 10 iterations with CLARABEL.\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.RTED.run(solver='CLARABEL')" ] }, { "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": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2.1, 5.2, 0.7, 2. ])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.RTED.pg.v" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here, ``get_all_idxes()`` can be used to get the index of a variable." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['PV_1', 'PV_3', 'PV_5', 'Slack_4']" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.RTED.pg.get_all_idxes()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Part of the solved results can be accessed with given indices." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2.1, 5.2])" ] }, "execution_count": 12, "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": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "OrderedDict([('pg', Var: StaticGen.pg),\n", " ('vBus', Var: Bus.vBus),\n", " ('aBus', Var: Bus.aBus),\n", " ('pru', Var: StaticGen.pru),\n", " ('prd', Var: StaticGen.prd)])" ] }, "execution_count": 13, "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": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "np.float64(0.19537500002212993)" ] }, "execution_count": 14, "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": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "OrderedDict([('pb', Constraint: pb [ON]),\n", " ('sba', Constraint: sbus [ON]),\n", " ('pglb', Constraint: pglb [ON]),\n", " ('pgub', Constraint: pgub [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": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.RTED.constrs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also inspect the `Constraint` values." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ -997.9 , -997.0349, -1002.9651, -997. ])" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.RTED.rgu.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 }