Solving the Navier-Stokes equations in Python | CFD in Python | Lid-Driven Cavity
56,898 views
0

 Published On Nov 19, 2021

Have you ever wanted to start coding Computational Fluid Dynamics (CFD) to simulate fluids? Here is the first example for you. We solve the incompressible NS equations using Chorin's Projection to enforce incompressibility. Here is the code: https://github.com/Ceyron/machine-lea...

We will discretize the incompressible Navier Stokes equations, consisting of a momentum equation and an incompressibility constraint, by Finite Differences. Partial Derivatives are resolved using a central difference scheme, the Laplace operator via the five-point stencil, and we use an explicit Euler in time. We account for incompressibility by using Chorin's Projection, which consists of the three steps:
1. Solve for a tentative velocity by forward stepping the momentum equations without the pressure gradient
2. Solve the Pressure-Poisson equation for a corrective pressure
3. Correct the Velocity to enforce incompressibility.

The second step is done approximately by 50 Jacobi iterations to reduce complexity.

Step (1) and (3) involve Dirichlet Boundary Conditions for the Velocity, which are homogeneous everywhere except for the horizontal velocity at the top that is prescribed. The Pressure Boundary Conditions are homogeneous Neumann (zero gradient over the boundary) everywhere except for the top, where it is a homogeneous Dirichlet BC. That is of course because we do not have an outflow over our solid walls.

If this simulation runs for at least 0.1 seconds, swirly motion takes place that can be beautifully seen by the streamlines we plot in the end.

Finally, we implement a stability constraint that is based on the von Neumann stability analysis of the Forward in time Central in Space (FTCS) scheme of the heat equation (also known as the CFL condition).

This video is inspired by the work of @lorenabarba. Check out her awesome course on CFD in Python https://lorenabarba.com/blog/cfd-pyth... which also helped me a lot in my understanding of the topics.

-------

đź“ť : Check out the GitHub Repository of the channel, where I upload all the handwritten notes and source-code files (contributions are very welcome): https://github.com/Ceyron/machine-lea...

📢 : Follow me on LinkedIn or Twitter for updates on the channel and other cool Machine Learning & Simulation stuff:   / felix-koehler   and   / felix_m_koehler  

💸 : If you want to support my work on the channel, you can become a Patreon here:   / mlsim  

-------

⚙️ My Gear:

(Below are affiliate links to Amazon. If you decide to purchase the product or something else on Amazon through this link, I earn a small commission.)

- 🎙️ Microphone: Blue Yeti: https://amzn.to/3NU7OAs
- ⌨️ Logitech TKL Mechanical Keyboard: https://amzn.to/3JhEtwp
- 🎨 Gaomon Drawing Tablet (similar to a WACOM Tablet, but cheaper, works flawlessly under Linux): https://amzn.to/37katmf
- 🔌 Laptop Charger: https://amzn.to/3ja0imP
- đź’» My Laptop (generally I like the Dell XPS series): https://amzn.to/38xrABL
- 📱 My Phone: Fairphone 4 (I love the sustainability and repairability aspect of it): https://amzn.to/3Jr4ZmV

If I had to purchase these items again, I would probably change the following:

- 🎙️ Rode NT: https://amzn.to/3NUIGtw
- đź’» Framework Laptop (I do not get a commission here, but I love the vision of Framework. It will definitely be my next Ultrabook): https://frame.work

As an Amazon Associate I earn from qualifying purchases.

-------

Timestamps:
00:00 Introduction
00:25 Problem Description
00:54 Boundary Conditions
01:30 Chorin's Projection (a splitting method)
02:00 Expected Outcome: Swirls
02:12 Strategy in Index Notation
02:37 Imports
02:56 Defining Constants (Parameters of the Simulation)
04:05 Main Switch (Boilerplate)
04:27 Define Mesh: Spatial Discretizations
05:11 Prescribe Initial Condition
05:48 Central Differences in x
07:13 Central Differences in y
08:11 Five-Point Stencil for Laplace Operator
09:33 Time stepping Boilerplate
09:49 Solving Momentum for Tentative Velocity
12:48 Enforce Velocity Boundary Conditions
14:30 Solving Pressure Poisson for Pressure Correction
20:29 Velocity Correction
21:54 Again Enforce Velocity Boundary Conditions
22:14 Advance in Time
22:37 Plot Solution (+ Bug Fix)
23:42 Discussing the Solution
24:32 Streamline Plot
25:13 Check for Numerical Stability
28:33 Outro

show more

Share/Embed