Inviscid and incompressible Navier-Stokes equations in 2D implemented in Julia and compiled to WASM

I recently made an online demo of the 2D Navier-Stokes equation running in a web-browser.

https://alexander-barth.github.io/FluidSimDemo-WebAssembly/

My kids like to play with it to build dams and other obstacles in the flow which you can do via the mouse :slight_smile:

I took some notes here to get the WASM part to work:

Basically, I allocate all arrays in JavaScript and use the array type from StaticTools.jl (MallocArray). GPUCompiler is used to generate the WASM output. Only a small subset of julia can currently be used this way, but enough for fluid simulations. The upside is that the WASM binary is very small and the demo loads quickly.

34 Likes

Very impressive!

1 Like

Thats a pretty slick demo! Good to see how much you can do already in WASM.

Now if we can somehow integrate this with WGLMakie.jl…

2 Likes

Yes that would be awesome! But that won’t be for tomorrow… Currently only elementary math operations work. Named tuples worked too because they could be optimised-out after in-lining all function calls.

1 Like

What is CPUCompiler? or do you mean GPUCompiler.jl?

1 Like

yes, indeed. I corrected the typo. Thanks!
(ironically, nothing runs on the GPU actually).

1 Like

Here is another example of the shallow water equations (for example long waves on the surface of the ocean, like tides)

https://alexander-barth.github.io/FluidSimDemo-WebAssembly/ShallowWater/

4 Likes

That is interesting. Do you know what sort of fakery is involved in transferring the field to the new domain after a hole had been made?

When you make a hole, the corresponding grid cells are set to zero velocity and zero surface elevation which creates these kind of shock waves that you are seeing to adjust the flow field.

1 Like

I was dreaming of some Julia/WASM project like that. Thanks you so much for sharing, I will study your code in detail now :slight_smile: (I hope I can use a similar strategy for my projects too.)

I was about to go to sleep :zzz: Now I cannot sleep. :boom:

1 Like

To me the most difficult part is to figure-out how to pass data between javascript and julia… But this example might be useful then.

Yes these waves look a bit hypnotic sometimes :slight_smile:

1 Like