Skip to content

Repository files navigation

PowerFlow

.NET CI.NET 10License: MIT

A steady-state AC/DC power flow solver in C#. Includes Newton-Raphson with sparse LU factorisation, Q-limit enforcement, distributed slack, DC warm-start, and MATPOWER .m case-file parsing. Validated against MATPOWER on five IEEE benchmark cases.

Quick start — library

usingPowerFlow.Core.Models;usingPowerFlow.Core.Parsing;usingPowerFlow.Core.Solver;// Parse a MATPOWER case filevarnetwork=MatpowerParser.Parse("case14.m");// Basic AC Newton-Raphsonvarresult=newNewtonRaphsonSolver().Solve(network);Console.WriteLine($"Converged: {result.Converged}");Console.WriteLine($"Losses: {result.Balance?.TotalLossesMw:F2} MW");// DC warm-start (better initial guess for hard cases)varresult2=newNewtonRaphsonSolver{WarmStartFromDc=true}.Solve(network);// Distributed slack (share imbalance across all generators by Pmax)varresult3=newNewtonRaphsonSolver{DistributedSlack=true}.Solve(network);Console.WriteLine($"λ = {result3.Lambda:F6} pu");// Linearised DC power flowvardcResult=newDcPowerFlowSolver().Solve(network);

AC vs DC

AC Newton-RaphsonDC (linearised)
VariablesVm, VaVa only (Vm ≈ 1 pu)
Solves forP, Q balanceP balance
Losses✅ computed❌ lossless
Reactive power✅ full Q model❌ ignored
Speediterative (≈ 3–6 iters)one sparse LU

Validated test cases

CaseBusesBranchesGeneratorsAC iters
IEEE 14-bus142053
IEEE 30-bus304163
IEEE 57-bus578073
IEEE 118-bus118186544
IEEE 300-bus300411696

All five cases match MATPOWER runpf results to |ΔVm| < 1 × 10⁻⁶ pu and |ΔVa| < 1 × 10⁻⁵°.

Validation workflow

usingPowerFlow.Core.Validation;varresult=NetworkValidator.Validate(network);if(!result.IsValid){foreach(varerrinresult.Errors)Console.WriteLine($"[{err.Severity}] {err.Code}: {err.Message}");}// Or throw immediately:NetworkValidator.Validate(network).ThrowIfInvalid();

Checks include: missing slack bus, broken bus references, network islands, invalid tap ratios, phase-shift range, P-limit violations, conflicting Vg setpoints, and duplicate bus IDs.

Project structure

PowerFlow/
├── PowerFlow.Core/ # Models, parser, solver, validator
├── PowerFlow.Runner/ # Console entry point
└── PowerFlow.Tests/ # xUnit tests (193 tests)

Getting started — CLI

git clone https://github.com/aartinian/powerflow.git
cd powerflow
dotnet restore && dotnet build && dotnet test
dotnet run --project PowerFlow.Runner

Runner

Web UI

dotnet run --project PowerFlow.Web

Open http://localhost:5032, upload any MATPOWER .m file, and click Solve.

Console

# Bundled IEEE 14-bus demo
dotnet run --project PowerFlow.Runner
# Any MATPOWER case file
dotnet run --project PowerFlow.Runner -- case118.m
# DC power flow
dotnet run --project PowerFlow.Runner -- --dc case118.m
# Distributed slack, custom tolerance
dotnet run --project PowerFlow.Runner -- --distributed-slack --tol 1e-8 case118.m
# DC warm-start (seed AC initial angles from DC solution)
dotnet run --project PowerFlow.Runner -- --warm-start case300.m

Options

FlagDefaultDescription
--flat-startoffForce Vm = 1 pu, Va = 0° initial guess
--warm-startoffSeed AC initial Va from a DC solve
--distributed-slackoffShare imbalance by Pmax participation
--dcoffLinearised DC power flow
--no-limitsoffDisable Q-limit enforcement
--tol <ε>1e-6Convergence tolerance (pu)
--max-iter <n>50NR iteration cap
--no-coloroffDisable ANSI colour output
--no-busesoffSuppress the bus results table
--no-branchesoffSuppress the branch results table
--summary-onlyoffSuppress both tables

Exit code 0 = converged, 2 = did not converge, 1 = input error.

Known limitations

  • Single-phase positive-sequence model only (no three-phase, no unbalanced)
  • No optimal power flow (OPF) — fixed dispatch, solve for voltages/flows
  • No load-tap-changer (LTC) automatic tap control
  • No switched shunts (discrete shunt control)
  • No multi-area interchange constraints
  • Generator P-limit violations are flagged by the validator but not redispatched
  • When multiple generators share a PV bus with different Vg setpoints, last-generator-wins
  • PSS/E .raw and CIM formats not supported; MATPOWER .m only

Reference

Zimmerman et al., MATPOWER: Steady-State Operations, Planning and Analysis Tools for Power Systems Research and Education, IEEE Transactions on Power Systems, 2011.

About

A steady-state AC/DC power flow solver in C#. Includes Newton-Raphson with sparse LU factorisation, Q-limit enforcement, distributed slack, DC warm-start, and MATPOWER case-file parsing.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Used by

Contributors

Languages