# Linear Power Flow
[](https://badge.fury.io/py/linear-power-flow)
[](https://pypi.org/project/linear-power-flow/)
[](https://github.com/yourusername/linear_power_flow/actions/workflows/test.yml)
[](https://opensource.org/licenses/BSD-3-Clause)
[](https://github.com/psf/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
```bash
pip install linear-power-flow
```
### From Source
```bash
git clone https://github.com/th1275/linear_power_flow.git
cd linear_power_flow
pip install -e .
```
### For Development
```bash
pip install -e .[dev]
```
### For Running Examples
```bash
pip install -e .[examples]
```
## Quick Start
```python
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
```python
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
```python
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
```python
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:
```python
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
```bash
pytest tests/
```
### Code Formatting
```bash
black linear_power_flow/
flake8 linear_power_flow/
```
### Type Checking
```bash
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:
```bibtex
@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](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
Raw data
{
"_id": null,
"home_page": "https://github.com/th1275/linear_power_flow",
"name": "linear-power-flow",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": "power flow, linear power flow, power systems, electrical engineering, grid analysis",
"author": "Tianqi Hong",
"author_email": "Tianqi Hong <tianqi.hong@uga.edu>",
"download_url": "https://files.pythonhosted.org/packages/5a/e0/271954b259bb0cad8e31163ab791988bd22f6a00d5ebd0e7ac9d96e9c916/linear_power_flow-0.0.2.tar.gz",
"platform": null,
"description": "# Linear Power Flow\n\n[](https://badge.fury.io/py/linear-power-flow)\n[](https://pypi.org/project/linear-power-flow/)\n[](https://github.com/yourusername/linear_power_flow/actions/workflows/test.yml)\n[](https://opensource.org/licenses/BSD-3-Clause)\n[](https://github.com/psf/black)\n\nA 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.\n\nThe rectangular model used in this tool comes from:\n\nT. 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.\n\nkeywords: {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}.\n\n\n## Features\n\n- **Fast Linear Power Flow Computation**: Linearized power flow models for rapid analysis\n- **Multiple Load Models**: Support for constant PQ, constant current, and constant impedance load models\n- **Sparse Matrix Support**: Efficient handling of large-scale power systems using sparse matrices\n- **Flexible Bus Types**: Support for slack (reference), PV (generator), and PQ (load) buses\n- **Customizable Linearization**: Taylor expansion and linear regression-based linearization methods\n- **Easy Integration**: Compatible with popular power system packages like PYPOWER and pandapower\n\n## Installation\n\n### From PyPI\n\n```bash\npip install linear-power-flow\n```\n\n### From Source\n\n```bash\ngit clone https://github.com/th1275/linear_power_flow.git\ncd linear_power_flow\npip install -e .\n```\n\n### For Development\n\n```bash\npip install -e .[dev]\n```\n\n### For Running Examples\n\n```bash\npip install -e .[examples]\n```\n\n## Quick Start\n\n```python\nimport numpy as np\nfrom linear_power_flow import lpf_run, LPF_OPTION_DEFAULT\n\n# Define your power system\ny_bus = np.array([...]) # Bus admittance matrix\nbus_power = np.array([...]) # Bus power injections\ns_idx = np.array([0]) # Slack bus indices\nv_idx = np.array([1, 2]) # PV bus indices\no_idx = np.array([3, 4, 5]) # PQ bus indices\nvs = 1.0 + 0j # Slack bus voltage\nu_v = np.array([1.0, 1.0]) # PV bus voltage magnitudes\n\n# Configure options\nlpf_option = LPF_OPTION_DEFAULT.copy()\nlpf_option[\"pv_model\"] = 0 # Constant current model\nlpf_option[\"new_M\"] = True # Compute linearization factors\n\n# Run linear power flow\nv_result = lpf_run(y_bus, bus_power, s_idx, v_idx, o_idx, vs, u_v, lpf_option)\n\nprint(f\"Voltage magnitudes: {np.abs(v_result)}\")\n```\n\n## Detailed Usage\n\n### Matrix Partitioning\n\n```python\nfrom linear_power_flow import partition_matrix_pv\n\n# Partition admittance matrix for PV buses\nYss, Ysv, Yso, Yvs, Yvv, Yvo, Yos, Yov, Yoo = partition_matrix_pv(\n y_bus, s_idx, v_idx\n)\n```\n\n### Computing Branch Currents\n\n```python\nfrom linear_power_flow import compute_simple_branch_currents\n\n# Compute branch currents\nbranches = np.array([[0, 1], [1, 2], [2, 3]]) # From-to bus pairs\ncurrents = compute_simple_branch_currents(y_bus, voltages, branches)\n```\n\n### Custom Linearization\n\n```python\nfrom linear_power_flow.rect_model import fit_multi_reference_approximation\n\n# Fit linearization around operating points\nv_m_ref = np.array([1.0, 0.95, 1.05]) # Reference voltage magnitudes\nv_a_ref = np.array([0.0, -0.1, 0.1]) # Reference voltage angles\n\nM1, M2, M3, metrics = fit_multi_reference_approximation(\n v_m_ref, v_a_ref,\n delta_v_m=0.01, # Magnitude variation range\n delta_v_a=0.05, # Angle variation range\n load_model=0 # Constant PQ model\n)\n```\n\n## Configuration Options\n\nThe `lpf_option` dictionary controls the behavior of the linear power flow:\n\n```python\nlpf_option = {\n \"M1\": complex, # Linearization coefficient 1\n \"M2\": complex, # Linearization coefficient 2 \n \"M3\": complex, # Linearization coefficient 3\n \"k_pv\": float, # PV bus penalty factor\n \"new_M\": bool, # Compute new linearization factors\n \"pv_model\": int, # 0: const current, 1: const voltage magnitude\n \"l_type\": list, # [PQ_type, PV_type]: 0=Taylor, 1=regression\n \"du_range\": float, # Voltage magnitude range for linearization\n \"da_range\": float, # Voltage angle range for linearization\n \"u_0\": array, # Initial voltage magnitudes\n \"a_0\": array, # Initial voltage angles\n}\n```\n\n## Examples\n\nSee the `linear_power_flow/examples/` directory for complete examples:\n\n- `pypwr_examples.py`: Integration with PYPOWER test cases\n- Comparison with nonlinear power flow solutions\n- Visualization of results\n\n## Development\n\n### Running Tests\n\n```bash\npytest tests/\n```\n\n### Code Formatting\n\n```bash\nblack linear_power_flow/\nflake8 linear_power_flow/\n```\n\n### Type Checking\n\n```bash\nmypy linear_power_flow/\n```\n\n## Contributing\n\nContributions 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.\n\n1. Fork the repository\n2. Create your feature branch (`git checkout -b feature/AmazingFeature`)\n3. Commit your changes (`git commit -m 'Add some AmazingFeature'`)\n4. Push to the branch (`git push origin feature/AmazingFeature`)\n5. Open a Pull Request\n\n## Citation\n\nIf you use this package in your research, please cite:\n\n```bibtex\n@software{linear_power_flow,\n title = {Linear Power Flow: A Python Package for Linear Power Flow Analysis},\n author = {Tianqi Hong},\n year = {2025},\n url = {https://github.com/th1275/linear_power_flow}\n}\n\n@article{hong2021relaxed,\n author = {Hong, T. and Zhao, D. and Zhang, Y.},\n title = {A Relaxed PV Bus Model in Linear Power Flow},\n journal = {IEEE Transactions on Power Delivery},\n volume = {36},\n number = {2},\n pages = {1249--1252},\n year = {2021},\n month = {April},\n doi = {10.1109/TPWRD.2020.3031758},\n 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}\n}\n```\n\n## License\n\nThis project is licensed under the BSD 3-Clause License - see the [LICENSE](LICENSE) file for details.\n\n## Acknowledgments\n\n- This package builds upon established power flow linearization techniques\n- Compatible with PYPOWER and pandapower for easy integration\n- Inspired by the need for fast power flow solutions in real-time applications\n\n## Contact\n\nTianqi Hong - tianqi.hong@uga.edu\n",
"bugtrack_url": null,
"license": "BSD 3-Clause License",
"summary": "A Python package for linear power flow analysis in electrical power systems",
"version": "0.0.2",
"project_urls": {
"Bug Tracker": "https://github.com/th1275/linear_power_flow/issues",
"Documentation": "https://github.com/th1275/linear_power_flow#readme",
"Homepage": "https://github.com/th1275/linear_power_flow",
"Source Code": "https://github.com/th1275/linear_power_flow"
},
"split_keywords": [
"power flow",
" linear power flow",
" power systems",
" electrical engineering",
" grid analysis"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "899d7dd72e5abcf2cb84f350ed69e9ef67c5595741444aca69cbc521f7364b40",
"md5": "b8301c67d6ddbf282a3cc4e7549c80bd",
"sha256": "30fba2f203dbf77b9aecdd5c152403ad5671ef528ed1673150cd08fbec4fe118"
},
"downloads": -1,
"filename": "linear_power_flow-0.0.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "b8301c67d6ddbf282a3cc4e7549c80bd",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 18637,
"upload_time": "2025-08-14T00:10:36",
"upload_time_iso_8601": "2025-08-14T00:10:36.030466Z",
"url": "https://files.pythonhosted.org/packages/89/9d/7dd72e5abcf2cb84f350ed69e9ef67c5595741444aca69cbc521f7364b40/linear_power_flow-0.0.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "5ae0271954b259bb0cad8e31163ab791988bd22f6a00d5ebd0e7ac9d96e9c916",
"md5": "7608e81d0172dbb171a052f211f54575",
"sha256": "cad74d9bd31d222ab0680b33da1ee6dad4e4ffc3597c5f263aa9bcec8542495b"
},
"downloads": -1,
"filename": "linear_power_flow-0.0.2.tar.gz",
"has_sig": false,
"md5_digest": "7608e81d0172dbb171a052f211f54575",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 19834,
"upload_time": "2025-08-14T00:10:37",
"upload_time_iso_8601": "2025-08-14T00:10:37.531678Z",
"url": "https://files.pythonhosted.org/packages/5a/e0/271954b259bb0cad8e31163ab791988bd22f6a00d5ebd0e7ac9d96e9c916/linear_power_flow-0.0.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-08-14 00:10:37",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "th1275",
"github_project": "linear_power_flow",
"github_not_found": true,
"lcname": "linear-power-flow"
}