Kenny Peng's Raspberry Pi Fluid Simulation Puts an Ocean in the Palm of Your Hand
Simulating well beyond the confines of its compact display, this motion-sensing project relies on smoothed-particle hydrodynamics.
Embedded developer Kenny Peng has turned a Raspberry Pi single-board computer into a toy full of virtual sloshing fun, running a live fluid simulator that responds to movement — and is displayed on a compact OLED panel.
"This project uses an SSD1306 OLED module, a [TDK InvenSense] MPU6050 accelerometer, and a Raspberry Pi 4 to realize a fluid simulation toy that lets you toss around an ocean of water in your hand," Peng explains of the just-for-fun creation. "That is, it simulates fluid dynamics at a scale larger than the actual screen. This can be thought of as a 'free-surface flow' problem, and it's solved by a 'smoothed-particle hydrodynamics' (SPH) technique."
Unlike more complicated physics simulations and their need for high-end general-purpose graphics processing unit (GPGPU) or dedicated acceleration hardware, Peng's toy ocean runs on the Raspberry Pi's CPU — "the results can only be better if someone leveraged more modern SPH methods or the Raspberry Pi 4's GPU," Peng notes, referring to the ability to offload massively-parallel computation to the graphics processor in order to improve performance.
The secret to the project's fluid movement, pun entirely intended, is, as Peng says, smoothed-particle hydrodynamics — a computational approach to solid and fluid simulation which uses a mesh-free simulation method to better simulate complex boundary dynamics without requiring too much CPU or GPU power, allowing it to run perfectly in real-time on the Raspberry Pi 4's relatively modest CPU. The performance is further boosted by using OpenMP to spread the workload across all four CPU cores.
"Besides the SSD1306 driver," Peng says, "this project is just under 750 lines of C!" In that small chunk of code, though, Peng has implemented more simulated physics than you'd expect of what is normally a simple approximation for entertainment — including artificial viscosity, ordinary advection, and surface tension effects smartly faked using artificial pressure.
Peng has published the source code for the project on GitHub under the permissive MIT license.