lmweather guide

Architecture, numerical formulations, expression initializers, stability limits, and output formats for the lmweather host.

Overview

lmweather packages two classical geophysical fluid dynamics solvers into a single zero-dependency C++17 executable hosting the lmcode-cpp bytecode VM. Rather than wrapping complex operational forecast suites that require Fortran, NetCDF, and gigabytes of boundary datasets, lmweather provides the exact single-layer rotating shallow-water equations used to test numerical weather prediction cores and study wave dynamics, geostrophic adjustment, and barotropic instability.

Spherical spectral transform model (Sphere)

Created via wx.sphere(T) for Earth parameters or wx.planet(T, radius, omega, g) for arbitrary planetary bodies, the Sphere model integrates the shallow-water equations in vorticity-divergence form using orthonormal associated Legendre functions at triangular truncation T in [4, 170]. Quadratic nonlinear products are evaluated on a dealiasing Gaussian grid where nlon is the smallest power of two at least 3*T + 1 and nlat = nlon / 2 (for example, T21 uses 64x32, T42 uses 128x64, and T85 uses 256x128).

Built-in initializers configure standard benchmarks from Williamson et al. (1992):

Custom flows, surface topography, and passive scalars are configured with fields(h_expr, u_expr, v_expr), orography(hs_expr), and tracer(q_expr). Setting a state resets model time to 0 and records the reference height and tracer fields used by error_l2(), error_linf(), tracer_error_l2(), and tracer_error_linf(); call mark_reference() at any time to re-anchor the reference snapshot. Optional del^4 hyperdiffusion is enabled with diffusion(nu4), which damps high wavenumbers exponentially after each step while preserving rigid-body rotation (n = 1).

Beta-plane staggered C-grid model (Plane)

Created via wx.plane(nx, ny, Lx, Ly), the Plane model solves the rotating shallow-water equations on a rectangular beta-plane channel of width Lx and meridional extent Ly with Coriolis parameter f(y) = f0 + beta * (y - Ly / 2). The grid is periodic in x and bounded by free-slip rigid walls (v = 0) at y = 0 and y = Ly. Fluid thickness h lives at cell centers, zonal velocity u at west cell faces, meridional velocity v at south cell faces, and relative vorticity zeta at cell corners using energy- and enstrophy-consistent Sadourny averaging.

Call depth(H), gravity(g), and coriolis(f0, beta) before initializing fields. Calling linear() switches the solver to the equations linearized about resting depth H (conserving discrete potential vorticity anomaly at every interior corner), while nonlinear() (the default) retains full momentum advection and finite-amplitude thickness fluxes. Optional Laplacian viscosity and linear Rayleigh drag are configured with viscosity(nu) and drag(r).

p = _lmcode_ cli="lmweather" install="github:lmctl-ai/lmweather@latest";
let ch = wx.plane(160, 80, 16000000.0, 8000000.0)
  .depth(2000.0)
  .coriolis(0.0001, 1.6e-11)
  .linear()
  .fields("50.0 * cos(2.0 * pi * x / lx) * sin(pi * y / ly)", "0", "0");
ch.run(2.0 * 86400.0);
wx.assert_close(ch.mean_depth(), 2000.0, 0.000001);
ch.save_csv("rossby_plane.csv");
end _lmcode_
r = run_lmcode(p);
require(r.ok, "beta-plane run failed");

Initial-condition expression language

Because the bytecode DSL has no closures, spatial fields (fields, orography, tracer) accept mathematical expression strings evaluated at each grid node:

Time stepping and CFL stability enforcement

Both models integrate in time using classical fourth-order Runge-Kutta (RK4). Calling run(seconds) selects a safe time step automatically via stable_dt(); calling run_dt(seconds, dt) uses the caller-supplied maximum step dt (shortening the final step if needed to land exactly on seconds).

Because explicit RK4 becomes unstable when |omega * dt| exceeds 2 * sqrt(2) (approximately 2.828), lmweather computes the fastest gravity-plus-advection frequency omega_max before starting run_dt and every 64 steps during integration. If dt exceeds the stability limit initially, or if nonlinear flow acceleration pushes omega_max past the limit mid-run, lmweather halts immediately with an explicit run error (exit code 3) rather than producing corrupted finite numbers.

Output tables and gnuplot integration

Output files are formatted as space-separated ASCII tables with 10 significant digits, organized for direct plotting with lmplot or gnuplot:

Memory budget and resource limits

A single invocation may allocate at most 4096 MiB by default across spectral Legendre tables, grid buffers, and output strings. If a script exceeds this budget, lmweather releases a 1 MiB emergency reserve, emits a structured run failure frame, and exits with code 3. Override the limit in MiB by setting LMWEATHER_MAX_MEMORY_MB in the environment.