Fluid dynamic simulation, with obstacles
Asked Answered
L

6

10

I'm trying to write a fluid dynamic simulator on the HTML5 canvas. I've found some real damn cool stuff on the internets that always look like a promising starting point, but they are all cell-based and use some crazy math.

I'd like to be able to add arbitrary obstacles (lines of any orientation, circles, etc) to make things more interesting, but I've no idea where do begin.

Does anyone know of some fairly simple equations for fluid simulation that include obstacles of any orientation? Alternatively, could anybody point me towards the math required to take one of the above examples and add obstacles?

I know that this question borders on something I should ask mathoverflow, but they seem more into the theory stuff. Apologies if I'm in the wrong area. I don't really know where to begin - if anyone's worked on fluid simulation with arbitrary obstacles before, I could use some pointers.

Accuracy takes a back seat to simplicity here.

Thanks!

Lactoprotein answered 4/2, 2011 at 2:32 Comment(3)
I'm sorry, but this is something where the closest you're going to come to doing this without understanding the math, is copying and pasting somebody else's code.Horsehair
physics.weber.edu/schroeder/fluidsPacifier
immersed boundary method is probably the easiestKozlowski
A
13

Fluid dynamics isn't a simple topic. All that "theory" they like over at the other site is just the way this field works.

The simplest example of fluid flow is 2D, incompressible, irrotational, laminar flow. I'd start by looking into that.

But it's not an easy field. There's no "Teach Yourself Computational Fluid Dynamics In Ten Days" books out there.

Alvis answered 4/2, 2011 at 2:35 Comment(0)
U
8

The best book to read for introduction to graphics-oriented fluid simulation is "Fluid Simulation for Computer Graphics" by Robert Bridson (disclaimer: he was my PhD advisor). http://www.cs.ubc.ca/~rbridson/fluidbook/

Ultimately, there is plenty of math involved, but there's also plenty of code examples to clarify things for the less math-inclined.

It covers mainly the cell-based approach you mentioned. The other main alternative is "Smoothed Particle Hydrodynamics" or SPH. Matthias Muller has some papers about this if you're looking to get started.

Ungley answered 12/3, 2011 at 18:13 Comment(1)
+1 - This looks like a nice book. I'd consider you an authority on this subject if you're the C. Batty who's cited as future work since 2008 that should be added. Very nice, indeed. Thank you for bringing it to my attention. I'll add it to my "should read" backlog. I've downloaded the free surface flow paper citation. I'll be sure to read it carefully.Alvis
U
3

If you don't care about real accuracy but just want something swooshy and cool, I developed a very simple pressure-based simulation that delivers a very fast interactive interface in Javascript. You can see it here.

Undry answered 28/11, 2011 at 20:57 Comment(1)
this is cool. Quick note for others that you have to click on the black screen for it to react to the perturbation.Knotting
H
3

Here is a pretty decent list of everything You need to know about fluid dynamics and simulation: http://www.dgp.toronto.edu/~stam/reality/Research/pub.html

Also you should check this site, where you can find the concrete source code written in Java and transported to Actionscript3. It's pretty documented, so shouldn't be a problem to transport to Javascript.

Hyperphysical answered 12/4, 2012 at 7:23 Comment(1)
Very good resources, this paper from your first link is particularly helpful: dgp.toronto.edu/people/stam/reality/Research/pdf/GDC03.pdf. The math is explained and C code is providedForthwith
M
0

I have tried that and just to let you know there is an important part of Fluid simulation of any kind called Projection which is computationally extensive even on CPU it takes much and you might well know that Javascript is quite slow for many reasons.

Modernize answered 20/5, 2011 at 18:19 Comment(0)
S
0

I found pretty simple js code of fluid here:

https://codepen.io/aecend/pen/WbONyK

There no "crazy math":

function update_pressure(cell_data) {

//This calculates the collective pressure on the X axis by summing the surrounding velocities
var pressure_x = (
    cell_data.up_left.xv * 0.5 //Divided in half because it's diagonal
    + cell_data.left.xv
    + cell_data.down_left.xv * 0.5 //Same
    - cell_data.up_right.xv * 0.5 //Same
    - cell_data.right.xv
    - cell_data.down_right.xv * 0.5 //Same
);

//This does the same for the Y axis.
var pressure_y = (
    cell_data.up_left.yv * 0.5
    + cell_data.up.yv
    + cell_data.up_right.yv * 0.5
    - cell_data.down_left.yv * 0.5
    - cell_data.down.yv
    - cell_data.down_right.yv * 0.5
);

//This sets the cell pressure to one-fourth the sum of both axis pressure.
cell_data.pressure = (pressure_x + pressure_y) * 0.25;

}

/* This function updates the velocity value for an individual cell using the velocities of neighboring cells. */ function update_velocity(cell_data) {

/*
This adds one-fourth of the collective pressure from surrounding cells to the 
cell's X axis velocity.
*/
cell_data.xv += (
    cell_data.up_left.pressure * 0.5
    + cell_data.left.pressure
    + cell_data.down_left.pressure * 0.5
    - cell_data.up_right.pressure * 0.5
    - cell_data.right.pressure
    - cell_data.down_right.pressure * 0.5
) * 0.25;

//This does the same for the Y axis.
cell_data.yv += (
    cell_data.up_left.pressure * 0.5
    + cell_data.up.pressure
    + cell_data.up_right.pressure * 0.5
    - cell_data.down_left.pressure * 0.5
    - cell_data.down.pressure
    - cell_data.down_right.pressure * 0.5
) * 0.25;

/*
This slowly decreases the cell's velocity over time so that the fluid stops
if it's left alone.
*/
cell_data.xv *= 0.99;
cell_data.yv *= 0.99;

}

Similarity answered 29/6 at 4:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.