From 9d86f0695556c77a10d4b26c448b88b4faa22e42 Mon Sep 17 00:00:00 2001 From: Steffen Meinecke Date: Mon, 1 Mar 2021 15:11:40 +0100 Subject: [PATCH] add get_connecting_branches() and PeP8 --- pandapower/toolbox.py | 153 ++++++++++++++++---------- pandapower/topology/graph_searches.py | 4 +- 2 files changed, 97 insertions(+), 60 deletions(-) diff --git a/pandapower/toolbox.py b/pandapower/toolbox.py index e1e06e185..a4ea6ee97 100644 --- a/pandapower/toolbox.py +++ b/pandapower/toolbox.py @@ -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()): @@ -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: @@ -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: @@ -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) @@ -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]) @@ -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]: @@ -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 @@ -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) @@ -2429,12 +2456,14 @@ 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) @@ -2442,28 +2471,34 @@ def get_connected_buses(net, buses, consider=("l", "s", "t", "t3", "i"), respect 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 @@ -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)) & @@ -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() @@ -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 @@ -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 return new_idx diff --git a/pandapower/topology/graph_searches.py b/pandapower/topology/graph_searches.py index 7d3a08e95..6aef72e80 100644 --- a/pandapower/topology/graph_searches.py +++ b/pandapower/topology/graph_searches.py @@ -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) @@ -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)