# 2d fluid simulations for beginners

This topic is 3508 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I want to write something that can simulate fluids in 2D. I'm particularly interested in the splashing and sloshing around, rather than simulating large bodies of water (although I may end up adding in heightfields and buoyancy code for some bodies of water, if it turns out to be an optimisation to do so). I gather I should be looking at Smoothed Particle Hydrodynamics (SPH) as my first resort, but unfortunately I'm not nearly as good at reading mathematical notation as I am at reading code, and I'm having trouble finding resources which can dumb down the basic concepts enough for my addled brain to understand. Even the Wikipedia article, for instance, is pretty tough going for me. Can anyone point me to a simple code example of a 2d SPH fluid simulation, ideally in C/C++? Failing that, can anyone point me to an article (or write a quick post here) explaining the techniques for handling all the particles in a way that could be understood by someone who is a decent programmer but a lousy mathematician? Any help much appreciated :)

##### Share on other sites
Quote:
 Original post by ElectroDruidCan anyone point me to a simple code example of a 2d SPH fluid simulation, ideally in C/C++?
I expect mathematics involved to be similar in 2D or 3D so it's probably worth you checking out the Sofware section of the Wikipedia article.

##### Share on other sites
Hi, Jos Stam has a 2d grid-based solver on his site in OpenGL/C++ :

http://www.dgp.toronto.edu/~stam/reality/Research/pub.html

Though I strongly suggest you learn the maths behind this stuff, I do appreciate that sometimes just seeing how the maths translates into code can be helpful in understanding it.

http://www.gamedev.net/community/forums/topic.asp?topic_id=482831

I got a lot of really great, in-depth replies.

There were also a few other papers I looked at that I can't get to because I'm at work. One was a presentation at SIGGRAPH by Mattias Muller-Fischer (spelling might be wrong there). I'll try looking out my notes from my dissertation when I get home, but I think Mark Harris was a good name too (infact if you just search threads started by me on fluid dynamics you'll get quite a few good links and books)

##### Share on other sites
What I've just managed to get running (albeit slowly, but it works at least) is an SPH system - similar to this one -> http://www.rowlhouse.co.uk/water/index.html but not quite the same.

A particle-based system seems to be quite a good fit for my needs, and I'm quite happy with the system that I've got so far, but I'm curious about the grid-based approaches as well. I've not seen much in the way of direct comparisons between particle-based and grid-based approaches. Are there inherent differences in performance? Simulation quality? Is one approach easier than the other to tune and tweak? Are they generally used for different purposes (grid-systems for instance, seem to be good for smoke, gas, fire, and maybe brownian motion, but I don't know if they're also any good at simulating a splashing liquid)?

##### Share on other sites
I always wanted to implement the interaction with rigid bodies etc as described here, but never got around to it...

##### Share on other sites
@MrRowl: I'll need to re-read that paper a few times before I can really absorb and understand it, but it sounds like exactly the kind of thing I need for rigid body interactions. Most approaches, including yours, seem to do fluid/body interactions using a kind of Verlet approach, representing rigid bodies as a set of particles with distance constraints. It seems to work quite well, but it's a problem for me since I'm trying to add fluid simulation into an existing physics engine (one with a rigid body solver based on Euler integration), so that paper looks like an ideal place to start.

I'll need to do some work to figure out how to calculate the forces and torques that the particles exert on the body, because the paper seems to gloss over the specifics of that a bit. Have you seen or read any other papers or code that cover similar approaches to this? I've been trawling the Interwebs for stuff describing how to do fluid vs. rigid body interactions, but everything I've found (except this) seems to recommend the Verlet approach.

(Also, completely off-topic, but I took another look at your site, and I really like your music) :)

##### Share on other sites
Sorry to bump this thread, but I'm still curious as to whether anyone else has looked into approaches for getting SPH fluids to play nicely with rigid bodies, and if they can offer any advice. I'm stuck :(

I've reworked my 2D SPH simulation such that I can switch between Verlet and Euler integration (Verlet seems to look marginally better, and involves less maths and code, but they're largely interchangeable). The idea was to try to use Verlet to simulate the interactions between the fluid and solid bodies, but given that the rigid body physics engine I'm using (Box2D) is based on Euler integration, it was such a nightmare trying to maintain 2 copies of the bodies (Box2d's representation, and my own Verlet one) and keeping them in sync that I'm not convinced it's even possible. It certainly doesn't seem wise.

I've tried the approach described in the white paper MrRowl linked to, but I can't get that working properly either. The solid bodies have a tendency to spin uncontrollably and the whole simulation breaks down pretty quickly. Part of the problem, I think, is that the description in the paper is quite vague on some of the details, and I'm not able to adequately reproduce it (or even guess at where the problems are) without some kind of reference implementation, or at least some much more detailed pseudocode.

Has anyone else tried this? Are there examples of successful approaches anywhere? Rigid body interactions in SPH fluid simulations is starting to feel like quite a lonely path of research...

EDIT: One thing I'm thinking about is to try to find a simpler way to approximate the forces acting on a body by applying one force per grid square which the body covers, rather than doing things at a particle level directly. So, the approach might involve looking at all the particles in a grid square to get a general "gist" of the overall force/velocity/pressure/density/whatever I need for a square, and somehow interpreting all of that as a single impulse. So, taking information from the (Langragian) SPH simulation, and turning that into a grid-based (Eulerian) simulation which runs in the same physical space, but which is only used for controlling any bodies which might be floating on/in the fluid.

I'm not sure if that's been tried before though, or where to start, or if it'd even work.

1. 1
2. 2
3. 3
Rutin
16
4. 4
JoeJ
13
5. 5

• 9
• 14
• 10
• 25
• 9
• ### Forum Statistics

• Total Topics
632645
• Total Posts
3007632
• ### Who's Online (See full list)

There are no registered users currently online

×