A physics-informed neural network (PINN) for the 1D acoustic wave equation, in ~150 lines of PyTorch, with every claim validated against the closed-form solution. Solves the forward problem with no solution data at all, and the inverse problem: recovering an unknown wave speed from sparse, noisy measurements.
PINNs are easy to demo and easy to fool yourself with. This repository keeps the problem small enough that an exact solution exists, so accuracy is measured against ground truth rather than against another simulation, and the failure modes are visible instead of hidden.
u_tt = c^2 u_xx x in [0,1], t in [0,1]
u(0,t) = u(1,t) = 0 fixed ends
u(x,0) = sin(pi x) plucked shape, released from rest
Exact solution: u = sin(pi x) cos(pi c t). The network is a plain tanh MLP
(x, t) -> u; the PDE enters as an autograd residual u_tt - c^2 u_xx on
collocation points, with boundary and initial conditions as extra loss terms.
- Forward, c known, no solution data: max error 3.9% of the field amplitude over the whole space-time domain.
- Inverse, c unknown: from 80 measurements with 1% noise, starting the search at c = 0.5, the PINN recovers c = 1.299 vs true 1.30 (0.08% error) because the wave speed is a trainable parameter that the PDE residual couples to the data.
from pinn_acoustics import train_forward, train_inverse, exact_solution
model = train_forward(c=1.0, iters=4000) # forward: physics only
model = train_inverse(x_data, t_data, u_data) # inverse: recovers model.cpython -m pytest tests -q- the residual vanishes on the exact solution and is large for a mismatched wave speed — the two-sided check that the autograd PDE is right
- the autograd residual matches central finite differences on an unrelated smooth field
- a short forward run converges toward the analytic solution (evaluated at t = 0.25, deliberately not t = 0.5 where the standing wave is identically zero and relative error is undefined)
- a short inverse run recovers the wave speed within 5% from a 2.6x-wrong initial guess
- the learnable speed is parameterised as log c, so it stays positive by construction
- One equation, one dimension. This is a validated reference implementation, not a general PDE framework. 2D/3D, variable coefficients, and open boundaries all need more than is here.
- Soft constraints. Boundary and initial conditions are penalty terms with hand-chosen weights, the standard PINN weakness; hard-constraint formulations exist and are not implemented.
- Standing-wave regime. High-frequency or travelling-pulse solutions are much harder for tanh MLPs (spectral bias); expect the accuracy here to degrade there.
- Training uses Adam only; L-BFGS fine-tuning would tighten the forward error further.
MIT.
