Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support loadcase #7

Merged
merged 1 commit into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading