# rwl/pypower architecture

> Architecture diagram of the GitHub repository rwl/pypower, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/rwl/pypower
- Repository: https://github.com/rwl/pypower

## Overview

PYPOWER is a Python power-system analysis library. Its principal workflow loads a case, configures a power-flow or optimal-power-flow run, solves it, and returns or reports the resulting operating state. The graph separates case data and indexing, power-flow calculation, OPF formulation and solver execution, and result handling. README documents DC/AC power flow and OPF; source-index references support the main runtime connections shown. Unsampled modules are represented only where the tree or index establishes a subsystem, so some internal solver details are not expanded.

Main entry points: [`pypower/runpf.py`](https://github.com/rwl/pypower/blob/master/pypower/runpf.py) (Power flow entry), [`pypower/runopf.py`](https://github.com/rwl/pypower/blob/master/pypower/runopf.py) (OPF entry), [`pypower/opf.py`](https://github.com/rwl/pypower/blob/master/pypower/opf.py) (OPF orchestration), [`pypower/loadcase.py`](https://github.com/rwl/pypower/blob/master/pypower/loadcase.py) (Case loader)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_cases["Case data"]
  node_case_data["Case loader<br/>[loadcase.py]"]
  node_case_library["Example cases<br/>[case14.py]"]
  node_bus_schema["Bus fields<br/>[idx_bus.py]"]
  node_gen_schema["Generator fields<br/>[idx_gen.py]"]
  node_branch_schema["Branch fields<br/>[idx_brch.py]"]
  node_cost_schema["Cost fields<br/>[idx_cost.py]"]
  node_options["Run options<br/>[ppoption.py]"]
end

subgraph group_powerflow["Power flow"]
  node_pf_entry["Power flow entry<br/>[runpf.py]"]
  node_index_internal["Internal indexing<br/>[ext2int.py]"]
  node_pf_methods["PF methods<br/>[newtonpf.py]"]
end

subgraph group_opf["Optimal power flow"]
  node_opf_entry["OPF entry<br/>[runopf.py]"]
  node_opf_core["OPF orchestration<br/>[opf.py]"]
  node_opf_form["OPF formulation<br/>[opf_setup.py]"]
  node_opf_dispatch["Solver dispatch<br/>[opf_execute.py]"]
end

subgraph group_results["Results"]
  node_pf_result["Power flow report<br/>[printpf.py]"]
  node_case_save["Case export<br/>[savecase.py]"]
end

node_user(("Python user"))

node_user -->|"supplies case"| node_case_data
node_case_library -.->|"loads case data"| node_case_data
node_user -->|"requests power flow"| node_pf_entry
node_user -->|"requests OPF"| node_opf_entry
node_pf_entry -->|"loads case"| node_case_data
node_pf_entry -->|"uses options"| node_options
node_pf_entry -->|"converts indexing"| node_index_internal
node_pf_entry -->|"dispatches methods"| node_pf_methods
node_pf_entry -->|"prints results"| node_pf_result
node_pf_entry -->|"saves case"| node_case_save
node_opf_entry -->|"uses options"| node_options
node_opf_entry -->|"runs OPF"| node_opf_core
node_opf_entry -->|"prints results"| node_pf_result
node_opf_entry -->|"saves case"| node_case_save
node_opf_core -->|"converts indexing"| node_index_internal
node_opf_core -->|"sets up model"| node_opf_form
node_opf_core -->|"executes model"| node_opf_dispatch

click node_case_data "https://github.com/rwl/pypower/blob/master/pypower/loadcase.py"
click node_case_library "https://github.com/rwl/pypower/blob/master/pypower/case14.py"
click node_bus_schema "https://github.com/rwl/pypower/blob/master/pypower/idx_bus.py"
click node_gen_schema "https://github.com/rwl/pypower/blob/master/pypower/idx_gen.py"
click node_branch_schema "https://github.com/rwl/pypower/blob/master/pypower/idx_brch.py"
click node_cost_schema "https://github.com/rwl/pypower/blob/master/pypower/idx_cost.py"
click node_options "https://github.com/rwl/pypower/blob/master/pypower/ppoption.py"
click node_pf_entry "https://github.com/rwl/pypower/blob/master/pypower/runpf.py"
click node_index_internal "https://github.com/rwl/pypower/blob/master/pypower/ext2int.py"
click node_pf_methods "https://github.com/rwl/pypower/blob/master/pypower/newtonpf.py"
click node_opf_entry "https://github.com/rwl/pypower/blob/master/pypower/runopf.py"
click node_opf_core "https://github.com/rwl/pypower/blob/master/pypower/opf.py"
click node_opf_form "https://github.com/rwl/pypower/blob/master/pypower/opf_setup.py"
click node_opf_dispatch "https://github.com/rwl/pypower/blob/master/pypower/opf_execute.py"
click node_pf_result "https://github.com/rwl/pypower/blob/master/pypower/printpf.py"
click node_case_save "https://github.com/rwl/pypower/blob/master/pypower/savecase.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_case_data,node_case_library,node_bus_schema,node_gen_schema,node_branch_schema,node_cost_schema,node_options,node_user toneBlue
class node_pf_entry,node_index_internal,node_pf_methods toneAmber
class node_opf_entry,node_opf_core,node_opf_form,node_opf_dispatch toneMint
class node_pf_result,node_case_save toneRose
```

## Components

### Case data

- [Case loader](https://github.com/rwl/pypower/blob/master/pypower/loadcase.py): `pypower/loadcase.py`
- [Example cases](https://github.com/rwl/pypower/blob/master/pypower/case14.py): `pypower/case14.py`
- [Bus fields](https://github.com/rwl/pypower/blob/master/pypower/idx_bus.py): `pypower/idx_bus.py`
- [Generator fields](https://github.com/rwl/pypower/blob/master/pypower/idx_gen.py): `pypower/idx_gen.py`
- [Branch fields](https://github.com/rwl/pypower/blob/master/pypower/idx_brch.py): `pypower/idx_brch.py`
- [Cost fields](https://github.com/rwl/pypower/blob/master/pypower/idx_cost.py): `pypower/idx_cost.py`
- [Run options](https://github.com/rwl/pypower/blob/master/pypower/ppoption.py): `pypower/ppoption.py`

### Power flow

- [Power flow entry](https://github.com/rwl/pypower/blob/master/pypower/runpf.py): `pypower/runpf.py`
- [Internal indexing](https://github.com/rwl/pypower/blob/master/pypower/ext2int.py): `pypower/ext2int.py`
- [PF methods](https://github.com/rwl/pypower/blob/master/pypower/newtonpf.py): `pypower/newtonpf.py`

### Optimal power flow

- [OPF entry](https://github.com/rwl/pypower/blob/master/pypower/runopf.py): `pypower/runopf.py`
- [OPF orchestration](https://github.com/rwl/pypower/blob/master/pypower/opf.py): `pypower/opf.py`
- [OPF formulation](https://github.com/rwl/pypower/blob/master/pypower/opf_setup.py): `pypower/opf_setup.py`
- [Solver dispatch](https://github.com/rwl/pypower/blob/master/pypower/opf_execute.py): `pypower/opf_execute.py`

### Results

- [Power flow report](https://github.com/rwl/pypower/blob/master/pypower/printpf.py): `pypower/printpf.py`
- [Case export](https://github.com/rwl/pypower/blob/master/pypower/savecase.py): `pypower/savecase.py`

### Other components

- **Python user**

## Connections

- Python user → Case loader: supplies case (evidence: `pypower/loadcase.py`)
- Example cases → Case loader: loads case data
- Python user → Power flow entry: requests power flow
- Python user → OPF entry: requests OPF
- Power flow entry → Case loader: loads case (evidence: `pypower/runpf.py`)
- Power flow entry → Run options: uses options (evidence: `pypower/runpf.py`)
- Power flow entry → Internal indexing: converts indexing (evidence: `pypower/runpf.py`)
- Power flow entry → PF methods: dispatches methods (evidence: `pypower/runpf.py`)
- Power flow entry → Power flow report: prints results (evidence: `pypower/runpf.py`)
- Power flow entry → Case export: saves case (evidence: `pypower/runpf.py`)
- OPF entry → Run options: uses options (evidence: `pypower/runopf.py`)
- OPF entry → OPF orchestration: runs OPF (evidence: `pypower/runopf.py`)
- OPF entry → Power flow report: prints results (evidence: `pypower/runopf.py`)
- OPF entry → Case export: saves case (evidence: `pypower/runopf.py`)
- OPF orchestration → Internal indexing: converts indexing (evidence: `pypower/opf.py`)
- OPF orchestration → OPF formulation: sets up model (evidence: `pypower/opf.py`)
- OPF orchestration → Solver dispatch: executes model (evidence: `pypower/opf.py`)
