Skip to main content

A Python package for linear power flow analysis in electrical power systems

Project description

Linear Power Flow

PyPI version Python Version Tests License: BSD 3-Clause Code style: black

A Python package for efficient linear power flow analysis in electrical power systems. This package provides fast, linearized power flow solutions suitable for large-scale power system studies.

The rectangular model used in this tool comes from:

T. Hong, D. Zhao and Y. Zhang, "A Relaxed PV Bus Model in Linear Power Flow," in IEEE Transactions on Power Delivery, vol. 36, no. 2, pp. 1249-1252, April 2021, doi: 10.1109/TPWRD.2020.3031758.

keywords: {Load flow;Numerical models;Computational modeling;Mathematical model;Voltage control;Reactive power;Load modeling;PV bus model;linear power flow;rectangular coordinates;multiphase power system}.

Features

  • Fast Linear Power Flow Computation: Linearized power flow models for rapid analysis
  • Multiple Load Models: Support for constant PQ, constant current, and constant impedance load models
  • Sparse Matrix Support: Efficient handling of large-scale power systems using sparse matrices
  • Flexible Bus Types: Support for slack (reference), PV (generator), and PQ (load) buses
  • Customizable Linearization: Taylor expansion and linear regression-based linearization methods
  • Easy Integration: Compatible with popular power system packages like PYPOWER and pandapower

Installation

From PyPI

pip install linear-power-flow

From Source

git clone https://github.com/th1275/linear_power_flow.git
cd linear_power_flow
pip install -e .

For Development

pip install -e .[dev]

For Running Examples

pip install -e .[examples]

Quick Start

import numpy as np
from linear_power_flow import lpf_run, LPF_OPTION_DEFAULT

# Define your power system
y_bus = np.array([...])  # Bus admittance matrix
bus_power = np.array([...])  # Bus power injections
s_idx = np.array([0])  # Slack bus indices
v_idx = np.array([1, 2])  # PV bus indices
o_idx = np.array([3, 4, 5])  # PQ bus indices
vs = 1.0 + 0j  # Slack bus voltage
u_v = np.array([1.0, 1.0])  # PV bus voltage magnitudes

# Configure options
lpf_option = LPF_OPTION_DEFAULT.copy()
lpf_option["pv_model"] = 0  # Constant current model
lpf_option["new_M"] = True  # Compute linearization factors

# Run linear power flow
v_result = lpf_run(y_bus, bus_power, s_idx, v_idx, o_idx, vs, u_v, lpf_option)

print(f"Voltage magnitudes: {np.abs(v_result)}")

Detailed Usage

Matrix Partitioning

from linear_power_flow import partition_matrix_pv

# Partition admittance matrix for PV buses
Yss, Ysv, Yso, Yvs, Yvv, Yvo, Yos, Yov, Yoo = partition_matrix_pv(
    y_bus, s_idx, v_idx
)

Computing Branch Currents

from linear_power_flow import compute_simple_branch_currents

# Compute branch currents
branches = np.array([[0, 1], [1, 2], [2, 3]])  # From-to bus pairs
currents = compute_simple_branch_currents(y_bus, voltages, branches)

Custom Linearization

from linear_power_flow.rect_model import fit_multi_reference_approximation

# Fit linearization around operating points
v_m_ref = np.array([1.0, 0.95, 1.05])  # Reference voltage magnitudes
v_a_ref = np.array([0.0, -0.1, 0.1])  # Reference voltage angles

M1, M2, M3, metrics = fit_multi_reference_approximation(
    v_m_ref, v_a_ref,
    delta_v_m=0.01,  # Magnitude variation range
    delta_v_a=0.05,  # Angle variation range
    load_model=0  # Constant PQ model
)

Configuration Options

The lpf_option dictionary controls the behavior of the linear power flow:

lpf_option = {
    "M1": complex,      # Linearization coefficient 1
    "M2": complex,      # Linearization coefficient 2  
    "M3": complex,      # Linearization coefficient 3
    "k_pv": float,      # PV bus penalty factor
    "new_M": bool,      # Compute new linearization factors
    "pv_model": int,    # 0: const current, 1: const voltage magnitude
    "l_type": list,     # [PQ_type, PV_type]: 0=Taylor, 1=regression
    "du_range": float,  # Voltage magnitude range for linearization
    "da_range": float,  # Voltage angle range for linearization
    "u_0": array,       # Initial voltage magnitudes
    "a_0": array,       # Initial voltage angles
}

Examples

See the linear_power_flow/examples/ directory for complete examples:

  • pypwr_examples.py: Integration with PYPOWER test cases
  • Comparison with nonlinear power flow solutions
  • Visualization of results

Development

Running Tests

pytest tests/

Code Formatting

black linear_power_flow/
flake8 linear_power_flow/

Type Checking

mypy linear_power_flow/

Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

Citation

If you use this package in your research, please cite:

@software{linear_power_flow,
  title = {Linear Power Flow: A Python Package for Linear Power Flow Analysis},
  author = {Tianqi Hong},
  year = {2025},
  url = {https://github.com/th1275/linear_power_flow}
}

@article{hong2021relaxed,
  author = {Hong, T. and Zhao, D. and Zhang, Y.},
  title = {A Relaxed PV Bus Model in Linear Power Flow},
  journal = {IEEE Transactions on Power Delivery},
  volume = {36},
  number = {2},
  pages = {1249--1252},
  year = {2021},
  month = {April},
  doi = {10.1109/TPWRD.2020.3031758},
  keywords = {Load flow; Numerical models; Computational modeling; Mathematical model; Voltage control; Reactive power; Load modeling; PV bus model; linear power flow; rectangular coordinates; multiphase power system}
}

License

This project is licensed under the BSD 3-Clause License - see the LICENSE file for details.

Acknowledgments

  • This package builds upon established power flow linearization techniques
  • Compatible with PYPOWER and pandapower for easy integration
  • Inspired by the need for fast power flow solutions in real-time applications

Contact

Tianqi Hong - tianqi.hong@uga.edu

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

linear_power_flow-0.0.2.tar.gz (19.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

linear_power_flow-0.0.2-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

File details

Details for the file linear_power_flow-0.0.2.tar.gz.

File metadata

  • Download URL: linear_power_flow-0.0.2.tar.gz
  • Upload date:
  • Size: 19.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for linear_power_flow-0.0.2.tar.gz
Algorithm Hash digest
SHA256 cad74d9bd31d222ab0680b33da1ee6dad4e4ffc3597c5f263aa9bcec8542495b
MD5 7608e81d0172dbb171a052f211f54575
BLAKE2b-256 5ae0271954b259bb0cad8e31163ab791988bd22f6a00d5ebd0e7ac9d96e9c916

See more details on using hashes here.

File details

Details for the file linear_power_flow-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for linear_power_flow-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 30fba2f203dbf77b9aecdd5c152403ad5671ef528ed1673150cd08fbec4fe118
MD5 b8301c67d6ddbf282a3cc4e7549c80bd
BLAKE2b-256 899d7dd72e5abcf2cb84f350ed69e9ef67c5595741444aca69cbc521f7364b40

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page