Skip to content

Latest commit

Β 

History

31 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

NeuralWaveSim

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

Python 3.11+ PyTorch FastAPI Streamlit


webapp UI

🎯 Features

  • 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 Performance

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!


πŸš€ Quick Start

Prerequisites

  • Python 3.11+
  • pip or conda

Installation

# 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.txt

Run API Server

uvicorn api.main:app --reload --host 0.0.0.0 --port 8080

API docs available at http://localhost:8080/docs

Run Web UI

streamlit run api/ui.py

Open browser at http://localhost:8501

πŸ“– Usage

Web UI

  1. Select Model: Choose from Physics-based, Data-driven v2, or PINNs v2
  2. Configure Parameters:
    • Grid size (nx, nt)
    • Wave speed (c)
    • Time/space steps (dt, dx)
  3. Set Initial Condition:
    • Wave type (Gaussian, Sine)
    • Position, width, height
  4. Run Simulation: Click "Run Simulation"
  5. Analyze Results: View heatmaps, energy plots, and metrics

API Example

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)

Command Line

# 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.py

πŸ“Š Model Details

Physics-based Model

Classical finite difference method (FDM):

βˆ‚Β²u/βˆ‚tΒ² = cΒ² βˆ‚Β²u/βˆ‚xΒ²

Pros: Fast, reliable, well-understood
Cons: Fixed grid, numerical dispersion

PINNs (Original)

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

PINNs v2 (Recommended) ⭐

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

Data-driven v2

LSTM-based model with regularization (experimental):

Status: ❌ Not recommended for wave equations
Reason: Cumulative error in sequential prediction (129% energy variation)


πŸ“ Project Structure

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

πŸ§ͺ Testing & Validation

Energy Conservation Test

python tests/verify_pinns_v2.py

Output:

Energy Conservation:
  Physics-based : 4.22% βœ…
  PINNs v2      : 3.08% βœ… Best!

Compare All Models

python tests/compare_all_models.py

πŸ“š Documentation

  • 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)


πŸ”¬ Research Insights

Key Findings

  1. PINNs outperform classical methods in energy conservation (3.08% vs 4.22%)
  2. Data-driven models fail for long-term wave propagation (cumulative error)
  3. Explicit physics constraints are crucial for conservation laws
  4. Energy regularization improves neural network performance

When to Use Each Model

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)

πŸ› οΈ Development

Training New Models

# 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.py

Adding Custom Initial Conditions

from 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

About

A comparison framework of classical physics, data-driven AI, and physics-informed neural networks for simulating wave propagation.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages