Skip to content

Spatial and temporal discretization ​

This page describes the discretization used in the solver, in the same notation as the code. The domain is discretized on a staggered Cartesian grid as proposed by Harlow and Welch [5], and the equations are stepped in time with explicit Runge-Kutta methods [6].

Staggered grid ​

The rectangular domain Ω=∏α=1d[aα,bα] is partitioned into finite volumes

ΩI=∏α=1d[xIα−1α,xIαα],

indexed by the Cartesian index I=(I1,…,Id). The Cartesian index lets us write dimension-agnostic expressions such as uI instead of uij in 2D or uijk in 3D, and the code uses the same convention (u[I] instead of u[i, j] or u[i, j, k]). The α-th unit vector is denoted eα, e.g. I+e2=(I1,I2+1,I3) in 3D. The grid is defined by the volume boundary coordinates xiα passed to Setup (the vectors x[α]), from which the volume widths Δiα=xiα−xi−1α (the vectors Δ[α]) follow. The grid does not need to be uniform.

The unknowns are staggered:

  • The pressure pI is defined in the volume center.

  • The velocity component uIα is defined on the right face of volume I in direction α, i.e. on the boundary between ΩI and ΩI+eα. In the code, all components are stored in one array with the component as the last index: u[I, α].

In addition to the interior volumes, all fields carry one layer of ghost volumes on each boundary. The ghost values are filled by apply_bc_u! and apply_bc_p! such that the interior finite-volume stencils automatically account for the boundary conditions (Dirichlet, periodic, symmetric, or pressure). This is why the field arrays have size N while the degrees of freedom are indexed by Ip and Iu.

Mass equation ​

Integrating the mass equation over the pressure volume ΩI and approximating the face integrals with the mid-point rule gives the discrete divergence-free constraint

∑α=1duIα−uI−eααΔIαα=0,

a backward difference of the face velocities surrounding the pressure volume. This is the divergence operator. Since we divided by the volume size, the discrete equation resembles the continuous one ∇⋅u=0.

Momentum equations ​

The momentum equation for uα is integrated over the shifted volume centered on the α-face. Approximating the face integrals with the mid-point rule and the diffusive fluxes with central differences gives

ddtuIα=−∑β=1d(δβ(uαuβ))I+ν∑β=1d(δβδβuα)I+fIα−(δαp)I,

where δβ denotes the central difference in direction β at the position in question. The pressure gradient is a forward difference of the two pressure values adjacent to the α-face (pressuregradient). In the convective term, the two velocity components in the product uαuβ are not stored at the required position; they are obtained by averaging with weights 1/2 for the α-component and linear interpolation for the β-component. This particular choice preserves the skew-symmetry of the convection operator on uniform grids, such that convection neither creates nor destroys kinetic energy [7]. Convection and diffusion are implemented together in the right-hand side force navierstokes!, with convection and diffusion as separate differentiable operators.

All operators come in two variants: a fast mutating one (e.g. divergence!) and a differentiable non-mutating one (e.g. divergence). See Operators and Differentiating code.

Discrete pressure Poisson equation ​

Instead of discretizing the continuous pressure Poisson equation, we require that the discrete velocity field stays divergence free. Let M denote the discrete divergence, G the discrete pressure gradient, W the diagonal matrix of pressure volume sizes, and F(u) all discrete forces except the pressure gradient. Applying M to the discrete momentum equations and requiring ddtMu=0 yields

Lp=WMF(u),

where L=WMG is a symmetric positive semi-definite discrete Laplacian. This equation is solved by the pressure solvers (poisson, see Pressure solvers). Subtracting the resulting pressure gradient projects a velocity field onto the space of discretely divergence-free fields; this is the project operator. Without pressure boundary conditions L has a zero eigenvalue and the pressure is determined up to a constant, which we set to zero.

Sparse matrix representations of M, G, L, W, and the boundary condition kernels are available, see Sparse matrices.

Time discretization ​

The spatially discretized system is a differential-algebraic system: an ODE for the velocity subject to the algebraic divergence-free constraint. It is stepped in time with explicit Runge-Kutta methods, where each stage velocity is made divergence free by a pressure projection. This retains the accuracy of the underlying Runge-Kutta method for the velocity; see Sanderse and Koren [6] for an analysis.

Given the state un at time tn, a stage i of an explicit method with tableau (A,b,c) computes

ui=Π(un+Δt∑j<iaijF(uj,tj)),

where Π is the pressure projection, and the next step un+1 follows analogously with weights bj. The default method is LMWray3, a low-storage third-order method of Wray [8] that only needs three vector fields of storage. A large collection of tableaus is available in RKMethods.

The time step can be fixed (Δt) or chosen adaptively from a CFL condition based on the convective and diffusive stability limits (Δt = nothing in solve_unsteady, the default).