Hacker News new | past | comments | ask | show | jobs | submit login
Fast real time fluid simulator based on MPM algorithm (kotsoft.github.io)
206 points by andrewla 24 days ago | hide | past | favorite | 54 comments



Hi, I made this demo. This is actually all from the paper Particle-based Viscoelastic Fluid Simulation (Simon Clavet, Philippe Beaudoin, and Pierre Poulin). It is an SPH type (particle-particle interactions), not MPM (particle-grid-particle). I do a lot of MPM nowadays, and I have a multi-grid thing to help with incompressibility, but this was me re-implementing the first fluid sim paper I ever implemented.

I did the implementation in JS to help other people reading the paper, and tried to keep everything as similar as possible to the pseudocode from just this single paper. Maybe it would be cool to integrate an additional grid on which incompressibility is enforced better but I didn't want to make the source confusing.

It is also a little difficult to do density ratios with just what is shown in the paper, here the masses are set to (1, .8, .6, .4). This is what causes the lightest particles to get launched so violently in the air. Probably would be useful to integrate some ideas from the paper Density Contrast SPH Interfaces (Barbara Solenthaler, Renato Pajarola).

I started revisiting SPH because I have some new ideas to combine it with an MPM/FLIP grid for closeups. I'm trying to do a multi-scale MPM simulation that can handle better surface tension droplets in closeups while also doing extremely large scale scenes. You can see some larger scale parallel sims on my YouTube: https://www.youtube.com/c/GrantKot


Hey this was also the first fluid sim paper I ever implemented! I haven't done any grid-based simulations (except maybe the classic "Real-Time Fluid Dynamics for Games" by Jos Stam), but I've been able to greatly improve compressibility by using "Position-Based Fluids" by Miles Macklin. Someday I'll dive into WebGPU and post a JS version on HN...

Short clip of my implementation here: https://imgur.com/a/2IARiBq

I like the different fluids in your sim though, I may try that myself.


Very cool! What does the changing color of the fluid indicate, particle velocity?


I believe specific gravity.

