r/ControlTheory • u/Candid_Discipline848 • May 17 '25
Other I built a Python framework for simulating dynamical systems similar to Simulink
Hey everyone,
after spending way too many weekends on this, I wanted to share a project I've been working on called PathSim. Its a framework for simulating interconnected dynamical systems similar to Matlab Simulink, but in Python!
Check it out here: GitHub, documentation, PyPi
The standard approach to system simulation typically uses centralized solvers, but I took a different route by building a fully decentralized architecture. Each block handles its own state while communicating with others through a lightweight connection layer.
Some interesting aspects that emerged from this and other fun features:
You can modify the system structure during runtime (add/remove components mid-simulation)
Supports hierarchical modelling through (nested) subsystems
LOTS of different numerical integrators (probably too many)
Has a discrete event handling system for hybrid dynamical systems (zero crossings, schedules)
Has a built in automatic differentiation framework which makes the whole simulation differentiable (gradients propagate through both continuous dynamics and discrete events)
For example, this is how you would build and simulate a linear feedback system with PathSim:
from pathsim import Simulation, Connection
from pathsim.blocks import Source, Integrator, Amplifier, Adder, Scope
#blocks that define the system
Src = Source(lambda t : int(t>3))
Int = Integrator()
Amp = Amplifier(-1)
Add = Adder()
Sco = Scope(labels=["step", "response"])
blocks = [Src, Int, Amp, Add, Sco]
#the connections between the blocks
connections = [
Connection(Src, Add[0], Sco[0]), #one to many connection
Connection(Amp, Add[1]), #connecting to port 1
Connection(Add, Int), #default ports are 0
Connection(Int, Amp, Sco[1])
]
#initialize simulation with the blocks, connections and timestep
Sim = Simulation(blocks, connections, dt=0.01)
#run the simulation for some time
Sim.run(10)
#plot from the scope directly
Sco.plot()I'd love to hear your thoughts or answer any questions about the approach. The framework is still evolving and community feedback would be really valuable.