A clean, educational implementation of Physics-Informed Neural Networks (PINNs) solving the 1D wave equation. This repository demonstrates how neural networks can learn to satisfy partial differential equations (PDEs) without any training data - just physics!
Figure: PINN output before and after training for the 1D wave equation
For the 1D wave equation with initial conditions:
- Initial condition:
u(x,0) = sin(πx)
- Initial velocity:
∂u/∂t(x,0) = 0
- Boundary conditions:
u(0,t) = u(1,t) = 0
The analytical solution is: u(x,t) = sin(πx)cos(πct)
Why the straight line? The graph on the right shows the solution at a specific time when cos(πct) ≈ 0
. This occurs at:
t = π/(2πc) = 1/(2c)
t = 3π/(2πc) = 3/(2c)
- And so on...
At these times, u(x,t) = sin(πx) × 0 = 0
for all x, resulting in a horizontal line at zero. This actually demonstrates that the PINN has correctly learned the physics - it knows the wave amplitude should be zero at these specific times!
- <1% error compared to analytical solution
- 69 seconds training time on GPU
- Zero training data required - only physics laws
- 4000x loss reduction in 5000 epochs
- Continuous predictions at any point in space-time
- 🌟 Overview
- 🚀 Installation
- ⚡ Quick Start
- 📁 Project Structure
- 🧠 How It Works
- 📊 Examples
- 📈 Results
- 🔧 Applications
- 📖 Citation
- 📄 License
Physics-Informed Neural Networks (PINNs) are a novel approach to solving partial differential equations using deep learning. Unlike traditional numerical methods that discretize the domain into grids, PINNs learn a continuous solution function that satisfies the underlying physics everywhere.
This project solves the 1D wave equation:
∂²u/∂t² = c² ∂²u/∂x²
With:
- Initial condition:
u(x,0) = sin(πx)
- Initial velocity:
∂u/∂t(x,0) = 0
- Boundary conditions:
u(0,t) = u(1,t) = 0
- Python 3.8+
- CUDA-capable GPU (optional but recommended)
- Clone the repository:
git clone https://github.com/janhavi-giri/pinn-wave-equation.git
cd pinn-wave-equation
- Create a virtual environment:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
- Install dependencies:
pip install -r requirements.txt
from src.model import WavePINN
from src.train import train_model
import torch
# Create model
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = WavePINN().to(device)
# Train
history = train_model(model, epochs=5000)
# Evaluate
from src.evaluate import evaluate_model
evaluate_model(model)
python examples/generate_visualizations.py
This creates all plots in the outputs/
directory.
pinn-wave-equation/
├── README.md
├── LICENSE
├── requirements.txt
├── setup.py
├── .gitignore
│
├── src/
│ ├── __init__.py
│ ├── model.py # PINN architecture
│ ├── losses.py # Physics-informed loss functions
│ ├── train.py # Training loop
│ ├── evaluate.py # Evaluation metrics
│ └── visualization.py # Plotting utilities
│
├── examples/
│ │ ├── generate_visualizations.py # Create all plots
│
├── notebooks/
│ ├── notebook-01-intro.ipynb
│ ├── notebook-02-wave.ipynb
│ └── notebook-03-advanced.ipynb
│
├── tests/
│ ├── test-model.py
│ ├── test-losses.py
│ └── test-physics.py
│
├── assets/
│ ├── pinn-architecture-svg.svg
│ ├── pinn-before-after-svg.svg
│ └── results-dashboard-svg.svg
│ └── generate-assets.py
└── outputs/
└── (generated files)
Input Layer: (x, t) coordinates
Hidden Layers: 4 × 64 neurons with Tanh activation
Output Layer: u(x,t) - the wave amplitude
The network is trained to minimize:
Loss = λ₁·||PDE||² + λ₂·||IC||² + λ₃·||BC||²
Where:
- PDE Loss: How well the wave equation is satisfied
- IC Loss: Initial condition matching
- BC Loss: Boundary condition enforcement
PyTorch's autograd computes derivatives of the network output:
u_t = ∂u/∂t # First time derivative
u_tt = ∂²u/∂t² # Second time derivative
u_x = ∂u/∂x # First spatial derivative
u_xx = ∂²u/∂x² # Second spatial derivative
from src.model import WavePINN
from src.train import train_model
from src.visualization import plot_wave_evolution
# Train model
model = WavePINN()
history = train_model(model, epochs=5000)
# Visualize results
plot_wave_evolution(model, save_path='outputs/wave_evolution.png')
from src.losses import PhysicsInformedLoss
# Define custom wave speed
c = 2.0 # Wave speed
# Create custom loss function
loss_fn = PhysicsInformedLoss(
wave_speed=c,
pde_weight=1.0,
ic_weight=50.0,
bc_weight=50.0
)
Metric | Value |
---|---|
Training Time | 69 seconds |
Final Loss | 0.005074 |
Max Error | <1% |
Parameters | ~20,000 |
Epochs | 5000 |
Location | Time | PINN Prediction | True Value | Error |
---|---|---|---|---|
x=0.5 | t=0.0 | 1.0023 | 1.0000 | 0.23% |
x=0.0 | t=0.5 | 0.0031 | 0.0000 | 0.31% |
x=0.5 | t=0.5 | 0.0072 | 0.0000 | 0.72% |
This technique can be extended to:
- Battery Health Monitoring: Ultrasound wave propagation through battery cells
- Seismic Analysis: Earthquake wave modeling
- Medical Imaging: Ultrasound tissue characterization
- Structural Health Monitoring: Vibration analysis in buildings
- Acoustic Design: Sound wave optimization
For industrial applications, consider:
- NVIDIA PhysicsNeMo: Production-scale physics simulation
- Multi-GPU Training: Distributed computing for large domains
- 3D Extensions: Higher dimensional PDEs
- Multi-Physics: Coupled PDE systems
If you use this code in your research, please cite:
@software{pinn_wave_equation,
author = {Janhavi Giri},
title = {Physics-Informed Neural Networks for Wave Equation},
year = {2025},
publisher = {GitHub},
url = {https://github.com/janhavi-giri/pinn-wave-equation}
}
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
-
Raissi, M., Perdikaris, P., & Karniadakis, G. E. (2019). Physics-informed neural networks: A deep learning framework for solving forward and inverse problems involving nonlinear partial differential equations. Journal of Computational Physics, 378, 686-707.
This project is licensed under the MIT License - see the LICENSE file for details.
- Thanks to the scientific machine learning community
- Inspired by the original PINN paper by Raissi et al.
- NVIDIA for the PhysicsNeMo framework
Made with ❤️ by Janhavi Giri