python-drs
Welcome to python-drs, a PyTorch-inspired, event-driven Discrete Rate Simulation (DRS) framework.
DRS models systems where quantities flow continuously over time — water networks, electrical grids, traffic, chemical processing, supply chains, and more. Instead of stepping through time at fixed intervals, the engine calculates exactly when the next limit will be hit, jumps the simulation clock to that precise moment, and triggers the matching state transition.
- Blazing fast — because time jumps from event to event, years of operation can be simulated in a fraction of a second.
- Event-driven control —
Levelthresholds are first-class citizens, and control logic hooks into the loop with an@engine.on_stepdecorator. - PyTorch-inspired architecture — everything is a
Modulethat ownsVariable,Level, andTimerstate, so writing a simulation feels like building a neural network.
1. The Core Idea
Most simulations use a fixed-step approach. Imagine watching a tank fill, checking it exactly once every minute.
- The problem: If it overflows at 1 minute and 30 seconds, you won't notice until minute 2.
- The workaround: Check every second — but that makes the simulation slow.
Discrete Rate Simulation is different. The engine looks at the fill rate, computes exactly when the tank will be full, sets an alarm for that moment, and lets the simulation "jump" forward in time. It runs incredibly fast and never misses a limit.
2. How the Engine Works
Every simulation follows the same repeating loop:
- Run control policies — each registered
@engine.on_stephandler sets the current rates and thresholds of the system. - Find the next event — the engine calculates how long until any
Levelcrosses one of its thresholds. - Jump time — the simulation clock advances by exactly that amount, and all levels are integrated forward.
- Repeat.
3. Getting Started
from drs import DRSEngine, Module, Level
class Tank(Module):
pass
model = Tank()
model.volume = Level("Volume", initial_value=100.0)
engine = DRSEngine()
engine.register(model)
@engine.on_step
def policy(t):
# Fill at 50 units per time step
model.volume.rate = 50.0
result = engine.run(max_time=20.0)
print(result.summary())
Every component is a drs.Module that owns its physical state as a drs.Level (something that fills or empties). Modules can be nested into hierarchies; the engine steps the state of every registered component.
4. Where to Go Next
- Tutorial 1: Introduction to DRS — core types, modules, and your first simulation.
- Tutorial 2: Event-Driven Time Jumping — how the engine leaps from event to event using thresholds.
- Tutorial 3: Streaming Inputs & Data Sources — feeding discrete data streams into a continuous simulation.
- Tutorial 4: Telemetry & Control Policies — monitoring runs and hooking control logic into the engine.
- Tutorial 5: Design Patterns: Operating Modes — controllers and mode-driven routing.
The API reference documents the full public interface.