# ganmocai/power_gym architecture

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

- Interactive diagram: https://gitdiagram.com/ganmocai/power_gym
- Repository: https://github.com/ganmocai/power_gym

## Overview

##### Project Type and Purpose
- This is a research/engineering tool that trains and evaluates a reinforcement-learning (RL) agent (PPO) to manage distributed energy resources (DER) on power distribution networks.
- It combines:
- A custom OpenAI-Gym-style environment (“powergym”) that wraps OpenDSS circuit definitions (in systems/) plus load and EV-demand models.
- An EV-demand generator (ev_demand/ev_demand.py) that produces time-series demand profiles.
- A PPO agent implementation (ppo_agent.py) that drives control actions (e.g., BMS setpoints) and learns from environment feedback.
- Prebuilt circuit definitions and loadshape data under systems/ for various test feeders (13Bus, 123Bus, 34Bus, 8500-Node).

A. Data Sources
- EV Demand Generator: ev_demand/ev_demand.py + CSV inputs in ev_demand/
- Load Profiles & DER Profiles: powergym/loadprofile.py, rate files under systems/*/loadshape/
- Circuit Definitions: .dss files in systems/13Bus, 123Bus, 34Bus, 8500-Node/
B. Simulation Environment (“powergym”)
- env.py: the Gym environment interface (step(), reset())
- circuit.py: OpenDSS API wrapper to load and solve circuits
- ev_bms.py: battery management system model for each DER
- env_register.py: Gym registration and configuration
C. RL Agent (ppo_agent.py)
- Policy network and training loop (collect batches, compute rewards, backprop)
- Interfaces to Gym (action selection, state ingestion)
D. Visualization & Analysis
- Plots: generated PDFs and scripts under plots/ppo_model/
- convert_freq.py: utility to process time-series frequency or power data

##### Relationships and Data Flow
- EV Demand Generator produces time-series profiles → fed into Gym environment as part of observation.
- powergym.env uses circuit.py to load a chosen system’s .dss files and loadshape CSVs, builds the simulation model.
- ppo_agent interacts with env:
action ← policy(state) → env.step(action) → next_state, reward → policy update
- env.step() calls circuit.solutions via OpenDSS, applies control from ev_bms, returns voltages, power flows, rewards.
- Training logs & performance plots are saved under plots/ppo_model/.

##### Architectural Patterns and Principles
- Modular separation:
- Data generation (ev_demand)
- Simulation core (powergym)
- Learning algorithm (ppo_agent)
- Visualization (plots/)
- Clear API boundaries: Gym’s step/reset interface shields agent from low-level OpenDSS calls.
- Extensibility: New circuits can be added under systems/ without changing code.

##### Checklist
- [ ] Clear component names matching file-tree modules
- [ ] Labels for interfaces (Gym API, OpenDSS API)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Data Sources
    subgraph "Data Sources"
        direction LR
        EVGen["EV Demand Generator"]:::code
        LoadProf["Load Profiles & DER Profiles"]:::code
        CircuitDef["Circuit Definitions (.dss)"]:::data
    end

    %% Simulation Environment
    subgraph "Simulation Environment"
        direction TB
        env_py["env.py<br/>(Gym Interface)"]:::code
        circuit_py["circuit.py<br/>(OpenDSS API)"]:::code
        ev_bms_py["ev_bms.py<br/>(BMS Model)"]:::code
        env_reg_py["env_register.py<br/>(Gym Registration)"]:::code
    end

    %% RL Agent
    Agent["RL Agent<br/>(ppo_agent.py)"]:::agent

    %% Visualization & Analysis
    subgraph "Visualization & Analysis"
        direction TB
        VizPlot["plots/ppo_model/"]:::data
        ConvFreq["convert_freq.py"]:::code
    end

    %% External Tool
    DSS["OpenDSS"]:::external

    %% Data Flow
    EVGen -->|"demand profiles"| env_py
    LoadProf -->|"load profiles"| circuit_py
    CircuitDef -->|"circuit definitions"| circuit_py

    env_py -->|"observations (voltages, SoC, power)"| Agent
    Agent -->|"actions (charge/discharge setpoints)"| env_py
    circuit_py -->|"solve()"| DSS
    env_py --> ev_bms_py
    ev_bms_py --> env_py

    env_py -->|"episode metrics, rewards"| VizPlot
    VizPlot --> ConvFreq

    %% Click Events
    click EVGen "https://github.com/ganmocai/power_gym/blob/main/ev_demand/ev_demand.py"
    click LoadProf "https://github.com/ganmocai/power_gym/blob/main/powergym/loadprofile.py"
    click CircuitDef "https://github.com/ganmocai/power_gym/tree/main/systems/13Bus/"
    click CircuitDef "https://github.com/ganmocai/power_gym/tree/main/systems/123Bus/"
    click CircuitDef "https://github.com/ganmocai/power_gym/tree/main/systems/34Bus/"
    click CircuitDef "https://github.com/ganmocai/power_gym/tree/main/systems/8500-Node/"
    click env_py "https://github.com/ganmocai/power_gym/blob/main/powergym/env.py"
    click circuit_py "https://github.com/ganmocai/power_gym/blob/main/powergym/circuit.py"
    click ev_bms_py "https://github.com/ganmocai/power_gym/blob/main/powergym/ev_bms.py"
    click env_reg_py "https://github.com/ganmocai/power_gym/blob/main/powergym/env_register.py"
    click Agent "https://github.com/ganmocai/power_gym/blob/main/ppo_agent.py"
    click VizPlot "https://github.com/ganmocai/power_gym/tree/main/plots/ppo_model/"
    click ConvFreq "https://github.com/ganmocai/power_gym/blob/main/convert_freq.py"

    %% Styles
    classDef code fill:#AED6F1,stroke:#21618C,color:#154360
    classDef data fill:#A9DFBF,stroke:#1D8348,color:#145A32
    classDef agent fill:#F5B041,stroke:#B9770E,color:#7E5109
    classDef external fill:#D5D8DC,stroke:#7B7D7D,color:#566573
```
