Skip to content

Add Klein-Gordon equation stepper#104

Closed
gpartin wants to merge 3 commits intoCeyron:mainfrom
gpartin:feature/add-klein-gordon-stepper
Closed

Add Klein-Gordon equation stepper#104
gpartin wants to merge 3 commits intoCeyron:mainfrom
gpartin:feature/add-klein-gordon-stepper

Conversation

@gpartin
Copy link
Copy Markdown

@gpartin gpartin commented Mar 14, 2026

Summary

Adds a spectral timestepper for the Klein-Gordon equation, extending exponax's PDE solver collection with the relativistic generalization of the wave equation.

What this PR adds

  1. Stepper: exponax/stepper/_klein_gordon.py

    • Solves the d-dimensional Klein-Gordon equation: u_tt = c² Δu - m² u
    • Supports 1D, 2D, and 3D with periodic boundary conditions
    • Uses the same diagonalization approach as the Wave stepper, adapted for the Klein-Gordon dispersion relation ω(k) = √(c²|k|² + m²)
    • Unconditionally stable (solved analytically in Fourier space)
    • Setting mass=0 exactly recovers the Wave stepper
  2. Tests: tests/test_klein_gordon.py (9 test cases)

    • Instantiation for 1D/2D/3D, output shape validation, wrong input shape error
    • mass=0 recovers Wave stepper (short and long evolution)
    • Analytical correctness against exact standing mode solutions (4 wavenumbers)
    • Mass gap verification (k=0 mode oscillates at ω = m)
    • Energy conservation over 200 steps
  3. Integration:

    • Added to exponax/stepper/__init__.py exports and \all\
    • Added to tests/test_builtin_solvers.py instantiation test
    • Updated module docstring in __init__.py

Why Klein-Gordon?

The Klein-Gordon equation is the simplest relativistic wave equation and a cornerstone of quantum field theory. It generalizes the existing Wave stepper with a mass term that introduces a mass gap in the dispersion relation. exponax already has diffusion, advection, wave, Burgers, KdV, and Kuramoto-Sivashinsky steppers — Klein-Gordon is a natural addition.

Design decisions

  • Extends the Wave stepper pattern: Uses the same forward/inverse transform diagonalization, just with ω(k) = √(c²|k|² + m²) instead of ω(k) = c|k|
  • DC correction for mass=0: When mass=0, the k=0 mode needs special handling (same as Wave stepper), ensuring exact backward compatibility
  • Rich docstring: Includes the equation, dispersion relation, mass gap explanation, parameter documentation, and stability notes

gpartin added 2 commits March 11, 2026 12:01
Add a KleinGordon stepper for the relativistic wave equation
uₜₜ = c² Δu - m² u, using the same spectral diagonalization
approach as the Wave stepper.

Key differences from Wave:
- Uses Klein-Gordon dispersion: ω(k) = √(c²|k|² + m²)
- Has mass gap: no modes with ω < m exist
- DC mode (k=0) is diagonalizable when m > 0 (ω(0) = m)
- Setting mass=0 recovers the standard wave equation

Files:
- exponax/stepper/_klein_gordon.py: KleinGordon stepper class
- exponax/stepper/__init__.py: Updated exports
- tests/test_builtin_solvers.py: Added to instantiation tests
- Add test_klein_gordon.py with dedicated solver tests:
  - Instantiation and output shape checks
  - mass=0 recovers Wave stepper (numerical equivalence)
  - Analytical standing-mode correctness
  - Mass gap test (k=0 oscillates at omega=m)
  - Energy conservation bounds
- Update stepper list in __init__.py docstring to include KleinGordon
Copilot AI review requested due to automatic review settings March 14, 2026 12:53
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new Fourier-spectral, analytically-stepped timestepper for the d-dimensional Klein–Gordon equation, expanding exponax’s built-in PDE solver suite in the same style as the existing Wave stepper.

Changes:

  • Introduces KleinGordon stepper with Fourier-space diagonalization and mass=0 compatibility with Wave.
  • Adds a dedicated test suite covering instantiation, mass→wave equivalence, analytical mode evolution, mass gap, and energy behavior.
  • Wires the new stepper into exponax.stepper exports and the built-in instantiation test.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 6 comments.

File Description
exponax/stepper/_klein_gordon.py New Klein–Gordon spectral stepper implementation (diagonalization + optional DC correction for mass=0).
exponax/stepper/__init__.py Exposes KleinGordon in imports, docs, and __all__.
tests/test_klein_gordon.py New tests for Klein–Gordon behavior and compatibility with Wave.
tests/test_builtin_solvers.py Ensures KleinGordon is included in the “all built-ins instantiate” test.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines 99 to 101
from ._klein_gordon import KleinGordon
from ._wave import Wave

import jax.numpy as jnp
import pytest

import exponax as ex
from exponax.stepper import KleinGordon, Wave

L = 2 * jnp.pi
PI = jnp.pi
def test_energy_bounded(self):
"""Total energy should be conserved (bounded) over many steps."""
k0, c, m, N, dt = 3, 1.0, 2.0, 64, 0.005
stepper, x, u0, omega = self._make_stepper_and_ic(k0, c, m, N, dt)
Comment on lines +163 to +164
# KE + gradient PE + mass PE
return jnp.sum(v**2 + c**2 * jnp.abs(jnp.fft.rfft(h))**2 + m**2 * h**2)
u = stepper(u)
e_final = energy(u)

# Spectral solver should conserve energy to machine precision
…gy formula

- Move KleinGordon import to alphabetical position (after _hyper_diffusion)
- Remove unused 'import exponax as ex' (F401)
- Remove unused PI constant (F841)
- Decouple test_energy_bounded from helper to avoid unused variables (F841)
- Fix energy formula: add |k|^2 weighting for gradient PE, proper dx scaling
- Fix misleading 'machine precision' comment to match rel=1e-3 tolerance
@Ceyron
Copy link
Copy Markdown
Owner

Ceyron commented Mar 16, 2026

Thanks again for your interest in extending Exponax. 😊

As I mentioned in #103, unfortunately, I do not have the resources to review AI-generated feature-extending Pull Requests. For contributions that extend Exponax's feature set, please first open an issue to discuss them.

@Ceyron Ceyron closed this Mar 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants