Continuation Power Flow
Goal
A continuation power flow (CPF) traces the solution curve of a grid as its injections move from where they are now towards a target state, and reports where that curve turns back on itself – the “nose”, i.e. the maximum loading the grid can serve at all. It answers “how much more load can this grid take, and which bus gives way first”, which a sequence of ordinary powerflows cannot: near the collapse point the Newton-Raphson simply stops converging, and a divergence does not tell you whether the grid is at its limit or your starting point was bad.
The parameter along the curve is called \(\lambda\): \(\lambda = 0\) is the grid’s
own state, \(\lambda = 1\) the target state. This is MATPOWER’s runcpf
formulation, and the option names and defaults below follow its cpf.* options.
Note
This is a natural parameterisation: the corrector solves at a fixed
\(\lambda\), so the curve stops at the nose and does not trace the lower
branch. Rounding the nose needs an arc-length parameterisation (MATPOWER’s
cpf.parameterization 2 / 3), which is not implemented yet.
Basic usage
import pandapower.networks as pn
from lightsim2grid.network import init_from_pandapower
from lightsim2grid import run_cpf
grid = init_from_pandapower(pn.case118())
res = run_cpf(grid, loading_factor=3.0)
print(res.msg)
print(f"collapse at {res.lam_max:.3f} of the requested increase")
print(f"ie a load {1 + res.lam_max * (3.0 - 1.0):.3f} times the base one")
res is a lightsim2grid.continuationPowerflow.CPFResult: lam, V,
Vm, Va_deg, tangent_lam, lam_max, success and msg. The helper
plot_pv_curve(res) draws the PV curve of the worst buses if matplotlib is installed.
Steering which elements move
By default every load grows together and generation follows, so the direction is
“the whole grid, uniformly”. Two vectors change that, one coefficient per element in
[0, 1]: 0 holds that element at its base value, 1 moves it fully.
import numpy as np
# only the loads of one area grow; everything else stays put
steering = np.zeros(len(grid.get_loads()))
steering[my_area_load_ids] = 1.0
res = run_cpf(grid, loading_factor=3.0, load_steering=steering)
# ... and this one grows at only 40% of the rate of the others
steering[another_load_id] = 0.4
gen_steering is the same, per generator, and also defaults to all ones: generation
follows the load. Passing gen_steering=0 instead holds generation fixed and makes the
slack supply the whole increase; that is a legitimate study, but it traces a different
curve with a different nose, so do not compare the two margins.
Note
MATPOWER requires a target case to be “same as base case w.r.t. Qg and slack Pg”, and
slack machines are deliberately not excluded here even so. MATPOWER’s rule exists
because a slack bus has no active-power equation, so its Pg is an output of the
powerflow rather than an input – specifying a change there asks for something the
solver ignores. That reasoning holds for a single slack in lightsim2grid too
(scaling the slack machine’s target_p leaves the solution bit-identical), which
makes excluding it a no-op rather than a correction. It does not hold for a
distributed slack, where every participant’s target_p genuinely enters its
bus’ equation – excluding them would silently freeze most of the generation on a grid
whose slack is spread over many machines. One rule for every generator is simpler and
correct in both cases.
Either way the slack machine’s output still follows the load: with every other machine scaled by \(k\) it ends up producing roughly \(k\) times its base power. Holding its setpoint would never have held its production.
scale_q=True (the default) scales each load’s reactive power by the same coefficient
as its active power, i.e. at constant power factor. For a direction that no combination
of these expresses, direction= takes explicit per-element deltas in MW / MVAr:
res = run_cpf(grid, direction={"load_p": delta_load_p, "gen_p": delta_gen_p})
Performance
The continuation is a batch algorithm
(lightsim2grid.lightsim2grid_cpp.ContinuationSweepCPP), not a Python loop around
ac_pf: the topology is fixed for the whole curve, so the Jacobian’s sparsity pattern
is too, and the whole curve costs one symbolic factorization whatever the number of
points. Every corrector after the first only refactorizes, and each predictor is a single
triangular solve reusing the factorization the corrector left standing – which is what
makes tracing a curve of several hundred points affordable.
You can check this on any run:
cpf = ContinuationPowerFlow(grid)
res = cpf.run(loading_factor=3.0)
assert cpf.cpp.get_linear_solver_stats().nb_analyze == 1
Only the Newton-Raphson algorithms are supported (the predictor needs a Jacobian);
AlgorithmType.GaussSeidel, the fast-decoupled family and the DC algorithms are
refused with an explicit message. So are the NROuter_* ones when the grid has outer
loops: between two correctors they would change the system being continued.
ContinuationPowerFlow built on such a grid uses the plain Newton-Raphson instead.
Options
Named and defaulted after MATPOWER’s cpf.*:
option |
default |
meaning |
|---|---|---|
|
0.05 |
nominal step, an arc length along the tangent |
|
1e-4 |
failing at this step ends the curve |
|
0.2 |
largest step the adaptation may grow to |
|
|
adapt the step to the predictor’s error |
|
0.7 |
damping of that adaptation |
|
1e-3 |
predictor error the adaptation aims at |
|
1e-5 |
|
|
|
stop at this lambda instead of at the nose |
|
1000 |
hard cap on the number of traced points |
|
|
refactorize J at each point before its tangent |
tangent_lam is the tangent’s \(\lambda\) component. With this parameterisation it
is strictly positive and tends to zero at the nose – it is a threshold, never a sign
change.
Detailed usage
Continuation powerflow (CPF): trace the PV curve of a grid up to its voltage-collapse “nose” point.
Everything numerical happens in C++ (lightsim2grid.lightsim2grid_cpp.ContinuationSweepCPP,
see src/core/batch_algorithm/ContinuationSweep.hpp). This module only turns a
loading factor and a pair of steering vectors into the target injection state the C++
class continues towards, and presents the traced curve back.
Classes:
|
One traced curve. |
|
Continuation powerflow on a |
Functions:
|
Plot |
|
One-shot helper: build a |
- class lightsim2grid.continuationPowerflow.CPFResult(lam, V, tangent_lam, success, msg, nb_retries, cpp)[source]
One traced curve. Attributes:
- lam:
(n_points,) The continuation parameter at each point.
lam[0]is 0 (the base case) andlam == 1is the target state, i.e. the full requested increase. So the load of loadlat pointiisload_p[l] * (1 + alpha[l] * (k - 1) * lam[i])withkthe loading factor andalphathe steering vector.- V:
(n_points, n_bus)complex Bus voltages at each point, in the grid’s own bus ordering. Buses that are not part of the solved system read exactly
0.- Vm, Va_deg:
(n_points, n_bus) Magnitude (pu) and angle (degrees) of
V.- tangent_lam:
(n_points,) The tangent’s lambda component at each point, in
(0, 1]. It tends to zero as the curve turns: that is the collapse indicator. The last point has no tangent computed after it and reads 0.- lam_max: float
The largest lambda reached, i.e. the loading margin along the chosen direction.
- success: bool
Whether the curve reached its requested end (the nose, or
stop_at_lam).- msg: str
Why the run stopped, in words.
- nb_retries: int
How many times a corrector failed and the step had to be halved.
Attributes:
the underlying C++ object, for the timers, the flows and the linear-solver counters
- cpp
the underlying C++ object, for the timers, the flows and the linear-solver counters
- lam:
- class lightsim2grid.continuationPowerflow.ContinuationPowerFlow(grid, algorithm=None)[source]
Continuation powerflow on a
lightsim2grid.network.LSGrid.The curve runs from the grid’s own injection state (
lambda = 0) to a target state (lambda = 1), following the classical predictor / corrector scheme. This is MATPOWER’sruncpfformulation, and the option names and defaults below follow itscpf.*options so that nothing here is a new concept.The target is built for you from a loading factor and two steering vectors:
load_steering– one coefficient per load, in[0, 1]. A load with 0 does not move during the continuation; a load with 1 moves fully. Defaults to all ones (every load scaled).gen_steering– the same, per generator. Defaults to all ones, so generation follows the load. Pass0to hold generation fixed and let the slack supply the whole increase instead; that traces a different curve, with a different nose.
Note
MATPOWER requires a target case to be “same as base case w.r.t. Qg and slack Pg”, and the slack machines are deliberately NOT excluded here even so. The reason for MATPOWER’s rule is that a slack bus has no active-power equation, so its
Pgis an output of the powerflow rather than an input – asking for it to change would be asking for something the solver ignores. That reasoning transfers to a single slack in lightsim2grid (scaling the slack machine’starget_pthere is provably inert: the solution is bit-identical), which makes excluding it a no-op rather than a correction. It does NOT transfer to a distributed slack, where every participant’starget_pgenuinely enters its bus’ equation – and excluding them all would silently freeze most of the generation on a grid whose slack is spread over many machines. One rule for every generator is both simpler and right in both cases.Either way the slack machine’s output still follows the load: with every other machine scaled by
kit ends up producing roughlyktimes its base power. Holding its setpoint would never have held its production.Warning
This is a natural parameterisation: the corrector solves at a fixed lambda, so the curve stops AT the nose and does not trace the lower branch. That needs an arc-length parameterisation (MATPOWER’s
cpf.parameterization2 / 3), which is not implemented yet; when it is, it will become the default here as it is there.Examples
import numpy as np from lightsim2grid.network import init_from_pandapower from lightsim2grid.continuationPowerflow import ContinuationPowerFlow import pandapower.networks as pn grid = init_from_pandapower(pn.case118()) # how much margin is there if every load grows together? cpf = ContinuationPowerFlow(grid) res = cpf.run(loading_factor=3.0) print(res.lam_max) # ... and if only the loads of one area grow? steering = np.zeros(len(grid.get_loads())) steering[my_area_load_ids] = 1.0 res = cpf.run(loading_factor=3.0, load_steering=steering)
Attributes:
The underlying C++ object (timers, flows, linear-solver counters).
Methods:
run([loading_factor, load_steering, ...])Trace the curve and return a
CPFResult.- property cpp
The underlying C++ object (timers, flows, linear-solver counters).
- run(loading_factor=2.0, load_steering=None, gen_steering=None, scale_q=True, direction=None, stop_at_lam=None, step=0.05, step_min=0.0001, step_max=0.2, adapt_step=False, adapt_step_damping=0.7, adapt_step_tol=0.001, nose_tol=1e-05, max_steps=1000, exact_tangent=False, v_init=None, max_iter=10, tol=1e-08)[source]
Trace the curve and return a
CPFResult.- Parameters:
loading_factor (
float) – What the fully-steered elements reach atlambda = 1: 2.0 means “twice the base load”. Must be > 1.load_steering (
np.ndarray, optional) – One coefficient per load, in[0, 1]; see the class docstring.None(default) means all ones.gen_steering (
np.ndarrayorfloat, optional) – One coefficient per generator, in[0, 1], or a single number applied to every generator.None(default) means all ones, so generation follows the load. Pass0to hold generation fixed and let the slack supply the whole increase. See the class docstring on why slack machines are not excluded.scale_q (
bool) – Scale each load’s reactive power by the same coefficient as its active power (constant power factor, the default).Falseholds Q at its base value.direction (
dict, optional) – Escape hatch: explicit per-element DELTAS from the base state, in MW / MVAr, as{"load_p": ..., "load_q": ..., "gen_p": ..., "sgen_p": ...}(any subset). Mutually exclusive with the steering arguments.stop_at_lam (
float, optional) – Stop once lambda reaches this value (MATPOWER’s numericcpf.stop_at).None(default) traces until the nose.step – The continuation’s step control, named and defaulted as MATPOWER’s
cpf.*.step_min – The continuation’s step control, named and defaulted as MATPOWER’s
cpf.*.step_max – The continuation’s step control, named and defaulted as MATPOWER’s
cpf.*.adapt_step – The continuation’s step control, named and defaulted as MATPOWER’s
cpf.*.adapt_step_damping – The continuation’s step control, named and defaulted as MATPOWER’s
cpf.*.adapt_step_tol – The continuation’s step control, named and defaulted as MATPOWER’s
cpf.*.nose_tol – The continuation’s step control, named and defaulted as MATPOWER’s
cpf.*.max_steps (
int) – Hard cap on the number of traced points.exact_tangent (
bool) – Refactorize the Jacobian at each converged point before taking its tangent. Off by default: the tangent then reuses the factorization the corrector left standing, which is one Newton iterate behind the converged point.v_init (
np.ndarray, optional) – Starting voltage, one entry per grid bus. Defaults to a flat 1.04 pu start.max_iter – Passed to every corrector’s Newton-Raphson.
tolis in MVA (not in per unit), as forlightsim2grid.network.LSGrid.ac_pf().tol – Passed to every corrector’s Newton-Raphson.
tolis in MVA (not in per unit), as forlightsim2grid.network.LSGrid.ac_pf().
- lightsim2grid.continuationPowerflow.plot_pv_curve(res, bus_indices=None, figsize=(10, 4))[source]
Plot
|V|against lambda for the given buses, and the tangent’s lambda component against the point index (which falls towards 0 at the nose).Needs matplotlib.
bus_indicesdefaults to the ten buses whose voltage drops most along the curve – the ones that actually say something about the collapse.
- lightsim2grid.continuationPowerflow.run_cpf(grid, **kwargs)[source]
One-shot helper: build a
ContinuationPowerFlowongridand run it. Every keyword argument is forwarded toContinuationPowerFlow.run(), exceptalgorithmwhich selects the Newton-Raphson algorithm to use.from lightsim2grid.continuationPowerflow import run_cpf res = run_cpf(grid, loading_factor=3.0) print(f"collapse at {res.lam_max:.3f} of the requested increase")