This iPython notebook shows how to solve the lid-driven cavity problem with a Navier-Stokes solver. The main features are summarized as follows:
Incompressible Navier-Stokes equations in two dimensions with primitive variables p, u and v and constant material properties
Dirichlet boundary conditions for the velocities and Neumann boundary conditions for the pressure, except for a fixed point in the lower left (SW) corner
Explicit projection method to decouple pressure and velocity
Finite Difference discretization on a uniform orthogonal cartesian grid, first order explicit Euler time-stepping
Pressure defined in cell centers and velocities defined in backward staggered grid
Second derivatives discretized with centered differences, even for the non-linear terms in the momentum equation
Viscous term in momentum equation is calculated implicitly
Poisson equations for pressure and implicit viscous momentum term are solved with a direct solver or alternatively with an iterative solver and a multigrid preconditioner
The incompressible Navier-Stokes equations are given in vector notation with the velocity vector u, the time t, the density ρ, the pressure p, the kinematic viscosity ν and the Nabla operator ∇.
∇⋅→u=0The physical problem is a rectangular cavity with a moving lid as depicted in the figure below. The width and height of the cavity are Lx=Ly=1m. The top lid is a non-slip wall and it is moving with a velocity u=1ms in positive x-direction. All other boundarys are stationary non-slip walls with zero velocities. The pressure gradient is →n⋅∇p=0 at all four boundarys as it is common for non-slip walls. Finally, the pressure is set to a constant value p=0 at the lower left corner to completely define the pressure boundary and to obtain a positive definite coefficient matrix.
The density is set to a fixed value ρ=1kgm3. The kinematic viscosity ν is varied to obtain different Reynolds numbers
Re=ULν,where U=u=1ms is the characteristic velocity and L=Lx=1m is the characteristic length.
Two different test cases and an appropriate discretization adjusted to the test case are defined as shown in the table below.
Re | ν | Δx | Δy | grid | Δt | tsim |
---|---|---|---|---|---|---|
20 | 0.05 Pa s | 0.05 m | 0.05 m | 20x20 | 0.05 s | 5 s |
100 | 0.01 Pa s | 0.02 m | 0.02 m | 50x50 | 0.02 s | 20 s |
The grid spacings Δx and Δy were adjusted to obtain a reasonable Peclet number for mass transport:
Pe=UΔxν≤2.The time step Δt was adjusted to obey the time step restriction for an explicit scheme declared by the Courant-Friedrichs-Levy number:
CFL=UΔtΔx≤1.The simulation time tsim is the time after the simulation was found to be in a steady state.
First, a low Reynolds number Re=20 was investigated. The resulting pressure and velocity fields at a steady state after t = 5 s are illustrated in the figure below. To compare the results, a simulation of the same test case with a similar discretization was performed in OpenFOAM. The OpenFoam simulation data was imported and plotted with the Python script plotOpenFoamResults.py.
Python results | OpenFOAM results |
---|---|
![]() |
![]() |
A higher Reynolds number Re=100 was investigated. The resulting pressure and velocity fields at a steady state after t = 5 s are again illustrated in the figure below together with OpenFOAM results.
Python results | OpenFOAM results |
---|---|
![]() |
![]() |
The results of the Python CFD code were also compared to literature data by Ghia et al. [Ghia1982]. Firstly, the x-velocity u was evaluated on a vertical profile going through the center of the cavity. The results are plotted in the figure below.
![]() |
Secondly, the y-velocity v was evaluated on a horizontal profile going through the center of the cavity. The results are plotted in the figure below.
![]() |
TBD
[Ghia1982] U Ghia, K.N Ghia, C.T Shin. High-Re solutions for incompressible flow using the Navier-Stokes equations and a multigrid method. Journal of Computational Physics. Volume 48, Issue 3, 1982, Pages 387-411. https://doi.org/10.1016/0021-9991(82)90058-4.