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.
•
u/No_Engineering_1155 May 17 '25
Nice job! I really like this kind of content, as myself as well already implemented a similar thing (unpublished), but that was in Julia back in the days. However, I was wondering how did Modelica do its thing, because the signal-flow based simulation approach has its limitations and it frustrated me, that acausal connections are not possible with signal-flows, and therefore extending the model requires to rewrite everything from scratch again.
So my questions:
do you consider it to extend this framework to acausal connections as well? (Probably requires index reduction, basically implementing Pantelides + dummy-derivative)
how do you handle algebraic loops, where only non-differentiated variables occur?
can your solver module solve index 1 dae? How about higher index?
is it possible to obtain intermediate results, e.g. the result of an adder, for analysis, postprocessing?
I wish you much success and fun with this project!
→ More replies (0)
•
•
u/gtd_rad May 17 '25
Wow. Insane work here. I'm going to have to try this out. Will you plan on implementing a GUI?
→ More replies (0)
•
u/Fermi-4 May 18 '25
You gotta do snake case (or even just lower case) for variables/instance of classes.. and more than three letters
Framework looks cool though
•
u/TristyTreat May 17 '25
Have you worked with engineering systems modeling software products such as Fluent and CD Adapco?
→ More replies (0)
•
u/dokuez May 17 '25
Actually something similar exists and it has both GUI and code generation. You may wanna get in touch with the authors and contribute instead of developing your own tool.
→ More replies (0)
•
u/lapeno May 17 '25
Would it be possible to export the computational graph to CasADi for automatic differentiation and C code generation?
→ More replies (0)
•
•
u/Aircraft_Control May 17 '25
Ooooh. I was looking for this a week ago and posted it in the thread. Great work man. I am going to try it soon.
•
•
u/sfscsdsf May 17 '25
i wonder what’s the other libraries that can be used to develop control algorithms easily with this?
•
u/Logical-Wish-9230 May 17 '25
Woah that is really really good progress, looking forward seeing the GUI beating the Simulink. I would guarantee you tons of students and teachers will use your package
→ More replies (0)
•
•
u/bringthe707out_ May 17 '25
this is so fkn neat!