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.

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 Δx\Delta x and periodic boundaries, the standard 2nd-order centered difference is

(xu)i  =  ui+1ui12Δx,i{0,,N1},(\partial_x u)_i \;=\; \frac{u_{i+1} - u_{i-1}}{2\,\Delta x},\qquad i \in \{0, \ldots, N-1\},

with indices taken modulo NN. On a non-uniform grid with cell centres xix_i and variable spacing Δxi=xi+1xi1\Delta x_i = x_{i+1} - x_{i-1}, the same stencil generalizes to

(xu)i  =  ui+1ui1xi+1xi1  +  O(ΔxˉΔx),(\partial_x u)_i \;=\; \frac{u_{i+1} - u_{i-1}}{x_{i+1} - x_{i-1}} \;+\; \mathcal{O}(\bar{\Delta x}\,\Delta x'),

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 Δx\Delta x 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:

f(ϕ,λ)  =  =0maxm=amPˉm(sinϕ)eimλ,f(\phi, \lambda) \;=\; \sum_{\ell=0}^{\ell_{\max}} \sum_{m=-\ell}^{\ell} a_\ell^m\, \bar P_\ell^m(\sin\phi)\, e^{im\lambda},

where Pˉm\bar P_\ell^m are the fully-normalized associated Legendre functions and ama_\ell^m are spectral coefficients. In this basis every interesting operator becomes algebra on coefficients:

λ    im,2    (+1)a2,cosϕϕ    three-term -recurrence,\partial_\lambda \;\longleftrightarrow\; im, \qquad \nabla^2 \;\longleftrightarrow\; -\frac{\ell(\ell+1)}{a^2}, \qquad \cos\phi\,\partial_\phi \;\longleftrightarrow\; \text{three-term }\ell\text{-recurrence},

with no 1/cosϕ1/\cos\phi 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 2Nϕ12N_\phi - 1.

For reference, the physical-space identities that motivate the SH form are Durran (2010):

 ⁣ ⁣v  =  1acosϕλu  +  1acosϕϕ(vcosϕ),ζ  =  1acosϕλv    1acosϕϕ(ucosϕ).\nabla\!\cdot\!\mathbf{v} \;=\; \frac{1}{a\cos\phi}\,\partial_\lambda u \;+\; \frac{1}{a\cos\phi}\,\partial_\phi(v\cos\phi),\qquad \zeta \;=\; \frac{1}{a\cos\phi}\,\partial_\lambda v \;-\; \frac{1}{a\cos\phi}\,\partial_\phi(u\cos\phi).

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 uˉi\bar u_i and fluxes Fi+1/2F_{i+1/2} at cell interfaces. The semidiscrete conservation law

duˉidt  =  Fi+1/2Fi1/2Δxi\frac{\mathrm{d}\bar u_i}{\mathrm{d}t} \;=\; -\,\frac{F_{i+1/2} - F_{i-1/2}}{\Delta x_i}

is conservative by construction — the interior flux at i+1/2i+1/2 is exactly cancelled by the adjacent cell’s i1/2i-1/2 flux, so the discrete sum iuˉiΔxi\sum_i \bar u_i \Delta x_i 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

Notebooks

References

References
  1. Durran, D. R. (2010). Numerical Methods for Fluid Dynamics: With Applications to Geophysics (2nd ed.). Springer. 10.1007/978-1-4419-6412-0
  2. LeVeque, R. J. (2002). Finite Volume Methods for Hyperbolic Problems. Cambridge University Press. 10.1017/CBO9780511791253
  3. 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