# powergridmodel/power-grid-model architecture

> Architecture diagram of the GitHub repository powergridmodel/power-grid-model, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/powergridmodel/power-grid-model
- Repository: https://github.com/powergridmodel/power-grid-model

## Overview

1. **Repository purpose**
   - This is a **multi-language numerical engine** for steady-state power system analysis.
   - The central product is a **C++ core library** that is exposed through both a **C API** and a **Python package**.
   - The main calculation domains are **Power Flow**, **State Estimation**, and **Short Circuit**.

2. **Top-level architecture**
   - The repo is organized around one native engine and two public wrappers:
     - `power_grid_model_c` / `power_grid_model_cpp`: native C and C++ interfaces
     - `src/power_grid_model`: Python wrapper and validation layer
     - `code_generation`: generated metadata and bindings support
   - The docs and tests are first-class parts of the repo and mirror the same architecture boundaries.

3. **Core C++ engine**
   - The heart of the system lives under `power_grid_model_c/power_grid_model/include/power_grid_model/...`.
   - It is split into stable subsystems:
     - **component**: the domain model for network elements
     - **main_core**: model state, topology, input preparation, output assembly
     - **math_solver**: numerical algorithms
     - **optimizer**: tap/regulator optimization
     - **common**: shared utilities, logging, exceptions, timers, tensor math
   - This is the layer where all calculation logic is implemented.

4. **Domain model and data containers**
   - The `component` headers define the electrical objects: nodes, lines, transformers, sources, loads, shunts, sensors, faults, regulators, etc.
   - `dataset`, `container`, `index_mapping`, and related auxiliary headers provide the typed storage model used by the engine.
   - The engine appears designed around **columnar/record-style datasets** rather than ad hoc object graphs, which is important for diagramming the data flow.

5. **Model lifecycle inside the native core**
   - The `main_core` layer is the orchestration boundary:
     - input is sanitized and mapped into internal state
     - topology and Y-bus/state structures are built
     - solver-specific state is prepared
     - outputs and calculation info are extracted
   - Files like `calculation_input_preparation`, `state`, `topology`, `y_bus`, and `update` suggest the engine is stateful and supports incremental updates, not just single-shot runs.

6. **Numerical computation layer**
   - `math_solver` contains multiple algorithm families, including:
     - linear and Newton-Raphson power flow
     - iterative variants
     - state estimation solvers
     - short-circuit solver
     - sparse LU and sparse ordering utilities
   - This layer consumes the prepared internal model and returns voltages, currents, residuals, and related calculation results.
   - For architecture diagrams, this is the main compute block between model preparation and output serialization.

7. **Optimization and control**
   - `optimizer` contains tap position optimization logic, centered around transformer regulation.
   - This is a distinct subsystem from the core solver: it likely loops over solver evaluations while adjusting control variables.
   - Tap-changing and voltage regulation are therefore a separate control path layered on top of the numerical engine.

8. **Serialization and native data interface**
   - Under `power_grid_model_c/power_grid_model/include/power_grid_model/auxiliary/serialization` and the C API sources, the project defines explicit serialization/deserialization for inputs, outputs, and updates.
   - The repo includes docs for a **native data interface**, implying the public API is built around structured binary/array data exchange rather than only Python objects or text files.
   - This boundary is important: external callers provide datasets and parameter blocks, the native engine transforms them into internal state, then emits structured result datasets.

9. **C API and C++ wrapper boundary**
   - `power_grid_model_c` is the C-facing API surface: headers like `model.h`, `dataset.h`, `handle.h`, `serialization.h`, and `meta_data.h`.
   - `power_grid_model_cpp` provides a C++ wrapper around the C API, likely to make integration easier for C++ consumers while keeping the stable ABI at the C layer.
   - `power_grid_model_c_example/main.c` is the minimal native integration example.

10. **Python package architecture**
   - `src/power_grid_model/_core` is the Python-side integration layer.
   - It handles:
     - loading the native shared library
     - buffer management
     - dataset conversion and typing
     - error translation
     - serialization helpers
     - options and model entry points
   - `src/power_grid_model/validation` is a separate Python validation subsystem that checks user input before it reaches the native engine.

