Skip to content

Add Klein-Gordon equation examples (data gen + FNO training)#713

Open
gpartin wants to merge 2 commits intoneuraloperator:mainfrom
gpartin:add-klein-gordon-example
Open

Add Klein-Gordon equation examples (data gen + FNO training)#713
gpartin wants to merge 2 commits intoneuraloperator:mainfrom
gpartin:add-klein-gordon-example

Conversation

@gpartin
Copy link
Copy Markdown

@gpartin gpartin commented Mar 14, 2026

Summary

Adds two Sphinx Gallery examples demonstrating the Klein-Gordon equation — a relativistic wave equation with a mass/dispersion term. This fills a gap in the existing example gallery, which covers elliptic (Darcy), parabolic (diffusion-advection), and incompressible fluid (Burgers, Navier-Stokes) PDEs, but no hyperbolic PDE with a mass term.

New files

1. examples/data_gen/plot_klein_gordon_1d_solver.py

A finite-difference solver for the 1D Klein-Gordon equation using the library's FiniteDiff class and leapfrog (Verlet) time integration:

add-klein-gordon-example\frac{\partial^2 u}{\partial t^2} = c^2 \frac{\partial^2 u}{\partial x^2} - m^2 uadd-klein-gordon-example

Demonstrates:

  • Wave evolution for three mass values (m=0, 2, 10), illustrating the transition from non-dispersive to strongly dispersive behavior
  • Spacetime diagrams (kymographs)
  • Energy conservation verification (~0.08% drift)

2. examples/models/plot_FNO_klein_gordon.py

Trains a 1D FNO to learn the Klein-Gordon time-evolution operator:

  • Generates training data using a leapfrog finite-difference solver
  • Trains FNO with AdamW + cosine annealing + H1 loss
  • Achieves ~1% relative L2 test error at the training mass
  • Cross-mass generalization sweep: tests the trained model on different mass parameters, showing that prediction error increases substantially as the test mass moves away from the training mass (as expected — the mass term changes the dispersion relation)

Notes

  • Follows existing Sphinx Gallery formatting conventions (# %% cells, .. math:: blocks, .. raw:: html dividers)
  • Formatted with black
  • Uses only standard neuralop APIs (FiniteDiff, FNO, AdamW, LpLoss, H1Loss)
  • Both examples run on CPU in under 2 minutes
  • The Klein-Gordon equation is standard textbook physics (Klein 1926, Gordon 1926)

Add two new Sphinx Gallery examples demonstrating the Klein-Gordon equation:

1. examples/data_gen/plot_klein_gordon_1d_solver.py
   - 1D Klein-Gordon solver using FiniteDiff and leapfrog integration
   - Compares wave behavior across mass parameters (m=0, 2, 10)
   - Spacetime diagrams and energy conservation verification

2. examples/models/plot_FNO_klein_gordon.py
   - Generates training data via leapfrog finite-difference solver
   - Trains 1D FNO to learn the KG time-evolution operator
   - Evaluates predictions on held-out test data
   - Demonstrates cross-mass generalization limits

These examples fill a gap in the existing gallery: no hyperbolic PDE with
a mass/dispersion term was previously covered. The Klein-Gordon equation
(Klein 1926, Gordon 1926) is a standard benchmark in computational physics.
Copilot AI review requested due to automatic review settings March 14, 2026 02:44
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 two new Sphinx Gallery examples to the examples/ gallery demonstrating the 1D Klein–Gordon equation, covering both (1) finite-difference data generation/visualization and (2) training an FNO to learn the time-evolution operator and evaluating cross-mass generalization.

Changes:

  • Add a 1D Klein–Gordon finite-difference solver example using FiniteDiff, with visualization and an energy drift check.
  • Add an FNO training example that generates synthetic Klein–Gordon data, trains an FNO, evaluates test error, and sweeps mass for generalization.

Reviewed changes

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

File Description
examples/data_gen/plot_klein_gordon_1d_solver.py New FD-based Klein–Gordon solver + plots + energy conservation check.
examples/models/plot_FNO_klein_gordon.py New end-to-end example: synthetic Klein–Gordon data gen, FNO training, evaluation, and cross-mass sweep.

💡 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.

import matplotlib.pyplot as plt
import sys
from neuralop.models import FNO
from neuralop import Trainer
Comment on lines +232 to +234
l2loss = LpLoss(d=1, p=2)
h1loss = H1Loss(d=1)

Comment on lines +110 to +117
u_xx = fd.dx(fd.dx(u))
u_prev = u + 0.5 * dt**2 * (c**2 * u_xx - m**2 * u)

snapshots = [u.cpu().numpy().copy()]
energies = []

for step in range(nt):
u_xx = fd.dx(fd.dx(u))
Comment on lines +110 to +117
u_xx = fd.dx(fd.dx(u))
u_prev = u + 0.5 * dt**2 * (c**2 * u_xx - m**2 * u)

snapshots = [u.cpu().numpy().copy()]
energies = []

for step in range(nt):
u_xx = fd.dx(fd.dx(u))
Comment on lines +300 to +304
x_grid = np.linspace(0, 1, nx)

fig, axes = plt.subplots(3, 1, figsize=(10, 8))
for idx in range(3):
data_x = test_x[idx, 0].numpy()
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.

2 participants