Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

1. Finite-volume grids, stretched meshes and the discrete Gauss theorem

School of Engineering, Nazarbayev University

In a turbulent channel flow at friction Reynolds number Reτ=1000\mathrm{Re}_\tau = 1000, 90%90\% of the velocity drop occurs inside a viscous wall sublayer thinner than 1%1\% of the channel height. Covering the whole domain with uniform cells fine enough to put 5 points inside that sublayer would waste 99%99\% of the CPU budget in the quiescent core. Yet if we stretch the mesh non-uniformly, how do we prevent geometric metric errors from creating fake mass or momentum out of thin air? In this lecture, we build cell-centered Finite-Volume meshes (uniform_fvm_grid_1d, uniform_fvm_grid_2d, and Vinokur’s tanh_stretched_grid_1d) and show that telescoping face fluxes satisfy the 2-D discrete Gauss divergence theorem (verify_discrete_gauss_divergence_2d) to rounding error on any flux field.

Learning objectives

  1. Distinguish cell-centered FVM control volumes (where centroids sit Δx/2\Delta x/2 inside walls) from vertex-centered grids using uniform_fvm_grid_1d and uniform_fvm_grid_2d.

  2. Cluster wall-normal FVM cells into thin viscous or concentration boundary layers via tanh_stretched_grid_1d (one-sided and two-sided) while preserving exact volume partition ∑iΔVi=L\sum_i \Delta V_i = L.

  3. Quantify boundary-layer quadrature and wall-gradient resolution improvements on stretched vs. uniform FVM grids.

  4. Prove exact telescoping flux cancellation and free-stream preservation in 2-D using verify_discrete_gauss_divergence_2d.

Time: about 60 min

1. Cell-Centered Finite-Volume Grids vs. Vertex Grids

1.1 Partitioning Space into Control Volumes

In a cell-centered Finite-Volume Method (FVM) on [xmin⁡,xmax⁡][x_{\min}, x_{\max}], we first place N+1N + 1 cell faces:

xmin⁡=x1/2<x3/2<⋯<xN+1/2=xmax⁡(1.1)x_{\min} = x_{1/2} < x_{3/2} < \dots < x_{N + 1/2} = x_{\max} \tag{1.1}

Cell i∈{0,…,N−1}i \in \{0, \dots, N-1\} occupies control volume Ωi=[xi+1/2,xi+3/2]\Omega_i = [x_{i+1/2}, x_{i+3/2}] (or in zero-based array indexing, faces[i] to faces[i+1]) with exact cell volume and geometric centroid:

ΔVi=xi+1face−xiface,xicenter=12(xiface+xi+1face),∑i=0N−1ΔVi=xmax⁡−xmin⁡(1.2)\Delta V_i = x_{i+1}^{\mathrm{face}} - x_i^{\mathrm{face}}, \qquad x_i^{\mathrm{center}} = \frac{1}{2}\left(x_i^{\mathrm{face}} + x_{i+1}^{\mathrm{face}}\right), \qquad \sum_{i=0}^{N-1} \Delta V_i = x_{\max} - x_{\min} \tag{1.2}

Unlike vertex-centered finite differences, which store unknowns directly on walls, cell-centered FVM stores cell averages ϕˉi=1ΔVi∫Ωiϕ dx\bar{\phi}_i = \frac{1}{\Delta V_i}\int_{\Omega_i}\phi\,\mathrm{d}x at interior centroids (x0center=xmin⁡+ΔV0/2x_0^{\mathrm{center}} = x_{\min} + \Delta V_0/2) and enforces boundary fluxes directly on the exterior faces faces[0] and faces[-1].


Demonstration 1: 1-D & 2-D Cell-Centered FVM Mesh Invariants & Midpoint Quadrature
==================================================================================
1-D FVM: 8 cells, 9 faces, dx = 0.2500
First/last centroids: x_0 = 0.1250, x_{N-1} = 1.8750 (half-cell inset dx/2 = 0.1250)
2-D FVM: XC shape = (12, 8), cell_area = 0.03125, total area = 3.0000
Bilinear 2-D integral: FVM = 9.375000000000 vs Exact = 9.375000000000
<Figure size 1056x462 with 2 Axes>

Figure 1.1. Finite-volume grids of 16×1616 \times 16 cells, uniform in xx and stretched in yy with β=2.8\beta = 2.8. (a) Cells clustered at both walls. (b) Cells clustered at the wall y=0y = 0.

What Demonstration 1 shows. Because every cell centroid xicenterx_i^{\mathrm{center}} is the exact first-moment center of mass of Ωi\Omega_i, cell-centered FVM integrates linear and bilinear fields to machine precision (<10−14< 10^{-14}) even on coarse grids, and its total volume ∑ΔVi\sum \Delta V_i equals the physical domain size without boundary trapezoid half-weights.


