Physics-Informed Neural Networks for Wave Equation Simulation
A comprehensive comparison of different approaches to solving the 1D wave equation: classical finite difference methods, data-driven neural networks, and physics-informed neural networks (PINNs).
-
Multiple Model Implementations
- Physics-based (Finite Difference Method)
- Data-driven (LSTM Neural Network)
- PINNs (Physics-Informed Neural networks incorporating wave equation constraints.)
- PINNs v2 (Physics-Informed Neural Networks with Energy Conservation)
-
Energy Conservation Analysis
- Real-time energy tracking
- Comparative performance metrics
- Visualization tools
-
Interactive Web UI
- Built with Streamlit
- Real-time simulation
- Parameter tuning interface
-
REST API
- FastAPI backend
- JSON-based requests
- Easy integration
| Model | Energy Variation | Speed | Accuracy | Overall |
|---|---|---|---|---|
| Physics-based (FDM) | 4.22% β | β‘β‘β‘ | ββββ | ββββ |
| Data-driven v2 (LSTM) | 154.63% β | β‘ | β | β |
| PINNs | 319.79% β | β‘ | β | β |
| PINNs v2 | 3.08% β | β‘ | βββββ | βββββ |
PINNs v2 achieves better energy conservation than classical methods!
- Python 3.11+
- pip or conda
# Clone repository
git clone https://github.com/yourusername/neuralwavesim.git
cd neuralwavesim
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txtuvicorn api.main:app --reload --host 0.0.0.0 --port 8080API docs available at http://localhost:8080/docs
streamlit run api/ui.pyOpen browser at http://localhost:8501
- Select Model: Choose from Physics-based, Data-driven v2, or PINNs v2
- Configure Parameters:
- Grid size (nx, nt)
- Wave speed (c)
- Time/space steps (dt, dx)
- Set Initial Condition:
- Wave type (Gaussian, Sine)
- Position, width, height
- Run Simulation: Click "Run Simulation"
- Analyze Results: View heatmaps, energy plots, and metrics
import requests
response = requests.post(
"http://localhost:8000/simulate",
json={
"model_type": "pinns-v2",
"nx": 100,
"nt": 200,
"c": 1.0,
"initial_condition": {
"wave_type": "gaussian",
"center": 5.0,
"width": 1.0,
"height": 1.0
}
}
)
data = response.json()
wave_history = data["wave_history"] # Shape: (nt, nx)# Generate training data
python training/generate_training_data.py --samples 50
# Train PINNs v2
python training/train_pinns_v2.py
# Verify model performance
python tests/verify_pinns_v2.pyClassical finite difference method (FDM):
βΒ²u/βtΒ² = cΒ² βΒ²u/βxΒ²
Pros: Fast, reliable, well-understood
Cons: Fixed grid, numerical dispersion
Physics-Informed Neural Network incorporating wave equation constraints.
** Loss Function**:
L_total = Ξ»_pde * L_pde + Ξ»_bc * L_bc + Ξ»_ic * L_ic
where:
L_pde = MSE(βΒ²u/βtΒ² - cΒ² βΒ²u/βxΒ²) # Physics loss
L_bc = MSE(u(0,t), u(L,t)) # Boundary loss (u=0 at x=0,L)
L_ic = MSE(u(x,0) - u_initial(x)) # Initial condition loss
Physics-Informed Neural Network with explicit energy conservation:
Loss Function:
L_total = Ξ»_pde * L_pde + Ξ»_bc * L_bc + Ξ»_ic * L_ic + Ξ»_energy * L_energy
where L_energy enforces energy conservation:
E = β« [Β½(βu/βt)Β² + Β½cΒ²(βu/βx)Β²] dx = const
Pros:
- Best energy conservation (3.08%)
- Flexible boundary conditions
- Data-efficient
Training: 10,000 epochs, Xavier initialization, learning rate scheduling
LSTM-based model with regularization (experimental):
Status: β Not recommended for wave equations
Reason: Cumulative error in sequential prediction (129% energy variation)
neuralwavesim/
βββ api/
β βββ main.py # FastAPI backend
β βββ ui.py # Streamlit UI
βββ core/
β βββ config.py # Configuration classes
β βββ solver.py # Wave equation solver (FDM)
βββ models/
β βββ physics.py # Physics-based model
β βββ data_driven_v2.py # LSTM model
β βββ pinns_v2.py # PINNs v2 model
β βββ factory.py # Model factory
βββ training/
β βββ generate_training_data.py # Dataset generation
β βββ train_pinns_v2.py # PINNs training
β βββ train_data_driven_v2.py # LSTM training
βββ tests/
β βββ verify_pinns_v2.py # PINNs verification
β βββ verify_data_driven_v2.py # Data-driven verification
βββ docs/
βββ physics_validation.md # Detailed analysis
python tests/verify_pinns_v2.pyOutput:
Energy Conservation:
Physics-based : 4.22% β
PINNs v2 : 3.08% β
Best!
python tests/compare_all_models.py-
Physics Validation:
docs/physics_validation.md- Energy conservation analysis
- Model comparison
- Failure case studies
-
API Documentation:
http://localhost:8000/docs(when server running) -
Training Logs:
models/*.pth(model checkpoints)
- PINNs outperform classical methods in energy conservation (3.08% vs 4.22%)
- Data-driven models fail for long-term wave propagation (cumulative error)
- Explicit physics constraints are crucial for conservation laws
- Energy regularization improves neural network performance
| Scenario | Recommended Model |
|---|---|
| Real-time simulation | Physics-based (FDM) |
| High accuracy required | PINNs v2 β |
| Complex boundary conditions | PINNs v2 β |
| Limited training data | PINNs v2 β |
| Unknown physics | Data-driven (with caution) |
# Generate diverse training data
python training/generate_training_data.py --samples 100
# Train PINNs v2
python training/train_pinns_v2.py
# Verify results
python tests/verify_pinns_v2.pyfrom core.config import InitialCondition
import numpy as np
# Gaussian pulse
ic = InitialCondition(
wave_type="gaussian",
center=5.0,
width=1.0,
height=1.0
)
# Custom wave
def my_wave(x):
return np.sin(2*np.pi*x/10) + 0.5*np.cos(4*np.pi*x/10)
ic = InitialCondition(
wave_type="custom",
center=5.0,
width=1.0,
height=1.0
)
ic._custom_generator = my_wave