Skip to content

Commit

Permalink
adding possibility to init lightsim2grid by providing desired slack name
Browse files Browse the repository at this point in the history
  • Loading branch information
BDonnot committed Apr 5, 2024
1 parent 6881f2a commit 3b9c945
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ Change Log
--------------------
- [FIXED] CI was broken after migration to artifact v4, set it back to v3
(and make the names of the folder clearer)
- [FIXED] CI when using latest pandapower version (2.14)
- [FIXED] CI when using latest pandapower version (2.14) which broke some previous tests
- [IMPROVED] remove some compilation warnings for clang
- [IMPROVED] possibility to specify generator used as slack by its name when initializing
from `pypowsybl`.

[0.8.1] 2024-03-26
--------------------
Expand Down
15 changes: 13 additions & 2 deletions lightsim2grid/gridmodel/from_pypowsybl.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import numpy as np
import pandas as pd
import pypowsybl as pypo
from typing import Union

from lightsim2grid_cpp import GridModel

Expand All @@ -36,7 +37,7 @@ def _aux_get_bus(bus_df, df, conn_key="connected", bus_key="bus_id"):


def init(net : pypo.network,
gen_slack_id: int = None,
gen_slack_id: Union[int, str] = None,
slack_bus_id: int = None,
sn_mva = 100.,
sort_index=True,
Expand Down Expand Up @@ -260,7 +261,17 @@ def init(net : pypo.network,
elif gen_slack_id is not None:
if slack_bus_id is not None:
raise RuntimeError(f"You provided both gen_slack_id and slack_bus_id which is not possible.")
model.add_gen_slackbus(gen_slack_id, 1.)

if isinstance(gen_slack_id, str):
gen_slack_id_int = int((df_gen.index == gen_slack_id).nonzero()[0][0])
else:
try:
gen_slack_id_int = int(gen_slack_id)
except Exception:
raise RuntimeError("'slack_bus_id' should be either an int or a generator names")
if gen_slack_id_int != gen_slack_id:
raise RuntimeError("'slack_bus_id' should be either an int or a generator names")
model.add_gen_slackbus(gen_slack_id_int, 1.)
elif slack_bus_id is not None:
gen_bus = np.array([el.bus_id for el in model.get_generators()])
gen_is_conn_slack = gen_bus == model._orig_to_ls[slack_bus_id]
Expand Down
2 changes: 1 addition & 1 deletion lightsim2grid/lightSimBackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ def _load_grid_pypowsybl(self, path=None, filename=None):
grid_tmp = pypow_net.load(full_path)
gen_slack_id = None
if "gen_slack_id" in loader_kwargs:
gen_slack_id = int(loader_kwargs["gen_slack_id"])
gen_slack_id = loader_kwargs["gen_slack_id"]
self._grid, subs_id = init_pypow(grid_tmp, gen_slack_id=gen_slack_id, sort_index=True, return_sub_id=True)
(buses_sub_id, gen_sub, load_sub, (lor_sub, tor_sub), (lex_sub, tex_sub), batt_sub, sh_sub, hvdc_sub_from_id, hvdc_sub_to_id) = subs_id
self.__nb_bus_before = len(self._grid.get_bus_vn_kv())
Expand Down

0 comments on commit 3b9c945

Please sign in to comment.