2. Boundary-Layer Clustering via Hyperbolic-Tangent Stretching

To pack cells near walls smoothly (C∞C^\infty mapping ξ∈[0,1]↦x(ξ)∈[xmin⁡,xmax⁡]\xi \in [0, 1] \mapsto x(\xi) \in [x_{\min}, x_{\max}] so metric derivatives dx/dξ\mathrm{d}x/\mathrm{d}\xi remain smooth and preserve second-order accuracy), grids_fvm.tanh_stretched_grid_1d implements Vinokur’s hyperbolic-tangent transformations parameterized by clustering strength β>0\beta > 0:

stwo-sided(ξ)=12[1+tanh⁡ ⁣(β(2ξ−1))tanh⁡(β)],sone-sided(ξ)=1−tanh⁡ ⁣(β(1−ξ))tanh⁡(β)(2.1)s_{\mathrm{two\text{-}sided}}(\xi) = \frac{1}{2}\left[1 + \frac{\tanh\!\left(\beta(2\xi - 1)\right)}{\tanh(\beta)}\right], \qquad s_{\mathrm{one\text{-}sided}}(\xi) = 1 - \frac{\tanh\!\left(\beta(1 - \xi)\right)}{\tanh(\beta)} \tag{2.1}
  • two_sided=True clusters faces symmetrically against both walls xmin⁡x_{\min} and xmax⁡x_{\max} (plane Poiseuille / turbulent channel / cavity flow).

  • two_sided=False clusters faces near xmin⁡x_{\min} and expands smoothly toward xmax⁡x_{\max} (external flat-plate boundary layer).


Demonstration 2: Vinokur Hyperbolic-Tangent Boundary-Layer Meshes (N = 32)
==========================================================================
Mesh Mode              |  beta |     min_dx |     max_dx |    Ratio |   Sum(dV) | Cells in y<=0.02
------------------------------------------------------------------------------------------------
Uniform                |   0.0 |   0.031250 |   0.031250 |     1.00 |  1.000000 |                1
Two-sided (channel)    |   1.5 |   0.010193 |   0.051636 |     5.07 |  1.000000 |                2
Two-sided (channel)    |   2.5 |   0.002466 |   0.078547 |    31.85 |  1.000000 |                4
Two-sided (channel)    |   3.2 |   0.000817 |   0.099016 |   121.26 |  1.000000 |                6
One-sided (flat plate) |   1.5 |   0.009765 |   0.051749 |     5.30 |  1.000000 |                2
One-sided (flat plate) |   2.5 |   0.002277 |   0.079024 |    34.71 |  1.000000 |                6
One-sided (flat plate) |   3.2 |   0.000735 |   0.100000 |   135.97 |  1.000000 |               10

What Demonstration 2 shows. With N=32N = 32 total cells, a uniform grid places only 1 cell centroid inside the y≤0.02y \leq 0.02 wall sublayer (Δy=0.03125\Delta y = 0.03125), so that the wall shear stress cannot be resolved. Turning on tanh_stretched_grid_1d with β=3.2\beta = 3.2 shrinks the wall cell to Δymin⁡=8.2×10−4\Delta y_{\min} = 8.2\times 10^{-4} (6 centroids inside y≤0.02y \leq 0.02) while keeping ∑i=031ΔVi=1.000000\sum_{i=0}^{31} \Delta V_i = 1.000000 to 10-16 because FVM cell volumes are defined directly as ΔVi=xi+1face−xiface\Delta V_i = x_{i+1}^{\mathrm{face}} - x_i^{\mathrm{face}}.


3. Resolving Steep Wall Shear Layers on Stretched vs. Uniform Meshes

Consider a Hartmann / turbulent-like steep channel velocity profile on y∈[0,H]y \in [0, H] (H=1 mH = 1\ \mathrm{m}) with thin wall layers of thickness δ=H/Ha\delta = H / \mathrm{Ha} (Ha=40\mathrm{Ha} = 40):

u(y)=U0[1−cosh⁡ ⁣(Ha (2y/H−1))cosh⁡(Ha)],dudy∣y=0=2U0 HaHtanh⁡(Ha)≈80 U0H(3.1)u(y) = U_0 \left[1 - \frac{\cosh\!\left(\mathrm{Ha}\,(2y/H - 1)\right)}{\cosh(\mathrm{Ha})}\right], \qquad \left.\frac{\mathrm{d}u}{\mathrm{d}y}\right|_{y=0} = \frac{2 U_0\,\mathrm{Ha}}{H}\tanh(\mathrm{Ha}) \approx 80\,\frac{U_0}{H} \tag{3.1}

We compare wall-shear estimation (du/dy)∣y=0≈u(y0center)/y0center\left.(\mathrm{d}u/\mathrm{d}y)\right|_{y=0} \approx u(y_0^{\mathrm{center}}) / y_0^{\mathrm{center}} (for no-slip u(0)=0u(0)=0) and bulk flow-rate integration across uniform vs. stretched meshes.


Demonstration 3: Resolving Steep Channel Wall Layers (Ha = 40) with N = 24 Cells
================================================================================
Exact wall gradient du/dy|_0 = 80.0000 s^-1 | Uniform (N=24): 38.9340 (err 51.3%) | Tanh (beta=2.8): 76.5897 (err 4.26%)
Exact bulk flow rate Q       = 0.975000 m^2/s | Uniform err = 8.68e-03 | Tanh err = 5.53e-04
<Figure size 1232x462 with 2 Axes>

Figure 1.2. (a) A boundary-layer velocity profile sampled at the cell centers of a uniform grid and of a tanh-stretched grid, both with 24 cells. (b) Position of the cell faces against the face index for the uniform grid and for the two-sided and one-sided stretched grids.

What Demonstration 3 shows. On N=24N = 24 uniform cells, the first centroid sits at y0=0.0208 my_0 = 0.0208\ \mathrm{m}, already outside most of the viscous wall layer, so u0/y0u_0/y_0 underpredicts wall shear stress τw=μ(du/dy)0\tau_w = \mu(\mathrm{d}u/\mathrm{d}y)_0 by 51.3%51.3\%. Clustering those same 24 cells with β=2.8\beta = 2.8 cuts the wall-shear error down to 4.26%4.26\% and reduces bulk flow-rate quadrature error by over 15×15\times.


4. The 2-D Discrete Gauss Divergence Theorem & Free-Stream Preservation

On a 2-D Cartesian mesh of Nx×NyN_x \times N_y cells with staggered east/west face fluxes Fi+1/2,jxF^x_{i+1/2, j} of shape (nx + 1, ny) and north/south face fluxes Fi,j+1/2yF^y_{i, j+1/2} of shape (nx, ny + 1), the cell-averaged discrete divergence in cell (i,j)(i, j) is:

(∇h⋅F)i,j=Fi+1,jx−Fi,jxΔx+Fi,j+1y−Fi,jyΔy(4.1)(\nabla_h \cdot \mathbf{F})_{i,j} = \frac{F^x_{i+1, j} - F^x_{i, j}}{\Delta x} + \frac{F^y_{i, j+1} - F^y_{i, j}}{\Delta y} \tag{4.1}

Summing (∇h⋅F)i,j Δx Δy(\nabla_h \cdot \mathbf{F})_{i,j}\,\Delta x\,\Delta y over all interior cells cancels every shared interior face flux +Fi+1,jxΔy−Fi+1,jxΔy=0+F^x_{i+1,j}\Delta y - F^x_{i+1,j}\Delta y = 0 in exact telescoping pairs, leaving only exterior boundary faces (grids_fvm.verify_discrete_gauss_divergence_2d):

∑i=0Nx−1∑j=0Ny−1(∇h⋅F)i,j Δx Δy=∑j=0Ny−1(FNx,jx−F0,jx)Δy+∑i=0Nx−1(Fi,Nyy−Fi,0y)Δx(4.2)\sum_{i=0}^{N_x-1}\sum_{j=0}^{N_y-1} (\nabla_h \cdot \mathbf{F})_{i,j}\,\Delta x\,\Delta y = \sum_{j=0}^{N_y-1}\left(F^x_{N_x, j} - F^x_{0, j}\right)\Delta y + \sum_{i=0}^{N_x-1}\left(F^y_{i, N_y} - F^y_{i, 0}\right)\Delta x \tag{4.2}

Demonstration 4: 2-D Discrete Gauss Theorem & Free-Stream Preservation
======================================================================
Smooth nonlinear F: vol_int = 3.6864000000, bnd_int = 3.6864000000, discrepancy = 8.88e-16
Comparison vs analytical integral Lx^2*Ly^2 = 3.6864000000 (O(h^2) midpoint diff = 0.00e+00)
Free-stream constant F: max |div_cells| = 0.00e+00, discrepancy = 0.00e+00
Discontinuous random fluxes: vol_int = +0.01118185, bnd_int = +0.01118185, discrepancy = 1.73e-18

Inside the algorithm

The third test of Demonstration 4 shows that even when the face fluxes are random, the discrete FVM volume integral equals the boundary surface flux integral to 10-14. Because of this telescoping property, finite-volume shock-capturing solvers never create or destroy mass internally. Below we inspect tanh_stretched_grid_1d and verify_discrete_gauss_divergence_2d and reproduce the 2-D divergence stencil from scratch.

Check against the library


