Flow Lab

Syllabus / Module 10: Numerical Methods for PDEs

Lesson 10.11

2D Grids, Boundary Conditions & Staggered (MAC) Grids

Collocated vs staggered grids, checkerboard pressure, MAC layout, ghost cells, no-slip/free-slip/inflow/outflow boundaries, obstacles.

Written byClaude Sonnet

🌊
Why this matters for simulation: The MAC grid (Harlow & Welch, 1965) is the standard layout for incompressible solvers — and what you'll use in the wind tunnel.

By the end of this lesson you can…

  • Place $p$, $u$ and $v$ on a staggered (MAC) grid, count the unknowns, and write the divergence and gradient stencils that use only adjacent values.
  • Show that the MAC divergence times the MAC gradient is exactly the five-point Laplacian of the iterative-solvers-for-elliptic-problems lesson (10.8), and that the discrete divergence of a discrete curl is zero.
  • Explain why centred differences on a collocated grid leave the checkerboard $p = (-1)^{i+j}$ in the null space, and why the MAC grid does not.
  • Write the matrix row of a cell beside a closed wall (Neumann), beside an outlet (Dirichlet) and beside an obstacle, and say what each does to the null space and to solvability.
  • State the compatibility condition $\sum \text{div} = 0$ for an all-Neumann pressure problem and say what it means physically.
  • Choose ghost-cell values for free-slip and no-slip walls, and say which step of the solver uses them.
  • Build the divergence on a small MAC grid and measure how much divergence 20 Gauss-Seidel sweeps leave as the grid is refined.
On this page
·
  1. Where the unknowns live
  2. Divergence, gradient, and the Laplacian
  3. The collocated grid and its checkerboard
  4. Wall rows: Neumann, Dirichlet, and the null space
  5. Ghost cells for the tangential velocity
  6. The lab: project a field and count what is left
  7. Worked example: a 2×22 \times 22×2 box by hand
  8. Playground: assemble DDD and GGG
  9. The exercise
  10. Concept checks
  11. Problems
  12. What comes next
  13. Key Takeaways

10.8 Elliptic Solvers: Jacobi, Gauss–Seidel & SOR solved Au=bA\mathbf u = \mathbf b for a matrix that arrived out of nowhere: 44 on the diagonal, −1-1 at four neighbours. It ended with Stam’s twenty Gauss-Seidel sweeps leaving 60%60\% of the error on a plate. This lesson answers the question that came before: where does that matrix come from when the unknown is a pressure that straightens a velocity? It depends on where the numbers are stored. Put uu, vv and pp at the same points and the natural stencils decouple the grid. Stagger them and the matrix is the five-point one, with a row for every kind of wall. 11.10 The Projection Method for Incompressible Flow and 11.11 Stam's Stable Fluids, Step by Step already lean on this layout; here we build the arrays, the rows and the boundary rules, and measure what twenty sweeps buy.

▶ Video Collocated and staggered grids, the checkerboard pressure that central differences cannot see, the divergence and gradient stencils on a MAC grid, the wall rows of the pressure matrix, and a velocity field projected with twenty sweepscaptions only
Transcript
  1. A MAC grid cuts the box into N by N cells. The pressure sits at each cell centre, the horizontal velocity u on the vertical faces, and the vertical velocity v on the horizontal faces. At N equal 8 that is 64 pressures and 72 values each of u and v. The faces on the walls are known, so only 112 faces are unknowns. Every difference now sits between the two values it compares.
  2. The divergence of a cell is the net outflow, read off its four faces. The gradient of p lives on the interior faces, as the difference of the two cells either side. Divergence of gradient, at an interior cell, is the second difference in each direction, which is exactly minus the five-point matrix over h squared. So D G equals minus A over h squared, and the sweeps of lesson 10.8 solve the pressure equation. The divergence of a discrete curl is 0 for any stream function, and G is minus the transpose of D, so A is symmetric.
  3. Now the alternative, with every value at the cell centres. A centred gradient skips the centre, so a checkerboard pressure has the same value at i plus 1 and i minus 1, and its gradient is exactly 0. A solver can leave any amount of it behind. On the MAC grid the gradient compares neighbours, so the checkerboard shows up as plus or minus 2 a over h on every face. At N equal 16 with a equal 0.2, the largest x gradient is 0 collocated and 6.4 on MAC. The pressure operator gives 1.39 times 10 to the minus 17, round-off, on the collocated grid, and 1.6, which is 8 a, on MAC.
  4. Every cell has a row in A, set by its four faces. Beside a closed wall the missing neighbour is dropped, so the diagonal counts fluid neighbours: 3 beside a wall, 2 in a corner. The row sums are zero, so the constant is a null vector, and a solution exists only if the divergences sum to zero, which says the net flux through the walls is zero. In the lab it is 2.26 times 10 to the minus 17 at N equal 8. An outlet at pressure 0 adds 2 to the diagonal, giving 5, makes the matrix non-singular, and lets flux leave: net flux 1, and the field still projects to divergence 0. Tangential velocity reads ghost cells: the interior value for free slip, minus it for no slip.
  5. A tiny case by hand: 2 by 2 cells. Push 1 through one face. The divergences are 2, minus 2, 0 and 0, and they sum to 0, so a solution exists. Solving gives p equal minus 3, 3, minus 1, 1, over 16. The gradients are 0.75, 0.25, 0.25 and minus 0.25. Subtract them and the push becomes a counterclockwise loop of speed 0.25, with divergence 0 in every cell.
  6. Now a real field: a source blob plus a swirl on closed walls, projected with 20 Gauss-Seidel sweeps from zero pressure. At N equal 32 the largest divergence starts at 16.33, and after 20 sweeps it is 3.11, on the same colour scale. The exact solve leaves 8.54 times 10 to the minus 13, which is round-off. What survives is not cell-scale noise. Gauss-Seidel removes that part fastest, and the grid-wide part of the compression is left.
  7. Refine the grid and the sweeps fall behind. At N equal 8 they take the largest divergence from 12.08 to 0.0344, a fraction 0.00285. At 16, from 16.03 to 0.348, a fraction 0.0217. At 32, from 16.33 to 3.11, a fraction 0.190. The uniform push is harder still, leaving 0.109, 0.184 and 0.187. The slowest Gauss-Seidel factor approaches 1 like 1 minus order h squared, so a finer grid has more grid-wide compression for 20 sweeps to miss. At N equal 32 a fifth of it is left.
  8. So where does this leave us? We have the layout, the stencils, the wall rows, and the measured cost of 20 sweeps: a fifth of the compression at N equal 32. The next lesson in build order is the wind-tunnel capstone, 16.1, not yet written. It assembles this projection with an inflow, an outlet and a staircase airfoil, and its bottleneck is exactly the sweeps we just measured. Multigrid, 10.9, also not yet written, is the cure, since a smooth error is a rough one on a coarser grid.

Where the unknowns live

Cut the unit box into N×NN \times N square cells of side h=1/Nh = 1/N. On a MAC grid (marker-and-cell) each cell carries a pressure at its centre, and the velocity components sit on the faces they point through:

  • pi,jp_{i,j} at the cell centre: N2N^2 values;
  • ui,ju_{i,j} on the vertical face at x=ihx = ih, y=(j+12)hy = (j+\tfrac12)h: (N+1)N(N+1)N values;
  • vi,jv_{i,j} on the horizontal face at x=(i+12)hx = (i+\tfrac12)h, y=jhy = jh: N(N+1)N(N+1) values.

At N=8N = 8 that is 6464 pressures and 7272 values each of uu and vv. The extra column of uu faces and row of vv faces lie on the walls, where the velocity through the wall is known (00 at a closed wall, the inflow speed at an inlet), so only the interior faces are unknowns, 2N(N−1)=1122N(N-1) = 112 at N=8N = 8. Every difference sits between the two values it compares: the change of uu across a cell belongs at the centre, the change of pp between centres on the face where uu lives.

Divergence, gradient, and the Laplacian

The divergence of a cell is net outflow per unit area, read off its four faces, and the gradient lives on the interior faces:

(Du)i,j=ui+1,j−ui,jh+vi,j+1−vi,jh,(Gp)i,ju=pi,j−pi−1,jh,(Gp)i,jv=pi,j−pi,j−1h.(D\mathbf u)_{i,j} = \frac{u_{i+1,j} - u_{i,j}}{h} + \frac{v_{i,j+1} - v_{i,j}}{h}, \qquad (Gp)^u_{i,j} = \frac{p_{i,j} - p_{i-1,j}}{h}, \quad (Gp)^v_{i,j} = \frac{p_{i,j} - p_{i,j-1}}{h} .

The projection step of 11.10 The Projection Method for Incompressible Flow wants D(u−Gp)=0D(\mathbf u - Gp) = 0, that is DG p=DuD G\,p = D\mathbf u. At an interior cell the divergence takes the difference of the east gradient (pE−pC)/h(p_E - p_C)/h and the west gradient (pC−pW)/h(p_C - p_W)/h, over hh:

(DGp)C=pE−2pC+pWh2+pN−2pC+pSh2=−1h2(4pC−pE−pW−pN−pS).(DGp)_C = \frac{p_E - 2p_C + p_W}{h^2} + \frac{p_N - 2p_C + p_S}{h^2} = -\frac{1}{h^2}\big(4p_C - p_E - p_W - p_N - p_S\big).

So DG=−A/h2DG = -A/h^2, with AA the five-point matrix of 10.8 Elliptic Solvers: Jacobi, Gauss–Seidel & SOR. No new solver is needed: the pressure equation is A p=−h2 DuA\,p = -h^2\,D\mathbf u, and the sweeps of 10.8 solve it. Two more facts matter later.

Curl is invisible to DD. Put a stream function ψ\psi at the cell corners and set u=(ψi,j+1−ψi,j)/hu = (\psi_{i,j+1} - \psi_{i,j})/h, v=−(ψi+1,j−ψi,j)/hv = -(\psi_{i+1,j} - \psi_{i,j})/h on the faces between them. In a cell’s divergence each of the four ψ\psi values appears twice with opposite signs, so Du=0D\mathbf u = 0 exactly, for any ψ\psi. A discrete curl is discretely divergence-free, which is why projection keeps the swirl and removes only the compression.

Adjointness. Summing p (Du)p\,(D\mathbf u) over cells and moving each difference onto pp gives G=−DTG = -D^{\mathsf T}, so A=−h2DGA = -h^2DG is symmetric and positive semi-definite, and conjugate gradients and Gauss-Seidel apply.

The collocated grid and its checkerboard

The tempting alternative stores uu, vv, pp all at cell centres and differences over two cells, (pi+1−pi−1)/2h(p_{i+1} - p_{i-1})/2h. 11.10 The Projection Method for Incompressible Flow derived what goes wrong; the short version follows.

The centred gradient skips the centre. A pressure pi,j=a(−1)i+jp_{i,j} = a(-1)^{i+j} has the same value at i+1i+1 and i−1i-1, so its gradient is exactly zero, and subtracting a gradient cannot remove it. Centred divergence times centred gradient is the wide stencil (pi+2−2pi+pi−2)/4h2(p_{i+2} - 2p_i + p_{i-2})/4h^2, which links every second cell: the grid falls into four sub-grids that never talk. On a periodic box the null space has dimension 44 (the playground computes it for 8×88 \times 8): the constant, (−1)i(-1)^i, (−1)j(-1)^j and the checkerboard. A solver may leave any amount of those modes in the pressure.

The MAC gradient (pi−pi−1)/h(p_i - p_{i-1})/h is one cell wide. A checkerboard of amplitude aa gives ±2a/h\pm 2a/h on every face, and the five-point operator turns it into 8a8a in every cell. The lab measures both at N=16N = 16, a=0.2a = 0.2: the largest xx-gradient the checkerboard makes is 00 collocated and 6.46.4 (=2a/h= 2a/h) on MAC, and the pressure operator applied to it gives 1.39×10−171.39\times10^{-17} (round-off) for the wide collocated stencil and 1.61.6 (=8a= 8a) for MAC.

Wall rows: Neumann, Dirichlet, and the null space

Every cell has one row in AA, and the row depends on what its four faces are.

A closed wall. The wall face carries normal velocity 00 and is not an unknown, so no gradient is applied there and the cell’s missing neighbour is dropped. For a cell with mm fluid neighbours:

m pC−∑fluid neighbourspnb=−h2(Du)C.m\,p_C - \sum_{\text{fluid neighbours}} p_{nb} = -h^2 (D\mathbf u)_C .

The diagonal is mm: 33 beside a wall, 22 in a corner. This is the discrete Neumann condition ∂p/∂n=0\partial p/\partial n = 0 (11.10 The Projection Method for Incompressible Flow derives it from u⋅n=0\mathbf u\cdot\mathbf n = 0). The row sums are zero, so A1=0A\mathbf 1 = \mathbf 0 and the constant vector is in the null space. The playground builds AA from DD and GG at N=4N = 4 and gets rank 1515 of 1616, smallest eigenvalue 00, next 0.58580.5858 (=2−2cos⁡(π/4)= 2 - 2\cos(\pi/4)).

Compatibility. Because AA is symmetric, 1TA p=0\mathbf 1^{\mathsf T}A\,p = 0 for every pp, so a solution exists only if the right side sums to zero:

∑cells(Du)C=0⟺net flux through the walls=0.\sum_{\text{cells}} (D\mathbf u)_C = 0 \quad\Longleftrightarrow\quad \text{net flux through the walls} = 0 .

In the sum every interior face appears twice with opposite signs and only wall faces survive, so the sum times hh is the net flux out. With closed walls it vanishes to round-off (the lab at N=8N = 8 prints 2.26×10−172.26\times10^{-17}). Feed air through an inlet with no way out and no pressure can make the velocity divergence-free. When the condition holds the solution is unique up to a constant, which changes no gradient; Gauss-Seidel and CG still converge, and most codes subtract the mean.

An outlet. A wall open to ambient air has a known pressure, taken as 00: a Dirichlet condition. With the face at distance h/2h/2 from the centre, the ghost value is −pC-p_C and the face gradient (0−pC)/(h/2)=−2pC/h(0 - p_C)/(h/2) = -2p_C/h is twice an ordinary one, so the diagonal gains 22: a cell with the other three neighbours fluid has diagonal 3+2=53 + 2 = 5. The rows no longer sum to zero, so AA is non-singular (rank 1616 of 1616 and smallest eigenvalue 0.15220.1522 at N=4N = 4) and there is no compatibility condition. Flux may leave: in the lab the uniform push with the right wall open has net flux 11 out of the box and still projects to divergence 00.

Obstacles. A solid cell is a wall to its fluid neighbours: the shared face carries the solid’s normal velocity and the fluid row drops that neighbour, so a staircase body gives a staircase of Neumann rows. Cut cells (a glimpse, from memory) give each face an open fraction, which removes the staircase error but breaks the simple matrix structure. A tunnel with prescribed inflow and zero-gradient outflow is all-Neumann, so inflow must equal outflow; a Dirichlet outlet avoids that.

Ghost cells for the tangential velocity

The projection touches only the normal components, which live on the walls. The tangential component lives half a cell away, and wherever a stencil needs it near the wall (diffusion, 10.4 Implicit Methods & Crank–Nicolson; the interpolation inside advection) it reads a ghost cell one layer outside the box:

boundarynormal velocitytangential velocity (ghost value)pressure
closed, free-slip00 on the faceghost = interior value, so ∂ut/∂n=0\partial u_t/\partial n = 0Neumann
closed, no-slip00 on the faceghost = minus interior value, so the wall average is 00Neumann
inflowprescribed UU on the faceprescribed (often 00)Neumann, flux matched
outflowfree (set by the projection)copied (zero gradient)Dirichlet, p=0p = 0

The no-slip rule is the ghost-cell device of 10.1 Finite Differences: Discretizing Derivatives applied to velocity: a ghost equal to minus its neighbour averages to zero at the wall. Stam’s set_bnd (from memory) flips the normal component at a wall and copies the rest: free slip. A no-slip wall also flips the tangential one, which is where the boundary layer of 11.8 Boundary Layers would be born if the grid could resolve it.

The lab: project a field and count what is left

◆ Interactive A velocity field on a MAC grid, projected with k Gauss-Seidel sweeps against the exact solve, and a checkerboard pressure seen by collocated and MAC gradients

Loading simulation…

The top row draws the grid: dots are pressures, cyan arrows uu, amber arrows vv, cell colour the divergence. The first panel is a source blob plus a swirl, with zero normal velocity on the walls; the second the same field after subtracting GpGp, with pp from kk Gauss-Seidel sweeps on Ap=−h2DuA p = -h^2 D\mathbf u from p=0p = 0; the third that pressure.

For the source field on closed walls, the largest ∣Du∣\lvert D\mathbf u\rvert before projection and after 2020 sweeps:

NNbeforeafter 2020 sweepsfraction left
8812.0812.080.03440.03440.002850.00285
161616.0316.030.3480.3480.02170.0217
323216.3316.333.113.110.1900.190

The exact solve (conjugate gradients to 10−1310^{-13}) leaves 1.63×10−141.63\times10^{-14}, 3.75×10−133.75\times10^{-13} and 8.54×10−138.54\times10^{-13}: round-off. The uniform push (every interior uu equal to 11) is harder: it starts at 1/h=8,16,321/h = 8, 16, 32 and 2020 sweeps leave 0.8760.876, 2.952.95 and 5.975.97, so 0.1090.109, 0.1840.184 and 0.1870.187 of it.

This is the mechanism of 10.8 Elliptic Solvers: Jacobi, Gauss–Seidel & SOR on a real divergence: Gauss-Seidel removes the cell-scale part fastest, and the grid-wide part survives, which a finer grid has more of because the slowest factor approaches 11 like 1−O(h2)1 - O(h^2). At N=8N = 8 twenty sweeps do nearly all the job; at N=32N = 32 they leave a fifth of the compression.

The bottom row is the checkerboard: the exact pressure plus a(−1)i+ja(-1)^{i+j}, then its xx-gradient as a collocated grid computes it (the checkerboard has vanished) and on the MAC faces (a stripe pattern of amplitude 2a/h2a/h that the solver cannot ignore).

Worked example: a 2×22 \times 2 box by hand

Take N=2N = 2, h=12h = \tfrac12, cells 0,10, 1 along the bottom and 2,32, 3 along the top. Each cell has two fluid neighbours, so every diagonal is 22:

A=(2−1−10−120−1−102−10−1−12),A1=0.A = \begin{pmatrix} 2 & -1 & -1 & 0 \\ -1 & 2 & 0 & -1 \\ -1 & 0 & 2 & -1 \\ 0 & -1 & -1 & 2 \end{pmatrix}, \qquad A\mathbf 1 = \mathbf 0 .

The unknown faces are two uu faces on x=12x = \tfrac12 and two vv faces on y=12y = \tfrac12. Set u=1u = 1 on the bottom uu face and everything else to 00. The divergences are 22 in cell 00 (flow leaves to the right), −2-2 in cell 11, and 00 elsewhere: they sum to 00, so a solution exists. The right side is −h2Du=(−12,12,0,0)-h^2 D\mathbf u = (-\tfrac12, \tfrac12, 0, 0). By the antisymmetry p1=−p0p_1 = -p_0, p3=−p2p_3 = -p_2 the rows reduce to 3p0−p2=−123p_0 - p_2 = -\tfrac12 and 3p2−p0=03p_2 - p_0 = 0, so p2=p0/3p_2 = p_0/3, 83p0=−12\tfrac83p_0 = -\tfrac12, and with mean zero

