# georgkordowich/diffpssi architecture

> Architecture diagram of the GitHub repository georgkordowich/diffpssi, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/georgkordowich/diffpssi
- Repository: https://github.com/georgkordowich/diffpssi

## Overview

##### Project Type and Purpose
- DiffPSSi is a simulation framework for dynamic power systems that supports both traditional dynamic simulation and optimization using automatic differentiation.
- It is designed for research and educational purposes, enabling users to simulate power system dynamics while leveraging backends like numpy and torch for gradient computation.
- The repository includes example models (IBB, IEEE-9 bus, K2a, etc.) and components to simulate, optimize, and analyze power system behavior.

##### Analyzing the File Structure
- Top-level directories include:
- “examples”: Contains usage examples and example models (e.g., ibb_model, ieee_9bus, k2a) with model definitions, simulation scripts, and associated data.
- “src”: Contains the core library “diffpssi”, which has submodules like:
▪ “optimization_lib”: Includes optimizers (e.g., optimizers.py and ps_optimization.py) for gradient-based parameter optimization.
▪ “power_sim_lib”: Contains core simulation components such as simulation backend (backend.py), load flow analysis (load_flow.py), various power system models (models directory), the main simulation engine (simulator.py), and numerical solvers (solvers.py).
- “pf_util”: A utility module for performing power flow calculations.
- “tests”: Integration tests to validate simulation and optimization functionality.
- The distribution of examples with different power system models highlights that the framework is modular and supports various simulation setups.

##### Main Components
- Simulation Engine: Core element powering the simulation. (Found in src/diffpssi/power_sim_lib/simulator.py)
- Mathematical Solvers: Euler, Runge Kutta, and possibly custom solver implementations (in src/diffpssi/power_sim_lib/solvers.py).
- Power System Models: Abstract and concrete models for components such as exciters, governors, stabilizers, synchronous machines, etc. (located in src/diffpssi/power_sim_lib/models)
- Optimization Library: Responsible for gradient computation and parameter optimization enabled by automatic differentiation. (src/diffpssi/optimization_lib)
- Backend Selection: Choice between torch and numpy for numerical computations, enabling differentiation.
- Examples and Use Cases: Demonstrate how end-users build simulations by either passing a model dictionary or manually defining buses, generators, lines, and events.
- Testing and Utility Modules: Integration tests (in tests directory) and utilities (pf_util, tools) that support additional features and validations.

##### Depicting Relationships and Interactions
- A key relationship is between the simulation setup and the simulation engine. The simulation engine interacts with the model definitions (which include various power system component representations) and numerical solvers.
- The Optimization Library wraps around the simulation engine to enable gradient computations. It communicates with the simulation components to adjust parameters and records simulation outputs.
- The selection of a backend (torch or numpy) affects both the simulation and optimization layers.
- Utilities and tests ensure the integration and proper functioning of these modules.

##### Architectural Patterns and Design Principles
- Modularity: Notice how the repository cleanly separates simulations (simulation engine, solvers) from model specifications and optimization routines. This indicates a modular architecture that supports extensibility.
- Separation of Concerns: The framework segments responsibilities such as model definitions, simulation execution, numerical solving, and optimization.
- Extensibility: The use of dedicated modules for models (like governors, exciters, etc.) suggests that new components can easily be added.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "User Layer"
        A["Examples and Use Cases"]:::simulationUser
    end

    subgraph "Simulation Core"
        B["Simulation Engine"]:::simulationCore
        C["Mathematical Solvers"]:::simulationCore
        D["Power System Models"]:::simulationCore
        E["Backend Selection (numpy/torch)"]:::simulationCore
        F["Parallel Executor"]:::simulationCore
    end

    subgraph "Optimization Layer"
        G["Optimization Library"]:::simulationOpt
    end

    subgraph "Utilities & Testing"
        H["Testing Modules"]:::simulationUtil
        I["Power Flow Calculations Utility"]:::simulationUtil
        J["Additional Utilities/Visualizations"]:::simulationUtil
    end

    %% Relationships
    A -->|"provides_config"| B
    B -->|"uses"| C
    B -->|"integrates"| D
    B -->|"selects_backend"| E
    B -->|"executes_parallel"| F
    B <-->|"gradient_feedback"| G
    B -->|"utilizes"| I
    H -->|"validates"| B
    H -->|"validates"| G
    J -->|"supports"| A

    %% Click Events
    click A "https://github.com/georgkordowich/diffpssi/tree/main/examples"
    click B "https://github.com/georgkordowich/diffpssi/blob/main/src/diffpssi/power_sim_lib/simulator.py"
    click C "https://github.com/georgkordowich/diffpssi/blob/main/src/diffpssi/power_sim_lib/solvers.py"
    click D "https://github.com/georgkordowich/diffpssi/tree/main/src/diffpssi/power_sim_lib/models"
    click E "https://github.com/georgkordowich/diffpssi/blob/main/src/diffpssi/power_sim_lib/backend.py"
    click G "https://github.com/georgkordowich/diffpssi/tree/main/src/diffpssi/optimization_lib"
    click H "https://github.com/georgkordowich/diffpssi/tree/main/tests"
    click I "https://github.com/georgkordowich/diffpssi/blob/main/pf_util/pf_tools.py"
    click J "https://github.com/georgkordowich/diffpssi/blob/main/tools/colors.py"

    %% Styles
    classDef simulationCore fill:#f9c,stroke:#333,stroke-width:2px;
    classDef simulationOpt fill:#ccf,stroke:#333,stroke-width:2px;
    classDef simulationUser fill:#cfc,stroke:#333,stroke-width:2px;
    classDef simulationUtil fill:#ffc,stroke:#333,stroke-width:2px;
```
