LBM Fluid Simulation in Python with JAX | van Karman Vortex Street

Поделиться
HTML-код
  • Опубликовано: 10 сен 2024

Комментарии • 90

  • @MachineLearningSimulation
    @MachineLearningSimulation  2 года назад +6

    Important remarks regarding what I said in the video (see also @Paulo Dutra's comment):
    1. LBM does not solve the incompressible Navier-Stokes equations, but the compressible version. However, we are in the below 0.3 Mach regime which yields to small density fluctuations making the fluid appear almost incompressible.
    2. The stability of the scheme is not linked to the Reynolds number, but rather to the Mach number. However, here (under unchanging physical dimensions) higher Reynolds numbers lead to locally higher velocity and therefore locally higher Mach numbers. This causes the instabilities at Reynolds numbers > 350.

  • @MachineLearningSimulation
    @MachineLearningSimulation  2 года назад +9

    There are some nice additions to the Python script from the audience that I merged into the channel's GitHub Repo. Check out the following:
    - An extension to 3D: github.com/Ceyron/machine-learning-and-simulation/blob/main/english/simulation_scripts/contrib/D3Q19_lattice_boltzmann_method_python_jax.py (also available on Colab: colab.research.google.com/drive/1F3EH9_2N3lkEpgQXOScR3lcQ6oqCARPk?usp=sharing )
    - An extension which computes the forces on the sphere in 3D: github.com/Ceyron/machine-learning-and-simulation/blob/main/english/simulation_scripts/contrib/D3Q19_lattice_boltzmann_method_stress_force_drag.py (also available on Colab: colab.research.google.com/drive/1F3EH9_2N3lkEpgQXOScR3lcQ6oqCARPk?usp=sharing )
    - An extension that uses a Multiple Relaxation Time (MRT) scheme: github.com/Ceyron/machine-learning-and-simulation/blob/main/english/simulation_scripts/contrib/D3Q19_lattice_boltzmann_method_MRT.py
    - An extension that showcases unit conversion: github.com/Ceyron/machine-learning-and-simulation/blob/main/english/simulation_scripts/contrib/D3Q19_lattice_boltzmann_method_unit_conversion.py

  • @EarlWallaceNYC
    @EarlWallaceNYC 2 года назад +2

    Great introduction to LBM and JAX. Thanks.

  • @pawpawpascal1524
    @pawpawpascal1524 Год назад +1

    Your videos are fantastic, I've wanted to move beyond COMSOL this year because it's always been a black box. Everytime I come across a method I want to try (FDM, SPH, LBM...) You have a brilliant easy to understand video, great work!

    • @MachineLearningSimulation
      @MachineLearningSimulation  Год назад +2

      Thanks 🙏
      It's great to hear you find the videos helpful. I hope the videos are a great introduction. Of course they are not yet as sophisticated was what you would be able to simulate with COMSOL, but at least give a good starting point 👍

  • @curious_tee
    @curious_tee 2 года назад +1

    Amazing work! Thank you for your contributions to the field and hope this great work continues!

    • @MachineLearningSimulation
      @MachineLearningSimulation  2 года назад +1

      Thanks for the great feedback

    • @curious_tee
      @curious_tee 2 года назад +1

      @@MachineLearningSimulation That's very excited; I personally very much look foward to it! All the best!

  • @ademirxavierjr
    @ademirxavierjr Год назад +1

    Stunning video and a very useful presentation!

  • @idreeskhan-zp5ey
    @idreeskhan-zp5ey 7 дней назад

    Thanks for the effort. I am planning to convert my Fortran code for LBM DHIT to python. For one GPU parallelization JAX or Lettuce is preferable?

  • @sniperdogruffo
    @sniperdogruffo 2 года назад +3

    Have you looked at the time-dependent Ginzburg Landau equation that models vortices in superconductors? I think there's definitely some cross-talk between fluid dynamics and SC vortex dynamics. Tetsuyo Matsuno's videos have some beautiful simulations that he runs in Java, unfortunately he does not share the code. I think you'd find that field very interesting!

    • @MachineLearningSimulation
      @MachineLearningSimulation  2 года назад

      Thanks a lot for the suggestion. I will take a look at it. :)
      Do you happen to have a link to one of the videos, you are talking about?

    • @sniperdogruffo
      @sniperdogruffo 2 года назад +1

      @@MachineLearningSimulation ruclips.net/video/85Yb3a1R1GI/видео.html

    • @MachineLearningSimulation
      @MachineLearningSimulation  2 года назад

      @@sniperdogruffo Thanks :)
      I will take a look at it

  • @archi_designer
    @archi_designer Год назад +2

    how to use custom shapes instead of cylinder?

    • @MachineLearningSimulation
      @MachineLearningSimulation  Год назад

      You can adapt the obstacle mass to fit your particular shape, e.g., if you have your shape as a signed distance function, this should be relatively straightforward. Just make sure that there are enough mesh points between the features of your shape to allow "fluid to pass through the gap".
      Good luck 😊

  • @AJ-et3vf
    @AJ-et3vf 2 года назад +1

    Awesome video! Thank you!

  • @DiogoSanti
    @DiogoSanti 11 месяцев назад +1

    You are a bless dude!

  • @jrfriggo
    @jrfriggo Год назад +2

    Amazing video, great explanation!
    I'm using a code based on this one in my research for the past couple months (And it has worked just fine!), but I do have a question about the implementation of the Boundary Conditions:
    Why do you set the Neumann BC in the outflow as f(-1, : , left velocities) = f(-2, : , left velocities) and in the inflow you set it as f(0, : , right velocities) = f_eq(0, : , right velocities)? This is not what is written in the original article by Zou-He "On pressure and velocity boundary conditions for the lattice Boltzmann BGK model", Physics of Fluids, 1998. As I have understood (with a great help of Amit Gupta in the video LBM Lecture 13: Boundary conditions in finite-domains (part 1)), one should set:
    AT THE INFLOW
    f1 = f3 + (2/3)*ux_inflow*macroscopic_density
    f5 = f7 + 0.5*( f4 - f2 )+(1/6)*ux_inflow*macroscopic_density + (1/2)*uy_inflow*macroscopic_density
    f8 = f6 - 0.5*( f4 - f2 )+(1/6)*ux_inflow*macroscopic_density - (1/2)*uy_inflow*macroscopic_density
    AT THE OUTFLOW
    f3 = f1 + (2/3)*ux_inflow*macroscopic_density
    f7 = f5 + 0.5*( f4 - f2 ) + (1/6)*ux_inflow*macroscopic_density + (1/2)*uy_inflow*macroscopic_density
    f6 = f8 - 0.5*( f4 - f2 ) + (1/6)*ux_inflow*macroscopic_density - (1/2)*uy_inflow*macroscopic_density
    However, when I run it this way without using the f(right) = f_eq(right) line it does not work (It diverges quickly!) So I'm confused, cause your code is great and it works just fine for me but it is not accordingly with the article. What have I done wrong?

    • @MachineLearningSimulation
      @MachineLearningSimulation  Год назад +2

      Thanks for the comment and the kind feedback :)
      I replied to your message over on LinkedIn. Your concern regarding the implementation of the BC is very valid. I will update the thread once we found more insights.

    • @TheGodofwarghost
      @TheGodofwarghost 10 месяцев назад

      @@MachineLearningSimulation Hi! I've been working on implementing a LBM simulation from scratch and I encountered your videos. However I have the same question, I can't understand why you're using those specific inflow BCs. I'd be grateful if you could clarify. Thanks!

    • @advaitmenon2937
      @advaitmenon2937 6 месяцев назад

      ⁠​⁠@@MachineLearningSimulationhey, I’m not sure, but I was just referring to the text by Kruger on handling velocity dirichlet boundary conditions for wet nodes. I think you’ve used the “equilibrium” scheme to replace the unknown distributions (right discrete velocities) at the boundaries instead of the Non Equilibrium Bounceback (Zou-He) method. Again, I’m not sure this is exactly what has happened, but I think it is.

    • @advaitmenon2937
      @advaitmenon2937 6 месяцев назад

      Are you calculating the wall macroscopic densities for the boundaries first (calculated using the unknown distribution functions)? Because those are the ones used for the Zou He scheme right? If the density is calculated just using the get_density function, it would just evaluate the density based on the addition of known distribution functions

    • @advaitmenon2937
      @advaitmenon2937 6 месяцев назад

      Never mind… just realized that the rho wall was updated in the code anyway 😅

  • @PauloDutra
    @PauloDutra 2 года назад +2

    The LBM actually stimulates compressible fluids! *But it isn't stable at mach > 0.4 usually...

    • @MachineLearningSimulation
      @MachineLearningSimulation  2 года назад +1

      Hey,
      thanks for the feedback and clarification. You are right, it should have been clearer in the video.
      Regarding compressibility: In my understanding, the fluid speeds are lower than the 0.3 Mach limit and can be considered incompressible. Practically, the employed D2Q9 of course uses compressibility (as can be seen by the computation of macroscopic density throughout the algorithm). Therefore, it could be said that the governing equations (at least in the macroscopic limit) are the compressible Navier-Stokes equations, but we are solving them in the incompressibility regime (

    • @PauloDutra
      @PauloDutra 2 года назад +1

      @@MachineLearningSimulation Yeah, you can consider the fluid incompressible in these Regimes... Its like saying that Water is incomprehensible :) About te mach stability I was talking more about mach >= 1 (for aerodynamic sims...) But yeah 1/√3 is guaranteed stability 👏 Great Video and explanation

    • @Masum912
      @Masum912 2 года назад +1

      LBM is stable upto mach 2.0

  • @itszeen7855
    @itszeen7855 Год назад +1

    Fantastic video! How would you make the fluids bounce off the rectangular boundary instead of going through one side and popping up on the opposite side?

    • @MachineLearningSimulation
      @MachineLearningSimulation  Год назад

      Hi, you're very welcome. Thanks for the kind comment ☺️
      I think the easiest way would be to employ a bounce back boundary condition similar to what was done on the circular obstacle.

  • @Martin-iv5rp
    @Martin-iv5rp Год назад +1

    thanks for this video, it helps me a lot. but i have a question, how to plot the streamline?? thanks

    • @MachineLearningSimulation
      @MachineLearningSimulation  Год назад

      Thanks a lot :). I am happy that I could help.
      Check out this video of mine, in which I create a streamplot in matplotlib: ruclips.net/video/BQLvNLgMTQE/видео.html
      Once you have the macroscopic quantities you could plug them into the same matplotlib routine.
      However, experience shows that sometimes MPL has a hard time with streamlines due to some trajectories clipping at the boundary. In that case, it could be helpful to use plotly: plotly.com/python/streamline-plots/ as I experienced it to be more robust.

  • @devendrapatil6719
    @devendrapatil6719 Год назад +2

    Really an amazing explanation with lots of information. I would like to know how we can print the density or velocities values. I tried using printing but it ends up showing the location. It would be really helpful if anyone could tell me how can i print density or any other quantities. Thanks.

    • @MachineLearningSimulation
      @MachineLearningSimulation  Год назад

      Hi, thanks for the comment :). I am bit unsure, what you refer to, but if you just want to inspect the JAX array, calling print(...) on it should do the job?

    • @devendrapatil6719
      @devendrapatil6719 Год назад

      @@MachineLearningSimulation Thank you for your quick reply.
      Say for example if I print(get_density) it shows me output as "" which is the location where density code is written. If I try for print(get_density()) it asks for arguments which is discrete_velocities for density. So my question is if I want to see the array of density or velocity or any other quantity which we have calculated, how can we print. Am i using the syntax wrong or is there anything else.
      Thanks.

    • @MachineLearningSimulation
      @MachineLearningSimulation  Год назад

      ​@@devendrapatil6719 You're welcome :)
      The result you get with "print(get_density)" is as expected, as Python tells you that you try to print out a function object.
      Since we defined the get_density function with a certain input signature, i.e., that it takes a certain number of positional arguments, you should be calling it with those. Try "print(get_density([THE VELOCITY ARRAY]))" and replace [THE VELOCITY ARRAY] with the variable name you chose for it.

    • @devendrapatil6719
      @devendrapatil6719 Год назад +1

      @@MachineLearningSimulation once again thanks for your help. I was able to print all the data. In addition i would like to know, is there any literature or paper you would suggest to study zou/he boundary condition. In addition, If I have to improve the convergence order by grid refinement, how can i proceed? Do we have to change the Lattice coordinates to 1/2 like (1, 1) will be (1/2, 1/2)? so that my grid will become half the size.

    • @MachineLearningSimulation
      @MachineLearningSimulation  Год назад

      ​@@devendrapatil6719 You're welcome :)
      The standard reference for the theory of LBM is "the lattice boltzmann method principles and practice" by Timm Krüger et. al. This is probably a good first step to find further information. Good luck with learning such an interesting (and different from classical Eulerian approaches) simulation technique to fluid flow :)

  • @xianxuhou4012
    @xianxuhou4012 2 года назад +1

    Great explanation. I am just confused about the Inflow BC with the so-called Zou/He scheme and It is not very intuitive.

    • @MachineLearningSimulation
      @MachineLearningSimulation  2 года назад

      Thanks for the comment. 😊
      I can understand the confusion with the Zou/He scheme. It was rather quick in this video and probably needs some more elaboration.
      If you want more details, please also check out "the lattice Boltzmann method" by Timm Krüger.

    • @jrfriggo
      @jrfriggo Год назад

      Amazing video, great explanation!
      I'm using a code based on this one in my research for the past couple months (And it has worked just fine!), but I do have a question about the implementation of the Boundary Conditions:
      Why do you set the Neumann BC in the outflow as f(-1, : , left velocities) = f(-2, : , left velocities) and in the inflow you set it as f(0, : , right velocities) = f_eq(0, : , right velocities)? This is not what is written in the original article by Zou-He "On pressure and velocity boundary conditions for the lattice Boltzmann BGK model", Physics of Fluids, 1998. As I have understood (with a great help of Amit Gupta in the video LBM Lecture 13: Boundary conditions in finite-domains (part 1)), one should set:
      AT THE INFLOW
      f1 = f3 + (2/3)*ux_inflow*macroscopic_density
      f5 = f7 + 0.5*( f4 - f2 )+(1/6)*ux_inflow*macroscopic_density + (1/2)*uy_inflow*macroscopic_density
      f8 = f6 - 0.5*( f4 - f2 )+(1/6)*ux_inflow*macroscopic_density - (1/2)*uy_inflow*macroscopic_density
      AT THE OUTFLOW
      f3 = f1 + (2/3)*ux_inflow*macroscopic_density
      f5 = f7 + 0.5*( f4 - f2 ) + (1/6)*ux_inflow*macroscopic_density + (1/2)*uy_inflow*macroscopic_density
      f6 = f8 - 0.5*( f4 - f2 ) + (1/6)*ux_inflow*macroscopic_density - (1/2)*uy_inflow*macroscopic_density
      However, when I run it this way without using the f(right) = f_eq(right) line it does not work (It diverges quickly!) So I'm confused, cause your code is great and it works just fine for me but it is not accordingly with the article. What have I done wrong?

  • @luisbarba9532
    @luisbarba9532 Год назад +1

    Very interesting! Could this be extended to incorporate an STL?

    • @MachineLearningSimulation
      @MachineLearningSimulation  Год назад

      Thanks 😊
      I don't know what a STL is, but generally speaking, LBM is a flexible approach, so probably yes.

  • @madhurjyamahanta6145
    @madhurjyamahanta6145 11 месяцев назад +1

    I want to learn lbm method. Please help how to start learning.

    • @MachineLearningSimulation
      @MachineLearningSimulation  11 месяцев назад +1

      Hi, I can recommend the book "the lattice Boltzmann method" by Krüger et al.
      Generally speaking, with all numerical methods, I can recommend to start coding them as early as possible to be able to play around with all components. You can, for instance, search on GitHub for sample codes 😊

    • @MachineLearningSimulation
      @MachineLearningSimulation  11 месяцев назад

      Good luck on your learning journey.

  • @arminamani8503
    @arminamani8503 9 месяцев назад

    Hi thank you very much for this where can I find this simulation method on an airfoil I really want the lbm simulation flow on an airfoil in python

    • @MachineLearningSimulation
      @MachineLearningSimulation  8 месяцев назад

      Hi, thanks for the kind comment 😊
      If you look for example code, I can recommend searching on GitHub.

    • @arminamani8503
      @arminamani8503 8 месяцев назад

      Hello and courtesy
      Yes, I need a similar code that simulates an airfoil instead of a circular cylinder with lbm.
      I will be very grateful to you for leaving the links in the comments.

  • @mrbenson63
    @mrbenson63 2 года назад +1

    Great, do LBM works on an arbitrary geometry (not a box) ?

    • @MachineLearningSimulation
      @MachineLearningSimulation  2 года назад

      Thanks :).
      I'm pretty sure they do, but I do not have experience with that. I would advise you to check out some literature like Timm Krüger's "Lattice Boltzmann Method"

    • @Masum912
      @Masum912 2 года назад

      yeah they do work on arbitrary geometry. You can do voxelization of geometry STL file to the domain and solve fluid simulation

  • @xGRANxTORINOx
    @xGRANxTORINOx 2 года назад

    Great video, learned a lot. I just have two questions. If I wanted to add the current time to the velocity and vorticity graphs, how would I go about that? And also what would need to be improved in the code to allow for higher mach numbers?

    • @MachineLearningSimulation
      @MachineLearningSimulation  2 года назад

      Thanks a lot :) You're very welcome.
      One way you could add the current time to the graphs is using a super title, e.g., right before the plt.draw write "plt.suptitle(f"current iteration = {iteration_index:06d}")"
      There are ways to go to higher Mach numbers, but I am not too deep into LBM, unfortunately. Techniques include, for instance, Multiple Relaxation Time (MRT) methods. I would refer you to the Lattice Boltzmann Method book by Krüger for more details ;)

    • @MachineLearningSimulation
      @MachineLearningSimulation  2 года назад

      There has just been an extension of the code for MRT schemes by someone from the community: github.com/Ceyron/machine-learning-and-simulation/blob/main/english/simulation_scripts/contrib/D3Q19_lattice_boltzmann_method_MRT.py
      Maybe you find this helpful :)

  • @carlosandresdelvalle9532
    @carlosandresdelvalle9532 Год назад

    Truly, a great video. Thanks. I saw that you extended the script with multiple relaxation times and 3D. Are you planning on doing a video explaining the upgrade to 3D and the multiple relaxation? Also, are you planning to implement an entropic lattice boltzman?
    About Jax I wanted to ask, does the code run on a GPU if I have one?
    Thanks.

    • @MachineLearningSimulation
      @MachineLearningSimulation  Год назад +1

      Hi, thanks for the comment and the kind feedback! :) Much appreciated.
      The extensions to 3D and MRT schemes was actually done by someone from the audience, so the credit goes to him ;).
      Definitely, I want to showcase more advanced cases (and also the theory) of LBM methods, but probably in the longer future. For now, I think it might be more introductory to work with Finite Difference Methods. But, eventually, I will come back to LBM, that's for sure :)
      I think it should run on the GPU out of the box, but I have not tested that particular script. You can give it a try on Colab?

    • @carlosandresdelvalle9532
      @carlosandresdelvalle9532 Год назад +1

      @@MachineLearningSimulation I tried it in Colab and saw a big speedup (X5) with GPU. I'm really looking forward for more Lattice boltzman videos. I also like very much that you try to make performance tips. Keep up the good work!

    • @MachineLearningSimulation
      @MachineLearningSimulation  Год назад +1

      @@carlosandresdelvalle9532 Nice, that's great to hear :). I like that JAX easily allows for switching the computational backend.
      Will definitely come, stay tuned :)

    • @carlosandresdelvalle9532
      @carlosandresdelvalle9532 Год назад +1

      @@MachineLearningSimulation I'm sorry to border you with one more question. I was trying to change the periodic boundaries to outflow, just to test some things but dint manages to make it work. Is this possible? How can I do such a thing? To do what we have on the in and out flow I need a Dirichlet condition to override the periodic nature of jnp.roll. What if I want all boundaries to be outflows? Could that be? How?

    • @MachineLearningSimulation
      @MachineLearningSimulation  Год назад

      ​@@carlosandresdelvalle9532 These are quite some questions :D. I think it's best to refer you to Tim Krüger et. al. book "The Lattice Boltzmann Method". If you have specific questions about the 3D extensions etc. you might also want to reach out to the contributor, e.g., check the PR here: github.com/Ceyron/machine-learning-and-simulation/pull/3

  • @aderachmatjoharpangestu9161
    @aderachmatjoharpangestu9161 Год назад

    sir, how to make the obstacle to be a rectangle and there are more than one obstacle? thanks

    • @MachineLearningSimulation
      @MachineLearningSimulation  Год назад

      You just have to adjust the obstacle mask. The mask is a boolean arrays. Just set the particle mesh points to True which should belong to your obstacle.

    • @aderachmatjoharpangestu9161
      @aderachmatjoharpangestu9161 Год назад

      @@MachineLearningSimulation i finally make it sir, thanks

  • @pablobotin9974
    @pablobotin9974 8 месяцев назад

    Awesome!how do you set Re? Can I see thecode?

    • @MachineLearningSimulation
      @MachineLearningSimulation  8 месяцев назад

      Thanks! :)
      You find the code linked in the video description. The Re is used to compute the kinematic viscosity which then affects the relaxation factor.

  • @albertorossi6093
    @albertorossi6093 2 года назад +1

    awesome video, thank you very much! Do you already have in mind a procedure to compute the stress tensor on the cylinder surface? Secondly, can JAX run on Windows machines?

    • @MachineLearningSimulation
      @MachineLearningSimulation  2 года назад

      Hey,
      thanks a lot for the feedback :), Glad you liked the video.
      Regarding the stress tensor: That's a great question and probably linked to calculating the forces on the cylinder. I would encourage you to look into the method of momentum exchange. However, I have not done this myself (yet). Sorry for that.
      Regarding JAX on Windows: I haven't tested this myself, since I do not use Windows. According to the JAX documentation, you should be able to build the jax-lib yourself from source (jax.readthedocs.io/en/latest/developer.html ). Also take a look at that GitHub page: github.com/cloudhan/jax-windows-builder . If that sounds a little too complicated for you, you could do one of the following:
      1. Use a Google colab instance (these are basically free online Jupyter Notebooks, even with free access to GPUs). They have JAX installed already. You should be able to just copy and paste the code in there.
      2. Exchange JAX NumPy for NumPy: Remove the "import jax" line and change "import jax.numpy as jnp" to "import numpy as jnp". Then you also have to adapt the mutation of the arrays back to the NumPy style. You might also lose quite some speed, but for coarse discretizations it should still run fine.

    • @albertorossi6093
      @albertorossi6093 2 года назад +1

      @@MachineLearningSimulation many thanks very useful information, especially the one regarding Google colab. I didn't know about that. I will try to work on a procedure for computing the forces on the cylinder and we'll see if I can get a reasonable solution. thanks again

    • @MachineLearningSimulation
      @MachineLearningSimulation  2 года назад +1

      Nice 👌
      Once you have sth working, you could also open a pull request on GitHub and I merge on your code such that all viewers of the video could benefit 😊
      Happy coding.

    • @Davids373
      @Davids373 2 года назад

      @@albertorossi6093 I made a Colab notebook that goes into the stress tensor (and force on an object), which is based on this video:
      colab.research.google.com/drive/1oryCdOPXapOWxGSgCDNkvSUQ_MahfoRX?usp=sharing
      Maybe it helps you, or maybe you have some insights on my code!

    • @MachineLearningSimulation
      @MachineLearningSimulation  2 года назад

      [correction: the new code posted here is for spheres and not cylinders]
      @@albertorossi6093 There has now been an addition from someone in the audience with a simulation (in 3D) that also computes the forces on the cylinder: github.com/Ceyron/machine-learning-and-simulation/blob/main/english/simulation_scripts/contrib/D3Q19_lattice_boltzmann_method_MRT.py

  • @charlesperry7300
    @charlesperry7300 Год назад +1

    The work is so brilliant. The syntax of araay in JAX is too complicated to be learned.
    This defeats the purpose of a high level interpreted language - easy to implement and
    read. if the computational part is written in FORTEAN, thhe codes would be much easier to be implemented and understood.

    • @MachineLearningSimulation
      @MachineLearningSimulation  Год назад

      Thanks for the comment! :)
      I think that it is super subjective. Me myself, for instance, I am not the biggest fan of FORTRAN.
      Have you had a look at Julia yet?

  • @vowo4931
    @vowo4931 2 года назад +1

    o.o

    • @MachineLearningSimulation
      @MachineLearningSimulation  2 года назад

      I hope it's not going to be too overwhelming :D
      Similar to the Stable Fluids, there will also be another video with more details coming later this year.