Things like "how do you properly make a B-52?" ( http://www.cocktailhunter.com/bartender-guide/specific-gravi... ) or a Pousse Café ( https://youtu.be/4OJd_phsa5w )


Yes, particle velocity! I believe this clip is after I implemented metaballs too, so the particles blend together to make it feel more fluid-like.


I have been following your work on twitter for quite a while. Didn't know you were on YouTube as well. I guess I was doing this since one of these demos https://youtu.be/BCUh1opDGA4


Beautiful and mesmerizing. Well done


It's neat, but also it's unstable and hyper-energetic. Particles go flying off into space for no reason even long after gross motion has settled on mobile. Two particles falling downward past each other impart upward momentum somehow.

Also the fluids appear to be extremely compressible as to appear bouncy, like the whole thing is breathing, so not liquids, but also non-mixing and with surface tension, so not gasses? What fluids behave like this? Some kind of...uh...immiscible foams?


> Particles go flying off into space for no reason even long after gross motion has settled on mobile.

My intuition tells me this is how evaporation works.


In principle yes, but real liquid atoms never "settle down" whereas the ones in the simulation clearly do.

What is going on is that the particles in this simulation represents something more like droplets: huge quantities of atoms grouped together and treated as solid sphere with properties matching some average of the atoms it is supposed to represent.

And water doesn't evaporate a droplet at a time, thus evaporation is something that this type of simulation cannot reproduce.


Except that it doesn't happen like evaporation at all, and it happens differently on mobile vs desktop.


My problem with it is that the substances coalesce into bubbles, but not into layers depending on weights.


They do actually. Red > orange > green > blue. Let it settle.


But there are still bubbles within layers, no?


I'm not sure if the between layers thing might be a similar thing to when bubbles show up on the sides and bottom of a glass of water. Some of the bubbles are also staying because once it's settled down, there isn't enough action to cause them to pop. The constant associated with the surface tension in the sim can also be turned down.


They appear more like a sort of gel, if you ask me. Also, some of the heavier fluids actually freeze into cryshals.


This is true of all non-physically accurate fluid simulations which are essentially all of them.


Let me highlight some positive things about this:

Love the interesting density stratification and the little bubbles that move around. Very foamy or multi-fluid looking.

Not sure if it's intentional but the fact a click/touch only sort of locks the nearby particles is very satisfying. The fact I can slowly absorb particles into the click region, that the particles still engage in the slow, sticky motion and evolve inside the region and that I can then fling them off is bizarrely good.

Sure it could use a bit of optimisation and tweaking to make it move and look more like a fluid but I think this is a fantastic proof of concept of this particular method as an online, interactive example.


The IOCCC also featured a very concise fluid simulator [1] [2], which can be seen in action in this video [3].

[1] https://www.ioccc.org/2012/endoh1/endoh1.c

[2] https://www.ioccc.org/2012/endoh1/hint.html

[3] https://www.youtube.com/watch?v=QMYfkOtYYlg


IMHO it behaves too much like a particle system. Even from the start, individual particles seem to pass through what should be a large pool of fluid at the bottom. Interesting, but doesn't seem physically realistic.



You probably also want to check the project from the same author that has more control sliders and code in human readable format instead of compiled wasm.

Demo : https://kotsoft.github.io/particle_based_viscoelastic_fluid/

Code : https://github.com/kotsoft/particle_based_viscoelastic_fluid...

Video :https://www.youtube.com/watch?v=3OxC4oqy74U

edit : you probably also want to check one of the mentioned link : https://matthias-research.github.io/pages/tenMinutePhysics/i...


Oh wow, great, thank you!


For the most relaxing thing you will do today - try to get the different layers to be homogeneous by perturbing them slightly with the cursor.


Nice. I like how it's full screen and responds fluidly to changing the browser window size.

Cool how it pauses when switching browser tabs. My RTX-3080* hums a little louder during the sim but not too much. I'm getting about 57 fps @ 3440x1440. *Actually probably my CPU fan doing the humming.


Thanks. It's a WebAssembly simulation so mostly CPU and very light on the GPU because it's just drawing points. It will actually run on mobile too with multitouch.



This one looks much better, and it seems to better model the stickiness of the water (though maybe the scale is different).


For someone with high-school level understanding of classical Newtonian mechanics, what would be the best way forward to learn Continuum Mechanics and things like MPM, etc.? Any book/course recommendations?




Continuum mechanics in itself is a very different subject from particle-based methods to approximate continuum mechanics, like MPM.

To actually learn continuum mechanics you need first a good understanding of vector calculus. But this is not necessary at all for using and tweaking particle based methods.


I see. What are the pre-requisites for understanding and playing around with MPM then? It's very hard to figure this out as someone who hasn't studied physics for a decade.


Disclaimer: I'm not an MPM expert at all, but I work in computational fluid dynamics.

I would suggest to start with the paper I link to below. First skim read it to understand the structure. Then try to follow in detail the parts that interest you most, and play with the code they provide. If you encounter concepts you don't understand, which are necessary for progressing, go look those up on Wikipedia or Google Books (for textbooks).

https://www.researchgate.net/publication/336796234_Material_...

And don't be discouraged by the math notation. It's mostly just shorthand for things you probably understand very well in code.

For example the capital sigma (sideways M) with a letter "p" or something underneath simply means "loop over the range of the index p, and sum up the following expression for the different values of the index".

The capital delta (triangle pointing up) means "difference", for example delta-t is the increment from one time step to the next.

The upside-down delta is the Nabla, which means gradient, which means derivative in each spatial dimension.


Thanks!


Sort of looks like the weird stuff on the TV in Vivarium


Keys 1-4 spawn new material at the mouse location


Very cool, could see something like this being in a passive display.

Interestingly, this code is from 2012! I just assumed it was something new.


I just want something like this for a pygame I'm making. But not very compue intensive.


Heightfield fluid sims are much less compute intensive. https://dl.acm.org/doi/10.1145/97880.97884

You will trade away details, so this depends entirely on what you need for visuals and interactivity; heightfield fluids won’t give you splashes or bubbles or swirling. You could try to mix a heightfield sim on most of the bottom with a thinner SPH layer on top, but that might be treading into research territory.


Fun that it supports multi-touch!


Trying to sort the particles into single color blobs is pretty fun!


Whoa also changing window size affects the simulation too!!


Nice, I feel splashy.

You can grab a chunk of the goo, drag it up and let it fall.


Maybe I'm spoiled, but I expected a 3-D simulation.


Apart from 3D being harder to implement and slower to solve, it's also quite tricky to visualize.

You can't do like in the examples linked in this discussion and simply plot every (Nth) point, that mainly just dissolves into visual chaos. So you have to do 2D slices, or isosurfaces or streamlines or 3D view of a few tracer particles, preferrably combined with animated rotation of the system.


Change your window size and watch the fireworks!



1. Doesn't look like fluid at all 2. Goes down to 40 FPS on my computer

Granted this is web, but I think I can simulate that many particles with a simple loop in java on 60 FPS.


1. It does to me, but then again:

2. Goes down to 15 FPS on my old laptop.

3. The proof is in the pudding: please post the link to your Java code.


this is absolutely fantastic. i wonder what the source code looks like


Salad dressing!


so satisfying, thank you


cool stuff




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: