Skip to content

Add Klein-Gordon equation stepper#103

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

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

Conversation

@gpartin
Copy link

@gpartin gpartin commented Mar 11, 2026

Summary

Adds a \KleinGordon\ stepper for the relativistic wave equation:

2>&1u_{tt} = c^2 \Delta u - m^2 u2>&1

This fills the gap between the existing \Wave\ stepper (massless, ω = c|k|) and more complex nonlinear equations. The Klein-Gordon equation is fundamental to quantum field theory, relativistic physics, and lattice field simulations.

Implementation

Uses the same spectral diagonalization approach as the \Wave\ stepper, extended to the Klein-Gordon dispersion relation:

2>&1\omega(k) = \sqrt{c^2 |k|^2 + m^2}2>&1

Key differences from \Wave:

  • Mass gap: No modes with ω < m exist — massive relativistic dispersion
  • DC mode: Diagonalizable when m > 0 (ω(0) = m ≠ 0), unlike Wave
  • Backward compatible: Setting \mass=0\ recovers the standard wave equation

Files Changed

File Change
\exponax/stepper/_klein_gordon.py\ New \KleinGordon\ stepper class
\exponax/stepper/init.py\ Added import and _all_\ entry
\ ests/test_builtin_solvers.py\ Added to instantiation test list

Usage

\\python
import exponax as ex

kg = ex.stepper.KleinGordon(
num_spatial_dims=2,
domain_extent=10.0,
num_points=64,
dt=0.1,
speed_of_sound=1.0,
mass=1.0,
)
u_next = kg(u)
\\

Motivation

The exponax library covers parabolic (diffusion, reaction-diffusion), hyperbolic (wave, advection), and mixed (KdV, KS, NS) PDEs. The Klein-Gordon equation is a natural addition to the hyperbolic family — it's the simplest massive wave equation and widely used in physics benchmarks (e.g., PDEBench includes Klein-Gordon as a benchmark problem).

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
Copilot AI review requested due to automatic review settings March 11, 2026 19:01
Copy link

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 exponax.stepper.KleinGordon time-stepper implementing the (linear) Klein–Gordon equation via Fourier-space diagonalization, analogous to the existing Wave stepper but with massive dispersion.

Changes:

  • Introduces KleinGordon stepper with dispersion relation ( \omega(k)=\sqrt{c^2|k|^2+m^2} ) and a mass=0 fallback to Wave’s DC-mode behavior.
  • Exposes KleinGordon from exponax.stepper (__init__.py import + __all__).
  • Adds KleinGordon to the built-in solver instantiation smoke test list.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
exponax/stepper/_klein_gordon.py New Klein–Gordon stepper implementing spectral diagonalization + DC handling when mass=0.
exponax/stepper/init.py Exports KleinGordon and adds brief module doc mention.
tests/test_builtin_solvers.py Ensures KleinGordon can be instantiated across spatial dimensions.

💡 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 20 to 22
ex.stepper.Wave,
ex.stepper.KleinGordon,
ex.stepper.Burgers,
Copy link

Copilot AI Mar 11, 2026

Choose a reason for hiding this comment

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

KleinGordon is only covered by the generic instantiation smoke test here. Since this is a new solver with nontrivial Fourier-space diagonalization and a documented compatibility guarantee (mass=0 should recover Wave), please add solver-specific tests (e.g., output shape/finite checks and a numerical equivalence test against Wave when mass=0, plus at least one basic behavior/energy or boundedness check similar to tests/test_wave.py).

Copilot uses AI. Check for mistakes.
Comment on lines +64 to +66
The KleinGordon stepper extends the Wave stepper with a mass term, using the
Klein-Gordon dispersion relation ω(k) = √(c²|k|² + m²). Setting m=0 recovers
the wave equation.
Copy link

Copilot AI Mar 11, 2026

Choose a reason for hiding this comment

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

This new paragraph documents KleinGordon, but the earlier “The concrete PDE steppers are:” list in the same module docstring still omits it. Please update that list to include KleinGordon so the public-facing documentation remains consistent.

Copilot uses AI. Check for mistakes.
@Ceyron
Copy link
Owner

Ceyron commented Mar 12, 2026

Hi,
Thanks a lot for proposing an extension to the library 😊.

First, a quick question: Are you an AI agent?

@gpartin
Copy link
Author

gpartin commented Mar 12, 2026

Hi @Ceyron, thanks for the interest! Yes, this PR was drafted with AI assistance (GitHub Copilot) but I reviewed and tested everything manually. The Klein-Gordon stepper follows the same spectral architecture as your existing Wave stepper — I verified numerical equivalence when mass=0.

I'll address the Copilot reviewer's suggestions:

  1. Add solver-specific tests (mass=0 should recover Wave, shape/finite checks)
  2. Update the stepper list in the module docstring

Should have those pushed shortly.

@gpartin
Copy link
Author

gpartin commented Mar 12, 2026

Hi @Ceyron, thanks for the interest! Yes, this PR was drafted with AI assistance (GitHub Copilot) but I reviewed and tested everything manually. The Klein-Gordon stepper follows the same spectral architecture as your existing Wave stepper -- I verified numerical equivalence when mass=0.

I'll address the Copilot reviewer's suggestions:

  1. Add solver-specific tests (mass=0 should recover Wave, shape/finite checks)
  2. Update the stepper list in the module docstring

Should have those pushed shortly.

- 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
@Ceyron
Copy link
Owner

Ceyron commented Mar 12, 2026

@Ceyron Ceyron closed this Mar 12, 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