11. **Generated metadata and schema synchronization**
   - `code_generation` is a major architectural support subsystem.
   - It consumes JSON schema-like definitions under `code_generation/data/...` and emits:
     - C++ headers for attribute classes and static asserts
     - C/C++ dataset class maps
     - Python-side dataset mappings
   - This generation step keeps the native and Python APIs synchronized, which is critical for a repo that exposes the same model in multiple languages.

12. **Validation and correctness boundaries**
   - Validation exists in both the native and Python worlds:
     - `power_grid_model_c/.../validation` tests and related core logic
     - `src/power_grid_model/validation` for Python-facing validation
   - The project emphasizes correctness via scenario-based fixtures under `tests/data`, with expected outputs stored alongside inputs.
   - Validation is clearly a pre-solver boundary: malformed or inconsistent datasets are rejected before they hit the math layer.

13. **Testing strategy**
   - The repo has broad coverage across layers:
     - `tests/cpp_unit_tests` for the C++ core
     - `tests/native_api_tests` for the C API contract
     - `tests/unit` for Python behavior and validation
     - large scenario fixtures for power flow, short circuit, and state estimation
   - This indicates the architecture is meant to be exercised at multiple seams: pure math, API boundary, and end-to-end data flow.

14. **Build, packaging, and delivery**
   - The repo is built with **CMake** on the native side and Python packaging metadata via `pyproject.toml`.
   - GitHub Actions workflows cover build/test/release, code quality, docs, and reproducibility.
   - The artifacts are intended for distribution as:
     - Python packages on PyPI / conda-forge
     - native libraries consumable from C and C++
   - Documentation is generated from the same source tree, so the architecture includes a strong docs-to-code coupling.

15. **How to diagram the main data flow**
   - A useful diagram would show:
     - user data entering through **Python API** or **C API**
     - validation and serialization at the boundary
     - dataset/model preparation in the native core
     - dispatch into one of the solvers or optimizer paths
     - results emitted as structured output datasets
   - The key architectural split is: **API/data-model layer** vs **native numerical engine** vs **multi-language wrappers**.

