From 7672be07dd9b11ff9df9f10b71e33d4fe53100a3 Mon Sep 17 00:00:00 2001 From: Muhammad Yasirroni Date: Mon, 14 Oct 2024 22:01:07 +0700 Subject: [PATCH] add runuopf --- README.md | 57 +++++++----- notebooks/runuopf.ipynb | 164 +++++++++++++++++++++++++++++++++++ notebooks/split_branch.ipynb | 11 ++- 3 files changed, 206 insertions(+), 26 deletions(-) create mode 100644 notebooks/runuopf.ipynb diff --git a/README.md b/README.md index 4770e1d..c4e90e9 100644 --- a/README.md +++ b/README.md @@ -88,9 +88,16 @@ mpc = m.loadcase('case9') mpopt = m.mpoption('verbose', 2) m.push("_mpopt", mpopt) m.push("_mpc", mpc, verbose=False) + m.eval("_r1 = runopf(_mpc, _mpopt);", verbose=False) -m.eval("_r1.raw = rmfield(_r1.raw, 'task');") -m.eval("_r1 = rmfield(_r1, 'om');") + +# `_r1` containts unsupported ``, needs to be removed first +m.eval( + """ + _r1.raw = rmfield(_r1.raw, 'task'); + _r1 = rmfield(_r1, 'om'); + """ +) mpc = m.pull("_r1") ``` @@ -103,19 +110,20 @@ from matpower import start_instance # start instance m = start_instance() -# use octave native to run some commands -m.eval("mpopt = mpoption('verbose', 2);") -m.eval("mpc = loadcase('case9');") -m.eval("r1 = runopf(mpc, mpopt);") # we avoid parse `r1` that containts unsupported `` +m.eval( + """ + mpopt = mpoption('verbose', 2); + mpc = loadcase('case9'); + _r1 = runopf(mpc, mpopt); + """ +) # fech data to python (.eval is used because .pull is not working in acessing field) -r1_mpc = {} -r1_mpc['baseMVA'] = m.eval('r1.baseMVA;') -r1_mpc['version'] = m.eval('r1.version;') -r1_mpc['bus'] = m.eval('r1.bus;') -r1_mpc['gen'] = m.eval('r1.gen;') -r1_mpc['branch'] = m.eval('r1.branch;') -r1_mpc['gencost'] = m.eval('r1.gencost;') +r1_mpc = m.eval( + "struct(" + " 'baseMVA', _r1.baseMVA, 'version', _r1.version, 'bus', _r1.bus, 'gen', _r1.gen," + " 'branch', _r1.branch, 'gencost', _r1.gencost);" +) # modify variable if necessary [GEN_BUS, PG, QG, QMAX, QMIN, VG, MBASE, GEN_STATUS, PMAX, PMIN, MU_PMAX, @@ -141,7 +149,7 @@ m.push('mpc', r1_mpc) # push r1_mpc in python to mpc in octave mpc = m.pull('mpc') # test if our pushed variable can be used -m.eval("r1 = runopf(mpc, mpopt);") +m.eval("_r1 = runopf(mpc, mpopt);") ``` `matpower-pip` also support using `matlab.engine`. @@ -162,22 +170,23 @@ mpc = m.runpf('case5', nargout=0) Impacted case: ```python - r1 = m.runopf(mpc) + _r1 = m.runopf(mpc) ``` Solution: ```python m.push('mpc', mpc) - m.eval("r1 = runopf(mpc, mpopt);") - - r1_mpc = {} - r1_mpc['baseMVA'] = m.eval('r1.baseMVA;') - r1_mpc['version'] = m.eval('r1.version;') - r1_mpc['bus'] = m.eval('r1.bus;') - r1_mpc['gen'] = m.eval('r1.gen;') - r1_mpc['branch'] = m.eval('r1.branch;') - r1_mpc['gencost'] = m.eval('r1.gencost;') + r1_mpc = m.eval( + """ + mpopt = mpoption('verbose', 2); + _r1 = runopf(mpc, mpopt); + """ + + "struct(" + " 'baseMVA', _r1.baseMVA, 'version', _r1.version, 'bus', _r1.bus, 'gen', _r1.gen," + " 'branch', _r1.branch, 'gencost', _r1.gencost);" + ) ``` ## Versioning diff --git a/notebooks/runuopf.ipynb b/notebooks/runuopf.ipynb new file mode 100644 index 0000000..ce86b8d --- /dev/null +++ b/notebooks/runuopf.ipynb @@ -0,0 +1,164 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "# install octave\n", + "!sudo apt-get -qq update\n", + "!sudo apt-get -qq install octave octave-signal liboctave-dev\n", + "\n", + "# install oct2py that compatible with colab\n", + "import os\n", + "\n", + "from pkg_resources import get_distribution\n", + "\n", + "os.system(\n", + " f\"pip install -qq\"\n", + " f\" ipykernel=={get_distribution('ipykernel').version}\"\n", + " f\" ipython=={get_distribution('ipython').version}\"\n", + " f\" tornado=={get_distribution('tornado').version}\"\n", + " f\" oct2py\"\n", + ")\n", + "\n", + "# install packages\n", + "!pip install -qq matpower matpowercaseframes" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "oct2py version: 5.7.2\n", + "matpower version: 8.0.0.2.2.2\n" + ] + } + ], + "source": [ + "import oct2py\n", + "\n", + "import matpower\n", + "\n", + "print(f\"oct2py version: {oct2py.__version__}\")\n", + "print(f\"matpower version: {matpower.__version__}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "from matpower import start_instance" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "m = start_instance()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "mpc = m.loadcase(\"case9\")\n", + "mpopt = m.mpoption(\"verbose\", 2)\n", + "m.push(\"_mpopt\", mpopt)\n", + "m.push(\"_mpc\", mpc, verbose=False)\n", + "m.eval(\"_r1 = runuopf(_mpc, _mpopt);\", verbose=False)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "# pull all after cleansing unsupported data\n", + "m.eval(\n", + " \"\"\"\n", + " _r1.raw = rmfield(_r1.raw, 'task');\n", + " _r1 = rmfield(_r1, 'om');\n", + " \"\"\"\n", + ")\n", + "mpc = m.pull(\"_r1\")" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "# selective pull\n", + "r1_mpc = m.eval(\n", + " \"struct('baseMVA', _r1.baseMVA, 'version', _r1.version, 'bus', _r1.bus,\"\n", + " \" 'gen', _r1.gen, 'branch', _r1.branch, 'gencost', _r1.gencost);\"\n", + ")\n", + "r1_mpc" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# # NOTE: you also can run like this\n", + "# r1_mpc = m.eval(\n", + "# \"\"\"\n", + "# mpopt = mpoption('verbose', 2);\n", + "# mpc = loadcase('case9');\n", + "# _r1 = runuopf(mpc, mpopt);\n", + "# \"\"\"\n", + "#\n", + "# \"struct(\"\n", + "# \" 'baseMVA', _r1.baseMVA, 'version', _r1.version, 'bus', _r1.bus, 'gen', _r1.gen,\"\n", + "# \" 'branch', _r1.branch, 'gencost', _r1.gencost);\"\n", + "# )\n", + "# r1_mpc" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "env", + "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.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/notebooks/split_branch.ipynb b/notebooks/split_branch.ipynb index c17e3e1..e061bac 100644 --- a/notebooks/split_branch.ipynb +++ b/notebooks/split_branch.ipynb @@ -105,7 +105,13 @@ { "cell_type": "markdown", "metadata": {}, - "source": [] + "source": [ + "## Understanding Parallel Line\n", + "\n", + "$$\n", + "Y_{total} = Y_1 + Y_2 + \\dots + Y_n\n", + "$$" + ] }, { "cell_type": "code", @@ -264,7 +270,8 @@ "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython3" + "pygments_lexer": "ipython3", + "version": "3.12.4" } }, "nbformat": 4,