Power Grid Model

repository·main·Indexed 42 days ago

https://github.com/powergridmodel/power-grid-model

A high-performance Python/C++ library for steady-state distribution power system analysis. It supports Power Flow, State Estimation, and Short Circuit calculations via C++ cores with Python and C interfaces.

Tokens
71K
Snippets
131
Records
281
Agent score
54%

What's inside Power Grid Model

  1. Overview of Power Grid Model

    main

    power-grid-model is a high-performance library designed for steady-state distribution power system analysis. It provides interfaces for both Python and C, with its core implementation written in C++.

    Supported calculations include:

    • Power Flow
    • State Estimation
    • Short Circuit

    For data conversion utilities, see the power-grid-model-io repository. For an extended Python interface, see the power-grid-model-ds repository.

  2. Overview of power-grid-model capabilities

    main

    The power-grid-model is a library for steady-state distribution power system analysis, hosted by the Linux Foundation Energy. It is written in C++ and provides interfaces for both Python and C.

    Supported calculation types (both symmetric and asymmetric):

    • Power Flow
    • State Estimation
    • Short Circuit
    • Data Conversion: Use power-grid-model-io to convert various data formats to the power-grid-model format.
    • Extended Python Interface: Use power-grid-model-ds for a more extensive Python interface.
  3. Understand the Power Grid Model repository structure

    main

    The repository is organized into several distinct components based on the language and interface used. Use this guide to locate specific parts of the codebase:

    Core Logic and APIs

    • C++ Calculation Core: Located in power_grid_model_c/power_grid_model/include/power_grid_model.
    • C API: Located in power_grid_model_c/power_grid_model_c.
    • Python Interface: Located in src/power_grid_model.
    • Input Data Validation: Located in src/power_grid_model/validation.

    Examples and Testing

    • C API Example: A C program demonstrating how to use the C API is in power_grid_model_c_example.
    • Tests: The tests folder contains several sub-directories:
      • cpp_unit_tests: Unit tests for the C++ core.
      • cpp_integration_tests: Integration tests for the C++ core.
      • cpp_validation_tests: Validation test cases for the C++ core.
      • c_api_tests: Tests for the C API communication layer.
      • benchmark_cpp: C++ benchmark test case generator.
      • package_tests: A CMake project to verify the C API package installation.
      • unit: Python code tests.
    • Test Data: The tests/data folder contains validation test cases for components and algorithms, as well as sample network types. These are validated against hand calculations or popular power system analysis software.
  4. Understand the Power Grid Model build targets

    main

    The repository provides three distinct ways to build and use the library:

    1. Python Package: A power-grid-model package available on PyPI that uses a C++ extension as its calculation core.
    2. CMake Project: A C++ project consisting of a header-only calculation core. It produces:
      • A dynamic library (.dll, .so, or .dylib) with a stable pure C API/ABI.
      • An install target for the dynamic library.
      • Native C++ unit tests and C API tests.
      • A performance benchmark program.
      • An example C program for calling the shared library.
    3. Example CMake Project: A separate project containing a small C++ program demonstrating how to find and use the installed package.

    If you only intend to develop the Python side, use the provided development container to set up the environment automatically.

  5. Understand the LU Solver implementation

    main

    The LU solver in the power grid model is a hybrid system designed to handle large-scale power system equations efficiently. It consists of two primary components:

    1. Block-sparse LU solver: This component manages the overall factorization by leveraging the topological structure of the power grid. It operates down to the block level to maintain sparsity.
    2. Dense LU factor: This component handles the individual dense blocks within the matrix equation using a modified version of Eigen's LUFullPiv algorithm.

    The solver uses an in-place decomposition approach and supports pivot perturbation to improve numerical stability.

  6. Tuning the backward error calculation for the LU Solver

    main

    The power grid model uses a modified backward error calculation to prevent rounding errors in single rows from causing iterative refinement to fail. The calculation uses a denominator cap determined by a parameter $\epsilon$.

    To ensure numerical stability, $\epsilon$ should be chosen to be small, but large enough to suppress numerical instabilities.

    • $\epsilon = 0$: No cut-off is applied. This is prone to rounding errors.
    • $\epsilon = 1$: Only the absolute values of the residuals are relevant (relative values are ignored). This may hide issues in rows with small coefficients by suppressing them in the backward error, potentially favoring rows with larger absolute but smaller relative residuals.

    Mathematical Formula:

    $$ \begin{aligned} D_{\text{max}} &= \max_i\left{ \left(\left|\mathbf{M}\right|\cdot\left|\boldsymbol{x}\right| + \left|\boldsymbol{b}\right|\right)_i \right} \ \text{backward_error} &= \max_i \left{ \frac{|\boldsymbol{r}|i}{ \max\left{ \left(\left|\mathbf{M}\right|\cdot\left|\boldsymbol{x}\right| + \left|\boldsymbol{b}\right|\right)i, \epsilon{\text{backward_error}} D{\text{max}} \right} \right} \end{aligned} $$

  7. Aggregate multiple sensors using Kalman filtering

    main

    When multiple sensors measure the same physical quantity (e.g., multiple voltage sensors on a single bus), power-grid-model can merge these into a single virtual measurement using a Kalman filter approach.

    The aggregated measurement $z$ is calculated as:

    $$z = \dfrac{\sum_{k=1}^{N_{sensor}} z_k \sigma_k^{-2}}{\sum_{k=1}^{N_{sensor}} \sigma_k^{-2}}$$

    Where $z_k$ and $\sigma_k$ are the measured value and standard deviation of the $k$-th individual sensor.

  8. Configure Calculation Options with `PGM_Options`

    main

    To avoid breaking changes when new calculation parameters are added, the C API uses an opaque PGM_Options object instead of passing individual arguments to PGM_calculate.

    Workflow:

    1. Create an object with default options using PGM_create_options.
    2. Customize specific options using PGM_set_* functions.
    3. Pass the PGM_Options pointer to PGM_calculate.
  9. Understand RealValueInput and RealValueOutput data types

    main

    The model uses two special data types for handling real-valued numerical data, which vary depending on whether the calculation is symmetric or asymmetric:

    • RealValueInput: Used for input attributes.
      • For symmetric classes (e.g., sym_load): A double.
      • For asymmetric classes (e.g., asym_load): A double[3] (array of 3 doubles).
    • RealValueOutput: Used for output attributes.
      • For symmetric calculations: A double.
      • For asymmetric and short circuit calculations: A double[3].
  10. Understand the Native Data Interface

    main

    The power-grid-model calculation core is written in C++. To exchange data between Python and the C++ core, the library uses a dictionary of NumPy structured arrays.

    In this format:

    • Each key in the dictionary represents a ComponentType (e.g., node, transformer).
    • Each value is a NumPy structured array where each element represents a single physical component.
    • The memory layout of these NumPy arrays must exactly match the C++ struct definitions used by the core.
  11. Perform a Developer Build with CMake Presets

    main

    For development, use CMake presets to enable all tests, warnings, examples, and benchmarks. These presets use the Ninja generator.

    Available targets in a developer build include:

    • power_grid_model_c: The stable pure C API/ABI dynamic library.
    • tests/cpp_unit_tests_*: C++ core unit tests using doctest.
    • tests/cpp_validation_tests: Validation tests using doctest.
    • tests/native_api_tests: C API tests using doctest.
    • tests/benchmark_cpp: C++ performance benchmarks.
    • power_grid_model_c_example: An example C program.

    To see available presets, use cmake --list-presets.