Skip to content

Commit a6de571

Browse files
committed
add support for multiple slack when reading a grid from pypowsybl
Signed-off-by: DONNOT Benjamin <benjamin.donnot@rte-france.com>
1 parent 8e1027f commit a6de571

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ TODO: integration test with pandapower (see `pandapower/contingency/contingency.
4646
- [ADDED] more kwargs arguments are possible in the LightSimBackend `loader_kwargs`
4747
- [ADDED] name of the substations are now read from the grid when initializing from
4848
pypowsybl.
49+
- [ADDED] support for multiple slack when reading a grid from pypowsybl.
4950
- [IMPROVED] the way to initialize the transformers from pypowsybl
5051
- [IMPROVED] possibility to load grid with phase shifters from pypowsybl
5152
- [IMPROVED] function to initialize the grid from pypowsybl has now a

lightsim2grid/gridmodel/from_pypowsybl/_from_pypowsybl.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2023-2024, RTE (https://www.rte-france.com)
1+
# Copyright (c) 2023-2025, RTE (https://www.rte-france.com)
22
# See AUTHORS.txt
33
# This Source Code Form is subject to the terms of the Mozilla Public License, version 2.0.
44
# If a copy of the Mozilla Public License, version 2.0 was not distributed with this file,
@@ -11,10 +11,13 @@
1111
import numpy as np
1212
import pandas as pd
1313
import pypowsybl as pypo
14-
from typing import Optional, Union
14+
from typing import Dict, Iterable, Optional, Union
1515
from packaging import version
1616
from lightsim2grid_cpp import GridModel
1717

18+
19+
from ._aux_handle_slack import handle_slack_iterable, handle_slack_one_el
20+
1821
PP_BUG_RATIO_TAP_CHANGER = version.parse("1.9")
1922
PYPOWSYBL_VER = version.parse(pypo.__version__)
2023

@@ -42,7 +45,7 @@ def _aux_get_bus(bus_df, df, conn_key="connected", bus_key="bus_id"):
4245

4346

4447
def init(net : pypo.network.Network,
45-
gen_slack_id: Union[int, str] = None,
48+
gen_slack_id: Union[int, str, Iterable[str], Dict[str, float]] = None,
4649
slack_bus_id: int = None,
4750
sn_mva : float = 100., # only used if not present in the grid
4851
sort_index : bool =True,
@@ -467,19 +470,23 @@ def init(net : pypo.network.Network,
467470
if slack_bus_id is not None:
468471
raise RuntimeError("You provided both gen_slack_id and slack_bus_id "
469472
"which is not possible.")
470-
471-
if isinstance(gen_slack_id, str):
472-
gen_slack_id_int = int((df_gen.index == gen_slack_id).nonzero()[0][0])
473+
if isinstance(gen_slack_id, (str, int, np.int32, np.int64, np.str_, tuple)):
474+
single_slack = True
475+
fun_slack = handle_slack_one_el
476+
else:
477+
single_slack = False
478+
fun_slack = handle_slack_iterable
479+
gen_slack_ids_int, gen_slack_weights = fun_slack(df_gen, gen_slack_id)
480+
if single_slack:
481+
if gen_slack_ids_int is None or gen_slack_weights is None:
482+
raise RuntimeError(f"The slack {gen_slack_id} is disconnected.")
483+
gen_slack_ids_int = [gen_slack_ids_int]
484+
gen_slack_weights = [1.]
473485
else:
474-
try:
475-
gen_slack_id_int = int(gen_slack_id)
476-
except Exception as exc_:
477-
raise RuntimeError("'gen_slack_id' should be either an int or "
478-
"a generator names") from exc_
479-
if gen_slack_id_int != gen_slack_id:
480-
raise RuntimeError("'gen_slack_id' should be either an int or a "
481-
"generator names")
482-
model.add_gen_slackbus(gen_slack_id_int, 1.)
486+
gen_slack_weights = np.asarray(gen_slack_weights)
487+
gen_slack_weights /= gen_slack_weights.sum()
488+
for gen_slack_id_int, gen_slack_weight in zip(gen_slack_ids_int, gen_slack_weights):
489+
model.add_gen_slackbus(gen_slack_id_int, gen_slack_weight)
483490
elif slack_bus_id is not None:
484491
gen_bus = np.array([el.bus_id for el in model.get_generators()])
485492
gen_is_conn_slack = gen_bus == model._orig_to_ls[slack_bus_id]

0 commit comments

Comments
 (0)