p=116(−3,  3,  −1,  1).p = \tfrac1{16}(-3,\; 3,\; -1,\; 1) .

The gradients are 0.750.75 (bottom uu face), 0.250.25 (top uu face), 0.250.25 (left vv face) and −0.25-0.25 (right vv face). Subtracting, uu becomes 0.250.25 (bottom) and −0.25-0.25 (top), vv becomes −0.25-0.25 (left) and 0.250.25 (right). Cell 00 now has divergence (0.25−0)/h+(−0.25−0)/h=0(0.25 - 0)/h + (-0.25 - 0)/h = 0, and so do the others: the one-face push has become a counterclockwise loop of speed 0.250.25. The playground checks it.

Playground: assemble DD and GG

🐍 PythonBuild the MAC divergence D and gradient G, then A = minus h squared times DG
⌘/Ctrl + Enter

The exercise

Implement the MAC divergence and the discrete curl, and test the identities above.

🧪 Coding exerciseExercise: divergence on a MAC grid, and a divergence-free curl
⌘/Ctrl + Enter

Concept checks

✅ Concept check

On a MAC grid with $N \times N$ cells, how many $u$ values are there, and where do they sit?

✅ Concept check

A pressure $p = a(-1)^{i+j}$ with $a = 0.2$ on a MAC grid with $N = 16$. What is the size of its gradient on every interior face?

✅ Concept check

A closed-box cell at the left wall, not in a corner, has fluid neighbours north, south and east. Which row of $A$ is right?

✅ Concept check

A cell in the last column, not at a corner, with its east face an outlet at $p = 0$. What is the diagonal entry of its row?

✅ Concept checkselect all that apply

Select all that are true of the pressure problem in a closed box whose walls carry zero normal velocity.

Problems

✏️ Pen & paperCounting unknowns●○○

For N=10N = 10 count the pressures, the uu and vv values and the unknown faces, and give the shapes of DD, GG and AA (wall faces kept in the face list).

Show worked solution

Pressures 100100; uu and vv each N(N+1)=110N(N+1) = 110, so 220220 face values; unknown faces N(N−1)=90N(N-1) = 90 of each kind, 180180. DD is 100×220100 \times 220, GG is 220×100220 \times 100 (zero rows for wall faces), AA is 100×100100 \times 100 with at most five nonzeros per row.

✏️ Pen & paperA three-cell duct and the compatibility condition●●○

A closed one-dimensional duct has N=3N = 3 cells, h=13h = \tfrac13, and face velocities u=(0,2,−1,0)u = (0, 2, -1, 0). Find AA, the divergences and the pressure, and verify that the projected velocity is zero. Then set the last face to u3=1u_3 = 1 and show the system has no solution.

Show worked solution

The end cells have one fluid neighbour and the middle cell two, so A=(1−10−12−10−11)A = \begin{pmatrix}1&-1&0\\-1&2&-1\\0&-1&1\end{pmatrix}. Divergences: (2−0)/h=6(2 - 0)/h = 6, (−1−2)/h=−9(-1 - 2)/h = -9, (0+1)/h=3(0 + 1)/h = 3, summing to 00. The right side is −h2Du=(−23,1,−13)-h^2D\mathbf u = (-\tfrac23, 1, -\tfrac13). Row 1 gives p1=p0+23p_1 = p_0 + \tfrac23, row 3 gives p2=p0+13p_2 = p_0 + \tfrac13, and row 2 holds. The gradients 22 and −1-1 equal the interior velocities, so u−Gp=0\mathbf u - Gp = 0. With u3=1u_3 = 1 the right side is (−23,1,−23)(-\tfrac23, 1, -\tfrac23). The rows of AA add to zero, the right side to −13-\tfrac13: no pressure exists.

