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)
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
- Domain
- backend-api-design, performance
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
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): identicalcheck_consistency/from_N_L/from_N_dx/from_L_dx/dealias_filter/transformlogic hand-unrolled per dimension. Feature drift: only 1D hask_dealias, only 2D/3D haveK2;Xreturns a tuple(X, Y)/(Z, Y, X)andKXreturns(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)^norK2→ mask → inverse →.real) repeated per method × dimension. Only 1D has__call__(u, order). 3D uses(z, y, x)whilevelocity_from_streamfunctionreturns(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/fftnon real data (grid.py:239-241, 455-457, 761-763);rffthalves the work. spectral=Truelets callers pass coefficients in, but (exceptapply_dealiasand filters) results always come back in physical space, so operators cannot be composed in spectral space; every notebook falls back togrid.transform+grid.K2by hand.jacobianalways does a finalapply_dealiasround trip even withdealias=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-axisk, cachedkmesh,K2,mask(outer product of 1D masks),transform(u, inverse=False)usingrfftn/irfftnon the last axis (with areal: boolstatic flag for users who need the full complex spectrum).FourierGrid1D/2D/3Dremain as thin constructors/aliases with the existingfrom_N_L(Nx=, Ny=, ...)signatures. Rename the tuple-returningX/KXproperties tomesh/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 acceptingspectral_in: boolandspectral_out: bool(or a singlereturn_spectral), so the RHS of a spectral model can be assembled with one forward and one inverse transform per product. Exposemultipliers(ik,−K2,K2**order) for IMEX integrators.SpectralFilter(grid)with one implementation of each filter (tensor-product form per #89).- Keep
SpectralDerivative1D/2D/3DandSpectralFilter1D/2D/3Das 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.pyunder 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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from jejjohnson/spectraldiffx
-
documentation
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
jejjohnson/spectraldiffx#172 ·
-
epic
Difficulty 1/5 Under an hour Newbie friendliness 62/100
jejjohnson/spectraldiffx#167 · 1 comment ·
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
jejjohnson/spectraldiffx#113 ·
-
documentation
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
jejjohnson/spectraldiffx#105 ·
-
dependencies
Difficulty 4/5 3-5 days Newbie friendliness 45/100
jejjohnson/spectraldiffx#181 ·
All issues in jejjohnson/spectraldiffx
Similar issues
-
bug(operator): VolterraOperator built inside jax.jit with a concrete decay raises TracerBoolConversionErrorPossibly taken @jejjohnson claimed this today. Opentype:bug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
jejjohnson/integrix#144 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
finos/ai-governance-framework#459 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
autowarefoundation/autoware_carla_scenario#2963 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
freelawproject/litigant-portal#1307 ·
Maintainers usually reply within 2 days
-
type/bug
Difficulty 1/5 Under an hour Newbie friendliness 86/100
h2oai/h2o-llmstudio#1163 ·
Maintainers usually reply within 1 day