How powergridmodel/power-grid-model works
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 C API · main_core Main core · _core Python core · math_solver Math solver
Components 20
Native engine
C++ core library and its public native interfaces.
- C API · Stable C-facing entry point for model, dataset, serialization, and options.
power_grid_model_c/power_grid_model_c - C++ wrapper · C++ convenience layer built on top of the C API.
power_grid_model_c/power_grid_model_cpp - Common · Shared utilities such as logging, exceptions, timers, tensor math, and helpers.
power_grid_model_c/power_grid_model/include/power_grid_model/common - Components · Electrical component definitions and typed network element model.
power_grid_model_c/power_grid_model/include/power_grid_model/component - Main core · 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 · 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 · Tap position and voltage regulation optimization layered over solver evaluations.
power_grid_model_c/power_grid_model/include/power_grid_model/optimizer - Data model · Datasets, containers, index mapping, and native structured data exchange.
power_grid_model_c/power_grid_model/include/power_grid_model/auxiliary - Model 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 · 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 · Public Python package entry points and user-facing API surface.
src/power_grid_model - Python core · Library loading, buffer management, dataset conversion, serialization, and error translation.
src/power_grid_model/_core - Validation · Python-side validation rules and assertions before data reaches the native engine.
src/power_grid_model/validation - Code generation · Generates synchronized metadata, dataset maps, and bindings support across languages.
code_generation
Support
Docs, tests, and repository-level build and release assets.
- Docs · Architecture, algorithms, API reference, and usage guides.
docs - C++ tests · Unit and validation coverage for the native core and numerical algorithms.
tests/cpp_unit_tests - API tests · Contract tests for the C API, native data interface, and package boundary.
tests/native_api_tests - Python tests · Python behavior, validation, serialization, and end-to-end wrapper tests.
tests/unit - Scenarios · Scenario fixtures and expected outputs for power flow, state estimation, and short circuit.
tests/data
Other components
- power-grid-model · Multi-language numerical engine for steady-state power system analysis.
Connections 35
- 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.