Skip to content

Commit

Permalink
add runuopf
Browse files Browse the repository at this point in the history
  • Loading branch information
yasirroni committed Oct 14, 2024
1 parent 6e86269 commit 7672be0
Show file tree
Hide file tree
Showing 3 changed files with 206 additions and 26 deletions.
57 changes: 33 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<object opf_model>`, needs to be removed first
m.eval(
"""
_r1.raw = rmfield(_r1.raw, 'task');
_r1 = rmfield(_r1, 'om');
"""
)
mpc = m.pull("_r1")
```

Expand All @@ -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 `<object opf_model>`
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,
Expand All @@ -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`.
Expand All @@ -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
Expand Down
164 changes: 164 additions & 0 deletions notebooks/runuopf.ipynb
Original file line number Diff line number Diff line change
@@ -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
}
11 changes: 9 additions & 2 deletions notebooks/split_branch.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 7672be0

Please sign in to comment.