ac-branch-pi-model
AC branch pi-model power flow equations (P/Q and |S|) with transformer tap ratio and phase shift, matching `acopf-math-model.md` and MATPOWER branch fields....
Documentation & Preview
""" Branch pi-model power flow helpers (MATPOWER compatible).
Computes per-unit branch flows (P_ij, Q_ij, P_ji, Q_ji) using the same equations as tasks/energy-ac-optimal-power-flow/environment/acopf-math-model.md. """
from __future__ import annotations
import math from typing import Dict, Tuple
import numpy as np
def deg2rad(deg: float) -> float: return deg * math.pi / 180.0
def build_bus_id_to_idx(buses: np.ndarray) -> Dict[int, int]: """Return mapping BUS_I -> 0-indexed row index.""" return {int(buses[i, 0]): i for i in range(int(buses.shape[0]))}
def compute_branch_flows_pu( Vm: np.ndarray, Va: np.ndarray, br: np.ndarray, bus_id_to_idx: Dict[int, int], ) -> Tuple[float, float, float, float]: """ Compute branch flows in per-unit: (P_ij, Q_ij, P_ji, Q_ji).
Inputs:
- Vm: bus voltage magnitudes (pu), aligned to
busesordering - Va: bus voltage angles (rad), aligned to
busesordering - br: one MATPOWER branch row:
[F_BUS, T_BUS, BR_R, BR_X, BR_B, RATE_A, RATE_B, RATE_C, TAP, SHIFT, BR_STATUS, ANGMIN, ANGMAX]
- bus_id_to_idx: mapping from bus id to index in Vm/Va arrays
""" r = float(br[2]) x = float(br[3]) bc = float(br[4])
Tap ratio: MATPOWER uses 0 for "no transformer" => treat as 1.0
tap = float(br[8]) if abs(tap) < 1e-12: tap = 1.0
shift = deg2rad(float(br[9]))
Series admittance y = 1/(r+jx) = g + jb
if abs(r) < 1e-12 and abs(x) < 1e-12: g = 0.0 b = 0.0 else: denom = r * r + x * x g = r / denom b = -x / denom
fbus = int(br[0]) tbus = int(br[1]) i = bus_id_to_idx[fbus] j = bus_id_to_idx[tbus]
Vm_i = float(Vm[i]) Vm_j = float(Vm[j]) Va_i = float(Va[i]) Va_j = float(Va[j])
inv_t = 1.0 / tap inv_t2 = inv_t * inv_t
i -> j
delta_ij = Va_i - Va_j - shift c = math.cos(delta_ij) s = math.sin(delta_ij) P_ij = g * Vm_i * Vm_i * inv_t2 - Vm_i * Vm_j * inv_t * (g * c + b * s) Q_ij = -(b + bc / 2.0) * Vm_i * Vm_i * inv_t2 - Vm_i * Vm_j * inv_t * (g * s - b * c)
j -> i
delta_ji = Va_j - Va_i + shift c2 = math.cos(delta_ji) s2 = math.sin(delta_ji) P_ji = g * Vm_j * Vm_j - Vm_i * Vm_j * inv_t * (g * c2 + b * s2) Q_ji = -(b + bc / 2.0) * Vm_j * Vm_j - Vm_i * Vm_j * inv_t * (g * s2 - b * c2)
return P_ij, Q_ij, P_ji, Q_ji