Post · 8 min read· pp. 316–322

Linearised Power Flow

Kirchhoff's Current Law (KCL)

The Kirchhoff's Current Law (KCL) mandates that the sum of power flows into a bus must equal the sum of power flows out of the bus at each time step. It is covered by the more general Energy Balance constraints which is also applied to non-electric buses. For electric buses, KCL is the specific form of the energy balance constraint.

Kirchhoff's Voltage Law (KVL)

For lines and transformers, whose power flows according the impedances, the power flow pl,tp_{l,t} in AC networks is governed by the cycle-based linearised formulation of Kirchhoff's Voltage Law (KVL)

∑lCl,cxlpl,t=0∀ c,t\sum_l C_{l,c} x_l p_{l,t} = 0 \quad \forall\, c,t

where CC is a cycle basis matrix of the network graph, where the independent cycles cc are expressed as directed linear combinations of lines ll, and xlx_l is the series reactance.

While there are different formulations of KVL, the cycle-based formulation was found to be substantially faster than other formulations due to its sparsity, as shown in Hörsch et al. (2018)[^1]. This formulation defines the same feasible space as other standard linearised formulations based on voltage angles that is commonly found in textbooks (B-Theta) or the formulation based on Power Transfer Distribution Factors (PTDFs).

By default, PyPSA constructs the cycle basis from breadth-first spanning trees by Deo et al. (1982)[^2] and refines it through cycle exchanges to shorten the cycles (n.optimize(cycle_basis_method="bfs-refined")).

These constraints are set in the function define_kirchhoff_voltage_constraints() and carry the name Kirchhoff-Voltage-Law.

Tip: KVL with DC networks

For DC networks, replace the series reactance xlx_l by the series resistance rlr_l.

Note: Retrieving the PTDF matrix

The PTDF matrix of sub-networks (i.e. synchronous zones) can be calculated with sn.calculate_PTDF() method.

Tip: Using the Link component for Net Transfer Capacities (NTCs)

For simplified transmission representation using Net Transfer Capacities (NTCs), use the Link component with controllable power flow like a transport model. The Link component can also be used to represent a point-to-point HVDC link.

Note: Handling impedance changes with transmission expansion

If FlF_l is also subject to optimisation (s_nom_extendable=True), the impedance xx of the line is not automatically changed with the capacity (e.g. to represent added parallel lines). However, the extension n.optimize.optimize_transmission_expansion_iteratively() covers this through an iterative process as done Hagspiel et al. (2014)[^3] .

Phase-Shifting Transformers (PSTs)

A phase-shifting transformer (PST) inserts a controllable voltage phase-angle shift between its terminals to redistribute power flows in a meshed network, without adding or removing active power. Following the linearised power-flow treatment of PSTs in Verboomen et al. (2008)[^4] (Section III-A "Linearized Power Flow", eq. (3)), the phase shift enters the cycle-based KVL constraint as an additive angle term:

∑lCl,c(xlpl,t+αl,t)=0∀ c,t\sum_l C_{l,c} \left( x_l p_{l,t} + \alpha_{l,t} \right) = 0 \quad \forall\, c,t

where αl,t\alpha_{l,t} is the voltage phase-angle shift of branch ll in radians (zero for lines and transformers without phase shift).

For a Transformer with phase_shift_min < phase_shift_max, the phase shift becomes a per-snapshot decision variable with bounds

αlmin≤αl,t≤αlmax∀ l,t\alpha_l^{min} \leq \alpha_{l,t} \leq \alpha_l^{max} \quad \forall\, l,t

given by the input attributes phase_shift_min and phase_shift_max (in degrees, converted to radians internally). Otherwise the phase shift is fixed to the static or series input phase_shift. The realised per-snapshot angle is written to the dynamic output n.transformers_t["phase_shift_opt"].

The variables are set in define_phase_shift_variables() and carry the name Transformer-phase_shift; the angle term is added within define_kirchhoff_voltage_constraints().

Note: Sign convention

PyPSA's power-flow sign convention is pl,t=(θ0−θ1−αl,t)/xlp_{l,t} = (\theta_0 - \theta_1 - \alpha_{l,t}) / x_l, consistent with n.pf() and n.lpf(). This is the negative of the angle convention used in Verboomen et al. (2008)[^4], i.e. αPyPSA=−αVerboomen\alpha^{PyPSA} = -\alpha^{Verboomen}.

Linear Loss Approximation

The AC transmission losses ψl,t\psi_{l,t} for line ll at timestep tt are given by the loss parabola

ψl,t=rlpl,t2,\psi_{l,t} = r_{l} p_{l,t}^2,

where rlr_l is the resistance of line ll, following Neumann et al. (2022)[^5].

In PyPSA this non-linear function is linearized with piecewise linear constraints:

0≤ψl,t≤rl(pˉl,tP‾l)2∀l,t0 \leq \psi_{l,t} \leq r_{l} (\bar{p}_{l,t} \overline{P}_{l})^2 \quad \forall l,t

ψl,t≥mk⋅pl,t+ak∀l,t, k=1,…,n\psi_{l,t} \geq m_k \cdot p_{l,t} + a_k \quad \forall l,t,\ k = 1, \dots, n

ψl,t≥−mk⋅pl,t+ak∀l,t, k=1,…,n\psi_{l,t} \geq -m_k \cdot p_{l,t} + a_k \quad \forall l,t,\ k = 1, \dots, n

where pˉl,t\bar{p}_{l,t} is the maximum per-unit powerflow at time tt and P‾l2\overline{P}_{l}^2 is the (maximum) line capacity. The index kk denotes the segments of the linear approximation with a total of nn segments, with offsets aka_k and with slopes mkm_k.

The losses also modify the power balance by adding the following term to its left-hand side:

−0.5⋅∑l∣Kn,l∣⋅ψl,t∀n,t-0.5 \cdot \sum_{l} |K_{n,l}| \cdot \psi_{l,t} \quad \forall n,t

splitting losses equally between both connection points.

The dispatch limits of pℓ,tp_{\ell,t} are now subtracted by ψl,t\psi_{l,t}.

Two linearization methods are available in PyPSA: the tangent-based approximation (define_tangent_loss_constraints()) and the secant-based approximation with error control (define_secant_loss_constraints()).

The transmission loss approximation is not activated by default, but must be enabled in n.optimize().

text
# Secant-based approximation with error control
n.optimize(transmission_losses=True)
# Or with custom tolerances
n.optimize(transmission_losses={"mode": "secants", "atol": 1, "rtol": 0.1})
# Tangent-based approximation
n.optimize(transmission_losses={"mode": "tangents", "segments": 3})

When passing a dict, the mode key selects the method. Additional keys are forwarded to the corresponding constraint function:

Hint: Calculating transmission losses

The losses can be calculated with n.lines_t.p0 + n.lines_t.p1.

Mapping of symbols to component attributes

SymbolAttributeType
pl,tp_{l,t}n.lines_t.p0 or n.transformers_t.p0Decision variable
ψl,t\psi_{l,t}n.lines_t.losses or n.transformers_t.lossesDecision variable
pˉl,t\bar{p}_{l,t}n.lines_t.s_max_pu or n.transformers_t.s_max_puParameter
Pˉl\bar{P}_{l}n.lines.s_nom_opt and n.transformers.s_nom_opt (if extendable) or n.lines.s_nom or n.transformers.s_nom (if non-extendable)Decision variable / Parameter
xlx_ln.lines.x_pu_eff or n.transformers.x_pu_effParameter
rlr_ln.lines.r_pu_eff or n.transformers.r_pu_effParameter
Cl,cC_{l,c}Cycle matrix calculated by find_cycles()Parameter
Kn,lK_{n,l}Incidence matrix calculated by n.incidence_matrix()Parameter

