{ "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/ams/ams/cases/5bus/pjm5bus_demo.xlsx\"...\n", "Input file parsed in 0.0746 seconds.\n", "Zero Line parameters detected, adjusted to default values: rate_b, rate_c.\n", "All bus type are PQ, adjusted given load and generator connection status.\n", "System set up in 0.0020 seconds.\n" ] } ], "source": [ "sp = ams.load(ams.get_case('5bus/pjm5bus_demo.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 0x30e664620),\n", " ('Bus', Bus (5 devices) at 0x30e6876b0),\n", " ('PQ', PQ (3 devices) at 0x30e684f50),\n", " ('Slack', Slack (1 device) at 0x30e6acc50),\n", " ('PV', PV (4 devices) at 0x30e6ad5b0),\n", " ('Shunt', Shunt (0 devices) at 0x30e6ade80),\n", " ('Line', Line (7 devices) at 0x30e687dd0),\n", " ('Jumper', Jumper (0 devices) at 0x30e6ae8d0),\n", " ('PVD1', PVD1 (1 device) at 0x30e2a7d10),\n", " ('ESD1', ESD1 (1 device) at 0x11086fb30),\n", " ('EV1', EV1 (0 devices) at 0x30e6afbc0),\n", " ('EV2', EV2 (0 devices) at 0x30e6afec0),\n", " ('REGCA1', REGCA1 (0 devices) at 0x30e6e0230),\n", " ('REGCV1', REGCV1 (4 devices) at 0x30e6e08f0),\n", " ('REGCV2', REGCV2 (0 devices) at 0x30e6e0c80),\n", " ('Area', Area (3 devices) at 0x30e6e1040),\n", " ('Zone', Zone (5 devices) at 0x30e6e13d0),\n", " ('SFR', SFR (3 devices) at 0x30e6e1730),\n", " ('SR', SR (3 devices) at 0x30e6e1d30),\n", " ('NSR', NSR (3 devices) at 0x30e6e1fa0),\n", " ('VSGR', VSGR (3 devices) at 0x30e6e2210),\n", " ('GCost', GCost (5 devices) at 0x30e4b81a0),\n", " ('SFRCost', SFRCost (5 devices) at 0x30e6e2e40),\n", " ('SRCost', SRCost (5 devices) at 0x30e6e30e0),\n", " ('NSRCost', NSRCost (5 devices) at 0x30e6e32f0),\n", " ('VSGCost', VSGCost (4 devices) at 0x30e6e3590),\n", " ('DCost', DCost (3 devices) at 0x30e6e3860),\n", " ('EDTSlot', EDTSlot (24 devices) at 0x30e6e3a70),\n", " ('UCTSlot', UCTSlot (24 devices) at 0x30e6aec90)])" ] }, "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 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": 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 0x30e687d10),\n", " ('PFlow', PFlow at 0x176a0bbf0),\n", " ('ACOPF', ACOPF at 0x176a2c740),\n", " ('DCOPF', DCOPF at 0x176a2e120),\n", " ('DCOPF2', DCOPF2 at 0x176a2f680),\n", " ('RTED', RTED at 0x176a48bf0),\n", " ('RTEDDG', RTEDDG at 0x176a4a690),\n", " ('RTEDESP', RTEDESP at 0x176ad4680),\n", " ('RTEDES', RTEDES at 0x176ad68d0),\n", " ('RTEDVIS', RTEDVIS at 0x176af0e60),\n", " ('RTED2', RTED2 at 0x176af2a50),\n", " ('RTED2DG', RTED2DG at 0x176af4560),\n", " ('RTED2ES', RTED2ES at 0x176af5ee0),\n", " ('ED', ED at 0x176b18680),\n", " ('EDDG', EDDG at 0x30e33eab0),\n", " ('EDES', EDES at 0x176b1ca40),\n", " ('ED2', ED2 at 0x176b1f710),\n", " ('ED2DG', ED2DG at 0x176b25a00),\n", " ('ED2ES', ED2ES at 0x176ad8440),\n", " ('UC', UC at 0x3121b5880),\n", " ('UCDG', UCDG at 0x3118a5220),\n", " ('UCES', UCES at 0x3127435f0),\n", " ('UC2', UC2 at 0x311aeec30),\n", " ('UC2DG', UC2DG at 0x3127a48c0),\n", " ('UC2ES', UC2ES at 0x3127a6b10),\n", " ('DOPF', DOPF at 0x311d29a00),\n", " ('DOPFVIS', DOPFVIS at 0x3127e3620),\n", " ('DCPF1', DCPF1 at 0x31281c560),\n", " ('PFlow1', PFlow1 at 0x31281d250),\n", " ('DCOPF1', DCOPF1 at 0x31281d880),\n", " ('ACOPF1', ACOPF1 at 0x176d71f70),\n", " ('OPF', OPF at 0x311aeee40)])" ] }, "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, it must first be initialized.\n", "In this example, we use DCOPF.\n", "\n", "In AMS, different routines require different input data.\n", "For instance, `RTED` necessitates regulating reserve-related data (`SFR`, `SFRCost`) for initialization.\n", "\n", "If you only have base power flow data or DCOPF data, other scheduling routines may not be able to init.\n", "\n", "You can use `pjm5bus_demo.xlsx` as an all-inclusive example to complete necessary input, as it contains all the necessary data for all routines." ] }, { "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.0085 seconds.\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.DCOPF.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', 'GUROBI', 'MOSEK', 'OSQP', 'PIQP', 'SCIP', 'SCIPY', '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.0185 seconds, converged in 9 iterations with CLARABEL.\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.DCOPF.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([0.2 , 1.43998388, 0.6 , 5.76001612, 2. ])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.DCOPF.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', 'PV_2', 'Slack_4']" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.DCOPF.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([0.2 , 1.43998388])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.DCOPF.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)])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.DCOPF.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(9.535953244734864)" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.DCOPF.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])])" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.DCOPF.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([-3.61999194, -2.78912153, -2.17089459, -4. , -2.43998388,\n", " -1.62910541, -3.61999194])" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.DCOPF.plflb.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.12.0" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }