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`)

Open
#106 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
20/100
Issue type
Refactor
Clarity
Mostly clear
Activity status
Active
Tech stack
jupyter-notebook, python

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

enhancement

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/3d become tuple-packing wrappers (same signature, plus approximation).
  • The 30 same-BC functions + 15 poisson forwarders are generated from a table (keep the names; deprecate the un-numbered solve_helmholtz_dst/dct aliases in favour of dst1/dct2).
  • modify_rhs_1d adopts the Array | None API of 2D/3D (fixes the traced-bc_values failure).
  • _BC_RHS_FORMULAS is 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.py is a starting point).
  • solve_helmholtz_2d(..., approximation="spectral") works for mixed BCs and matches solve_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

  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.