Skip to content

Add compute_switch_flows() for zero-impedance bus-bus switches#2956

Open
pehlert wants to merge 1 commit intoe2nIEE:developfrom
pehlert:feature/compute-zero-impedance-switch-flows
Open

Add compute_switch_flows() for zero-impedance bus-bus switches#2956
pehlert wants to merge 1 commit intoe2nIEE:developfrom
pehlert:feature/compute-zero-impedance-switch-flows

Conversation

@pehlert
Copy link
Copy Markdown
Contributor

@pehlert pehlert commented Apr 7, 2026

Add compute_switch_flows() to populate res_switch for zero-impedance bus-bus switches

Motivation

After runpp(), bus-bus switches with z_ohm=0 (the default) always have NaN in res_switch, since Pandapower fuses adjacent buses into a single PPC node. This makes it impossible to determine power flow through individual bus couplers, bus-section switches, or breaker-and-a-half arrangements.

Knowing cross-coupler flows is a common requirement in SCADA/EMS applications: real-time systems measure these flows via current transformers on the coupler path, and any simulation or state estimation that drives an SLD needs to reproduce them. Detailed substation models from CGMES/CIM imports represent the full busbar topology with explicit switching devices, and users expect res_switch to reflect the actual flow through each device.

Setting z_ohm to a small positive value is the existing workaround, but it causes convergence failures on large networks. We observed this consistently with 12,000+ bus-bus switches -- the Jacobian becomes near-singular from the many very-low-impedance branches.

Solution

New toolbox function compute_switch_flows(net), called after a converged load flow:

  1. Identify fused-bus groups from the internal bus lookup.
  2. Calculate net local injection per bus from res_* tables (loads, generators, shunts, wards, outgoing branches).
  3. Build the switch tree within each fused group. Raises ValueError if a cycle is detected (flow split is physically indeterminate without impedance).
  4. DFS from leaves to root to accumulate subtree demand and derive individual switch flows.
  5. Write p_from_mw, q_from_mvar, p_to_mw, q_to_mvar, i_ka, and loading_percent into net.res_switch.

Switches with z_ohm > 0 are not modified.

Usage

import pandapower as pp
from pandapower.toolbox import compute_switch_flows

pp.runpp(net)
compute_switch_flows(net)
net.res_switch  # now populated for z_ohm=0 bus-bus switches

After runpp(), bus-bus switches with z_ohm=0 have NaN in res_switch
because Pandapower fuses their buses into a single internal node.
This makes it impossible to determine the power flow through
individual bus couplers, bus-section switches, or similar zero-
impedance switching devices.

This commit adds a new toolbox function compute_switch_flows(net)
that populates res_switch (p_from_mw, q_from_mvar, p_to_mw,
q_to_mvar, i_ka, loading_percent) for these switches using a
post-hoc nodal balance approach:

1. Identify fused-bus groups from _pd2ppc_lookups["bus"]
2. Calculate net local injection at each bus from res_* tables
   (loads, generators, shunts, wards, and outgoing branch flows)
3. Build the zero-impedance switch tree within each fused group
4. DFS from leaves to root to accumulate subtree demand and
   derive individual switch flows

The function raises ValueError if zero-impedance switches form a
cycle within a fused group, since the flow split is physically
indeterminate without impedance information.

Switches with z_ohm > 0 are not modified, as they already have
results from the Newton-Raphson solver.

Also adds documentation (toolbox.rst, switch.rst, CHANGELOG.rst)
and 17 tests covering single couplers, chains, branching trees,
cycle detection, sign convention, cross-validation, and loading
percent.
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud bot commented Apr 7, 2026

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 7, 2026

Codecov Report

❌ Patch coverage is 90.84507% with 13 lines in your changes missing coverage. Please review.
✅ Project coverage is 72.06%. Comparing base (b0a54a3) to head (994cf0f).

Files with missing lines Patch % Lines
pandapower/toolbox/result_info.py 90.84% 13 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #2956      +/-   ##
===========================================
+ Coverage    71.99%   72.06%   +0.06%     
===========================================
  Files          352      352              
  Lines        38305    38447     +142     
===========================================
+ Hits         27577    27706     +129     
- Misses       10728    10741      +13     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant