Skip to content

gitvicky/NOs_for_POs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

157 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Neural Operators for Physical Operators (NOs for POs)

A PyTorch implementation for learning Physical Operators using Neural Operators within a Neural-ODE framework for solving Partial Differential Equations through operator splitting of linear and nonlinear terms.

πŸ“‹ Overview

This repository implements the novel approach described in "Learning Physical Operators using Neural Operators" where we decompose PDE solving by splitting linear and nonlinear operators, learning each component separately using Neural Operators, then integrating them through time using ODE solvers.

Key Innovation

Traditional Neural-PDE solvers approximate the entire right-hand side of a PDE with a single neural network. Our approach instead:

  1. Splits PDEs into linear and nonlinear operator components
  2. Learns each operator type separately using specialized Neural Operators
  3. Provides classical finite difference approximations for linear operators via convolution
  4. Integrates through time using operator splitting schemes and ODE solvers

For example, the Navier-Stokes equation:

βˆ‚u/βˆ‚t = -(uΒ·βˆ‡)u + Ξ½βˆ‡Β²u - βˆ‡p

becomes:

du/dt = NO_nonlinear(u) + NO_linear(u) + NO_pressure(u)
       = -NO_convection(u) + Ξ½Β·laplace(u) - gradient(p)

Where:

  • Linear operators: Diffusion (βˆ‡Β²u), Pressure gradient (βˆ‡p) - can use finite difference stencils
  • Nonlinear operators: Convection ((uΒ·βˆ‡)u), Advection terms - require neural learning

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Input u_n     β”‚ -> β”‚  Operator        β”‚ -> β”‚  Time           β”‚
β”‚                 β”‚    β”‚  Splitting       β”‚    β”‚  Integration    β”‚
β”‚                 β”‚    β”‚  (Linear/Nonlin) β”‚    β”‚  (ODE Solver)   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              β”‚
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚         β”‚         β”‚
          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β” β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β” β”Œβ”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
          β”‚ Nonlinear  β”‚ β”‚ Linear  β”‚ β”‚ Coupling   β”‚
          β”‚ Operators  β”‚ β”‚Operatorsβ”‚ β”‚ Terms      β”‚
          β”‚ (Neural)   β”‚ β”‚(FD/     β”‚ β”‚            β”‚
          β”‚            β”‚ β”‚  Neural)β”‚ β”‚            β”‚
          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
               β”‚              β”‚           β”‚
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”   β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”
    β”‚NO_convect  β”‚    β”‚FD_diffuseβ”‚   β”‚NO_press β”‚
    β”‚NO_advect   β”‚    β”‚FD_laplaceβ”‚   β”‚FD_grad  β”‚
    β”‚NO_reactive β”‚    β”‚NO_linear β”‚   β”‚NO_force β”‚
    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸš€ Features

  • Linear/Nonlinear Operator Splitting: Separates physics-based operator types
  • Hybrid Learning Approach: Neural operators for complex terms, finite differences for linear terms
  • Physical Residual Estimation (PRE): Classical stencil-based operator approximations
  • Multiple Neural Operators: Support for FNO, CNO, U-Net, ConvNets optimized for operator type
  • Time Integration: Euler, Midpoint, RK4 along with their adjoint versions
  • Physics-Informed: Maintains mathematical structure and stability properties
  • Multiple PDEs: Navier-Stokes, Euler, reaction-diffusion, MHD, and more

πŸ“ Repository Structure

β”œβ”€β”€ Expts/                          # Experiment scripts
β”‚   β”œβ”€β”€ Train.py                    # Main training pipeline
β”‚   β”œβ”€β”€ Evaluate.py                 # Model evaluation
β”‚   β”œβ”€β”€ Solve.py                    # Handcrafted solver comparison
β”‚   β”œβ”€β”€ data_loaders.py             # Data loading utilities
β”‚   β”œβ”€β”€ model_setup.py              # Model initialization
β”‚   └── operator_splitting.py       # Linear/nonlinear operator splitting
β”œβ”€β”€ PRE/                            # Physical Residual Estimation
β”‚   β”œβ”€β”€ ConvOps_Spatial.py          # Finite difference as convolution
β”‚   β”œβ”€β”€ VectorConvOps_Spatial.py    # Vector field FD operations
β”‚   β”œβ”€β”€ Stencils.py                 # FD stencils (2nd to 10th order)
β”‚   β”œβ”€β”€ boundary_conditions.py      # Boundary condition handling
β”‚   └── fft_conv_pytorch/           # Efficient convolution implementations
β”œβ”€β”€ Utils/                          # Utilities
β”‚   β”œβ”€β”€ explicit_time.py            # Explicit time stepping
β”‚   β”œβ”€β”€ torch_odesolve.py           # Neural-ODE integration
β”‚   β”œβ”€β”€ plots.py                    # Visualization tools
β”‚   └── metrics.py                  # Performance metrics
└── configs/                        # Configuration files

πŸ› οΈ Installation

git clone https://github.com/yourusername/NOs_for_POs.git
cd NOs_for_POs
git submodule init
git submodule update

# Install dependencies
pip install torch torchvision
pip install simvue  # For experiment tracking (optional)
pip install torchdiffeq  # For Neural-ODE integration
pip install scikit-learn
pip install einops neuraloperator
pip install tqdm matplotlib h5py pyyaml h5py

🎯 Quick Start

1. Training a Model with Operator Splitting

cd Expts
python Train.py --config configs/NS_linear_nonlinear_split.yaml

2. Evaluating a Trained Model

# Set the run name from your training
run_name = 'your-trained-model-name'
python Evaluate.py

3. Custom Configuration for Operator Splitting

Create a YAML config file:

Physics:
  pde: 'Navier-Stokes'
  variables: 2
  field: 'u, v'

Model:
  arch: 'FNO'
  operator splitting: true
  split_type: 'linear_nonlinear'  # New: specify splitting strategy
  
  # Linear operator configuration
  linear_operators:
    method: 'finite_difference'  # or 'neural'
    taylor_order: 4             # FD accuracy order
    boundary_cond: 'periodic'   # or 'dirichlet', 'neumann'
    
  # Nonlinear operator configuration  
  nonlinear_operators:
    arch: 'CNO'  # Better for local, complex interactions
    modes: 8
    width: 64

Train:
  odesolve:
    method: 'imex_euler'  # Implicit-Explicit for stiff problems
    source: 'custom'
  rollout_length: 20
  hybrid_training: true  # FD for linear, Neural for nonlinear

πŸ”§ Operator Types and Implementation Strategies

Linear Operators: Finite Difference as Convolution

PRE (Physical Residual Estimation) Approach: Linear differential operators can be exactly represented using finite difference stencils implemented as convolutions.

from PRE.ConvOps_Spatial import ConvOperator
from PRE.VectorConvOps_Spatial import Laplace, Gradient, Divergence

# Finite difference Laplacian as convolution
laplacian = ConvOperator(domain=('x','y'), order=2, scale=1/dx**2, 
                        taylor_order=4, boundary_cond='periodic')

# Vector operations for fluid dynamics
gradient = Gradient(scale=1/dx, taylor_order=4, boundary_cond='periodic')
divergence = Divergence(scale=1/dx, taylor_order=4, boundary_cond='periodic')
laplace_vector = Laplace(scale=1/dx**2, taylor_order=4, scalar=False)

# Usage in physics
def linear_operators_fd(u, p):
    diffusion = laplace_vector(u)      # Ξ½βˆ‡Β²u
    pressure_grad = gradient(p)        # βˆ‡p
    return diffusion, pressure_grad

Available Finite Difference Stencils:

  • 2nd to 10th order accuracy: Higher order for better spectral properties
  • Multiple boundary conditions: Periodic, Dirichlet, Neumann, symmetric
  • Optimized implementations: FFT-based convolution for large kernels

Hybrid Linear Operators: FD + Neural Learning

class HybridLinearOperators(nn.Module):
    def __init__(self, config):
        # Base finite difference operators
        self.fd_laplacian = ConvOperator(domain=('x','y'), order=2, ...)
        self.fd_gradient = Gradient(...)
        
        # Neural correction for complex geometries/coefficients
        self.neural_correction = FNO_multi2d(...)
        
    def forward(self, u):
        fd_result = self.nu * self.fd_laplacian(u)
        correction = self.neural_correction(u)
        return fd_result + correction  # FD + learned residual

Nonlinear Operators: Pure Neural Learning

Characteristics: Local interactions, sharp gradients, potentially unstable Examples: Convection, Reaction terms, Shock formation Optimal Networks:

  • CNO: Better for local, sharp features
  • U-Net: Good for multi-scale nonlinear interactions
  • ConvNets: Efficient for local nonlinear operations
# Nonlinear operator example  
class NonlinearOperators(nn.Module):
    def __init__(self, config):
        self.convection = CNO2d(...)     # (uΒ·βˆ‡)u
        self.reaction = UNet2d(...)      # f(u,v) reaction terms
        
    def forward(self, u):
        return -self.convection(u) + self.reaction(u)

πŸ“Š PRE (Physical Residual Estimation) Features

Finite Difference Stencils

Located in PRE/Stencils.py:

# 2nd order Laplacian stencil
stencil_2nd = tensor([[0, 1, 0],
                      [1, -4, 1], 
                      [0, 1, 0]])

# 4th order Laplacian stencil  
stencil_4th = tensor([[0, 0, -1/12, 0, 0],
                      [0, 0, 4/3, 0, 0],
                      [-1/12, 4/3, -5, 4/3, -1/12],
                      [0, 0, 4/3, 0, 0],
                      [0, 0, -1/12, 0, 0]])

Boundary Condition Handling

Located in PRE/boundary_conditions.py:

# Flexible boundary condition manager
bc_manager = BoundaryManager(kernel_size=(5, 5))
bc_manager.set_boundary_type('left', 'periodic')
bc_manager.set_boundary_type('right', 'dirichlet', value=0.0)
bc_manager.set_boundary_type('top', 'neumann')  # Zero gradient
bc_manager.set_boundary_type('bottom', 'symmetric')  # Reflection

Vector Field Operations

Located in PRE/VectorConvOps_Spatial.py:

# Complete set of vector calculus operations
from PRE.VectorConvOps_Spatial import Gradient, Divergence, Curl, Laplace

# Navier-Stokes using finite difference
def navier_stokes_fd(u, v, p, nu=0.01):
    # Nonlinear convection (would be learned)
    convection = dot(vectorize(u, v), gradient(u)), dot(vectorize(u, v), gradient(v))
    
    # Linear diffusion (finite difference)
    diffusion = nu * laplace_vector(u, v)
    
    # Pressure gradient (finite difference)  
    pressure_grad = gradient(p)
    
    return -convection + diffusion - pressure_grad

πŸ“ˆ Advantages of FD + Neural Hybrid Approach

Computational Benefits

  • Exact linear operators: No approximation error for linear terms
  • Efficient computation: Convolution operations are highly optimized
  • Reduced learning complexity: Only nonlinear terms need neural approximation
  • Memory efficient: Fixed stencils vs learned parameters

Physical Benefits

  • Conservation properties: FD stencils preserve discrete conservation laws
  • Spectral accuracy: High-order stencils maintain correct dispersion relations
  • Stability guarantees: Well-understood stability conditions for linear terms
  • Boundary condition accuracy: Proper treatment of complex boundaries

Learning Benefits

  • Faster convergence: Reduced parameter space for learning
  • Better generalization: Physics-based inductive bias for linear operators
  • Interpretability: Clear separation of known vs learned physics
  • Transfer learning: FD operators transfer across problems

πŸ§ͺ Experiments

Training with Hybrid Operators

# Pure finite difference linear operators
python Train.py --config configs/NS_hybrid_FD_neural.yaml

# Comparison: FD vs Neural vs Hybrid
python compare_operators.py --linear_method fd --nonlinear_method neural

# Accuracy vs efficiency trade-offs
python efficiency_study.py --fd_order 2,4,6,8 --neural_width 16,32,64

Finite Difference Validation

# Test FD operator accuracy
cd PRE
python test_fd_operators.py --taylor_order 2,4,6,8 --boundary periodic

# Convergence analysis
python convergence_test.py --operator laplacian --orders 2,4,6,8,10

πŸ“Š Results

Our hybrid FD + Neural approach demonstrates:

  • Computational Efficiency: 5-10x faster than pure neural for linear operators
  • Enhanced Accuracy: Exact representation of linear physics
  • Better Stability: Provable stability for finite difference components
  • Reduced Training Time: 3-5x faster convergence with hybrid approach
  • Improved Generalization: Better extrapolation due to exact linear operators
  • Conservation Properties: Discrete conservation laws automatically satisfied

πŸ“ Citation

If you use this code in your research, please cite:

@article{gopakumar2024learning,
  title={Learning Physical Operators using Neural Operators: Hybrid Finite Difference and Neural Approach},
  author={Gopakumar, Vignesh},
  journal={arXiv preprint},
  year={2024}
}

🀝 Contributing

We welcome contributions! Please:

  1. Fork the repository
  2. Create a feature branch focusing on operator splitting improvements
  3. Add tests for stability and accuracy
  4. Submit a pull request

Areas of particular interest:

  • New finite difference stencils for complex operators
  • Advanced boundary condition implementations
  • Novel hybrid FD + Neural architectures
  • High-order accurate schemes
  • Adaptive mesh refinement integration

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ”— Related Work

πŸ“§ Contact

πŸ™ Acknowledgments

This work was supported by UKAEA and UCL. Special thanks to the Simvue team for experiment tracking infrastructure, the numerical analysis community for finite difference theory, and the scientific computing community for convolution optimization techniques.


Note: This is research code under active development with focus on hybrid finite difference and neural operator methods. For production use, please contact the authors.

About

Learning Physical Operators using Neural Operators

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages