ac-branch-pi-model
$
npx mdskill add elizaOS/eliza/ac-branch-pi-modelImplement the exact branch power flow equations in `acopf-math-model.md` using MATPOWER branch data:
SKILL.md
.github/skills/ac-branch-pi-modelView on GitHub ↗
---
name: ac-branch-pi-model
description: "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. Use when computing branch flows in either direction, aggregating bus injections for nodal balance, checking MVA (rateA) limits, computing branch loading %, or debugging sign/units issues in AC power flow."
---
# AC Branch Pi-Model + Transformer Handling
Implement the exact branch power flow equations in `acopf-math-model.md` using MATPOWER branch data:
```
[F_BUS, T_BUS, BR_R, BR_X, BR_B, RATE_A, RATE_B, RATE_C, TAP, SHIFT, BR_STATUS, ANGMIN, ANGMAX]
```
## Quick start
- Use `scripts/branch_flows.py` to compute per-unit branch flows.
- Treat the results as **power leaving** the “from” bus and **power leaving** the “to” bus (i.e., compute both directions explicitly).
Example:
```python
import json
import numpy as np
from scripts.branch_flows import compute_branch_flows_pu, build_bus_id_to_idx
data = json.load(open("/root/network.json"))
baseMVA = float(data["baseMVA"])
buses = np.array(data["bus"], dtype=float)
branches = np.array(data["branch"], dtype=float)
bus_id_to_idx = build_bus_id_to_idx(buses)
Vm = buses[:, 7] # initial guess VM
Va = np.deg2rad(buses[:, 8]) # initial guess VA
br = branches[0]
P_ij, Q_ij, P_ji, Q_ji = compute_branch_flows_pu(Vm, Va, br, bus_id_to_idx)
S_ij_MVA = (P_ij**2 + Q_ij**2) ** 0.5 * baseMVA
S_ji_MVA = (P_ji**2 + Q_ji**2) ** 0.5 * baseMVA
print(S_ij_MVA, S_ji_MVA)
```
## Model details (match the task formulation)
### Per-unit conventions
- Work in per-unit internally.
- Convert with `baseMVA`:
- \(P_{pu} = P_{MW} / baseMVA\)
- \(Q_{pu} = Q_{MVAr} / baseMVA\)
- \(|S|_{MVA} = |S|_{pu} \cdot baseMVA\)
### Transformer handling (MATPOWER TAP + SHIFT)
- Use \(T_{ij} = tap \cdot e^{j \cdot shift}\).
- **Implementation shortcut (real tap + phase shift):**
- If `abs(TAP) < 1e-12`, treat `tap = 1.0` (no transformer).
- Convert `SHIFT` from degrees to radians.
- Use the angle shift by modifying the angle difference:
- \(\delta_{ij} = \theta_i - \theta_j - shift\)
- \(\delta_{ji} = \theta_j - \theta_i + shift\)
### Series admittance
Given `BR_R = r`, `BR_X = x`:
- If `r == 0 and x == 0`, set `g = 0`, `b = 0` (avoid divide-by-zero).
- Else:
- \(y = 1/(r + jx) = g + jb\)
- \(g = r/(r^2 + x^2)\)
- \(b = -x/(r^2 + x^2)\)
### Line charging susceptance
- `BR_B` is the **total** line charging susceptance \(b_c\) (per unit).
- Each end gets \(b_c/2\) in the standard pi model.
## Power flow equations (use these exactly)
Let:
- \(V_i = |V_i| e^{j\theta_i}\), \(V_j = |V_j| e^{j\theta_j}\)
- `tap` is real, `shift` is radians
- `inv_t = 1/tap`, `inv_t2 = inv_t^2`
Then the real/reactive power flow from i→j is:
- \(P_{ij} = g |V_i|^2 inv\_t2 - |V_i||V_j| inv\_t (g\cos\delta_{ij} + b\sin\delta_{ij})\)
- \(Q_{ij} = -(b + b_c/2)|V_i|^2 inv\_t2 - |V_i||V_j| inv\_t (g\sin\delta_{ij} - b\cos\delta_{ij})\)
And from j→i is:
- \(P_{ji} = g |V_j|^2 - |V_i||V_j| inv\_t (g\cos\delta_{ji} + b\sin\delta_{ji})\)
- \(Q_{ji} = -(b + b_c/2)|V_j|^2 - |V_i||V_j| inv\_t (g\sin\delta_{ji} - b\cos\delta_{ji})\)
Compute apparent power:
- \(|S_{ij}| = \sqrt{P_{ij}^2 + Q_{ij}^2}\)
- \(|S_{ji}| = \sqrt{P_{ji}^2 + Q_{ji}^2}\)
## Common uses
### Enforce MVA limits (rateA)
- `RATE_A` is an MVA limit (may be 0 meaning “no limit”).
- Enforce in both directions:
- \(|S_{ij}| \le RATE_A\)
- \(|S_{ji}| \le RATE_A\)
### Compute branch loading %
For reporting “most loaded branches”:
- `loading_pct = 100 * max(|S_ij|, |S_ji|) / RATE_A` if `RATE_A > 0`, else 0.
### Aggregate bus injections for nodal balance
To build the branch flow sum for each bus \(i\):
- Add \(P_{ij}, Q_{ij}\) to bus i
- Add \(P_{ji}, Q_{ji}\) to bus j
This yields arrays `P_out[i]`, `Q_out[i]` such that the nodal balance can be written as:
- \(P^g - P^d - G^s|V|^2 = P_{out}\)
- \(Q^g - Q^d + B^s|V|^2 = Q_{out}\)
## Sanity checks (fast debug)
- With `SHIFT=0` and `TAP=1`, if \(V_i = V_j\) and \(\theta_i=\theta_j\), then \(P_{ij}\approx 0\) and \(P_{ji}\approx 0\) (lossless only if r=0).
- For a pure transformer (`r=x=0`) you should not get meaningful flows; treat as `g=b=0` (no series element).
More from elizaOS/eliza
- academic-pdf-redactionRedact text from PDF documents for blind review anonymization
- ada-plan-view-accessibilityUse when checking simplified ADA-derived plan-view bathroom accessibility constraints such as turning space, door clear width, toilet centerline, grab bars, and lavatory knee/toe clearance.
- analyze-ciAnalyze failed GitHub Action jobs for a pull request.
- architectural-dxf-extractionUse when extracting plan-view architectural geometry from DXF files with semantic CAD layers, especially when outputs must normalize rooms, doors, fixtures, clearances, and grab bars into machine-checkable JSON.
- attitude-controller-plannerUse this skill when implementing the inner control loop for a quadrotor — attitude (roll/pitch/yaw) PID control and attitude planning (converting desired acceleration to desired Euler angles). Covers gain layout, integral reset pattern, and the attitude planner inverse kinematics.
- azure-bgpAnalyze and resolve BGP oscillation and BGP route leaks in Azure Virtual WAN–style hub-and-spoke topologies (and similar cloud-managed BGP environments). Detect preference cycles, identify valley-free violations, and propose allowed policy-level mitigations while rejecting prohibited fixes.
- box-least-squaresBox Least Squares (BLS) periodogram for detecting transiting exoplanets and eclipsing binaries. Use when searching for periodic box-shaped dips in light curves. Alternative to Transit Least Squares, available in astropy.timeseries. Based on Kovács et al. (2002).
- browser-testingVERIFY your changes work. Measure CLS, detect theme flicker, test visual stability, check performance. Use BEFORE and AFTER making changes to confirm fixes. Includes ready-to-run scripts: measure-cls.ts, detect-flicker.ts
- cache-policy-comparisonCompare and implement eviction policies (LRU, LFU, FIFO, S3FIFO, ARC) for bounded-capacity caches. Use when choosing or implementing an eviction policy for a buffer pool, page cache, CDN edge, or LLM KV cache, or when writing a replay simulator that supports multiple policies. Clarifies recency vs frequency semantics, queue topology, saturating counters, ghost buffers, and the second-chance rule that distinguishes modern FIFO-family policies from classic LRU.
- casadi-ipopt-nlpNonlinear optimization with CasADi and IPOPT solver. Use when building and solving NLP problems: defining symbolic variables, adding nonlinear constraints, setting solver options, handling multiple initializations, and extracting solutions. Covers power systems optimization patterns including per-unit scaling and complex number formulations.