gtopt::kirchhoff::cycle_basis namespace

Classes

struct CycleEdge
struct Edge
struct SwitchableEdge

Typedefs

using Cycle = std::vector<CycleEdge>
using SwitchableLineLookup = std::function<std::optional<SwitchableEdge>( std::size_t line_index, BlockUid buid)>

Functions

auto add_kvl_rows(const SystemContext& sc, const ScenarioLP& scenario, const StageLP& stage, LinearProblem& lp, const Collection<BusLP>& buses, const Collection<LineLP>& lines, const SwitchableLineLookup& switchable_lookup = {}) -> std::size_t
auto build_fundamental_cycles(std::size_t num_buses, std::span<const Edge> edges) -> std::vector<Cycle>

Typedef documentation

using gtopt::kirchhoff::cycle_basis::Cycle = std::vector<CycleEdge>

A fundamental cycle: an ordered list of edges in traversal order. The first and last edges close the loop; the order matters for sign-consistent KVL row assembly.

using gtopt::kirchhoff::cycle_basis::SwitchableLineLookup = std::function<std::optional<SwitchableEdge>( std::size_t line_index, BlockUid buid)>

Caller-supplied lookup that maps (line_index, BlockUid) → optional switchable-edge info. Returns std::nullopt when the line is not a switching candidate at this block (the default for every line when no LineCommitment rows exist). Used by add_kvl_rows to decide whether a cycle KVL row needs the big-M disjunctive form (any switchable line in the cycle ⇒ disjunctive) or the standard equality form (no switchable lines in the cycle ⇒ equality, the pre-OTS behaviour).

Function documentation

std::size_t gtopt::kirchhoff::cycle_basis::add_kvl_rows(const SystemContext& sc, const ScenarioLP& scenario, const StageLP& stage, LinearProblem& lp, const Collection<BusLP>& buses, const Collection<LineLP>& lines, const SwitchableLineLookup& switchable_lookup = {})

Returns The number of cycle KVL rows added (counts both halves of a disjunctive pair as 2).

System-level KVL row assembler for the loop-flow formulation.

Called once per (scenario, stage) AFTER every LineLP::add_to_lp has finished creating its flow vars. Builds the cycle basis from the active-line topology, then emits one KVL row per cycle per block in the form

Σ_{l ∈ C} ε_l · x_τ_l · row_scale · (f_p_l − f_n_l) = Σ_{l ∈ C} ε_l · φ_l · row_scale

where row_scale = 1 / scale_theta (adaptive, mirrors PyPSA's hardcoded × 1e5). Using the same scale_theta value as the node_angle strategy gives cross-mode coefficient consistency before LP-layer row-max equilibration.

Skips lines that are inactive at this stage, are self-loops (bus_a == bus_b), or have zero / missing x_τ (DC / HVDC).

When switchable_lookup is provided and at least one line in a cycle is switchable at the current block, the equality row is replaced by the OTS disjunctive form:

−Σ_{l ∈ C∩sw} M_C · (1 − u_l) ≤ LHS − RHS_eq ≤ +Σ_{l ∈ C∩sw} M_C · (1 − u_l)

with M_C = 2·θ_max · |C| · row_scale + Σ_{e ∈ C} |φ_e| · row_scale (Fisher-style loose bound; iterative tightening is a follow-up). At u_l = 1 ∀ l ∈ C∩sw the two inequalities collapse to the original equality; at any u_l = 0 the cycle slacks just enough to let that branch open without distorting the surviving cycle.

std::vector<Cycle> gtopt::kirchhoff::cycle_basis::build_fundamental_cycles(std::size_t num_buses, std::span<const Edge> edges)

Parameters
num_buses number of buses (vertices); bus indices are [0, num_buses).
edges input edges; bus indices must be < num_buses. Self-loops (bus_a == bus_b) are silently skipped — they do not contribute to KVL.
Returns One fundamental cycle per non-tree edge. For a connected graph with |L| edges and |B| buses, |L| − |B| + 1 cycles; with k islands, |L| − |B| + k. Empty when the graph is a forest (no cycles).

Build the fundamental cycle basis for an undirected multigraph.