fix(spherical): conventions and dead code — coefficient scale, negative-m rule, explicit `radius`, `weights` ignore R, `Lx ≠ 2π` inconsistency, unused `dealias`/`m`/`spectral` flags, docstring sign errors

Open
#100 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
Documentation
Clarity
Mostly clear
Activity status
Active
Tech stack
jupyter-notebook, python

Research direction

The issue lists many specific files: harmonics.py, grid.py, operators.py, filters.py, solvers.py, and docs/theory/spherical.md. Start by reading the theory documentation and the class docstrings to understand the spherical harmonic conventions. Then, examine the transform and coefficient scaling in harmonics.py and grid.py. Verify the numerical tests mentioned, and check for unused code with ruff. The goal is to correct documentation, fix sign errors, and remove dead code, ensuring all changes are backward compatible and tested.

Written by the indexing model from the issue text.

Description

bug documentation

Part of #81.

Everything here was verified numerically; none of it changes results for the default Lx = 2π, Ly = π grid, but all of it misleads users or blocks interop with other SH libraries.

Conventions (documented wrongly)

  • Coefficient scale. The basis actually used is P̃_l^{|m|}(cos θ) e^{imφ} with P̃ orthonormal on [−1, 1] w.r.t. dμ and an unnormalised FFT, so the stored coefficient of an orthonormal (scipy sph_harm_y) Y_lm is Nx / sqrt(2π) (measured 12.766 = 32/√(2π) for every (l, m) tested, leakage ≤ 2e-13). harmonics.py:16-19 says "4π-normalised in the real convention"; docs/theory/spherical.md §2 says "fully normalised". Neither describes the stored numbers; a power spectrum or a comparison with pyshtools/SHTns is off by Nx²/(2π). Document the exact scale, or normalise the transform so that coefficients are orthonormal-Y_lm coefficients (preferred; then a power spectrum is Σ_m |c_lm|²).
  • Negative m. c[l, −m] = conj(c[l, m]) (measured), vs the standard (−1)^m conj(c[l, m]); the coefficient of scipy's Y_l^{−m} is (−1)^m Nx/√(2π). Only documented in a code comment (grid.py:490-496). Document in the class docstring and theory page.
  • Condon–Shortley phase is included (via lpmv); harmonics.py:18-19 ("no Condon–Shortley phase … beyond what lpmv supplies") is confusing. State it plainly.
  • Alias-free range. The Ny-point Gauss–Legendre rule is exact for P̃_l^m P̃_l'^m (degree ≤ 2Ny − 2), so the linear transform is alias-free for l ≤ Ny − 1, |m| ≤ Nx/2. Nothing enforces Nx ≥ 2Ny − 1; with smaller Nx the truncation is silently rectangular. docs/theory/spherical.md §5 says J ≥ (3L+1)/2 nodes are needed — that is the quadratic-product condition; the grid provides J = L + 1 and dealias is never applied (below). Fix the doc; consider a warning when Nx < 2Ny − 1.

Radius handling

  • SphericalGrid2D.weights (grid.py:603-610) sums to 2·Lx = 4π regardless of radius (12.566 measured with R = 2; 4πR² = 50.27), while tests/test_spherical_grid.py:175-186 claims "area = 4πR²" and only tests R = 1. Either scale by R² or rename/document as unit-sphere weights; test at R ≠ 1.
  • Radius is Ly / π (solvers.py:54-57, operators.py:328, 374, 420, 498, filters.py:548, 646). Replace with an explicit radius attribute/constructor argument; keep Ly for back-compat with a deprecation.
  • Lx ≠ 2π is inconsistent: grid.m and derivatives use 2πk/Lx (operators.py:338) but _P_lm uses the integer FFT index as the ALP order (grid.py:487-500); measured grid.m[:4] = [0, 0.5, 1, 1.5] vs orders 0..3 for Lx = 4π. Fix Lx to 2π (raise otherwise) once radius exists.

Dead code / ignored arguments

  • dealias, l_dealias, dealias_filter (grid.py:191, 337-365, 456, 640-664) are never applied by any transform or operator (only tests touch them). Remove, or apply where products are formed.
  • SphericalGrid2D.m (grid.py:618-621) unused.
  • spectral argument of divergence/curl (operators.py:353, 396) accepted and ignored.
  • _gradient_alp_matrix (operators.py:56-89) is a two-line wrapper; nodes_weights, laplacian_eigenvalues unused internally.
  • SphericalGrid1D.L, dx, check_consistency, from_L_dx (grid.py:221-273) are copy-pasted from the Fourier grid and meaningless for non-uniform Gauss–Legendre nodes. Rename the class to LegendreGrid1D/ZonalGrid1D (with alias) and drop the uniform-grid members.

Docstring sign/formula errors

  • Curl sign in operators.py:23 and :280 is the negative of what the code and :400 implement.
  • Hyperviscosity docstring ∂u/∂t = (−1)^{p+1} ν ∇^p u (filters.py:522-524) is anti-diffusive for p = 2; should be (−1)^{p/2+1}.
  • grid.py:24-28 stability envelope (see the NaN issue); grid.py:420-426 shows a ½ factor not present in the code; solvers.py:176-179 zero_mean claim (see #92); docs/api/spherical/solvers.md omits the three GFD solvers.

Acceptance criteria

  • Test comparing transform(Y_lm) against scipy.special.sph_harm_y for a handful of (l, ±m) with the documented scale and sign.
  • Test weights.sum() == 4πR² at R ∈ {1, 2, 6.371e6} (or the documented unit-sphere value).
  • ruff reports no unused code after removal; mkdocs build --strict passes.
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.