refactor(solvers): one `HelmholtzSolver(grid, bc, approximation)` module replacing nine solver classes; unify `lambda_`/`alpha` and the BC vocabulary with Chebyshev

Open
#107 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
python
Domain
api, backend, devtools

Research direction

The work is in solvers.py, lines 1998-2523. Start by reading the existing nine solver classes and the solve_helmholtz_* functions to understand the four calling conventions. The goal is to unify them into a single HelmholtzSolver class, deprecate the old ones, and ensure all tests pass. Check the dependencies: this issue is part of #83 and depends on an N-D kernel issue. Coordinate with #79 for Chebyshev BC vocabulary changes after it merges.

Written by the indexing model from the issue text.

Description

enhancement

Part of #83. Depends on the N-D kernel issue in this epic.

Summary

There are four calling conventions for the same operation across the package:

Class / function Construction Call Notes
SpectralHelmholtzSolver{1,2,3}D (solvers.py:1998-2172) (grid) .solve(f, alpha, zero_mean, spectral) continuous k² (= approximation="spectral"), ignores grid.dealias, periodic only
DirichletHelmholtzSolver2D, NeumannHelmholtzSolver2D, RegularNeumannHelmholtzSolver2D, StaggeredDirichletHelmholtzSolver2D (:2180-2346) (dx, dy, alpha) (rhs) FD2 only, 2D only
MixedBCHelmholtzSolver{2,3}D (:2354-2523) (dx, dy, bc_x, bc_y, alpha) (rhs, bc_x_values=...) FD2 only
solve_helmholtz_* functions – (rhs, dx, dy, lambda_, approximation) lambda_ not alpha
ChebyshevHelmholtzSolver{1,2}D (grid) .solve(f, alpha, bc_left, bc_right, bc_type) different BC vocabulary
SphericalHelmholtzSolver (grid) .solve(f, alpha, zero_mean)

The three SpectralHelmholtzSolver* are 57 identical lines each except for K2; the four 2D wrappers are 3-field shells over one function each.

Proposed design

class HelmholtzSolver(eqx.Module):
    grid: FourierGrid                       # N-D grid (or spacings + shape until #83's grid item lands)
    bc: tuple[BCName, ...] = eqx.field(static=True)          # per axis; "periodic", "dirichlet", "neumann", ("dirichlet", "neumann"), "dirichlet_staggered", ...
    approximation: Literal["fd2", "spectral"] = eqx.field(static=True, default="fd2")
    alpha: float | Array = 0.0              # dynamic; can be overridden per call
    zero_mean: bool = eqx.field(static=True, default=True)

    def solve(self, rhs, alpha=None, bc_values=None) -> Array
    def eigenvalues(self) -> Array          # exposed for IMEX / preconditioning
    def as_lineax_operator(self)            # tracked in #84
  • Keep the nine existing classes as thin deprecated aliases for one release (SpectralHelmholtzSolver2D(grid) → HelmholtzSolver(grid, bc=("periodic",)*2, approximation="spectral"), etc.).
  • Adopt one parameter name for the Helmholtz constant across functions and classes (alpha is used by every class and by Chebyshev/spherical; the function family uses lambda_; pick alpha and accept lambda_ as a deprecated keyword).
  • Adopt the BC vocabulary of solve_helmholtz_2d (bc_x="dirichlet", tuples for mixed left/right) in Chebyshev too, replacing bc_type= + bc_left/bc_right (coordinate with #79 after it merges; do not touch Chebyshev before).
  • PoissonSolver = HelmholtzSolver with alpha=0; do not keep separate classes.

Acceptance criteria

  • All nine current classes remain importable and produce identical results (tests unchanged) with a DeprecationWarning.
  • HelmholtzSolver is jit-able as an argument (static fields), vmap-able over alpha, and grad w.r.t. alpha, rhs, bc_values (tests).
  • One API page documents HelmholtzSolver; the four 2D wrapper pages are removed from the nav.
  • docs/elliptic_solvers_guide.md uses only HelmholtzSolver and solve_helmholtz_nd.
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.