Skip to content

mathboylinlin/ai4plasma

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

13 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

AI4Plasma

License Python Version PyTorch Documentation

AI4Plasma is a Python- and PyTorch-based library for physics-informed machine learning and operator learning, specifically designed for plasma physics simulation. As the world's first AI package dedicated to plasma simulation, AI4Plasma bridges the gap between cutting-edge AI techniques and plasma physics research, making it easier for plasma researchers to leverage AI tools to enhance both the efficiency and accuracy of their simulations.

Although there are several excellent predecessors such as DeepXDE, AI4Plasma is (and will always be) the AI algorithm package that understands plasma the most and the plasma algorithm package that understands AI the most. We welcome your testing and use, and also invite you to join the AI4Plasma development team.

✨ Key Features

  • Physics-Informed Machine Learning (PIML)

    • 🧠 PINNs: Classic Physics-Informed Neural Networks for solving PDEs
    • 🎯 CS-PINNs: Coefficient-Subnet PINNs optimized for solving plasma equations with variable coefficients
    • ⏰ RK-PINNs: Runge-Kutta PINNs for time-dependent problems
    • πŸ”„ Meta-PINNs: Meta-learning approach for rapid adaptation across different physics scenarios
    • πŸ” NAS-PINNs: Neural Architecture Search for automatic PINN architecture optimization
  • Operator Learning

    • 🌐 DeepONet: Deep Operator Networks for learning solution operators
    • πŸ”¬ DeepCSNet: Deep Operator Networks for predicting cross sections
  • Plasma Physics Models

    • ⚑ Arc Plasma Simulation: Steady and transient arc plasma simulations (1-D in current version)
    • πŸ“Š Plasma Properties: Built-in plasma property calculations and estimations
  • Comprehensive Utilities

    • πŸ“ Geometry Tools: 1D, 2D, and 3D domain definitions with flexible boundary conditions
    • 🎨 Visualization: TensorBoard integration and custom plotting callbacks
    • πŸš€ Auto-differentiation: Efficient gradient computation for complex PDEs
    • πŸ’Ύ I/O Management: Easy model saving/loading and checkpoint management
    • πŸ–₯️ GPU Support: Automatic device detection and optimization

πŸ“¦ Installation

Install from PyPI

The easiest way to install AI4Plasma is via pip:

pip install ai4plasma

Or upgrade to the latest version:

pip install --upgrade ai4plasma

Install from Source

For the latest development version:

git clone https://github.com/ai4plasma/ai4plasma.git
cd ai4plasma
pip install -e .

Install with Conda

If you prefer using Conda/Mamba:

conda create -n ai4plasma python=3.12
conda activate ai4plasma
pip install -e .

Prerequisites

  • Python >= 3.10
  • PyTorch >= 2.0
  • NumPy, SciPy, Pandas, Matplotlib
  • TensorBoard, Imageio (for visualization)
  • FiPy (for traditional numerical methods comparison)
  • Shapely (for geometry processing)
  • Huggingface-hub (for downloading dataset from Hugging Face)

πŸš€ Quick Start

Example 1: Solving a Simple PDE with PINN

Solve the 1D ODE: dΒ²u/dxΒ² = -sin(x) with boundary conditions u(0) = u(1) = 0

import torch
import torch.nn as nn
from ai4plasma.config import REAL
from ai4plasma.piml.pinn import PINN
from ai4plasma.piml.geo import Geo1D
from ai4plasma.core.network import FNN
from ai4plasma.utils.math import df_dX, calc_relative_l2_err
from ai4plasma.utils.common import set_seed, numpy2torch

# Set seed for reproducibility
set_seed(2026)

# Define custom PINN class
class SimplePINN(PINN):
    def __init__(self, network):
        self.geo = Geo1D([0.0, 1.0])
        super().__init__(network)
    
    @staticmethod
    def _pde_residual(network, x):
        """Compute residual: dΒ²u/dxΒ² + sin(x)"""
        u = network(x)
        u_x = df_dX(u, x)
        u_xx = df_dX(u_x, x)
        return u_xx + torch.sin(x)
    
    @staticmethod
    def _bc_residual(network, x):
        """Dirichlet BC: u(x) = 0"""
        return network(x)
    
    def _define_loss_terms(self):
        """Set up domain and boundary loss terms"""
        x_domain = self.geo.sample_domain(100, mode='uniform')
        x_bc = self.geo.sample_boundary()
        
        self.add_equation('Domain', self._pde_residual, weight=1.0, data=x_domain)
        self.add_equation('Left BC', self._bc_residual, weight=10.0, data=x_bc[0])
        self.add_equation('Right BC', self._bc_residual, weight=10.0, data=x_bc[1])

