Skip to content

feat(wave): opt-in 9-point isotropic Laplacian for WaveSimulation2D (closes #3095) - #3104

Merged
zackees merged 1 commit into
masterfrom
fix/3095-wave-9point-stencil
Jun 15, 2026
Merged

zackees merged 1 commit into
masterfrom
fix/3095-wave-9point-stencil

Conversation

@zackees

@zackees zackees commented Jun 15, 2026 •

Copy link
Copy Markdown
Member

Implements #3095 (§5 from meta tracker #3098). Adds an opt-in 9-point isotropic Laplacian to the wave PDE solver behind WaveFx.

API

New enum class LaplacianStencil { FivePoint, NinePointIsotropic } plus setStencil/getStencil on WaveSimulation2D_Real. Default is FivePoint (backward compatible).

Formula

5-point: lap = N + S + E + W - 4*C (existing).
9-point isotropic: 6 * lap = (NW+NE+SW+SE) + 4*(N+S+E+W) - 20*C. The /6 is folded into the per-cell term computation after the Q15 multiply.

Both are O(h²); the 9-point form has isotropic leading error, giving rounder ripples at the cost of ~2× reads + ALU per cell. The kernel is otherwise unchanged (i64-promoted multiply, damping shift, half-duplex+Q15 clamp).

Auto-selection

WaveSimulation2D::init() (the super-sampled wrapper in wave_simulation.cpp.hpp) auto-selects NinePointIsotropic when mMultiplier >= 2. At native LED-grid resolution the 5-point anisotropy is invisible; at SUPER_SAMPLE_2X/4X/8X it becomes obvious, and the user has already opted into more CPU.

Validation

Three new test cases in tests/fl/math/wave/wave_simulation_real.cpp:

  • Stencil getter/setter round-trips and default == FivePoint.
  • Constant input ⇒ uniform output (proves lap(constant) = 0 for both).
  • Delta-impulse: diagonal neighbors are 0 under 5-point but nonzero under 9-point (proves isotropic energy spread).

bash test fl_math_wave_wave_simulation_real --cpp — 8/8 pass.
bash lint --cpp — clean.

Closes #3095
Refs #3098 (parent meta), #3099 §2, #3101 §3, #3103 §4

Summary by CodeRabbit

  • New Features

    • Wave simulations now support selecting between standard 5-point and 9-point isotropic Laplacian stencils for enhanced numerical accuracy and simulation quality.
  • Tests

    • Added comprehensive tests verifying stencil selection functionality, constant-input stability, and impulse response behavior across different stencil options.

Adds a discrete-Laplacian choice to WaveSimulation2D_Real:

  enum class LaplacianStencil { FivePoint, NinePointIsotropic };
  void setStencil(LaplacianStencil) FL_NOEXCEPT;
  LaplacianStencil getStencil() const FL_NOEXCEPT;

FivePoint (the existing N+S+E+W - 4C stencil) is the default — backward
compatible. NinePointIsotropic implements:

  6 * lap = (NW+NE+SW+SE) + 4*(N+S+E+W) - 20*C

which is O(h²) like 5-point but with isotropic leading error. The /6
is folded into the per-cell term computation after the Q15 multiply.
The wave kernel is otherwise unchanged (same i64-promoted multiply,
damping shift, half-duplex+Q15 clamp).

The super-sampled wrapper WaveSimulation2D::init() auto-selects
NinePointIsotropic when mMultiplier >= 2: the 5-point stencil's
anisotropy (square-ish ripples) is invisible at native LED-grid
resolution but becomes obvious at SUPER_SAMPLE_2X/4X/8X, where the
user has already opted into more CPU.

Validated by tests/fl/math/wave/wave_simulation_real.cpp:
  - Stencil getter/setter round-trips and default == FivePoint.
  - Constant input ⇒ uniform output (lap(constant) = 0 for both).
  - Delta-impulse: diagonal neighbors are 0 under 5-point but nonzero
    under 9-point (proves isotropic energy spread).
All 8 test cases pass.

Closes #3095
Parent meta: #3098
@coderabbitai

coderabbitai Bot commented Jun 15, 2026 •

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 61fafa2c-0199-4da7-a721-61d230269a22

📥 Commits

Reviewing files that changed from the base of the PR and between b22674e and 973f8f6.

📒 Files selected for processing (4)
  • src/fl/math/wave/wave_simulation.cpp.hpp
  • src/fl/math/wave/wave_simulation_real.cpp.hpp
  • src/fl/math/wave/wave_simulation_real.h
  • tests/fl/math/wave/wave_simulation_real.cpp

📝 Walkthrough

Walkthrough

Adds a LaplacianStencil enum (FivePoint, NinePointIsotropic) to WaveSimulation2D_Real with setStencil/getStencil API. The update() loop gains a two-path implementation selecting between the original 5-point formula and a 9-point isotropic formula (scaled by 6). WaveSimulation2D::init auto-selects NinePointIsotropic when mMultiplier >= 2. Three regression tests cover the new behavior.

Changes

9-point isotropic Laplacian stencil

Layer / File(s) Summary
LaplacianStencil enum and public API
src/fl/math/wave/wave_simulation_real.h
Declares LaplacianStencil enum (FivePoint = 0, NinePointIsotropic = 1), adds setStencil/getStencil public methods, and a private mStencil member default-initialized to FivePoint.
9-point isotropic Laplacian in update loop
src/fl/math/wave/wave_simulation_real.cpp.hpp
Hoists nine_point flag from mStencil; per-cell loop branches to the 9-point isotropic formula (diag + (nbr << 2) - 20 * c, then term / 6) or the original 5-point formula and Q15 term path.
Auto-select stencil for super-sampled mode
src/fl/math/wave/wave_simulation.cpp.hpp
In WaveSimulation2D::init, calls mSim->setStencil(LaplacianStencil::NinePointIsotropic) when mMultiplier >= 2.
Stencil regression tests
tests/fl/math/wave/wave_simulation_real.cpp
Three FL_TEST_CASE tests: getter/setter round-trip with default check; constant-input no-variation test for 9-point; and single-cell delta test confirming diagonal behavior differs between 5-point and 9-point.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related issues

Possibly related PRs

  • FastLED/FastLED#3103: Modifies the same WaveSimulation2D_Real::update() inner loop in wave_simulation_real.cpp.hpp, refactoring neighbor access via row pointers in the same Q15 update structure this PR extends.
  • FastLED/FastLED#3083: Also modifies WaveSimulation2D_Real::update() fixed-point Laplacian/term arithmetic, changing Q15 multiply/shift to use wider i64 intermediates and CFL clamping — directly adjacent to the stencil scaling logic added here.

Poem

🐇 Hop, hop across the grid so wide,
Nine points of Laplacian, spread far and wide!
No more square ripples — circles arise,
Diagonals counted, a rounder surprise.
Super-sampled waves now softly flow,
Through isotropic fields, a beautiful glow! ✨

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/3095-wave-9point-stencil

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@fastled-project-sync fastled-project-sync Bot moved this to Triage in FastLED Tracker Jun 15, 2026
@zackees
zackees merged commit 527216b into master Jun 15, 2026
7 of 8 checks passed
@zackees
zackees deleted the fix/3095-wave-9point-stencil branch June 15, 2026 21:31
@fastled-project-sync fastled-project-sync Bot moved this from Triage to Done in FastLED Tracker Jun 15, 2026
zackees added a commit that referenced this pull request Jun 15, 2026
…ample sketches (#3110)

Background: PR #3104 added the opt-in 9-point isotropic Laplacian to
WaveSimulation2D_Real and auto-selected it in the supersampled wrapper
only at mMultiplier >= 2. That auto-select gate is too conservative —
the 5-point anisotropy artifact (square-ish bright ring) is a property
of the stencil, not the grid resolution, and shows up at 1x on any
16x16+ grid with gradient coloring (which is exactly what Wave2d ships
with by default).

This PR lets humans A/B compare on their hardware via a UI checkbox in
five 2D-wave example sketches.

## Source-side API additions (header-only passthroughs)

- WaveSimulation2D::setStencil(LaplacianStencil) — passthrough to inner
  _Real. Marks the choice as "user set" so subsequent setSuperSample()
  calls preserve it across the wrapper's re-init instead of re-applying
  the multiplier-based default.
- WaveSimulation2D::getStencil() — getter.
- WaveFx::setStencil(LaplacianStencil) — passthrough to the wrapper.
- WaveFx::getStencil() — getter.
- Private bool mUserSetStencil in WaveSimulation2D tracks user override.

No new tests required; existing wave_simulation_real.cpp suite covers
setStencil/getStencil on the inner class and continues to pass (8/8).

## Example sketches — UI toggle added

- examples/Wave2d/Wave2d.h — single WaveSimulation2D; one checkbox in
  the existing waveSimControls group.
- examples/Fx/FxWave2d/wavefx.cpp — two-layer WaveFx demo; one checkbox
  per layer (Upper / Lower) so layers can A/B independently.
- examples/Downscale/Downscale.h — two-layer WaveEffect (wraps two
  WaveFxPtr); one checkbox, both layers track.
- examples/XYPath/complex.h — same WaveEffect pattern; one checkbox.
- examples/FestivalStick/curr.h — single WaveFx in drawWave(); one
  checkbox in the waveGroup.

UI label across all sketches: "Isotropic stencil (rounder ripples)".
Default is unchecked (5-point), preserving existing behavior. Toggle is
applied after setSuperSample() so the user choice wins over the
wrapper's auto-select.

## Validation

- bash test fl_math_wave_wave_simulation_real --cpp — 8/8 pass.
- bash lint --cpp — clean.
- bash compile wasm --examples Wave2d / Downscale / XYPath /
  FestivalStick — all build cleanly. (FxWave2d's nested examples/Fx/
  path can't be driven through the build tool from the command line but
  uses the same WaveFx::setStencil API exercised by the other four
  WaveFx-using sketches.)

Closes #3109
zackees added a commit that referenced this pull request Jun 15, 2026
…3112)

Delivers the research-bot deliverable specified by #3111: a definitive
recommendation document covering the 10 approach families catalogued in
the issue body (compact higher-order stencils, output anti-aliasing
filters, implicit time integration, spectral/FFT, lattice Boltzmann,
procedural/non-PDE, adaptive mesh refinement, low-precision Q7/Q8
storage, industry SOTA, anti-aliasing post-process).

Each candidate is scored against the FastLED embeddability matrix
(AVR / Cortex-M0 / Cortex-M4+ / ESP32-S3 / Host) with cycles-per-cell
estimates and explicit RAM overhead. Non-recommendations are
catalogued with reasons so future research doesn't re-walk dead ends.

Headline findings, after surveying ~25 papers + open-source impls:

1. Compact O(h^4) 9-point Laplacian (Walstijn-Kowalczyk style; Thampi-
   Ansumali Patra-Karttunen family) — same memory access pattern as
   the existing isotropic 9-point added in #3104, but O(h^4) accuracy
   instead of O(h^2). Drop-in win at the existing stencil-selection
   switch. *Top recommendation.*

2. Output Gaussian blur (separable 3x3) as an explicit alternative to
   super-sample. Super-sample's primary user benefit is AA on the
   rendered LED grid, not better physics — a screen-space blur
   delivers comparable visual quality at 1.05-1.2x scalar cost vs
   8-512x for SuperSample 2X/4X/8X. *Second recommendation.*

3. WaveProceduralFx — a sum-of-Gerstner-wavelets ripple synthesizer
   that bypasses the PDE entirely. For AVR / Low-memory targets where
   neither the scalar PDE nor compact-4 fits well, this is dramatically
   cheaper (zero grid memory, O(N_wavelets) per cell) and visually
   strong. References Finch (NVIDIA GPU Gems Ch.1), Quilez Simple
   Water, and Tessendorf-without-FFT. *Third recommendation.*

Conditional finds (worth implementing only on specific platforms):
- ADI implicit time integration (ESP32-S3 with PSRAM only)
- Tessendorf FFT propagation (ESP32-S3+ only — uses CMSIS-DSP)

Explicit dead ends:
- TAA: no motion vectors in static-camera wave field
- LBM D2Q5: memory cost rules out AVR/M0
- Crank-Nicolson: strictly dominated by ADI for our topology
- SMAA/FXAA: wrong scale (designed for 1080p+, not 16x16)
- 13-point/17-point stencils: diminishing returns past O(h^4)

Also recommends three new sub-issues spawned from items 1/2/3, and
explicitly puts the pending SIMD work (#3106/#3107/#3108) on hold —
items 1, 2, 4 above each beat SIMD's typical 4-8x speedup, and the
algorithmic wins compose with later SIMD tuning.

Document is ~4500 words, 25 inline citations, structured per the
output schema specified in the issue body. Lives at
agents/docs/research/wave_pde_alternatives.md.

Closes #3111
Refs #3098 (parent audit meta), #3104 (9-point Laplacian), #3110
(UI toggle)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

feat(wave): opt-in 9-point isotropic Laplacian for super-sampled mode (refs #3073 §5)

1 participant