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.

ODE integration (Diffrax)

Part 6: The JAX Superpower — JIT, vmap, and PDE Integration (Diffrax)

Coordax Fields are JAX pytrees, so they work seamlessly with jax.jit, jax.grad, and libraries like Diffrax.

6.1 JIT Compilation of a Complex Analysis Pipeline

Temperature: ('time', 'latitude', 'longitude'), shape: (20, 16, 32)
Eager: 270.0 ms
JIT (first, with compilation): 102.5 ms
JIT (subsequent): 0.3 ms
  temp_mean: max diff = 0.00e+00
  temp_anomaly: max diff = 1.43e-05
  spatial_mean: max diff = 0.00e+00

6.2 Solving a 1D Advection-Diffusion PDE (Diffrax)

Equation: ∂T/∂t = −U·∂T/∂x + κ·∂²T/∂x²

Initial condition: shape=(64,), dims=('space',)
T range: 288.0 — 308.0 K
RHS shape: (64,), range: -17.03 — 16.80 K/h

Solve with Diffrax

ODE solve: 1.45s, steps=34
Solution shape: (26, 64)
Final T: ('space',), range: 288.0 — 304.9 K
COM motion (anomaly): 25.00 km  (expected ~25.0 km)

6.3 Automatic Differentiation Through the PDE

Find an optimal diffusion coefficient by differentiating through a short simulation.

Loss at kappa=0.30: 0.0026
dL/dkappa: -0.0105  (sign points toward kappa_true=0.8)

Key Patterns

  • cx.Field objects are JAX pytrees — pass them directly to jax.jit, jax.grad, jax.vmap
  • Diffrax integrates any ODE whose state is a JAX array
  • The untag + spectral/FD derivative pattern works inside diffrax.ODETerm
  • Wrap Diffrax solutions back as coordax Fields with the appropriate axes