✏️ Pen & paperNull space of the wide stencil●●●

On a periodic collocated grid, show that −DG-DG with centred differences has eigenvalue (sin⁡2θx+sin⁡2θy)/h2(\sin^2\theta_x + \sin^2\theta_y)/h^2 on ei(θxi+θyj)e^{\mathrm i(\theta_x i + \theta_y j)}, find its null space, and compare with the MAC eigenvalue 4h2(sin⁡2θx2+sin⁡2θy2)\tfrac{4}{h^2}\big(\sin^2\tfrac{\theta_x}{2} + \sin^2\tfrac{\theta_y}{2}\big).

Show worked solution

In one direction −(pi+2−2pi+pi−2)/4h2-(p_{i+2} - 2p_i + p_{i-2})/4h^2 gives (2−2cos⁡2θ)/4h2=sin⁡2θ/h2(2 - 2\cos2\theta)/4h^2 = \sin^2\theta/h^2, zero at θ=0\theta = 0 and π\pi. In 2D both sines must vanish: (θx,θy)∈{0,π}2(\theta_x, \theta_y) \in \{0, \pi\}^2, four modes, 11, (−1)i(-1)^i, (−1)j(-1)^j, (−1)i+j(-1)^{i+j}. The MAC eigenvalue is zero only at θx=θy=0\theta_x = \theta_y = 0, and the checkerboard has its largest, 8/h28/h^2.

What comes next

The course was not built in syllabus order: 11.10 and 11.11 already exist and assume this grid. The next lesson in build order is the 2D virtual wind tunnel capstone (16.1 Capstone: 2D Virtual Wind Tunnel, 16.1), which is assembly rather than new theory: this MAC projection with an inflow, an outlet and a staircased airfoil, plus semi-Lagrangian advection (10.7 Semi-Lagrangian Advection) and implicit diffusion (10.4 Implicit Methods & Crank–Nicolson). The bottleneck is the one measured above: twenty cold-start sweeps leave a fifth of the divergence at N=32N = 32, and more on finer grids, so the capstone needs a better solver or must accept the loss. Multigrid (10.9 Multigrid) is the cure.

Key Takeaways

  • On a MAC grid pp sits at cell centres, uu on vertical faces and vv on horizontal faces (6464, 7272 and 7272 values at N=8N = 8); each divergence or gradient uses only adjacent values.
  • The MAC divergence times the MAC gradient is exactly the five-point Laplacian, DG=−A/h2DG = -A/h^2, and the divergence of a discrete curl is zero for any stream function.
  • Centred differences on a collocated grid skip the centre, so p=(−1)i+jp = (-1)^{i+j} has gradient 00 and the periodic wide operator has a null space of dimension 44. On MAC the same checkerboard has face gradient 2a/h2a/h (6.46.4 at a=0.2a = 0.2, N=16N = 16).
  • A closed wall drops the missing neighbour: the diagonal counts fluid neighbours, the constant is a null vector (rank 1515 of 1616 at N=4N = 4), and a solution needs the cell divergences to sum to zero, which closed walls guarantee.
  • An outlet at p=0p = 0 adds 22 to the diagonal, makes AA non-singular and lets flux leave; a solid cell is a wall to its neighbours.
  • Tangential velocity uses ghost cells: the interior value for free-slip, minus it for no-slip.
  • Exact solves leave round-off (10−1410^{-14} to 10−1210^{-12} in the lab); twenty cold-start sweeps leave 0.002850.00285, 0.02170.0217 and 0.1900.190 of the source field’s divergence at N=8,16,32N = 8, 16, 32.
  • Next: the wind-tunnel capstone (16.1 Capstone: 2D Virtual Wind Tunnel) assembles these pieces; multigrid (10.9 Multigrid) addresses sweeps that stop short.
Finished this lesson?

Mark it complete to track your progress (saved in this browser). Reading every section does this for you.

Where this leads

These later lessons build directly on this one: