refactor(fourier): N-D `FourierGrid` and `SpectralDerivative` with rfft path and spectral-in/spectral-out operators (replaces three copy-pasted trios, 1.5–1.75x on operators)

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

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
30/100
Issue type
Refactor
Clarity
Mostly clear
Activity status
Active
Tech stack
jupyter-notebook, python

Research direction

The work is in grid.py, operators.py, and filters.py. Start by reading the existing 1D/2D/3D FourierGrid and SpectralDerivative classes to understand the copy-pasted patterns. The goal is to create unified N-dimensional versions using rfft, add spectral-in/spectral-out flags, and ensure all tests pass. Measure performance against the benchmarks in the issue. 'Done' means the combined files are under 900 lines and the speed targets are met.

Written by the indexing model from the issue text.

Description

enhancement

Part of #83. Unblocks #65 (scan-based time stepping needs a cheap spectral-space RHS).

Current state

  • FourierGrid1D/2D/3D (grid.py:48-241, 244-457, 460-763; 194/214/304 lines): identical check_consistency/from_N_L/from_N_dx/from_L_dx/dealias_filter/transform logic hand-unrolled per dimension. Feature drift: only 1D has k_dealias, only 2D/3D have K2; X returns a tuple (X, Y) / (Z, Y, X) and KX returns (KZ, KY, KX) on the 3D grid.
  • SpectralDerivative1D/2D/3D (operators.py, 154/218/195 lines): ~450 of 600 lines are one six-line pattern (transform → multiply by (ik)^n or K2 → mask → inverse → .real) repeated per method × dimension. Only 1D has __call__(u, order). 3D uses (z, y, x) while velocity_from_streamfunction returns (u, v) inside a (z, y, x) class.
  • SpectralFilter1D/2D/3D: 84/40/40 lines, same formula three times.
  • Every transform is a complex fft/fft2/fftn on real data (grid.py:239-241, 455-457, 761-763); rfft halves the work.
  • spectral=True lets callers pass coefficients in, but (except apply_dealias and filters) results always come back in physical space, so operators cannot be composed in spectral space; every notebook falls back to grid.transform + grid.K2 by hand.
  • jacobian always does a final apply_dealias round trip even with dealias=None (operators.py:333, 546).

Measured headroom (CPU, x64, jitted, warm, all variants agree to 7e-12)

Case Current With rfft + fusion Speed-up
2D 256² jacobian (3 fft2 + 5 ifft2) 5.10 ms 2.92 ms 1.75x
2D 256² laplacian 1.37 ms 0.91 ms 1.50x
2D 256² gradient 2.01 ms 1.36 ms 1.48x
3D 64³ gradient (1 fftn + 3 ifftn) 12.89 ms 8.12 ms 1.59x
reference fft2/rfft2 256² 0.39 / 0.28 ms

A typical QG RHS through the public API costs (3+n) forward + (5+n) inverse transforms with ψ and q forward-transformed 2–3 times each; a spectral-in/spectral-out RHS needs 1 rfft + 4 irfft + 1 rfft.

Proposed design

  • FourierGrid(shape: tuple[int, ...], lengths: tuple[float, ...], dealias=...) with per-axis k, cached kmesh, K2, mask (outer product of 1D masks), transform(u, inverse=False) using rfftn/irfftn on the last axis (with a real: bool static flag for users who need the full complex spectrum). FourierGrid1D/2D/3D remain as thin constructors/aliases with the existing from_N_L(Nx=, Ny=, ...) signatures. Rename the tuple-returning X/KX properties to mesh/kmesh (keep old names with a deprecation).
  • SpectralDerivative(grid) with __call__(u, axis, order), gradient, divergence, curl (2D scalar / 3D vector), laplacian, biharmonic, hyperviscosity, inverse_laplacian, jacobian, advection_scalar, velocity_from_streamfunction, project_vector, apply_dealias, each accepting spectral_in: bool and spectral_out: bool (or a single return_spectral), so the RHS of a spectral model can be assembled with one forward and one inverse transform per product. Expose multipliers (ik, −K2, K2**order) for IMEX integrators.
  • SpectralFilter(grid) with one implementation of each filter (tensor-product form per #89).
  • Keep SpectralDerivative1D/2D/3D and SpectralFilter1D/2D/3D as aliases for one release.
  • Preserve (y, x) / (z, y, x) array conventions; return vector components in the same order as the axes everywhere (document the one place it changes).

Acceptance criteria

  • All existing operator, filter and grid tests pass through the aliases.
  • 2D 256² jacobian ≤ 3.2 ms and 3D 64³ gradient ≤ 9 ms on the reference machine (report in PR).
  • A spectral-space QG RHS test that does exactly 1 forward + 4 inverse + 1 forward transform (count via a monkeypatched jnp.fft.rfftn).
  • Non-square (48×20) and non-cubic (8×24×40, different L per axis) analytic tests for every operator (currently only one gradient test is non-square).
  • grid.py + operators.py + filters.py under 900 lines combined (currently 1536).
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.