refactor(filters): one implementation of `exponential_filter` / `hyperviscosity` shared by Fourier, Chebyshev and spherical (currently seven copies with inconsistent validation)

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

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Refactor
Clarity
Clearly specified
Activity status
Active
Tech stack
jupyter-notebook, python

Research direction

Start by reading the existing filter implementations in fourier/filters.py, chebyshev/filters.py, and spherical/filters.py to understand the seven copies. Look at the test files for each basis to see how filters are currently tested. The goal is to create a shared module _src/_filters.py with the two multiplier functions, then refactor each filter class to use them. Verify the changes by running the existing filter tests and adding new tests for mean preservation and composition.

Written by the indexing model from the issue text.

Description

enhancement

Part of #83.

Summary

The same two formulas are implemented seven times:

File:line Class
fourier/filters.py:32, 66 SpectralFilter1D.exponential_filter, .hyperviscosity
fourier/filters.py:111, 125 SpectralFilter2D
fourier/filters.py:153, 167 SpectralFilter3D
chebyshev/filters.py:63, 108 ChebyshevFilter1D
chebyshev/filters.py:178, 224 ChebyshevFilter2D
spherical/filters.py:52, 92 SphericalFilter1D
spherical/filters.py:161, 195 SphericalFilter2D

Only the Chebyshev and spherical copies validate alpha/power (with Python if, see #82); the Fourier 2D/3D copies have the corner-normalisation bug (#89); the spherical hyperviscosity docstring has the wrong sign (#100). Two different things share the name hyperviscosity: SpectralDerivative.hyperviscosity(u, nu, order) is a tendency −ν |k|^{2n} u, and SpectralFilter.hyperviscosity(u, nu, dt, power) is the integrating factor exp(−ν |k|^p dt).

Proposed fix

  • One private module _src/_filters.py with pure functions on a "mode magnitude" array:
    • exponential_multiplier(ratio, alpha, power) = exp(−α ratio^p), applied per axis (tensor product) for Fourier/Chebyshev and on l/lmax for spherical.
    • hyperviscous_multiplier(eig, nu, dt, power) = exp(−ν eig^{p/2} dt).
    • Validation via eqx.error_if in one place.
  • Each basis's filter class becomes: get the eigenvalue/mode-ratio arrays from the grid, call the shared multiplier, transform. The classes stay (they know their transforms) but hold no formulas.
  • Rename the integrating-factor method to hyperviscosity_integrating_factor (or hyperdiffuse), keeping hyperviscosity as a deprecated alias, so it no longer collides with the derivative-class tendency.

Acceptance criteria

  • One implementation of each formula; a grep for jnp.exp(-alpha finds exactly one site.
  • All existing filter tests pass; new tests: mean preservation, F(F(u)) == F²(u), F(axis Nyquist) == exp(−α) for every basis.
  • Validation works under jit (traced alpha) for every basis.
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.