Skip to content

Commit

Permalink
Merge pull request #1156 from SteffenMeinecke/develop
Browse files Browse the repository at this point in the history
add get_connecting_branches() and PeP8
  • Loading branch information
jkupka authored Mar 9, 2021
2 parents d41d956 + 319efcf commit 2890c27
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 60 deletions.
153 changes: 95 additions & 58 deletions pandapower/toolbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,7 @@ def reindex_elements(net, element, new_indices, old_indices=None):
element_in_cost_df = net[cost_df].et == element
if sum(element_in_cost_df):
net[cost_df].element.loc[element_in_cost_df] = get_indices(net[cost_df].element[
element_in_cost_df], lookup)
element_in_cost_df], lookup)


def create_continuous_elements_index(net, start=0, add_df_to_reindex=set()):
Expand Down Expand Up @@ -1238,10 +1238,32 @@ def drop_duplicated_measurements(net, buses=None, keep="first"):
net.measurement.drop(idx_to_drop, inplace=True)


def get_connecting_branches(net, buses1, buses2, branch_elements=None):
"""
Gets/Drops branches that connects any bus of buses1 with any bus of buses2.
"""
branch_dict = branch_element_bus_dict(include_switch=True)
if branch_elements is not None:
branch_dict = {key: branch_dict[key] for key in branch_elements}
if "switch" in branch_dict:
branch_dict["switch"].append("element")

found = {elm: set() for elm in branch_dict.keys()}
for elm, bus_types in branch_dict.items():
for bus1 in bus_types:
for bus2 in bus_types:
if bus2 != bus1:
idx = net[elm].index[net[elm][bus1].isin(buses1) & net[elm][bus2].isin(buses2)]
if elm == "switch":
idx = idx.intersection(net[elm].index[net[elm].et == "b"])
found[elm] |= set(idx)
return {key: val for key, val in found.items() if len(val)}


def _inner_branches(net, buses, task, branch_elements=None):
"""
Drops branches that connects buses within 'buses' at all branch sides (e.g. 'from_bus' and
'to_bus').
Drops or finds branches that connects buses within 'buses' at all branch sides (e.g. 'from_bus'
and 'to_bus').
"""
branch_dict = branch_element_bus_dict(include_switch=True)
if branch_elements is not None:
Expand Down Expand Up @@ -1319,18 +1341,21 @@ def set_isolated_areas_out_of_service(net, respect_switches=True):
tr3w_buses = net.trafo3w.loc[tr3w, ['hv_bus', 'mv_bus', 'lv_bus']].values
if not all(net.bus.loc[tr3w_buses, 'in_service'].values):
net.trafo3w.at[tr3w, 'in_service'] = False
open_tr3w_switches = net.switch.loc[(net.switch.et == 't3') & ~net.switch.closed & (net.switch.element == tr3w)]
open_tr3w_switches = net.switch.loc[(net.switch.et == 't3') & ~net.switch.closed & (
net.switch.element == tr3w)]
if len(open_tr3w_switches) == 3:
net.trafo3w.at[tr3w, 'in_service'] = False

for element, et in zip(["line", "trafo"], ["l", "t"]):
oos_elements = net[element].query("not in_service").index
oos_switches = net.switch[(net.switch.et == et) & net.switch.element.isin(oos_elements)].index
oos_switches = net.switch[(net.switch.et == et) & net.switch.element.isin(
oos_elements)].index

closed_switches.update([i for i in oos_switches.values if not net.switch.at[i, 'closed']])
net.switch.loc[oos_switches, "closed"] = True

for idx, bus in net.switch.loc[~net.switch.closed & (net.switch.et == et)][["element", "bus"]].values:
for idx, bus in net.switch.loc[~net.switch.closed & (net.switch.et == et)][[
"element", "bus"]].values:
if not net.bus.in_service.at[next_bus(net, bus, idx, element)]:
net[element].at[idx, "in_service"] = False
if len(closed_switches) > 0:
Expand Down Expand Up @@ -1745,7 +1770,7 @@ def replace_line_by_impedance(net, index=None, sn_mva=None, only_valid_replace=T
Zni = vn ** 2 / sn_mva[i]
new_index.append(create_impedance(
net, line_.from_bus, line_.to_bus, line_.r_ohm_per_km * line_.length_km / Zni,
line_.x_ohm_per_km * line_.length_km / Zni, sn_mva[i], name=line_.name,
line_.x_ohm_per_km * line_.length_km / Zni, sn_mva[i], name=line_.name,
in_service=line_.in_service))
i += 1
drop_lines(net, index)
Expand Down Expand Up @@ -2150,9 +2175,9 @@ def replace_ward_by_internal_elements(net, wards=None):
to_add_shunt = net.res_ward.loc[wards, ["p_mw", "q_mvar", "vm_pu"]]
to_add_shunt.index = new_shunt_idx
to_add_shunt.p_mw = net.res_ward.vm_pu[wards].values ** 2 * net.ward.pz_mw[wards].values * \
sign_in_service * sign_not_isolated
to_add_shunt.q_mvar = net.res_ward.vm_pu[wards].values ** 2 * net.ward.qz_mvar[wards].values * \
sign_in_service * sign_not_isolated
sign_in_service * sign_not_isolated
to_add_shunt.q_mvar = net.res_ward.vm_pu[wards].values ** 2 * net.ward.qz_mvar[
wards].values * sign_in_service * sign_not_isolated
to_add_shunt.vm_pu = net.res_ward.vm_pu[wards].values
net.res_shunt = pd.concat([net.res_shunt, to_add_shunt])

Expand Down Expand Up @@ -2193,8 +2218,9 @@ def replace_xward_by_internal_elements(net, xwards=None):
in_service=xward.in_service, name=xward.name)
create_gen(net, bus_idx, 0, xward.vm_pu, in_service=xward.in_service,
name=xward.name)
create_impedance(net, xward.bus, bus_idx, xward.r_ohm / (bus_v ** 2), xward.x_ohm / (bus_v ** 2),
net.sn_mva, in_service=xward.in_service, name=xward.name)
create_impedance(net, xward.bus, bus_idx, xward.r_ohm / (bus_v ** 2),
xward.x_ohm / (bus_v ** 2), net.sn_mva, in_service=xward.in_service,
name=xward.name)