# Create and train PINN
network = FNN([1, 64, 64, 64, 1])
pinn = SimplePINN(network)
pinn.set_loss_func(nn.MSELoss())

pinn.train(num_epochs=5000, lr=1e-3, print_loss=True, print_loss_freq=500)

# Predict on evaluation points
import numpy as np
x_eval = np.linspace(0, 1, 200, dtype=REAL()).reshape(-1, 1)
u_true = np.sin(x_eval) - x_eval * np.sin(1)  # Analytical solution for comparison
x_eval_tensor = numpy2torch(x_eval)
u_pred = pinn.predict(x_eval_tensor).detach().numpy()

print(f"Relative L2 error: {calc_relative_l2_err(u_true, u_pred):.4e}")

Example 2: Learning Operators with DeepONet

Learn the solution operator for -Ξ”u(x) = f(x) where f(x) = v·π²·sin(Ο€x) and u(x) = vΒ·sin(Ο€x)

import numpy as np
from ai4plasma.core.network import FNN
from ai4plasma.operator.deeponet import DeepONet, DeepONetModel
from ai4plasma.utils.common import set_seed, numpy2torch
from ai4plasma.utils.device import check_gpu
from ai4plasma.utils.math import calc_relative_l2_err
from ai4plasma.config import DEVICE, REAL

# Set seed for reproducibility
set_seed(2026)

## Set device ##
if check_gpu(print_required=True):
    DEVICE.set_device(0)  # Using cuda:0
else:
    DEVICE.set_device(-1) # Using cpu
print(DEVICE)

# Define branch and trunk networks
branch_net = FNN([1, 10, 10, 10, 10])
trunk_net = FNN([1, 10, 10, 10, 10])

# Create DeepONet model
network = DeepONet(branch_net, trunk_net)
model = DeepONetModel(network=network)

# Prepare training data
# Branch input: parameter v (A samples)
v = np.array([[2, 4, 6, 8, 10]], dtype=REAL()).reshape((-1, 1))
# Trunk input: spatial coordinate x (B points)
x = np.linspace(-1, 1, 40, endpoint=True, dtype=REAL()).reshape((-1, 1))
# Target output: solution u (A Γ— B)
u = v * np.sin(np.pi * x.T)

# Convert to tensors and prepare data
v, x, u = numpy2torch(v), numpy2torch(x), numpy2torch(u)
model.prepare_train_data(v, x, u)

# Train the model
model.train(num_epochs=10000, lr=1e-4, print_loss_freq=100)

# Test on unseen parameter
vv = np.array([[5.5]], dtype=REAL())
xx = np.linspace(-1, 1, 30, endpoint=True, dtype=REAL()).reshape((-1, 1))
uu = vv * np.sin(np.pi * xx.T)  # True solution

# Predict
vv, xx = numpy2torch(vv), numpy2torch(xx)
u_pred = model.predict(vv, xx).cpu().detach().numpy()

# Evaluate
l2_err = calc_relative_l2_err(uu, u_pred)
print(f"Relative L2 error: {l2_err:.4e}")

πŸ“š Module Overview

Core Modules (ai4plasma.core)

  • model.py: Base model class with training utilities and checkpoint management
  • network.py: Neural network architectures (FNN, CNN, ResNet, etc.)

Physics-Informed ML (ai4plasma.piml)

  • geo.py: Geometry definitions and domain sampling
  • pinn.py: Standard Physics-Informed Neural Networks
  • cs_pinn.py: Coefficient-Subnet PINNs (CS-PINN) for plasma applications
  • meta_pinn.py: Meta-learning PINNs (Meta-PINN) for multi-task scenarios
  • rk_pinn.py: Runge-Kutta PINNs (RK-PINN) for temporal problems
  • nas_pinn.py: Neural Architecture Search for PINNs (NAS-PINN)

Operator Learning (ai4plasma.operator)

  • deeponet.py: Deep Operator Networks implementation
  • deepcsnet.py: Deep Operator Networks for cross section prediction

Plasma Physics (ai4plasma.plasma)

  • arc.py: Arc plasma models and solvers
  • prop.py: Plasma property calculations

Utilities (ai4plasma.utils)

  • common.py: Common utilities (seed setting, timers, etc.)
  • device.py: GPU/CPU device management
  • io.py: File I/O and checkpoint utilities
  • math.py: Mathematical utilities (automatic differentiation, operators)

πŸ“– Documentation

Full documentation is available at https://ai4plasma.readthedocs.io

  • API Reference: Detailed documentation of all modules and classes
  • User Guide: Step-by-step tutorials for getting started
  • Examples: Comprehensive examples covering various use cases

🎯 Example Applications

The app/ directory contains numerous ready-to-run examples:

Physics-Informed Neural Networks

Plasma Simulations

Operator Learning

Meta-Learning & NAS

πŸ“Š Project Structure

ai4plasma/
β”œβ”€β”€ ai4plasma/              # Main package
β”‚   β”œβ”€β”€ core/              # Core model and network architectures
β”‚   β”œβ”€β”€ piml/              # Physics-informed machine learning
β”‚   β”œβ”€β”€ operator/          # Operator learning methods
β”‚   β”œβ”€β”€ plasma/            # Plasma physics models
β”‚   └── utils/             # Utility functions
β”œβ”€β”€ app/                   # Example applications
β”‚   β”œβ”€β”€ piml/              # PIML examples
β”‚   β”œβ”€β”€ operator/          # Operator learning examples
β”‚   └── plasma/            # Plasma simulation examples
β”œβ”€β”€ docs/                  # Documentation source files

🀝 Contributing

We welcome contributions from the community! Whether you're fixing bugs, adding new features, improving documentation, or sharing examples, your help is appreciated.

How to Contribute

  1. Fork the repository on GitHub
  2. Create a new branch for your feature or bugfix
    git checkout -b feature/your-feature-name
  3. Make your changes and commit with clear messages
  4. Submit a pull request with a detailed description

Code Style

  • Follow PEP 8 guidelines & "Numpy" style comment
  • Use type hints where applicable
  • Write docstrings for all public functions and classes
  • Keep functions focused and modular

πŸ“ Citation

If you use AI4Plasma in your research, please cite:

@software{ai4plasma2026,
  title={AI4Plasma: An AI Library for Plasma Physics Simulation},
  author={Zhong, Linlin and contributors},
  year={2026},
  url={https://github.com/mathboylinlin/ai4plasma},
  version={0.1.0}
}

For specific methods, please also cite the relevant papers:

  • PINNs: M. Raissi, P. Perdikaris, and G. E. Karniadakis, "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 (2019).

  • CS-PINN: L. Zhong, B. Wu, and Y. Wang, "Low-temperature plasma simulation based on physics-informed neural networks: Frameworks and preliminary applications," Physics of Fluids 34, 087116 (2022)

  • RK-PINN: L. Zhong, B. Wu, and Y. Wang, "Low-temperature plasma simulation based on physics-informed neural networks: Frameworks and preliminary applications," Physics of Fluids 34, 087116 (2022)

  • Meta-PINN: L. Zhong, B. Wu, and Y. Wang, "Accelerating physics-informed neural network based 1D arc simulation by meta learning," Journal of Physics D: Applied Physics 56, 074006 (2023).

  • NAS-PINN: Y. Wang, and L. Zhong, "NAS-PINN: Neural architecture search-guided physics-informed neural network for solving PDEs," Journal of Computational Physics 496, 112603 (2024).

  • DeepONet: L. Lu, P. Jin, G. Pang, Z. Zhang, and G. E. Karniadakis, "Learning nonlinear operators via DeepONet based on the universal approximation theorem of operators," Nature Machine Intelligence 3, 218 (2021).

  • DeepCSNet: Y. Wang, and L. Zhong, "DeepCSNet: a deep learning method for predicting electron-impact doubly differential ionization cross sections," Plasma Sources Science and Technology 33, 105012 (2024).

πŸ“„ License

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

πŸ™ Acknowledgments

  • Thanks to all contributors who have helped shape AI4Plasma
  • Inspired by DeepXDE and other physics-informed ML and operator learning frameworks
  • Built on top of PyTorch and the broader Python scientific computing ecosystem
  • Special thanks to the plasma physics and machine learning communities

πŸ“¬ Contact

🌟 Star History

If you find AI4Plasma useful, please consider giving it a star ⭐ on GitHub!


Made with ❀️ by the AI4Plasma Team

About

An AI Library for Plasma Physics Simulation

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages