Skip to content

Commit

Permalink
Merge pull request #7 from yasirroni/support_loadcase
Browse files Browse the repository at this point in the history
support loadcase
  • Loading branch information
yasirroni authored Jun 30, 2024
2 parents b5bf8bb + 446a635 commit 7e2ce5f
Show file tree
Hide file tree
Showing 5 changed files with 841 additions and 18 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,27 @@ cf = CaseFrames(case_path)
print(cf.gencost)
```

Since `matpower` itself suggests that we should use `loadcase` instead of parsing, we can use an engine using (require `matlab` or `octave`),

```python
from matpower import start_instance
from matpowercaseframes import CaseFrames

m = start_instance()

case_name = f"case16am.m"
cf_16am_lc = CaseFrames(case_name, load_case_engine=m)
cf_16am_lc.branch # see that the branch is already in p.u., converted by `loadcase`
```

Furthermore, `matpowercaseframes` also support generating data that is acceptable by `matpower` via `matpower-pip` package (require `matlab` or `octave`),

```python
from matpowercaseframes import CaseFrames

case_path = 'case9.m'
cf = CaseFrames(case_path)
mpc = cf.to_dict()
mpc = cf.to_mpc() # identical with cf.to_dict()

m = start_instance()
m.runpf(mpc)
Expand Down
14 changes: 11 additions & 3 deletions matpowercaseframes/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@


class CaseFrames:
def __init__(self, data, update_index=True):
def __init__(self, data, update_index=True, load_case_engine=None):
"""Convert data into CaseFrames format
Args:
Expand All @@ -34,9 +34,17 @@ def __init__(self, data, update_index=True):
# TODO: support read excel
# TODO: support Path object
if isinstance(data, str):
path = self._get_path(data)
# TYPE: str of path
self._read_matpower(filepath=path)
path = self._get_path(data)

if load_case_engine is None:
# read with matpower parser
self._read_matpower(filepath=path)
else:
# read using loadcase
mpc = load_case_engine.loadcase(path)
self._read_oct2py_struct(struct=mpc)

elif isinstance(data, dict):
# TYPE: dict | oct2py.io.Struct
self._read_oct2py_struct(struct=data)
Expand Down
71 changes: 66 additions & 5 deletions notebooks/load_case16am.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,19 @@
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# # NOTE: using loadcase cause data in float\n",
"# CASE_NAME = f\"case16am.m\"\n",
"# mpc = m.loadcase(CASE_NAME)\n",
"# cf_16am_lc = CaseFrames(mpc)\n",
"# cf_16am_lc.branch"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
Expand Down Expand Up @@ -648,21 +661,69 @@
"14 0.0 1.0 -360.0 360.0 "
]
},
"execution_count": 5,
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# NOTE: using loadcase cause data in float\n",
"CASE_NAME = f\"case16am.m\"\n",
"mpc = m.loadcase(CASE_NAME)\n",
"cf_16am = CaseFrames(mpc)\n",
"cf_16am.branch"
"cf_16am_lc = CaseFrames(CASE_NAME, load_case_engine=m)\n",
"cf_16am_lc.branch"
]
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"MATPOWER Version 7.1, 08-Oct-2020 -- AC Power Flow (Newton)\n",
"\n",
"Newton's method power flow (power balance, polar) did not converge in 10 iterations.\n",
"\n",
">>>>> Did NOT converge (0.02 seconds) <<<<<\n",
"\n"
]
}
],
"source": [
"mpc = cf_16am.to_mpc()\n",
"_ = m.runpf(mpc)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"MATPOWER Version 7.1, 08-Oct-2020 -- AC Power Flow (Newton)\n",
"\n",
"Newton's method power flow (power balance, polar) did not converge in 10 iterations.\n",
"\n",
">>>>> Did NOT converge (0.01 seconds) <<<<<\n",
"\n"
]
}
],
"source": [
"mpc = cf_16am_lc.to_mpc()\n",
"_ = m.runpf(mpc)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down
Loading

0 comments on commit 7e2ce5f

Please sign in to comment.