Main entry points: [`power_grid_model_c/power_grid_model_c`](https://github.com/powergridmodel/power-grid-model/tree/main/power_grid_model_c/power_grid_model_c) (C API), [`power_grid_model_c/power_grid_model/include/power_grid_model/main_core`](https://github.com/powergridmodel/power-grid-model/tree/main/power_grid_model_c/power_grid_model/include/power_grid_model/main_core) (Main core), [`src/power_grid_model/_core`](https://github.com/powergridmodel/power-grid-model/tree/main/src/power_grid_model/_core) (Python core), [`power_grid_model_c/power_grid_model/include/power_grid_model/math_solver`](https://github.com/powergridmodel/power-grid-model/tree/main/power_grid_model_c/power_grid_model/include/power_grid_model/math_solver) (Math solver)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_grp_native["Native engine"]
  node_cpp_api["C API<br/>native interface"]
  node_cpp_wrap["C++ wrapper<br/>native wrapper"]
  node_core_common["Common<br/>core utilities"]
  node_core_component["Components<br/>domain model"]
  node_core_main["Main core<br/>model orchestration"]
  node_core_solver["Math solver<br/>numerical algorithms"]
  node_core_optimizer["Optimizer<br/>control loop"]
  node_core_data["Data model<br/>typed storage"]
  node_core_model["Model engine<br/>native engine<br/>[main_model.hpp]"]
  node_native_impl["C API impl<br/>bridge implementation"]
end

subgraph group_grp_python["Python layer"]
  node_py_pkg["Python package"]
  node_py_core["Python core<br/>wrapper layer"]
  node_py_validation["Validation"]
  node_codegen["Code generation<br/>generation pipeline"]
end

subgraph group_grp_support["Support"]
  node_docs["Docs<br/>documentation"]
  node_tests_cpp["C++ tests<br/>test suite"]
  node_tests_api["API tests<br/>test suite"]
  node_tests_py["Python tests<br/>test suite"]
  node_scenarios["Scenarios<br/>fixtures"]
end

node_repo["power-grid-model"]

node_repo -->|"exposes"| node_cpp_api
node_repo -->|"wraps"| node_cpp_wrap
node_repo -->|"ships"| node_py_pkg
node_repo -->|"generates"| node_codegen
node_repo -->|"documents"| node_docs
node_repo -->|"verifies"| node_tests_cpp
node_repo -->|"verifies"| node_tests_api
node_repo -->|"verifies"| node_tests_py
node_repo -->|"validates"| node_scenarios
node_cpp_api -->|"calls"| node_native_impl
node_cpp_wrap -->|"depends on"| node_cpp_api
node_native_impl -->|"drives"| node_core_model
node_native_impl -->|"serializes"| node_core_data
node_core_model -->|"orchestrates"| node_core_main
node_core_model -->|"dispatches to"| node_core_solver
node_core_main -->|"uses"| node_core_component
node_core_main -->|"maps"| node_core_data
node_core_main -->|"prepares"| node_core_solver
node_core_solver -->|"supports"| node_core_optimizer
node_py_pkg -->|"imports"| node_py_core
node_py_pkg -->|"prechecks"| node_py_validation
node_py_core -->|"binds"| node_cpp_api
node_py_core -->|"converts"| node_core_data
node_py_validation -->|"guards"| node_core_data
node_codegen -->|"generates"| node_cpp_api
node_codegen -->|"generates"| node_py_core
node_docs -->|"describes"| node_cpp_api
node_docs -->|"describes"| node_py_pkg
node_tests_cpp -->|"covers"| node_core_solver
node_tests_cpp -->|"covers"| node_core_main
node_tests_api -->|"covers"| node_cpp_api
node_tests_py -->|"covers"| node_py_core
node_tests_py -->|"covers"| node_py_validation
node_scenarios -->|"feeds"| node_core_solver
node_scenarios -->|"feeds"| node_py_validation

click node_cpp_api "https://github.com/powergridmodel/power-grid-model/tree/main/power_grid_model_c/power_grid_model_c"
click node_cpp_wrap "https://github.com/powergridmodel/power-grid-model/tree/main/power_grid_model_c/power_grid_model_cpp"
click node_core_common "https://github.com/powergridmodel/power-grid-model/tree/main/power_grid_model_c/power_grid_model/include/power_grid_model/common"
click node_core_component "https://github.com/powergridmodel/power-grid-model/tree/main/power_grid_model_c/power_grid_model/include/power_grid_model/component"
click node_core_main "https://github.com/powergridmodel/power-grid-model/tree/main/power_grid_model_c/power_grid_model/include/power_grid_model/main_core"
click node_core_solver "https://github.com/powergridmodel/power-grid-model/tree/main/power_grid_model_c/power_grid_model/include/power_grid_model/math_solver"
click node_core_optimizer "https://github.com/powergridmodel/power-grid-model/tree/main/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer"
click node_core_data "https://github.com/powergridmodel/power-grid-model/tree/main/power_grid_model_c/power_grid_model/include/power_grid_model/auxiliary"
click node_core_model "https://github.com/powergridmodel/power-grid-model/blob/main/power_grid_model_c/power_grid_model/include/power_grid_model/main_model.hpp"
click node_native_impl "https://github.com/powergridmodel/power-grid-model/tree/main/power_grid_model_c/power_grid_model_c/src"
click node_py_pkg "https://github.com/powergridmodel/power-grid-model/tree/main/src/power_grid_model"
click node_py_core "https://github.com/powergridmodel/power-grid-model/tree/main/src/power_grid_model/_core"
click node_py_validation "https://github.com/powergridmodel/power-grid-model/tree/main/src/power_grid_model/validation"
click node_codegen "https://github.com/powergridmodel/power-grid-model/tree/main/code_generation"
click node_docs "https://github.com/powergridmodel/power-grid-model/tree/main/docs"
click node_tests_cpp "https://github.com/powergridmodel/power-grid-model/tree/main/tests/cpp_unit_tests"
click node_tests_api "https://github.com/powergridmodel/power-grid-model/tree/main/tests/native_api_tests"
click node_tests_py "https://github.com/powergridmodel/power-grid-model/tree/main/tests/unit"
click node_scenarios "https://github.com/powergridmodel/power-grid-model/tree/main/tests/data"

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_cpp_api,node_cpp_wrap,node_core_common,node_core_component,node_core_main,node_core_solver,node_core_optimizer,node_core_data,node_core_model,node_native_impl toneBlue
class node_py_pkg,node_py_core,node_py_validation,node_codegen toneAmber
class node_docs,node_tests_cpp,node_tests_api,node_tests_py,node_scenarios toneMint
class node_repo toneNeutral
```

## Components

### Native engine

C++ core library and its public native interfaces.

- [C API](https://github.com/powergridmodel/power-grid-model/tree/main/power_grid_model_c/power_grid_model_c): native interface · Stable C-facing entry point for model, dataset, serialization, and options. · `power_grid_model_c/power_grid_model_c`
- [C++ wrapper](https://github.com/powergridmodel/power-grid-model/tree/main/power_grid_model_c/power_grid_model_cpp): native wrapper · C++ convenience layer built on top of the C API. · `power_grid_model_c/power_grid_model_cpp`
- [Common](https://github.com/powergridmodel/power-grid-model/tree/main/power_grid_model_c/power_grid_model/include/power_grid_model/common): core utilities · Shared utilities such as logging, exceptions, timers, tensor math, and helpers. · `power_grid_model_c/power_grid_model/include/power_grid_model/common`
- [Components](https://github.com/powergridmodel/power-grid-model/tree/main/power_grid_model_c/power_grid_model/include/power_grid_model/component): domain model · Electrical component definitions and typed network element model. · `power_grid_model_c/power_grid_model/include/power_grid_model/component`
- [Main core](https://github.com/powergridmodel/power-grid-model/tree/main/power_grid_model_c/power_grid_model/include/power_grid_model/main_core): model orchestration · Input preparation, topology/state construction, update handling, and output assembly. · `power_grid_model_c/power_grid_model/include/power_grid_model/main_core`
- [Math solver](https://github.com/powergridmodel/power-grid-model/tree/main/power_grid_model_c/power_grid_model/include/power_grid_model/math_solver): numerical algorithms · Power flow, state estimation, short circuit, sparse LU, and solver dispatch. · `power_grid_model_c/power_grid_model/include/power_grid_model/math_solver`
- [Optimizer](https://github.com/powergridmodel/power-grid-model/tree/main/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer): control loop · Tap position and voltage regulation optimization layered over solver evaluations. · `power_grid_model_c/power_grid_model/include/power_grid_model/optimizer`
- [Data model](https://github.com/powergridmodel/power-grid-model/tree/main/power_grid_model_c/power_grid_model/include/power_grid_model/auxiliary): typed storage · Datasets, containers, index mapping, and native structured data exchange. · `power_grid_model_c/power_grid_model/include/power_grid_model/auxiliary`
- [Model engine](https://github.com/powergridmodel/power-grid-model/blob/main/power_grid_model_c/power_grid_model/include/power_grid_model/main_model.hpp): native engine · Top-level native model facade that ties API, core state, and solver execution together. · `power_grid_model_c/power_grid_model/include/power_grid_model/main_model.hpp`
- [C API impl](https://github.com/powergridmodel/power-grid-model/tree/main/power_grid_model_c/power_grid_model_c/src): bridge implementation · Native implementation sources for model control, buffering, serialization, metadata, and sanitization. · `power_grid_model_c/power_grid_model_c/src`

### Python layer

Python wrapper, validation, and generated bindings support.

- [Python package](https://github.com/powergridmodel/power-grid-model/tree/main/src/power_grid_model): package · Public Python package entry points and user-facing API surface. · `src/power_grid_model`
- [Python core](https://github.com/powergridmodel/power-grid-model/tree/main/src/power_grid_model/_core): wrapper layer · Library loading, buffer management, dataset conversion, serialization, and error translation. · `src/power_grid_model/_core`
- [Validation](https://github.com/powergridmodel/power-grid-model/tree/main/src/power_grid_model/validation): input validation · Python-side validation rules and assertions before data reaches the native engine. · `src/power_grid_model/validation`
- [Code generation](https://github.com/powergridmodel/power-grid-model/tree/main/code_generation): generation pipeline · Generates synchronized metadata, dataset maps, and bindings support across languages. · `code_generation`

### Support

Docs, tests, and repository-level build and release assets.

- [Docs](https://github.com/powergridmodel/power-grid-model/tree/main/docs): documentation · Architecture, algorithms, API reference, and usage guides. · `docs`
- [C++ tests](https://github.com/powergridmodel/power-grid-model/tree/main/tests/cpp_unit_tests): test suite · Unit and validation coverage for the native core and numerical algorithms. · `tests/cpp_unit_tests`
- [API tests](https://github.com/powergridmodel/power-grid-model/tree/main/tests/native_api_tests): test suite · Contract tests for the C API, native data interface, and package boundary. · `tests/native_api_tests`
- [Python tests](https://github.com/powergridmodel/power-grid-model/tree/main/tests/unit): test suite · Python behavior, validation, serialization, and end-to-end wrapper tests. · `tests/unit`
- [Scenarios](https://github.com/powergridmodel/power-grid-model/tree/main/tests/data): fixtures · Scenario fixtures and expected outputs for power flow, state estimation, and short circuit. · `tests/data`

### Other components

- **power-grid-model**: repository · Multi-language numerical engine for steady-state power system analysis.

## Connections

- power-grid-model → C API: exposes · Repository publishes the stable C API surface.
- power-grid-model → C++ wrapper: wraps · Repository publishes a C++ wrapper over the C layer.
- power-grid-model → Python package: ships · Repository ships the Python package.
- power-grid-model → Code generation: generates · Generation pipeline keeps schemas and bindings synchronized.
- power-grid-model → Docs: documents · Documentation explains architecture, algorithms, and APIs.
- power-grid-model → C++ tests: verifies · Native core is covered by C++ tests.
- power-grid-model → API tests: verifies · C API and native interface are covered by API tests.
- power-grid-model → Python tests: verifies · Python behavior and validation are covered by unit tests.
- power-grid-model → Scenarios: validates · Scenario fixtures exercise end-to-end calculations.
- C API → C API impl: calls · C API forwards into native implementation sources.
- C++ wrapper → C API: depends on · C++ wrapper builds on the C ABI.
- C API impl → Model engine: drives · Implementation instantiates and runs the model engine.
- C API impl → Data model: serializes · Implementation converts structured datasets and buffers.
- Model engine → Main core: orchestrates · Model engine coordinates lifecycle and execution.
- Model engine → Math solver: dispatches to · Model engine selects the appropriate solver path.
- Main core → Components: uses · Core orchestration consumes the electrical domain model.
- Main core → Data model: maps · Inputs are mapped into typed containers and datasets.
- Main core → Math solver: prepares · Prepared state is handed to numerical solvers.
- Math solver → Optimizer: supports · Optimization loops invoke solver evaluations for tap control.
- Python package → Python core: imports · Public Python API delegates to the internal core integration.
- Python package → Validation: prechecks · User inputs are validated before native execution.
- Python core → C API: binds · Python core loads and calls the native shared library.
- Python core → Data model: converts · Python core translates Python objects to native datasets.
- Validation → Data model: guards · Validation mirrors the native data model boundary.
- Code generation → C API: generates · Generated metadata feeds the C-facing headers and maps.
- Code generation → Python core: generates · Generated dataset mappings feed Python bindings.
- Docs → C API: describes · Documentation covers the native data interface and C API.
- Docs → Python package: describes · Documentation covers Python usage and wrapper design.
- C++ tests → Math solver: covers · Solver algorithms are tested at the C++ level.
- C++ tests → Main core: covers · Input preparation, topology, and state handling are tested.
- API tests → C API: covers · Native API contract and serialization are tested at the boundary.
- Python tests → Python core: covers · Python integration, buffer handling, and serialization are tested.
- Python tests → Validation: covers · Validation rules are exercised from Python.
- Scenarios → Math solver: feeds · Scenario datasets drive calculation result assertions.
- Scenarios → Validation: feeds · Fixtures also exercise validation and wrapper behavior.
