mikeash.com pyblog/fluid-simulation-for-dummies.html commentshttp://www.mikeash.com/?page=pyblog/fluid-simulation-for-dummies.html#commentsmikeash.com Recent CommentsMon, 23 Jul 2018 09:59:23 GMTPyRSS2Gen-1.0.0http://blogs.law.harvard.edu/tech/rssSam - 2017-12-07 04:33:33http://www.mikeash.com/?page=pyblog/fluid-simulation-for-dummies.html#commentsI haven't read the whole thing yet, but the pointers are confusing me a bit. Why initialize a pointer rather than a value?0d849a3aa790be86702d17c198d8d11dThu, 07 Dec 2017 04:33:33 GMTScott - 2015-08-23 23:47:11http://www.mikeash.com/?page=pyblog/fluid-simulation-for-dummies.html#commentsMike, Do you remember why you need to multiply 'a' by '(N-2)^2 ' in the diffuse function? For example Stam does this in 2D, so I'm expecting it to be '(N-2)^3' for 3D?175fbdecc0a84d22dc10176a022daf36Sun, 23 Aug 2015 23:47:11 GMTEWD - 2014-05-31 18:42:54http://www.mikeash.com/?page=pyblog/fluid-simulation-for-dummies.html#commentsCan anybody explain to me why in 2006 people still use variable names such as "b", "N" or "p"??? Are they trying to save the compiler the work with reading long words?? Were the variables called 'shit' and 'fuck', it would still be more helpful!
<br />
<br />But reading some of the comments here, some people deserve it.
<br />
<br />bob: Yeah, N*N*N elements for a 3-dimensional array is a terrible waste. Try to allocate N elements for a 3-dimensional array.1af60c1977536d500e3afc5d7ae71a65Sat, 31 May 2014 18:42:54 GMTbob - 2011-07-19 18:24:17http://www.mikeash.com/?page=pyblog/fluid-simulation-for-dummies.html#commentsWhy do you allocate N*N*N size for each dimension? This becomes impractical when work with large amounts of particles. For example,
<br />sizeof(float) * 8 callocs * (1000 particles ^ 3) = 32 gb.c7bea71b8d05cd380d67370e2d9edfacTue, 19 Jul 2011 18:24:17 GMTMartin - 2011-03-29 15:48:51http://www.mikeash.com/?page=pyblog/fluid-simulation-for-dummies.html#commentsI had a deep look at the advect function and ask myself if it's correct to call it with the N-value. Shouldn't this be N-2 (or Nflaot be initialized by N-2)?
<br />
<br />Anyway, thanks a lot for the code, it's really valuable for learning how this stuff works!
<br />2f4cbeb03ca90f339ba6ec442cd0cb38Tue, 29 Mar 2011 15:48:51 GMTlibero - 2011-02-04 21:45:29http://www.mikeash.com/?page=pyblog/fluid-simulation-for-dummies.html#commentsdoes anyone have a working main() for this code because seems to be crashing and I cant find the reason for that? any help you be appreciated.adad331c79d7ab67f53724e83690d940Fri, 04 Feb 2011 21:45:29 GMTAkash Verma - 2010-10-23 21:31:08http://www.mikeash.com/?page=pyblog/fluid-simulation-for-dummies.html#commentsCould someone please put up a main() for this to see it working?0287702b37ff2d99a9a0a17543a305b5Sat, 23 Oct 2010 21:31:08 GMTAaron Moffatt - 2010-06-23 09:01:50http://www.mikeash.com/?page=pyblog/fluid-simulation-for-dummies.html#commentsVery, very useful document. Just to the point, outlining and giving example code of the key concepts. Really appreciate you writing and posting this.c69cde36a16443fa6dce28d070d3dc27Wed, 23 Jun 2010 09:01:50 GMTuma sankar pradhan - 2010-04-02 22:13:04http://www.mikeash.com/?page=pyblog/fluid-simulation-for-dummies.html#commentsthe code is great.
<br />But in the projection method,
<br />during the calculation of divergence of velocity field,why your dividing it by N.I suppose ,it shoud be multiplied as dx=dy=dz=1/N
<br />div[IX(i, j, k)] = -0.5f*(
<br /> velocX[IX(i+1, j , k )]
<br /> -velocX[IX(i-1, j , k )]
<br /> +velocY[IX(i , j+1, k )]
<br /> -velocY[IX(i , j-1, k )]
<br /> +velocZ[IX(i , j , k+1)]
<br /> -velocZ[IX(i , j , k-1)]
<br /> )*N;
<br />instead of
<br />div[IX(i, j, k)] = -0.5f*(
<br /> velocX[IX(i+1, j , k )]
<br /> -velocX[IX(i-1, j , k )]
<br /> +velocY[IX(i , j+1, k )]
<br /> -velocY[IX(i , j-1, k )]
<br /> +velocZ[IX(i , j , k+1)]
<br /> -velocZ[IX(i , j , k-1)]
<br /> )/N;
<br />One more thing,
<br />the Vx0,Vy0 need to be initialised to zero before each iteration.
<br />I may be wrong.If i am wrong,kindly inform me.
<br />
<br />974dbf13c337cadbe264b3b2b7a36f96Fri, 02 Apr 2010 22:13:04 GMTfluid - 2009-10-09 10:33:03http://www.mikeash.com/?page=pyblog/fluid-simulation-for-dummies.html#commentsreally cool fluid simulation is here: <a href="http://www.escapemotions.com/experiments/fluid_fire_3/index.html">http://www.escapemotions.com/experiments/fluid_fire_3/index.html</a>3d52bd18092e61ef598411ef7e3919a3Fri, 09 Oct 2009 10:33:03 GMTKent - 2009-07-23 15:38:22http://www.mikeash.com/?page=pyblog/fluid-simulation-for-dummies.html#commentsThanks heaps for posting this code. I was just about to start moving Stams code from 2D to 3D myself. And I really like the way you have presented the code here. Nice work!2297a1c8125a4877ce476449f4aeab76Thu, 23 Jul 2009 15:38:22 GMTwillem - 2009-05-14 06:34:43http://www.mikeash.com/?page=pyblog/fluid-simulation-for-dummies.html#commentsgreat to see it written for a non-mathematician. thanks8eaf15fc44831b5121deb6d414688c26Thu, 14 May 2009 06:34:43 GMTstudent - 2009-05-10 06:36:10http://www.mikeash.com/?page=pyblog/fluid-simulation-for-dummies.html#commentsIm around this same code the 3d code you provide was very helpfull thanks.
<br />Im hava used several linear solvers gauss seidel relaxation (seq and parallel - Red Black), jacobi (seq and parallel) ....
<br />I'm trying to implement the CG method also because I adjusted the stams code to allow internal boundaries (moving or not), among other things.
<br />I have a pertinent question: I understood after considereble digging that we are solving a system of linear equations in the form Ax=b however what confuses me is what is A exactly?
<br />
<br />Any comments or regards on this would be quitte helpfull anyone?
<br />
<br />responses to: a14722@ubi.pt 79948841688a372e57cb8d276179ccf4Sun, 10 May 2009 06:36:10 GMTmikeash - 2009-04-14 02:33:14http://www.mikeash.com/?page=pyblog/fluid-simulation-for-dummies.html#commentsThere is no main function because it's a discussion of techniques, not a full working example. You will have to incorporate what is provided into your own code. If you know little C then an application of this complexity is probably not a good place to start. You need to learn to walk before you can run.dc9112748f6dbcf9eb301e4e9a3da614Tue, 14 Apr 2009 02:33:14 GMTTom - 2009-04-14 02:15:33http://www.mikeash.com/?page=pyblog/fluid-simulation-for-dummies.html#commentsWhy is there no main function?
<br />actually i know little about c, but i wanna learn this application and apply it into processing. b927e7cc0d41087398694ddefa6a1ff0Tue, 14 Apr 2009 02:15:33 GMTnabeel - 2009-04-03 16:20:54http://www.mikeash.com/?page=pyblog/fluid-simulation-for-dummies.html#commentsGreat stuff. Wonder why they never make sound this easy in texts. wonder if i can work out mixing in two phases with this???1b373fa7e49d4969f54c1a2988587b30Fri, 03 Apr 2009 16:20:54 GMTLars - 2008-12-17 05:12:52http://www.mikeash.com/?page=pyblog/fluid-simulation-for-dummies.html#commentsThanks very much for the 3D conversion of Jos Stam's 2D code -- it has saved me work and brainache. :-)
<br />I was hoping to make a fun screensaver and this should give me a good boost.efbff884cd89c3126ba9cec528b1b603Wed, 17 Dec 2008 05:12:52 GMTIvan DeWolf - 2008-09-17 10:25:18http://www.mikeash.com/?page=pyblog/fluid-simulation-for-dummies.html#comments
<br />here is another website with a really legible java implementation in 2d... be sure to look at the source code...
<br />it also uses a gauss-seidell relaxation scheme for it's linear solver. I really wish I understood the linear solver part more.
<br />
<br /><a href="http://www.multires.caltech.edu/teaching/demos/java/stablefluids.htm">http://www.multires.caltech.edu/teaching/demos/java/stablefluids.htm</a>
<br />d408071dee7b935fe3fecdeb60de53eaWed, 17 Sep 2008 10:25:18 GMTSetis - 2008-06-16 20:40:59http://www.mikeash.com/?page=pyblog/fluid-simulation-for-dummies.html#commentsAbout the rendering:
<br />
<br />Since you know how to take a 2d slice, showing the density of ink, you can render it in 3d this way:
<br />
<br />Take n 2d slices in a given time. Draw the slices in textures, so the more ink in a given cell, the less transparent and more colored is the corresponding pixel. Now draw a 3D cube of all the slices stacking. That way, you can see the ink moving in the cube. Just add some code for rotating the cube and so.907cd07f2d2308a8903316a05567b971Mon, 16 Jun 2008 20:40:59 GMTanonymous friend - 2008-01-25 03:07:26http://www.mikeash.com/?page=pyblog/fluid-simulation-for-dummies.html#commentsThe lin_solve function simply solve a system of linear equations. Since the matrix of this system is sparse (i.e contain a lot of zeros), this system can be solved efficiently (note that there exist more accurate algorithms) using a Gauss-Seidel algorithm.
<br />
<br />What do we solve exactly ? For the diffusion, we try to find tthe densities which, when diffused backward in time, gives the density we started with. In the case of the projection, it is a little more complicated. We are solving a linear system called a poisson equation. In mathematics, Poisson's equation is a partial differential equation. To make sure that the fluid is incompressible, we need to satisfy the non-divergence condition.
<br />
<br />The divergence of a vector field (the velicity of the fluid)at a particular location is a scalar value that essentially represents how much the field is ‘expanding’ at the point. A positive value represents a divergence and a negative value represents a convergence or compression.If we use the assumption that a fluid is incompressible, then that implies that the divergence of the velocity must be zero everywhere.
<br />
<br />A useful splitting method for solving the incompressible flow equations is the so-called ‘projection method’. In this technique, one first computes a divergent vector field that results from the advection & viscosity terms, and then projects that entire vector field onto the space of non-divergent vector fields
<br />This can be thought of as creating an instantaneous pressure field whose gradient compensates for any divergence caused by the advection & viscosity.
<br />
<br />Getting back to the poisson equation, we create these beast to solve the unknown pressure field. The gradient of this pressure field is an additional force on the fluid and the divergence of this gradient compensates for the divergence resulting from the convection-viscosity equation. The result is a final velocity field that has no divergence.6f48e28ec5771ac309136f0efd4a22b3Fri, 25 Jan 2008 03:07:26 GMTmikeash - 2006-12-12 23:27:00http://www.mikeash.com/?page=pyblog/fluid-simulation-for-dummies.html#commentsBrian, I&#8217;m glad you found it useful, and please do send me your results if you&#8217;d like.
<br />04d858d99d3447770963e778ac71f85bTue, 12 Dec 2006 23:27:00 GMTBrian Silva - 2006-12-02 18:48:00http://www.mikeash.com/?page=pyblog/fluid-simulation-for-dummies.html#commentsNice work! I am doing exactly the same work with Stam&#8217;s implementation, but I was having trouble getting the simulation step to function correctly. I took your methodology from the FluidCubeStep and it solved my problem. So, thank you!<br />
<br /><br />
<br /> I agree with you about the original sources: they are difficult to understand and don&#8217;t explain the math very well. I&#8217;m not entirely mathematically-inclined so I struggled for a long time to understand it. I&#8217;m setting out on a school project to explain the math from this particular implementation in a clear way. If you&#8217;re interested, I can send it to you when I finish.<br />
<br /><br />
<br /> Again, great work!
<br />0f289d8d038fc3e9c6d69c61a5fd0060Sat, 02 Dec 2006 18:48:00 GMTneil - 2006-08-16 08:54:00http://www.mikeash.com/?page=pyblog/fluid-simulation-for-dummies.html#commentsim a newbei and i want to learn more about simulation and animation, but my problem is i cant afford to enroll to schools, thats why im looking for free ebooks and tutorials which is available in this world. but my collections are not enough for my thirsty head. all i want is to learn more, if anyone could share their knowledge and books or sites i dont know yet, please enlighten me, by the way im making some simulation in physics principles or topics to help me in my carrier as future physics teacher, but i still need to learn lots of things, so guys please share your informations i assure you it wont go to waste. thanks. heres my add, <a href="mailto:kaerfmove@yahoo.com, thanks!" rel="nofollow">kaerfmove@yahoo.com, thanks!</a>
<br />d580917a5161e5dc314cdf8148cdebe1Wed, 16 Aug 2006 08:54:00 GMTArtur W - 2006-07-12 14:23:00http://www.mikeash.com/?page=pyblog/fluid-simulation-for-dummies.html#commentsThank you very much! Finally something for a dummy like myself. Less equations and more detail on turning them into actual implementation. <br />
<br />This helps!
<br />e7896d53730c970f62482f2245fc0ae9Wed, 12 Jul 2006 14:23:00 GMTN382AF - 2006-03-15 01:01:00http://www.mikeash.com/?page=pyblog/fluid-simulation-for-dummies.html#commentsNice work here, I saved it for reading later. A bit over my head, but I love learning this stuff!<br />
<br /><br />
<br /> Thanks!
<br />e070c725b1c6708ebaccb394034d3bb5Wed, 15 Mar 2006 01:01:00 GMT