Secant-Based Linearization with Error Control

The secant-based linearization is designed to overestimate the AC line losses and is guaranteed to stay within the error bounds defined by the specified absolute (atol) and relative (rtol) error tolerances. The slope mkm_k and offset aka_k are derived as

mk,l=rl(ρk−1+ρk)m_{k, l} = r_l (\rho_{k-1} + \rho_{k})

ak,l=−rlρk−1ρka_{k, l} = - r_l \rho_{k-1} \rho_{k}

where ρk\rho_k are suitably chosen points on the loss parabola. For details on how these points are derived see the original PR.

Tangent-Based Linearization

Warning: Depending on parameters, some lines can be lossless when the tangent-based linearization is used

  • The tangent-based approximation is designed to underestimate losses
  • Its segments depend on s_nom_max
  • The first segment models zero losses
  • If a line has s_nom_max > 2 * n_segments * s_nom_opt then that line is lossless, since all flows stay in the first segment of the approximation.

For the tangent-based linearization the slope mkm_k and offset aka_k are derived as:

ψl,t(k)=rl(kn⋅pˉl,tP‾l)2\psi_{l,t}(k) = r_{l} \left(\frac{k}{n} \cdot \bar{p}_{l,t} \overline{P}_{l} \right)^2

mk=dψl,t(k)dk=2rl(kn⋅pˉl,tP‾l)m_k = \frac{d \psi_{l,t}(k)}{dk} = 2 r_{l} \left(\frac{k}{n} \cdot \bar{p}_{l,t} \overline{P}_{l} \right)

ak=ψl,t(k)−mk(kn⋅pˉl,tP‾l)a_k = \psi_{l,t}(k) - m_k \left(\frac{k}{n} \cdot \bar{p}_{l,t} \overline{P}_{l} \right)

The higher the number of tangents, the more accurate the approximation, but also the more constraints are added to the optimisation problem. Typically, 2-4 tangents are sufficient for a reasonably accurate approximation.

More details on this implementation can be found in Neumann et al. (2022)[^5].

Examples

  • Meshed AC-DC Networks


    Builds a stylized 3-node AC network coupled via AC-DC converters to a 3-node DC network.

    Go to example

  • Negative LMPs from Line Congestion


    Explores how Kirchhoff's Voltage Law can lead to negative locational marginal prices when lines are congested.

    Go to example


[^1]: J. Hörsch, H. Ronellenfitsch, D. Witthaut, T. Brown (2018), Linear optimal power flow using cycle flows, Electric Power Systems Research, 158, 126-135, doi:10.1016/j.epsr.2017.12.034. [^2]: N. Deo, G. M. Prabhu, M. S. Krishnamoorthy (1982), Algorithms for Generating Fundamental Cycles in a Graph, ACM Transactions on Mathematical Software, 8 (1), 26-42, doi:10.1145/355984.355988. [^3]: S. Hagspiel, C. Jägemann, D. Lindenberger, T. Brown, S. Cherevatskiy, E. Tröster (2014), Cost-optimal power system extension under flow-based market coupling, Energy, 66, 654-666, doi:10.1016/j.energy.2014.01.025. [^4]: J. Verboomen, D. Van Hertem, P. H. Schavemaker, W. L. Kling, R. Belmans (2008), Analytical Approach to Grid Operation With Phase Shifting Transformers, IEEE Transactions on Power Systems, 23 (1), 41-46, doi:10.1109/TPWRS.2007.913197. See Section III-A "Linearized Power Flow", eq. (3). [^5]: F. Neumann, V. Hagenmeyer, T. Brown (2022), Assessments of linear power flow and transmission loss approximations in coordinated capacity expansion problems, Applied Energy, 314, 118859, doi:10.1016/j.apenergy.2022.118859.

diagram 2026-10-04

diagram 2025-10-09