Architecture
CosimGym relies on a layered software architecture designed to separate mathematical definitions from complex multiprocessing execution pipelines.
High-Level Execution Flow
When you run a simulation in CosimGym, here is what happens under the hood:
- Configuration Reading:
The
ScenarioManagerloads raw YAML and parses it through strict Pydantic definitions (config_dataclasses.py), ensuring topology rules and variable typings are valid. - Central State Distribution:
The
ScenarioManagerpushes both the scenario configuration and the staticModelCataloginto a Redis Data Store. This prevents messy file-system dependencies; spawned federates just pull their metadata directly from Redis viaRedisCatalog. - Broker Startup:
For every Federation declared, the manager launches a decoupled
helics_brokersubprocess. It also links multiple brokers automatically if there's a multi-federation architecture. - Federate Launching:
The manager then triggers
federate_launcher.pylogic which spawns multiple async processes. - Standard physics endpoints instantiate as standard
BaseFederateobjects. - If Reinforcement Learning is configured, a final
RL_Federateprocess is created, which boots up a tailored Gymnasium env driving the configured agent backend (Stable-Baselines3, custom PyTorch, or Ray RLlib). - A federate configured
type: interfaceinstantiates anInterfaceFederateinstead: no physics model, just a transport adapter (MQTT by default) bridging its HELICS connections to/from the external world — see Digital-Twin Interfaces & Live Streaming. - Execution Loop:
For simulation duration, HELICS manages internal time stepping, ensuring synchronized
publish/subscribeacross processes. If an RL agent is present, episode resets are coordinated via theenvironment.resetpolicy (full | rolling | none) across federates. Any federate can also opt intostreaming.stream: trueto mirror its I/O to MQTT each step, independent of HELICS, for live observability. - Graceful Teardown:
Once the endpoint is reached, or the agent completes testing epochs, the
ScenarioManagerharvests logging output, kills the subprocesses gracefully, and shuts down the brokers.
Diagram
Below is an illustration representing how the hierarchy of scenarios coordinates independent executable nodes.
The CosimGym distributed architecture utilizing HELICS Brokers.