refactor(solvers): one N-D Helmholtz kernel driven by a static per-axis BC spec table; the 38 `solve_*` functions become generated wrappers (2523 → ~600 lines, fixes 2D transform ordering, gives mixed BCs `approximation`)
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 20/100
- Issue type
- Refactor
- Clarity
- Mostly clear
- Activity status
- Active
- Tech stack
- jupyter-notebook, python
- Domain
- backend, build-system
Research direction
The work is in spectraldiffx/_src/fourier/solvers.py. Start by reading the current 2523-line file and the dense_check.py audit script. The goal is to replace many specific functions with a single N-D kernel driven by a static BC spec table. Acceptance involves passing all existing tests, adding a new parametrized test, and ensuring performance improvements. 'Done' means the file is under 700 lines and all criteria are met.
Written by the indexing model from the issue text.
Description
Part of #83. Do after #80 (correctness) and the static-fields item of #82.
Current structure of spectraldiffx/_src/fourier/solvers.py (2523 lines)
| Lines | Content |
|---|---|
| 63-86 | BC type aliases |
| 92-108 | _BC_DISPATCH: per-axis (transform, inverse, eigenvalue) table, 9 BC types incl. mixed left/right; no PS-eigenvalue or L info, so approximation is impossible for mixed BCs |
| 111-141 | _lookup_bc, _forward_1d, _inverse_1d |
| 148-204 | _BC_RHS_FORMULAS, _bc_type_for_side, _rhs_correction_1d |
| 207-243 | modify_rhs_1d |
| 246-259 | periodic validation (duplicated later) |
| 262-330 / 333-419 | modify_rhs_2d / modify_rhs_3d (copy) |
| 427-557 | solve_helmholtz_2d: 4-way hand-unrolled branch on (periodic?, periodic?) |
| 560-589 | solve_poisson_2d |
| 597-747 | solve_helmholtz_3d: already a generic per-axis loop, real axes first, FFT last |
| 750-789 | solve_poisson_3d |
| 792-809 | Approximation, _eig_1d |
| 821-1161 | ten 1-D same-BC functions |
| 1169-1413 | six 2-D (fft, dst, dct × helmholtz/poisson) |
| 1416-1420, 1582-1586 | comment-only dead sections |
| 1428-1579 | dst2, dct1 2-D + poisson |
| 1589-1597 | four aliases (solve_helmholtz_dst1 = solve_helmholtz_dst, …), exported, undocumented |
| 1604-1986 | ten 3-D functions |
| 1998-2172 | SpectralHelmholtzSolver1D/2D/3D (separate continuous-k² path, ignores grid.dealias) |
| 2180-2346 | four 3-field 2-D wrapper classes (DirichletHelmholtzSolver2D, NeumannHelmholtzSolver2D, RegularNeumann…, StaggeredDirichlet…), FD2 only |
| 2354-2523 | MixedBCHelmholtzSolver2D/3D |
Totals: 34 public solve_* + 4 aliases + 3 modify_rhs_* + 9 classes. The 30 same-BC solvers are one 12-line skeleton × {fft, dst1, dst2, dct1, dct2} × {1d, 2d, 3d} × {helmholtz, poisson}; the 15 solve_poisson_* are forwarders. All are special cases of the mixed kernel. All 170 BC/λ combinations were verified against dense FD2 Kronecker solves to 1e-15, so there is a strong regression net.
Also measured: solve_helmholtz_2d with periodic-x / real-y (:519-524, 539-543) FFTs first and then runs the real transform twice (real + imag parts) — 9.59 ms vs 5.54 ms for the transposed BC order at 256². The 3-D loop order (real axes first, FFT last) avoids this.
Proposed design
class _AxisSpec(NamedTuple): # static, hashable
family: str # "fft" | "dct" | "dst"
type_: int | None # 1..4 for dct/dst
eig_fd2: Callable[[int, float], Array]
eig_ps: Callable[[int, float], Array]
L_of: Callable[[int, float], float] # (N+1)dx for DST-I, (N-1)dx for DCT-I, N dx otherwise
has_null: bool
left: str; right: str # for modify_rhs corrections
_AXIS_SPECS: dict[BCName, _AxisSpec]
def solve_helmholtz_nd(rhs, spacings: tuple[float, ...], bcs: tuple[BCName, ...], lambda_=0.0, *,
approximation: Literal["fd2", "spectral"] = "fd2",
bc_values: tuple[tuple[Array | None, Array | None], ...] | None = None):
rhs = modify_rhs_nd(rhs, spacings, bcs, bc_values) # one loop over axes and sides
eig = sum of broadcast per-axis eigenvalues − lambda_ # shape = rhs.shape
null guard: static (any(spec.has_null) and lambda_ == 0 static) or jnp.where on the origin
forward: real axes first (dct/dst per axis), then fftn/rfftn over the periodic block
divide; inverse in reverse order; .real after ifftn
solve_helmholtz_2d/3dbecome tuple-packing wrappers (same signature, plusapproximation).- The 30 same-BC functions + 15 poisson forwarders are generated from a table (keep the names; deprecate the un-numbered
solve_helmholtz_dst/dctaliases in favour ofdst1/dct2). modify_rhs_1dadopts theArray | NoneAPI of 2D/3D (fixes the traced-bc_valuesfailure)._BC_RHS_FORMULASis typed as floats but called with arrays; fix the annotations.- The class layer is a separate issue in this epic (
HelmholtzSolver).
Acceptance criteria
- Every existing solver test passes unchanged (they compare against the monolithic implementations to 1e-10).
- New parametrised dense-Kronecker test over all 9×9 BC pairs × λ ∈ {0, 0.7} × {even, odd N} × anisotropic spacing (the audit's
dense_check.pyis a starting point). -
solve_helmholtz_2d(..., approximation="spectral")works for mixed BCs and matchessolve_helmholtz_dst(..., approximation="spectral")for the all-Dirichlet case. - 2D periodic-x/dirichlet-y at 256² within 10% of dirichlet-x/periodic-y (currently 1.7x apart).
-
solvers.py≤ 700 lines; no comment-only dead sections.
- 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