Skip to content

Add 1D wave equation example#7

Open
gpartin wants to merge 1 commit intorezaakb:mainfrom
gpartin:feature/add-wave-equation-example
Open

Add 1D wave equation example#7
gpartin wants to merge 1 commit intorezaakb:mainfrom
gpartin:feature/add-wave-equation-example

Conversation

@gpartin
Copy link
Copy Markdown

@gpartin gpartin commented Mar 11, 2026

Summary

Add a 1D wave equation example to the PINNs-TF2 examples collection.

Problem

The wave equation u_tt = c^2 * u_xx is one of the fundamental PDEs in physics and engineering, but there is currently no wave equation example in the repository.

Solution

This PR adds a complete wave equation example following the existing example structure:

  • examples/wave_equation/train.py — Defines the PDE residual, initial conditions (displacement and velocity), boundary conditions, and generates analytical reference data
  • examples/wave_equation/configs/config.yaml — Hydra configuration with training parameters
  • examples/wave_equation/README.md — Problem description and setup table

Problem Setup

The example solves:

u_tt = c^2 * u_xx,  x ∈ [0,1], t ∈ [0,1], c = 1
u(0, x) = sin(πx)           (initial displacement)
u_t(0, x) = 0               (initial velocity)
u(t, 0) = u(t, 1) = 0       (Dirichlet BCs)

Exact solution: u(x,t) = sin(πx) cos(πt)

Design Choices

  • Uses analytical data generation (root_dir: null) instead of loading from .mat files, keeping the example self-contained with no external data dependencies
  • Enforces both displacement (u) and velocity (v = u_t) initial conditions for the second-order-in-time PDE
  • Network architecture: [2, 50, 50, 50, 50, 50, 1] (5 hidden layers of 50 neurons)
  • 20,000 training epochs with MSE loss

This mirrors the wave equation example in PINNs-Torch, adapted for the TensorFlow 2 API.

Add a new example solving the 1D wave equation u_tt = c^2 * u_xx
with Dirichlet boundary conditions and initial displacement/velocity
conditions. Uses analytical reference data (sin(pi*x)*cos(pi*t))
for validation.

Includes:
- examples/wave_equation/train.py: PDE residual, initial conditions,
  boundary conditions, and analytical data generation
- examples/wave_equation/configs/config.yaml: Hydra configuration
  with training parameters
- examples/wave_equation/README.md: Problem description and setup
Copilot AI review requested due to automatic review settings March 11, 2026 21: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 self-contained 1D wave equation example to the examples/ collection, following the repo’s existing Hydra + pinnstf2.train() example structure and using analytically generated reference data.

Changes:

  • Introduces examples/wave_equation/train.py with PDE residual, initial/boundary conditions wiring, and analytical reference solution generation.
  • Adds examples/wave_equation/configs/config.yaml defining domains, samplers, network architecture, and training settings.
  • Adds examples/wave_equation/README.md documenting the PDE setup and training configuration table.

Reviewed changes

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

File Description
examples/wave_equation/train.py Implements wave-equation residual and analytical solution generation for mesh-based training/validation.
examples/wave_equation/configs/config.yaml Provides Hydra config for mesh, samplers (collocation/IC/BC), model, and trainer settings.
examples/wave_equation/README.md Documents the PDE, exact solution, and problem setup table for the new example.

💡 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 +24 to +29
nx, nt = 256, 201
x = np.linspace(0, 1, nx)
t = np.linspace(0, 1, nt)
X, T = np.meshgrid(x, t, indexing="ij")
exact_u = np.sin(np.pi * X) * np.cos(np.pi * C * T)
return {"u": exact_u}
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.

config.yaml requests initial-condition supervision for both u and v, but read_data_fn() only returns {"u": exact_u}. This will raise a KeyError when Mesh.on_initial_boundary() tries to slice self.solution["v"] for the initial condition sampler. Include an analytical v solution array in the returned dict (e.g., v = ∂u/∂t) or adjust the config to avoid requesting v from the mesh solution.

Copilot uses AI. Check for mistakes.
Comment on lines +16 to +17
| 1D 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.

The markdown table is not formatted consistently with other example READMEs: the rows start with || (double pipe), which renders as an extra empty column in many Markdown parsers. Use a standard table format (single leading | per row) like the other examples under examples/*/README.md.

Suggested change
| 1D Wave Equation | |
|------------------------------|---|
| Quantity | Value |
|------------------------------|-------------------------------|

Copilot uses AI. Check for mistakes.
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