fix(spherical): GFD solvers rebuild `SphericalDerivative2D` (and scipy tables) on every call and cannot be jitted

Open
#98 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
55/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
jupyter-notebook, python

Research direction

The issue points to specific lines in spectraldiffx/_src/spherical/solvers.py and operators.py. Start by reading the constructors of SphericalVorticityInversionSolver, SphericalDivergenceInversionSolver, and SphericalHelmholtzDecomposition. The fix involves moving operator construction from solve/decompose methods to __init__ and modifying SphericalDerivative1D.__init__ to compute nodes directly. Run the provided reproduction script to see the error, then write a test to verify JIT compatibility and performance.

Written by the indexing model from the issue text.

Description

bug

Part of #81.

Summary

SphericalVorticityInversionSolver.solve, SphericalDivergenceInversionSolver.solve and SphericalHelmholtzDecomposition.decompose construct a SphericalPoissonSolver and a SphericalDerivative2D inside the method body. SphericalDerivative2D.__init__ builds a fresh SphericalGrid1D (scipy lpmv + Gauss–Legendre nodes), and SphericalDerivative1D.__init__ does np.array(grid._nodes) (operators.py:130). Two consequences:

  1. Impossible to jax.jit. Under any trace, grid._nodes is a tracer and np.array(...) raises TracerArrayConversionError, even when the solver is closed over and even under eqx.filter_jit.
  2. Eager cost dominated by table construction. 39–250 ms per call at Nx = 32, Ny = 16 for ≤ 0.3 ms of arithmetic.

vmap and grad happen to work because they do not stage the construction.

Reproduction

import jax; jax.config.update("jax_enable_x64", True)
import jax.numpy as jnp, numpy as np
from spectraldiffx import SphericalGrid2D, SphericalVorticityInversionSolver
g = SphericalGrid2D.from_N_L(Nx=32, Ny=16, Lx=2*np.pi, Ly=np.pi)
s = SphericalVorticityInversionSolver(g)
zeta = jnp.sin(g.y)[:, None] * jnp.cos(g.x)[None, :]
jax.jit(lambda z: s.solve(z))(zeta)      # TracerArrayConversionError at operators.py:130

Same for SphericalHelmholtzDecomposition.decompose, eqx.filter_jit(lambda s, z: s.solve(z))(s, zeta), and jax.jit(lambda x: SphericalDerivative2D(g).gradient(x)).

Root cause

  • spectraldiffx/_src/spherical/solvers.py:274-277, 320-322, 410-415: operators built per call.
  • spectraldiffx/_src/spherical/operators.py:127-134, 296-305: SphericalGrid1D / numpy conversion inside __init__.

Proposed fix

  1. Build the derivative and Poisson operators once in each solver's __init__ and store them as fields (the same way deriv_theta is already a field on SphericalDerivative2D).
  2. In SphericalDerivative1D.__init__, compute nodes from _gauss_legendre_nodes_weights(grid.N) in numpy rather than converting grid._nodes back from JAX.
  3. Longer term (with the stable-recurrence issue in this epic): build the tables in JAX with lax.scan, so construction itself is traceable and eqx.filter_jit over a freshly constructed operator works.

Once the spectral θ-derivative issue lands, SphericalDerivative1D is no longer needed on the 2D path at all, which removes the scipy dependency from SphericalDerivative2D entirely.

Acceptance criteria

  • jax.jit(lambda z: solver.solve(z)) works for all three GFD solvers; a test asserts this and compares to the eager result to 1e-12.
  • A test counts SphericalDerivative2D constructions (monkeypatch) across three solve calls: exactly zero after __init__.
  • Eager solve at Nx = 32, Ny = 16 under 2 ms.
Dominant language
Python
Stars
2
Forks
0
Avg merge
8h 13m
Merged PRs (30d)
8

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from jejjohnson/spectraldiffx

All issues in jejjohnson/spectraldiffx

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.