Inside the Algorithm: NumPy Staggered Divergence vs grids_fvm
=============================================================
def tanh_stretched_grid_1d(
    x_min: float,
    x_max: float,
    n_cells: int,
    beta_cluster: float = 2.0,
    two_sided: bool = True,
) -> dict:
    """Generate boundary-layer-clustered 1-D FVM mesh via hyperbolic-tangent stretching."""
    if n_cells < 2 or x_max <= x_min or beta_cluster <= 0.0:
        raise ValueError("Require x_max > x_min, n_cells >= 2, and beta_cluster > 0.")
    xi = np.linspace(0.0, 1.0, int(n_cells) + 1)
    if two_sided:
        s = 0.5 * (1.0 + np.tanh(beta_cluster * (2.0 * xi - 1.0)) / np.tanh(beta_cluster))
    else:
        s = 1.0 - np.tanh(beta_cluster * (1.0 - xi)) / np.tanh(beta_cluster)
    faces = float(x_min) + (float(x_max) - float(x_min)) * s
    centers = 0.5 * (faces[:-1] + faces[1:])
    vols = np.diff(faces)
    return {
        "x_faces": faces,
        "x_centers": centers,
        "volumes": vols,
        "min_dx": float(np.min(vols)),
        "max_dx": float(np.max(vols)),
        "stretching_ratio": float(np.max(vols) / np.min(vols)),
        "n_cells": int(n_cells),
    }

def verify_discrete_gauss_divergence_2d(
    fx_faces: np.ndarray,
    fy_faces: np.ndarray,
    dx: float,
    dy: float,
) -> dict:
    """Verify exact 2-D FVM telescoping identity sum_{i,j} (div_h F)_{i,j} dx*dy == boundary_flux_integral."""
    fx = np.asarray(fx_faces, dtype=float)
    fy = np.asarray(fy_faces, dtype=float)
    nx = fx.shape[0] - 1
    ny = fx.shape[1]
    if fy.shape != (nx, ny + 1):
        raise ValueError("Require fx_faces shape (nx+1, ny) and fy_faces shape (nx, ny+1).")
    div_cells = (fx[1:, :] - fx[:-1, :]) / float(dx) + (fy[:, 1:] - fy[:, :-1]) / float(dy)
    vol_int = float(np.sum(div_cells) * dx * dy)
    bnd_int = float(
        np.sum(fx[-1, :] - fx[0, :]) * dy + np.sum(fy[:, -1] - fy[:, 0]) * dx
    )
    return {
        "div_cells": div_cells,
        "volume_integral": vol_int,
        "boundary_integral": bnd_int,
        "discrepancy": abs(vol_int - bnd_int),
    }

NumPy staggered divergence matches grids_fvm.verify_discrete_gauss_divergence_2d to 1e-15

Exercises

Worked solutions are in 01_solutions.ipynb. The cell below the list is a short self-check to start from.

  1. Verify a result. Verify the β→0+\beta \to 0^+ uniform limit (beta_cluster=1e-5) and two_sided=True mirror symmetry of tanh_stretched_grid_1d, then confirm O(h2)O(h^2) convergence of verify_discrete_gauss_divergence_2d against analytical ∇⋅F\nabla\cdot\mathbf{F} for F=(sin⁡(2x)cos⁡(y), cos⁡(x)sin⁡(2y))\mathbf{F} = (\sin(2x)\cos(y),\, \cos(x)\sin(2y)) across N∈{16,32,64,128}N \in \{16, 32, 64, 128\}.

  2. Find how it fails. Demonstrate across a discontinuous shock-like step uL=2.0 m/su_L = 2.0\ \mathrm{m/s}, uR=0.5 m/su_R = 0.5\ \mathrm{m/s} on [0,1][0, 1] that conservative FVM flux telescoping reproduces the exact momentum flux jump 12(uR2−uL2)=−1.875 m2/s2\frac{1}{2}(u_R^2 - u_L^2) = -1.875\ \mathrm{m^2/s^2} on every grid N∈{20,200,2000}N \in \{20, 200, 2000\}, whereas non-conservative ui(ui+1/2−ui−1/2)u_i(u_{i+1/2}-u_{i-1/2}) fails with an O(1)O(1) conservation error that never vanishes as N→∞N \to \infty. Also verify staggered shape ValueError guards.

  3. Implement it yourself. Implement design_channel_mesh_for_yplus(H_m, n_cells, u_tau_m_s, nu_m2_s, target_yplus=1.0) using bisection on beta_cluster in tanh_stretched_grid_1d(..., two_sided=True) so y1+=uτy0center/ν=target_yplusy_1^+ = u_\tau y_0^{\mathrm{center}} / \nu = \mathrm{target\_yplus}, and test on a 298 K298\ \mathrm{K} water channel (H=0.04 m,N=64,uτ=0.045 m/sH = 0.04\ \mathrm{m}, N = 64, u_\tau = 0.045\ \mathrm{m/s}).


Self-check for the exercises
============================
Beta -> 0 uniform limit and mirror-symmetry checks passed