# unqgit/polartwin architecture

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

- Interactive diagram: https://gitdiagram.com/unqgit/polartwin
- Repository: https://github.com/unqgit/polartwin

## Overview

PolarTwin turns station DSL into a compiled model, loads it into a deterministic simulation, and exposes station state and scenario controls through a web command center. Operators can inspect infrastructure in 3D and topology views, examine diagnostics, and run scenarios. The graph also preserves the compiler’s hierarchy/connection/spec stages, scenario and event authoring, behavior execution, and telemetry persistence. Relationships involving unsampled modules are included only where the README or SOURCE INDEX supports them; the source excerpts do not establish every runtime call or frontend API route.

Main entry points: [`frontend/src/App.tsx`](https://github.com/unqgit/polartwin/blob/main/frontend/src/App.tsx) (React command center), [`compiler/core.py`](https://github.com/unqgit/polartwin/blob/main/compiler/core.py) (Compiler orchestration), [`src/twin_sim/simulation/station_loader.py`](https://github.com/unqgit/polartwin/blob/main/src/twin_sim/simulation/station_loader.py) (Station loader), [`frontend/src/lib/api.ts`](https://github.com/unqgit/polartwin/blob/main/frontend/src/lib/api.ts) (Frontend API client)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_authoring["DSL compiler"]
  node_twin_dsl["Station DSL"]
  node_compiler["Compiler orchestration<br/>[core.py]"]
  node_hierarchy_parser["Hierarchy parser"]
  node_connection_parser["Connection parser"]
  node_spec_parser["Specification parser<br/>[spec_parser.py]"]
end

subgraph group_backend["Simulation backend"]
  node_domain_models["Shared domain models<br/>[domain.py]"]
  node_station_loader["Station loader<br/>[station_loader.py]"]
  node_runtime_model["Runtime station model<br/>[graph.py]"]
  node_api["FastAPI routes<br/>[main.py]"]
  node_sim_manager["Simulation manager<br/>[manager.py]"]
  node_engine["Deterministic engine<br/>[engine_core.py]"]
  node_behavior["Behavior execution<br/>[behaviors.py]"]
end

subgraph group_scenario["Scenarios and events"]
  node_scenario_manager["Scenario manager"]
  node_scene_parser["Scene parser<br/>[scene_parser.py]"]
  node_event_parser["Event parser<br/>[event_parser.py]"]
  node_event_stack["Event stack<br/>[event_stack.py]"]
end

subgraph group_telemetry["Telemetry"]
  node_telemetry_db[("Telemetry database<br/>[database.py]")]
end

subgraph group_frontend["Command center"]
  node_web_ui["React command center<br/>[App.tsx]"]
  node_api_client["Frontend API client<br/>[api.ts]"]
  node_simulation_controls["Simulation controls"]
  node_station_context["Station data context<br/>[StationContext.tsx]"]
  node_twin_view["3D station view<br/>[TwinViewer.tsx]"]
  node_topology_view["Topology view"]
  node_scenario_ui["Scenario workspace<br/>[ScenariosPage.tsx]"]
  node_diagnostics_ui["Diagnostics"]
end

node_operator(("Station operator"))

node_operator -->|"defines station"| node_twin_dsl
node_twin_dsl -->|"compiles"| node_compiler
node_compiler -->|"parses hierarchy"| node_hierarchy_parser
node_compiler -->|"parses connections"| node_connection_parser
node_compiler -->|"parses specifications"| node_spec_parser
node_hierarchy_parser -->|"creates models"| node_domain_models
node_connection_parser -->|"creates models"| node_domain_models
node_spec_parser -->|"creates models"| node_domain_models
node_compiler -->|"compiled artifacts"| node_station_loader
node_station_loader -->|"loads station"| node_runtime_model
node_api -->|"discovers stations"| node_station_loader
node_api -->|"routes simulation"| node_sim_manager
node_sim_manager -->|"uses loaded station"| node_station_loader
node_sim_manager -->|"runs ticks"| node_engine
node_engine -->|"executes behaviors"| node_behavior
node_operator -->|"authors scenarios"| node_scenario_ui
node_scenario_ui -.->|"sends commands"| node_api_client
node_api_client -.->|"calls API"| node_api
node_api -->|"routes scenario requests"| node_scenario_manager
node_scenario_manager -->|"parses scenes"| node_scene_parser
node_scenario_manager -->|"parses definitions"| node_event_parser
node_scene_parser -->|"parses event references"| node_event_parser
node_station_loader -->|"loads event stack"| node_event_stack
node_engine -->|"uses event stack"| node_event_stack
node_sim_manager -->|"persists run data"| node_telemetry_db
node_operator -->|"uses dashboard"| node_web_ui
node_web_ui -->|"provides station context"| node_station_context
node_station_context -->|"loads station data"| node_api_client
node_web_ui -.->|"offers controls"| node_simulation_controls
node_simulation_controls -->|"requests simulation"| node_api_client
node_web_ui -->|"renders station"| node_twin_view
node_web_ui -->|"renders topology"| node_topology_view
node_web_ui -->|"shows diagnostics"| node_diagnostics_ui
node_api_client -.->|"fetches telemetry"| node_telemetry_db
node_operator -->|"inspects state"| node_diagnostics_ui

click node_compiler "https://github.com/unqgit/polartwin/blob/main/compiler/core.py"
click node_hierarchy_parser "https://github.com/unqgit/polartwin/blob/main/compiler/parser/hierarchy_parser.py"
click node_connection_parser "https://github.com/unqgit/polartwin/blob/main/compiler/parser/connection_parser.py"
click node_spec_parser "https://github.com/unqgit/polartwin/blob/main/compiler/parser/spec_parser.py"
click node_domain_models "https://github.com/unqgit/polartwin/blob/main/packages/shared_models/domain.py"
click node_station_loader "https://github.com/unqgit/polartwin/blob/main/src/twin_sim/simulation/station_loader.py"
click node_runtime_model "https://github.com/unqgit/polartwin/blob/main/src/twin_sim/model/graph.py"
click node_api "https://github.com/unqgit/polartwin/blob/main/src/twin_sim/api/main.py"
click node_sim_manager "https://github.com/unqgit/polartwin/blob/main/src/twin_sim/api/manager.py"
click node_engine "https://github.com/unqgit/polartwin/blob/main/src/twin_sim/simulation/engine_core.py"
click node_behavior "https://github.com/unqgit/polartwin/blob/main/src/twin_sim/simulation/behaviors.py"
click node_scenario_manager "https://github.com/unqgit/polartwin/blob/main/src/twin_sim/api/scenario_manager.py"
click node_scene_parser "https://github.com/unqgit/polartwin/blob/main/src/twin_sim/dsl/scene_parser.py"
click node_event_parser "https://github.com/unqgit/polartwin/blob/main/src/twin_sim/dsl/event_parser.py"
click node_event_stack "https://github.com/unqgit/polartwin/blob/main/src/twin_sim/dsl/event_stack.py"
click node_telemetry_db "https://github.com/unqgit/polartwin/blob/main/src/twin_sim/telemetry/database.py"
click node_web_ui "https://github.com/unqgit/polartwin/blob/main/frontend/src/App.tsx"
click node_api_client "https://github.com/unqgit/polartwin/blob/main/frontend/src/lib/api.ts"
click node_simulation_controls "https://github.com/unqgit/polartwin/blob/main/frontend/src/components/SimulationManager.tsx"
click node_station_context "https://github.com/unqgit/polartwin/blob/main/frontend/src/components/StationContext.tsx"
click node_twin_view "https://github.com/unqgit/polartwin/blob/main/frontend/src/components/TwinViewer.tsx"
click node_topology_view "https://github.com/unqgit/polartwin/blob/main/frontend/src/pages/ConnectionsPage.tsx"
click node_scenario_ui "https://github.com/unqgit/polartwin/blob/main/frontend/src/pages/ScenariosPage.tsx"
click node_diagnostics_ui "https://github.com/unqgit/polartwin/blob/main/frontend/src/pages/DiagnosticsPage.tsx"

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_twin_dsl,node_compiler,node_hierarchy_parser,node_connection_parser,node_spec_parser toneBlue
class node_domain_models,node_station_loader,node_runtime_model,node_api,node_sim_manager,node_engine,node_behavior toneAmber
class node_scenario_manager,node_scene_parser,node_event_parser,node_event_stack toneMint
class node_telemetry_db toneRose
class node_web_ui,node_api_client,node_simulation_controls,node_station_context,node_twin_view,node_topology_view,node_scenario_ui,node_diagnostics_ui,node_operator toneIndigo
```

## Components

### DSL compiler

- **Station DSL**
- [Compiler orchestration](https://github.com/unqgit/polartwin/blob/main/compiler/core.py): `compiler/core.py`
- [Hierarchy parser](https://github.com/unqgit/polartwin/blob/main/compiler/parser/hierarchy_parser.py): `compiler/parser/hierarchy_parser.py`
- [Connection parser](https://github.com/unqgit/polartwin/blob/main/compiler/parser/connection_parser.py): `compiler/parser/connection_parser.py`
- [Specification parser](https://github.com/unqgit/polartwin/blob/main/compiler/parser/spec_parser.py): `compiler/parser/spec_parser.py`

### Simulation backend

- [Shared domain models](https://github.com/unqgit/polartwin/blob/main/packages/shared_models/domain.py): `packages/shared_models/domain.py`
- [Station loader](https://github.com/unqgit/polartwin/blob/main/src/twin_sim/simulation/station_loader.py): `src/twin_sim/simulation/station_loader.py`
- [Runtime station model](https://github.com/unqgit/polartwin/blob/main/src/twin_sim/model/graph.py): `src/twin_sim/model/graph.py`
- [FastAPI routes](https://github.com/unqgit/polartwin/blob/main/src/twin_sim/api/main.py): `src/twin_sim/api/main.py`
- [Simulation manager](https://github.com/unqgit/polartwin/blob/main/src/twin_sim/api/manager.py): `src/twin_sim/api/manager.py`
- [Deterministic engine](https://github.com/unqgit/polartwin/blob/main/src/twin_sim/simulation/engine_core.py): `src/twin_sim/simulation/engine_core.py`
- [Behavior execution](https://github.com/unqgit/polartwin/blob/main/src/twin_sim/simulation/behaviors.py): `src/twin_sim/simulation/behaviors.py`

### Scenarios and events

- [Scenario manager](https://github.com/unqgit/polartwin/blob/main/src/twin_sim/api/scenario_manager.py): `src/twin_sim/api/scenario_manager.py`
- [Scene parser](https://github.com/unqgit/polartwin/blob/main/src/twin_sim/dsl/scene_parser.py): `src/twin_sim/dsl/scene_parser.py`
- [Event parser](https://github.com/unqgit/polartwin/blob/main/src/twin_sim/dsl/event_parser.py): `src/twin_sim/dsl/event_parser.py`
- [Event stack](https://github.com/unqgit/polartwin/blob/main/src/twin_sim/dsl/event_stack.py): `src/twin_sim/dsl/event_stack.py`

### Telemetry

- [Telemetry database](https://github.com/unqgit/polartwin/blob/main/src/twin_sim/telemetry/database.py): `src/twin_sim/telemetry/database.py`

### Command center

- [React command center](https://github.com/unqgit/polartwin/blob/main/frontend/src/App.tsx): `frontend/src/App.tsx`
- [Frontend API client](https://github.com/unqgit/polartwin/blob/main/frontend/src/lib/api.ts): `frontend/src/lib/api.ts`
- [Simulation controls](https://github.com/unqgit/polartwin/blob/main/frontend/src/components/SimulationManager.tsx): `frontend/src/components/SimulationManager.tsx`
- [Station data context](https://github.com/unqgit/polartwin/blob/main/frontend/src/components/StationContext.tsx): `frontend/src/components/StationContext.tsx`
- [3D station view](https://github.com/unqgit/polartwin/blob/main/frontend/src/components/TwinViewer.tsx): `frontend/src/components/TwinViewer.tsx`
- [Topology view](https://github.com/unqgit/polartwin/blob/main/frontend/src/pages/ConnectionsPage.tsx): `frontend/src/pages/ConnectionsPage.tsx`
- [Scenario workspace](https://github.com/unqgit/polartwin/blob/main/frontend/src/pages/ScenariosPage.tsx): `frontend/src/pages/ScenariosPage.tsx`
- [Diagnostics](https://github.com/unqgit/polartwin/blob/main/frontend/src/pages/DiagnosticsPage.tsx): `frontend/src/pages/DiagnosticsPage.tsx`

### Other components

- **Station operator**

## Connections

- Station operator → Station DSL: defines station (evidence: `README.md`)
- Station DSL → Compiler orchestration: compiles (evidence: `README.md`)
- Compiler orchestration → Hierarchy parser: parses hierarchy (evidence: `compiler/core.py`)
- Compiler orchestration → Connection parser: parses connections (evidence: `compiler/core.py`)
- Compiler orchestration → Specification parser: parses specifications (evidence: `compiler/core.py`)
- Hierarchy parser → Shared domain models: creates models (evidence: `compiler/parser/hierarchy_parser.py`)
- Connection parser → Shared domain models: creates models (evidence: `compiler/parser/connection_parser.py`)
- Specification parser → Shared domain models: creates models (evidence: `compiler/parser/spec_parser.py`)
- Compiler orchestration → Station loader: compiled artifacts (evidence: `README.md`)
- Station loader → Runtime station model: loads station (evidence: `src/twin_sim/simulation/station_loader.py`)
- FastAPI routes → Station loader: discovers stations (evidence: `src/twin_sim/api/main.py`)
- FastAPI routes → Simulation manager: routes simulation (evidence: `src/twin_sim/api/main.py`)
- Simulation manager → Station loader: uses loaded station (evidence: `src/twin_sim/api/manager.py`)
- Simulation manager → Deterministic engine: runs ticks (evidence: `src/twin_sim/api/manager.py`)
- Deterministic engine → Behavior execution: executes behaviors (evidence: `src/twin_sim/simulation/engine_core.py`)
- Station operator → Scenario workspace: authors scenarios (evidence: `README.md`)
- Scenario workspace → Frontend API client: sends commands
- Frontend API client → FastAPI routes: calls API
- FastAPI routes → Scenario manager: routes scenario requests (evidence: `src/twin_sim/api/main.py`)
- Scenario manager → Scene parser: parses scenes (evidence: `src/twin_sim/api/scenario_manager.py`)
- Scenario manager → Event parser: parses definitions (evidence: `src/twin_sim/api/scenario_manager.py`)
- Scene parser → Event parser: parses event references (evidence: `src/twin_sim/dsl/scene_parser.py`)
- Station loader → Event stack: loads event stack (evidence: `src/twin_sim/simulation/station_loader.py`)
- Deterministic engine → Event stack: uses event stack (evidence: `src/twin_sim/simulation/engine_core.py`)
- Simulation manager → Telemetry database: persists run data (evidence: `src/twin_sim/api/manager.py`)
- Station operator → React command center: uses dashboard (evidence: `README.md`)
- React command center → Station data context: provides station context (evidence: `frontend/src/App.tsx`)
- Station data context → Frontend API client: loads station data (evidence: `frontend/src/components/StationContext.tsx`)
- React command center → Simulation controls: offers controls
- Simulation controls → Frontend API client: requests simulation (evidence: `frontend/src/components/SimulationManager.tsx`)
- React command center → 3D station view: renders station (evidence: `README.md`)
- React command center → Topology view: renders topology (evidence: `README.md`)
- React command center → Diagnostics: shows diagnostics (evidence: `README.md`)
- Frontend API client → Telemetry database: fetches telemetry
- Station operator → Diagnostics: inspects state (evidence: `README.md`)