# --- result data
if net.res_xward.shape[0]:
Expand Down Expand Up @@ -2395,8 +2421,8 @@ def get_connected_buses(net, buses, consider=("l", "s", "t", "t3", "i"), respect
will be respected
False: in_service status will be
ignored
**consider** (iterable, ("l", "s", "t", "t3", "i")) - Determines, which types of connections will
be considered.
**consider** (iterable, ("l", "s", "t", "t3", "i")) - Determines, which types of
connections will be considered.
l: lines
s: switches
t: trafos
Expand All @@ -2412,12 +2438,13 @@ def get_connected_buses(net, buses, consider=("l", "s", "t", "t3", "i"), respect
cb = set()
if "l" in consider or 'line' in consider:
in_service_constr = net.line.in_service if respect_in_service else True
opened_lines = set(net.switch.loc[(~net.switch.closed) &
(net.switch.et == "l")].element.unique()) if respect_switches else set()
connected_fb_lines = set(net.line.index[(net.line.from_bus.isin(buses)) &
~net.line.index.isin(opened_lines) & in_service_constr])
connected_tb_lines = set(net.line.index[(net.line.to_bus.isin(buses)) &
~net.line.index.isin(opened_lines) & in_service_constr])
opened_lines = set(net.switch.loc[(~net.switch.closed) & (
net.switch.et == "l")].element.unique()) if respect_switches else set()
connected_fb_lines = set(net.line.index[(
net.line.from_bus.isin(buses)) & ~net.line.index.isin(opened_lines) &
in_service_constr])
connected_tb_lines = set(net.line.index[(
net.line.to_bus.isin(buses)) & ~net.line.index.isin(opened_lines) & in_service_constr])
cb |= set(net.line[net.line.index.isin(connected_tb_lines)].from_bus)
cb |= set(net.line[net.line.index.isin(connected_fb_lines)].to_bus)

Expand All @@ -2429,41 +2456,49 @@ def get_connected_buses(net, buses, consider=("l", "s", "t", "t3", "i"), respect

if "t" in consider or 'trafo' in consider:
in_service_constr = net.trafo.in_service if respect_in_service else True
opened_trafos = set(net.switch.loc[(~net.switch.closed) &
(net.switch.et == "t")].element.unique()) if respect_switches else set()
connected_hvb_trafos = set(net.trafo.index[(net.trafo.hv_bus.isin(buses)) &
~net.trafo.index.isin(opened_trafos) & in_service_constr])
connected_lvb_trafos = set(net.trafo.index[(net.trafo.lv_bus.isin(buses)) &
~net.trafo.index.isin(opened_trafos) & in_service_constr])
opened_trafos = set(net.switch.loc[(~net.switch.closed) & (
net.switch.et == "t")].element.unique()) if respect_switches else set()
connected_hvb_trafos = set(net.trafo.index[(
net.trafo.hv_bus.isin(buses)) & ~net.trafo.index.isin(opened_trafos) &
in_service_constr])
connected_lvb_trafos = set(net.trafo.index[(
net.trafo.lv_bus.isin(buses)) & ~net.trafo.index.isin(opened_trafos) &
in_service_constr])
cb |= set(net.trafo.loc[connected_lvb_trafos].hv_bus.values)
cb |= set(net.trafo.loc[connected_hvb_trafos].lv_bus.values)

# Gives the lv mv and hv buses of a 3 winding transformer
if "t3" in consider or 'trafo3w' in consider:
in_service_constr3w = net.trafo3w.in_service if respect_in_service else True
if respect_switches:
opened_buses_hv = set(net.switch.loc[~net.switch.closed & (net.switch.et == "t3") &
net.switch.bus.isin(net.trafo3w.hv_bus)].bus.unique())
opened_buses_mv = set(net.switch.loc[~net.switch.closed & (net.switch.et == "t3") &
net.switch.bus.isin(net.trafo3w.mv_bus)].bus.unique())
opened_buses_lv = set(net.switch.loc[~net.switch.closed & (net.switch.et == "t3") &
net.switch.bus.isin(net.trafo3w.lv_bus)].bus.unique())
opened_buses_hv = set(net.switch.loc[
~net.switch.closed & (net.switch.et == "t3") &
net.switch.bus.isin(net.trafo3w.hv_bus)].bus.unique())
opened_buses_mv = set(net.switch.loc[
~net.switch.closed & (net.switch.et == "t3") &
net.switch.bus.isin(net.trafo3w.mv_bus)].bus.unique())
opened_buses_lv = set(net.switch.loc[
~net.switch.closed & (net.switch.et == "t3") &
net.switch.bus.isin(net.trafo3w.lv_bus)].bus.unique())
else:
opened_buses_hv = opened_buses_mv = opened_buses_lv = set()

hvb_trafos3w = set(net.trafo3w.index[net.trafo3w.hv_bus.isin(buses) &
~net.trafo3w.hv_bus.isin(opened_buses_hv) & in_service_constr3w])
mvb_trafos3w = set(net.trafo3w.index[net.trafo3w.mv_bus.isin(buses) &
~net.trafo3w.mv_bus.isin(opened_buses_mv) & in_service_constr3w])
lvb_trafos3w = set(net.trafo3w.index[net.trafo3w.lv_bus.isin(buses) &
~net.trafo3w.lv_bus.isin(opened_buses_lv) & in_service_constr3w])

cb |= (set(net.trafo3w.loc[hvb_trafos3w].mv_bus) | set(net.trafo3w.loc[hvb_trafos3w].lv_bus) -
opened_buses_mv - opened_buses_lv)
cb |= (set(net.trafo3w.loc[mvb_trafos3w].hv_bus) | set(net.trafo3w.loc[mvb_trafos3w].lv_bus) -
opened_buses_hv - opened_buses_lv)
cb |= (set(net.trafo3w.loc[lvb_trafos3w].hv_bus) | set(net.trafo3w.loc[lvb_trafos3w].mv_bus) -
opened_buses_hv - opened_buses_mv)
hvb_trafos3w = set(net.trafo3w.index[
net.trafo3w.hv_bus.isin(buses) & ~net.trafo3w.hv_bus.isin(opened_buses_hv) &
in_service_constr3w])
mvb_trafos3w = set(net.trafo3w.index[
net.trafo3w.mv_bus.isin(buses) & ~net.trafo3w.mv_bus.isin(opened_buses_mv) &
in_service_constr3w])
lvb_trafos3w = set(net.trafo3w.index[
net.trafo3w.lv_bus.isin(buses) & ~net.trafo3w.lv_bus.isin(opened_buses_lv) &
in_service_constr3w])

cb |= (set(net.trafo3w.loc[hvb_trafos3w].mv_bus) | set(
net.trafo3w.loc[hvb_trafos3w].lv_bus) - opened_buses_mv - opened_buses_lv)
cb |= (set(net.trafo3w.loc[mvb_trafos3w].hv_bus) | set(
net.trafo3w.loc[mvb_trafos3w].lv_bus) - opened_buses_hv - opened_buses_lv)
cb |= (set(net.trafo3w.loc[lvb_trafos3w].hv_bus) | set(
net.trafo3w.loc[lvb_trafos3w].mv_bus) - opened_buses_hv - opened_buses_mv)

if "i" in consider or 'impedance' in consider:
in_service_constr = net.impedance.in_service if respect_in_service else True
Expand Down Expand Up @@ -2571,7 +2606,8 @@ def get_connected_switches(net, buses, consider=('b', 'l', 't', 't3'), status="a
for et in consider:
if et == 'b':
cs |= set(net['switch'].index[
(net['switch']['bus'].isin(buses) | net['switch']['element'].isin(buses)) &
(net['switch']['bus'].isin(buses) |
net['switch']['element'].isin(buses)) &
(net['switch']['et'] == 'b') & switch_selection])
else:
cs |= set(net['switch'].index[(net['switch']['bus'].isin(buses)) &
Expand Down Expand Up @@ -2610,7 +2646,8 @@ def get_gc_objects_dict():
This function is based on the code in mem_top module
Summarize object types that are tracket by the garbage collector in the moment.
Useful to test if there are memoly leaks.
:return: dictionary with keys corresponding to types and values to the number of objects of the type
:return: dictionary with keys corresponding to types and values to the number of objects of the
type
"""
objs = gc.get_objects()
nums_by_types = dict()
Expand All @@ -2623,9 +2660,10 @@ def get_gc_objects_dict():

def repl_to_line(net, idx, std_type, name=None, in_service=False, **kwargs):
"""
creates a power line in parallel to the existing power line based on the values of the new std_type.
The new parallel line has an impedance value, which is chosen so that the resulting impedance of the new line
and the already existing line is equal to the impedance of the replaced line. Or for electrical engineers:
creates a power line in parallel to the existing power line based on the values of the new
std_type. The new parallel line has an impedance value, which is chosen so that the resulting
impedance of the new line and the already existing line is equal to the impedance of the
replaced line. Or for electrical engineers:
Z0 = impedance of the existing line
Z1 = impedance of the replaced line
Expand Down Expand Up @@ -2682,14 +2720,13 @@ def repl_to_line(net, idx, std_type, name=None, in_service=False, **kwargs):
max_i_ka = i_ka1 - i_ka0
name = "repl_" + str(idx) if name is None else name

# if this line is in service to the existing line, the power flow result should be the same as when replacing the
# existing line with the desired standard type
new_idx = create_line_from_parameters(net, from_bus=net.line.at[idx, "from_bus"],
to_bus=net.line.at[idx, "to_bus"],
length_km=net.line.at[idx, "length_km"], r_ohm_per_km=r_ohm_per_km,
x_ohm_per_km=x_ohm_per_km,
c_nf_per_km=c_nf_per_km, max_i_ka=max_i_ka, g_us_per_km=g_us_per_km,
in_service=in_service, name=name, **kwargs)
# if this line is in service to the existing line, the power flow result should be the same as
# when replacing the existing line with the desired standard type
new_idx = create_line_from_parameters(
net, from_bus=net.line.at[idx, "from_bus"], to_bus=net.line.at[idx, "to_bus"],
length_km=net.line.at[idx, "length_km"], r_ohm_per_km=r_ohm_per_km,
x_ohm_per_km=x_ohm_per_km, c_nf_per_km=c_nf_per_km, max_i_ka=max_i_ka,
g_us_per_km=g_us_per_km, in_service=in_service, name=name, **kwargs)
# restore the previous line parameters before changing the standard type
net.line.loc[idx, :] = bak

Expand Down
4 changes: 2 additions & 2 deletions pandapower/topology/graph_searches.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def connected_component(mg, bus, notravbuses=[]):
EXAMPLE:
import pandapower.topology as top
mg = top.create_nx_graph(net)
mg = top.create_nxgraph(net)
cc = top.connected_component(mg, 5)
Expand Down Expand Up @@ -68,7 +68,7 @@ def connected_components(mg, notravbuses=set()):
EXAMPLE:
import pandapower.topology as top
mg = top.create_nx_graph(net)
mg = top.create_nxgraph(net)
cc = top.connected_components(net, 5)
Expand Down

0 comments on commit 2890c27

Please sign in to comment.