A Physics-Informed Neural Network (PINN) framework for solving coupled electromagnetic-thermal problems. This repository implements a fully-differentiable, mesh-free solver that simultaneously predicts electric field (E), magnetic field (H), and temperature (T) distributions by embedding Maxwell's equations and the heat equation with Joule heating into the neural network's loss function.
The framework supports three neural network architectures with an interactive selection menu at startup, allowing users to choose the best trade-off between speed, accuracy, and memory usage.
Traditional numerical solvers (FEM, FVM) require mesh generation and iterative solutions for each set of parameters. This PINN-based approach offers:
- Mesh-free modeling with automatic differentiation
- Physics-constrained learning without labeled data
- Forward and inverse problem capabilities
- Spatially-varying parameter identification from limited observations
| Feature | Description |
|---|---|
| Forward Problem | Solve coupled PDEs directly from physics constraints |
| Inverse Problem | Identify unknown physical parameters from observational data |
| Spatially-Varying Parameters | Predict α(x) and σ(x) as functions of space |
| Uncertainty Quantification | Variance-based uncertainty estimation |
| Model Selection | Interactive menu to choose between MLP, MLPPINN, or TransformerPINN |
| Reproducibility | Fixed random seeds for consistent results |
| Early Stopping | Prevents overfitting with patience-based stopping |
| Learning Rate Scheduling | Adaptive LR for stable convergence |
| L-BFGS Refinement | Final optimization for higher accuracy |
| Comprehensive Metrics | L2 error, MAE, Max error |
| Interactive Dashboard | Streamlit-based UI for live parameter exploration and visualization |
A comprehensive technical report detailing the mathematical formulation, architecture design, training methodology, and extensive results is available:
The report covers:
- Governing equations and non-dimensionalization
- PINN architecture and loss function design
- Three model architectures (MLP, MLPPINN, TransformerPINN)
- Comprehensive error analysis and convergence study
- Industrial applications and future work
∂E/∂t + (1/ε) · ∂H/∂x = 0
∂H/∂t + (1/μ) · ∂E/∂x = 0
∂T/∂t = α · ∂²T/∂x² + (σ/ρc) · E²
Q = σ · E²
Where:
E= Electric fieldH= Magnetic fieldT= Temperatureα= Thermal diffusivityσ= Electrical conductivityρc= Volumetric heat capacityμ= Magnetic permeabilityε= Electric permittivity
PINNs embed the governing partial differential equations (PDEs) directly into the neural network's loss function. The network learns to approximate the solution (E, H, T)(x, t) by minimizing:
L_total = λ_pde · L_pde + λ_bc · L_bc + λ_data · L_data
Where:
L_pde= Residual of Maxwell's equations + Heat equationL_bc= Dirichlet boundary condition residualsL_data= Data mismatch (for inverse problems)
All derivatives are computed via automatic differentiation (PyTorch's autograd), eliminating the need for mesh generation or numerical differentiation.
The framework provides three architectures with an interactive selection menu at startup:
| # | Model | Description | Speed | Memory | Accuracy |
|---|---|---|---|---|---|
| 1 | MLP (ElectroThermalPINN) | Fully-connected network with Tanh activation. Lightweight baseline. | ⚡⚡⚡⚡⚡ | 💾💾 | 🎯🎯🎯 |
| 2 | MLPPINN (Lightweight Transformer) | Transformer-style without attention. Uses Fourier features, RMSNorm, and SwiGLU. | ⚡⚡⚡⚡ | 💾💾💾 | 🎯🎯🎯🎯 |
| 3 | TransformerPINN (Full Attention) | Full Transformer with attention, RoPE, GQA. Highest accuracy, memory-intensive. | ⚡⚡ | 💾💾💾💾💾 | 🎯🎯🎯🎯🎯 |
| 0 | config.yaml | Load architecture from configuration file. | — | — | — |
| Metric | E (Electric) | H (Magnetic) | T (Temperature) |
|---|---|---|---|
| Relative L2 Error | 9.09e-04 | 2.43e-03 | 1.42e-03 |
| MAE | 1.96e-04 | 3.24e-04 | 7.02e-04 |
| Max Error | 3.29e-04 | 6.78e-04 | 2.11e-03 |
| Metric | E (Electric) | H (Magnetic) | T (Temperature) |
|---|---|---|---|
| Relative L2 Error | 2.15e-04 | 8.75e-04 | 1.52e-03 |
| MAE | 6.25e-05 | 2.10e-04 | 4.20e-04 |
| Max Error | 1.74e-03 | 5.90e-04 | 1.70e-03 |
| Metric | E (Electric) | H (Magnetic) | T (Temperature) |
|---|---|---|---|
| Relative L2 Error | 8.71e-05 | 3.11e-04 | 5.67e-04 |
| MAE | 2.53e-05 | 8.70e-05 | 1.80e-04 |
| Max Error | 1.20e-04 | 4.20e-04 | 8.90e-04 |
Note: MLP is fastest and performs well for E-field. MLPPINN offers improved accuracy for H and T with moderate cost. TransformerPINN achieves the highest accuracy for all fields at the cost of increased memory and compute time.
electro-thermal-pinn/
├── app.py # Streamlit dashboard
├── train_for_dashboard.py # Quick training script for dashboard models
├── src/
│ ├── main.py # Unified entry point (menu for dashboard/terminal)
│ ├── main_terminal.py # Terminal-based training with model selection
│ ├── models/
│ │ └── electro_thermal_pinn.py # All three model architectures
│ ├── physics/
│ │ ├── maxwell_pde.py # Maxwell's equations residuals
│ │ ├── heat_pde.py # Heat equation with Joule heating
│ │ ├── boundary_conditions.py # Dirichlet BCs
│ │ └── varying_parameters.py # Parameter network for α(x), σ(x)
│ ├── training/
│ │ ├── trainer.py # Loss function with UQ
│ │ └── trainer_original.py # Forward problem trainer
│ ├── utils/
│ │ ├── data_sampling.py # Collocation & boundary points
│ │ ├── plotting.py # Visualization utilities
│ │ └── config_loader.py # YAML configuration loader
│ └── configs/
│ └── config.yaml # Hyperparameter configuration
├── experiments/
│ ├── figures/ # Generated plots
│ ├── results/ # .npy prediction files
│ └── saved_models/ # Trained model weights
├── README.md
├── LICENSE
└── requirements.txt
- Python 3.8+
- PyTorch 2.0+
# Clone the repository
git clone https://github.com/mohammad-hussein-dev/electro-thermal-pinn.git
cd electro-thermal-pinn
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txttorch>=2.0.0
numpy>=1.24.0
matplotlib>=3.7.0
pyyaml>=6.0
streamlit>=1.28.0
python src/main.pyThis command launches a unified menu with two execution modes:
======================================================================
⚡ ELECTRO-THERMAL PINN - MAIN MENU
======================================================================
+---+--------------------------------------------+
| # | Mode |
+---+--------------------------------------------+
| 1 | Dashboard (Streamlit) |
| | Interactive UI for employers & clients |
+---+--------------------------------------------+
| 2 | Terminal Mode |
| | Command-line with architecture selection |
+---+--------------------------------------------+
| 0 | Exit |
+---+--------------------------------------------+
- Option 1: Launches the Streamlit dashboard for interactive parameter exploration and visualization.
- Option 2: Enters the terminal-based training mode with architecture selection.
- Option 0: Exits the application.
After selecting option 2, you will see the architecture selection menu:
----------------------------------------------------------------------
PINN MODEL SELECTION
----------------------------------------------------------------------
Select the neural network architecture:
+---+------------------------+------------------------------------------+
| # | Model | Description / Use Case |
+---+------------------------+------------------------------------------+
| 1 | MLP | Fastest, lowest memory, good baseline |
| | (ElectroThermalPINN) | Quick tests, prototyping |
+---+------------------------+------------------------------------------+
| 2 | MLPPINN | Better accuracy, moderate speed |
| | (Lightweight Trans.) | High accuracy on CPU |
+---+------------------------+------------------------------------------+
| 3 | TransformerPINN | Highest accuracy, memory-intensive |
| | (Full Attention) | Research, GPU-accelerated |
+---+------------------------+------------------------------------------+
| 0 | config.yaml | Load from configuration file |
+---+------------------------+------------------------------------------+
All hyperparameters are managed via src/configs/config.yaml:
problem:
N_f: 500 # Number of collocation points
network:
layers: [2, 50, 50, 50, 50, 3] # MLP architecture
training:
adam_lr: 1e-3
adam_iters: 3000
use_lbfgs: true
inverse:
enable: true
lambda_data: 100.0
n_data_points: 500
noise_level: 0.005
use_uncertainty: true
varying_params: true
model:
type: "transformer" # "mlp" or "transformer" (MLPPINN)
transformer:
hidden_size: 128
num_hidden_layers: 2
use_fourier_features: false
max_seq_len: 512- Model Selection: Use the interactive menu or set
model.typeinconfig.yaml - Accuracy vs Speed: Adjust
N_f,hidden_size, andnum_hidden_layers - Forward Problem Only: Set
enable: falseunderinverse - Different Physics: Modify PDE residuals in
src/physics/
The Streamlit dashboard (app.py) provides a visual interface for exploring model predictions. It is launched from the main menu (Option 1).
Features:
- Model Architecture Selection: Choose between MLP, MLPPINN, and TransformerPINN.
- Parameter Adjustment: Adjust spatial domain (
x_min,x_max) and time (t). - Live Inference: Run the model with the selected parameters and see plots for:
- Electric field (E)
- Magnetic field (H)
- Temperature (T)
- Spatially-varying parameters (α(x), σ(x))
- Metrics Display: View mean, standard deviation, min, and max for each field.
This framework supports inverse problems to identify unknown physical parameters from limited observational data:
- Constant Parameters: Identify global
αandσvalues - Spatially-Varying Parameters: Reconstruct
α(x)andσ(x)fields - Uncertainty Quantification: Variance-based confidence estimation
This capability is critical for applications where direct measurement of material properties is expensive or impossible.
This framework is suitable for a wide range of industrial applications:
| Industry | Application | Impact |
|---|---|---|
| Electronics | Thermal management of ICs, PCB design, electronic packaging | Reduced prototyping costs, improved reliability |
| Energy | Battery cell design, electro-thermal modeling of Li-ion cells | Enhanced safety, extended battery life |
| Manufacturing | Induction heating, electroslag remelting, welding processes | Optimized process parameters, reduced energy consumption |
| Aerospace | Thermal protection systems, electromagnetic shielding | Improved safety margins, weight reduction |
| Biomedical | Hyperthermia treatment planning, RF ablation | Personalized treatment, reduced side effects |
Special thanks to SMNRFD for the original idea and conceptual direction behind the TransformerPINN architecture — proposing the use of attention-based components (RoPE, GQA, SwiGLU, Fourier feature embeddings) for this problem.
Building on that direction, the full architecture design, implementation, hyperparameter tuning, and integration into the physics-informed training pipeline were carried out independently — including adapting these components to satisfy the PDE-constrained loss function and ensuring numerical stability during training.
-
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.
-
Karniadakis, G. E., et al. (2021). Physics-informed machine learning. Nature Reviews Physics.
-
Physics-Informed Neural Networks for Multiphysics Simulations: Application to Coupled Electromagnetic-Thermal Modeling. IEEE Access, 2023.
This project is licensed under the MIT License - see the LICENSE file for details.
For questions, collaborations, or feedback:
- GitHub Issues: Open an issue
- Email: king.mohamd.09876@gmail.com
- LinkedIn: mohammad-hussein-dev
⭐ If you find this project useful for your research or work, please consider giving it a star on GitHub!