Problem 6: Python Implementation of the 2D Incompressible Navier–Stokes Equations

In this problem, we will examine a numerical solution to the two-dimensional incompressible Navier–Stokes equations using the pressure correction method (also known as the projection method) with finite difference discretization in Python. The scenario corresponds to a square lid-driven cavity, where the fluid is set into motion by the top wall moving horizontally at constant speed. The solution consists of iteratively solving for the pressure and velocity fields while enforcing the incompressibility condition.

(1) Implement the function build_up_b(b, rho, dt, u, v, dx, dy) to compute the source term b used in the Poisson pressure equation \[\nabla^2 p = b\] This term is derived from the divergence of the momentum equations and ensures that the final velocity field satisfies the incompressibility condition \(\nabla \cdot \vec{u} = 0\).

(2) Complete the pressure_poisson(p, dx, dy, b) function to iteratively solve the Poisson equation \(\nabla^2 p = b\) using second-order finite differences with Neumann boundary conditions on the vertical and bottom walls, a Dirichlet condition \(p = 0\) on the top wall, and at least nit = 50 iterations per step.

(3) Complete the cavity_flow function to advance the velocity fields u and v through time using convection, diffusion, and pressure gradient terms (with temporary arrays un and vn), enforce no-slip boundary conditions including the moving lid (u = 1 at the top wall), and visualize the final velocity field with a quiver plot and the pressure field with a contour plot.


1 This problem set contains 6 problems, and your final score will be based on the 4 problems on which you earned the highest scores.