Derivatives — finite differences and finite volumes on labeled grids
Derivatives — finite differences, spherical harmonics, and finite volumes¶
Once state lives in a Field with named axes, numerical derivatives become
coordinate-aware: the operator uses the axis ticks to pick a stencil, apply
boundary conditions, and emit a Field on the correct (possibly shifted)
grid. The three notebooks in this section build up from 1-D finite differences
Durran (2010), through spherical-harmonic derivatives on a lat-lon
grid (where FD runs into metric singularities at the poles), to finite-volume
flux divergence LeVeque (2002).
Finite differences¶
For a uniform grid with spacing and periodic boundaries, the standard 2nd-order centered difference is
with indices taken modulo . On a non-uniform grid with cell centres and variable spacing , the same stencil generalizes to
i.e., it stays second-order only when spacing varies slowly. Coordax implements this by reading the tick vector from the axis rather than assuming is constant.
Spherical harmonics on a lat-lon grid¶
On a lat-lon grid finite differences are not the right tool — not because they can be made to work (they can, with care), but because the alternative is dramatically better. The standard spectral approach Durran (2010)Williamson et al. (1992) expands a field in spherical harmonics:
where are the fully-normalized associated Legendre functions and are spectral coefficients. In this basis every interesting operator becomes algebra on coefficients:
with no anywhere inside the spectral flow — the pole singularity lives entirely in the physical-space metric, not in the coefficients. The transform uses a Gauss–Legendre latitude grid so Legendre quadrature is exact up to degree .
For reference, the physical-space identities that motivate the SH form are Durran (2010):
The notebook demonstrates forward + inverse SHT on a Gauss–Legendre grid, verifies machine-precision round-trip on an exact spherical harmonic, and compares spectral vs finite-difference accuracy on a bandlimited test field — typically 10+ orders of magnitude in favor of the spectral method.
Finite volumes¶
Finite-volume (FV) methods LeVeque (2002) work with cell averages and fluxes at cell interfaces. The semidiscrete conservation law
is conservative by construction — the interior flux at is exactly cancelled by the adjacent cell’s flux, so the discrete sum is preserved up to boundary contributions. This is the property you want for tracer transport, mass balance, and any quantity that must globally conserve.
Numerical considerations¶
- Stencil shifts. Centered differences live on the same grid; FV fluxes
live on the staggered grid. Coordax tracks this: a 1st-order derivative
returns a
Fieldon a shifted axis, and youreindex_likeback to the cell centres before combining with the original field. - Boundary conditions. Periodic BCs are “free” — just roll the array.
Dirichlet / Neumann require padding; the examples use
jnp.padand document the resulting stencil accuracy drop at the boundary. - Pole singularity. The factor blows up at . Real GCMs use pole filters, polar caps, or non-singular grids (cubed sphere, Yin-Yang). The notebooks clamp ϕ away from the pole and document the limit.
- CFL for advection. When these derivatives feed into a time stepper, the advective stability bound is ; diffusive stability needs . Pick the smaller.
cmappattern. For operators that naturally act on one axis at a time (most of the ones here), coordax provides acmap-style decorator that vmaps the rest — equivalent tojax.vmap(..., in_axes=...)but dispatched by axis name.
Notebooks¶
05_finite_difference— periodic and non-uniform 1-D derivatives; the coordinate-awarecmapdispatch pattern.06_spherical_harmonics_derivatives— spherical-harmonic derivatives on a Gauss-Legendre lat-lon grid; algebraic operators on coefficients (, , ); machine-precision round-trip and a spectral vs finite-difference accuracy showdown.07_finite_volume— cell-centred FV operators and flux divergence; a conservation check that verifies tracer mass preservation at the ~1e-6 relative level on the committed example (bulk flux cancellation; not formally bit-exact because the chosen driver only updates interior cells).
References¶
- Durran, D. R. (2010). Numerical Methods for Fluid Dynamics: With Applications to Geophysics (2nd ed.). Springer. 10.1007/978-1-4419-6412-0
- LeVeque, R. J. (2002). Finite Volume Methods for Hyperbolic Problems. Cambridge University Press. 10.1017/CBO9780511791253
- Williamson, D. L., Drake, J. B., Hack, J. J., Jakob, R., & Swarztrauber, P. N. (1992). A Standard Test Set for Numerical Approximations to the Shallow Water Equations in Spherical Geometry. Journal of Computational Physics, 102(1), 211–224. 10.1016/S0021-9991(05)80016-6