An explainable neural network architecture for solving 1D initial and boundary value problems using physics-informed learning combined with finite element methods.
FENNs (Finite Element Neural Networks) is a novel approach to solving partial differential equations (PDEs) that combines the interpretability of finite element methods with the flexibility of neural networks. Unlike traditional Physics-Informed Neural Networks (PINNs), FENNs use explicit polynomial basis functions constructed on adaptive mesh elements, making the learned solution both accurate and interpretable.
- Explainable Architecture: Uses Lagrange polynomial basis functions with clear physical interpretation
- Adaptive Mesh Refinement: Automatic h-refinement and r-refinement based on residual error
- Variational Formulation: Minimizes energy functionals for robust convergence
- Compact & Hierarchical Bases: Supports both compact (nodal) and hierarchical polynomial representations
- GPU Acceleration: Full PyTorch implementation with CUDA support
FENNs discretize the domain into finite elements and represent the solution as:
where:
-
$U_i$ are learnable nodal values (network parameters) -
$\phi_i(x)$ are polynomial basis functions (Lagrange polynomials) -
$N$ is the number of nodes in the mesh
The architecture supports two types of polynomial bases:
-
Compact Bases (
modules/compact_bases/):- Lagrange interpolation polynomials
- Local support on each element
-
$C^0$ continuity across elements
-
Hierarchical Bases (
modules/hierarchical_bases/):- Hierarchical polynomial representation
- Efficient for p-adaptivity
- Better conditioning for higher degrees
The modules/blocks.py file provides interpretable neural network components:
- LinearBlock: ReLU-based piecewise linear activations
- MultiplicationBlock: Composes blocks to form higher-degree polynomials
- InversionBlock: For handling singularities and complex boundary conditions
-โยท(Dโu) = f(x) in ฮฉ
u = uโ on โฮฉ
Implementation: models/beam_stress_poisson1D.py
Energy functional minimized:
-Dโยฒu + cโu = 0 in ฮฉ
u = uโ, u = uโ on โฮฉ
Implementation: models/convection_diffusion_1D.py
Features:
- Exact solution comparison
- Lยฒ error computation
- Adaptive refinement for boundary layers
FEniCS is required for validation and comparison. Install using conda:
conda create -n fenns python=3.10
conda activate fenns
conda install -c conda-forge fenics-dolfinx mpich pyvistaInside your conda environment, install UV package manager and dependencies:
pip install uv
uv pip install -e .This will install:
- PyTorch (with CUDA 12.6 support on Linux/Windows)
- NumPy
- Matplotlib
from models.convection_diffusion_1D import ConvectionDiffusion1D
import torch
# Initialize model
model = ConvectionDiffusion1D(
poly_deg=2, # Polynomial degree per element
num_elements=20, # Number of finite elements
eval_pts_per_elem=50, # Evaluation points per element
refine_alpha=0.6, # Refinement threshold
max_r_adapt=5, # Max r-adaptivity iterations
device="cuda" # Use GPU
)
# Training loop
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
for epoch in range(1000):
optimizer.zero_grad()
# Compute energy functional
J, convection, diffusion = model.forward()
# Apply boundary conditions
bc_penalty = (model.U_nodes[0] - model.u0)**2 + \
(model.U_nodes[-1] - model.uL)**2
loss = J + 1000.0 * bc_penalty
loss.backward()
optimizer.step()
if epoch % 100 == 0:
error = model.evaluate_C0_error()
print(f"Epoch {epoch}: Loss={loss.item():.6f}, Error={error:.6f}")
# Visualize solution
model.plot_solution()
model.plot_exact_sol()The architecture supports automatic mesh refinement:
# Perform h-refinement (split high-error elements)
model.h_adapt_mesh()
# Perform r-refinement (redistribute nodes)
model.r_adapt_mesh()
# Visualize refinement
model.visualize_mesh_adaptation()| Feature | FENNs | Traditional PINNs |
|---|---|---|
| Interpretability | โ Explicit polynomial bases | โ Black-box activations |
| Mesh Adaptivity | โ h/r-refinement | |
| Solution Continuity | โ
Guaranteed |
|
| Convergence | โ Monotonic (energy minimization) | |
| Derivative Accuracy | โ Exact polynomial derivatives | |
| Physical Insight | โ Node values = DOFs | โ Opaque parameters |
FENNs/
โโโ models/ # Problem-specific implementations
โ โโโ base_steady_state_1D.py # Base class for 1D steady-state problems
โ โโโ beam_stress_poisson1D.py # Poisson equation solver
โ โโโ convection_diffusion_1D.py # Convection-diffusion solver
โ โโโ hierarchical/ # Hierarchical basis implementations
โ โโโ beam_stress_poisson1D.py
โโโ modules/ # Core neural network modules
โ โโโ blocks.py # Interpretable activation blocks
โ โโโ compact_bases/ # Lagrange polynomial bases
โ โ โโโ poly_general.py
โ โโโ hierarchical_bases/ # Hierarchical polynomial bases
โ โโโ poly_deg2.py
โ โโโ poly_general.py
โโโ utils/ # Utilities and validation
โ โโโ base_logger.py # Training logger
โ โโโ lagrange_funcs.py # Lagrange interpolation utilities
โ โโโ simulations/ # FEM validation solvers
โ โโโ heat_equation_1D.py
โ โโโ solve_poisson_problem.py
โโโ notebooks/ # Jupyter notebooks
โโโ Basis Functions.ipynb # Basis function visualization
โโโ Linear Problem Setup.ipynb # Problem formulation examples
โโโ PINNs for Comparison.ipynb # Comparison with traditional PINNs
Instead of satisfying the PDE pointwise, FENNs minimize the energy functional:
where
The domain
On each element, the solution is approximated by:
where
Parameters (nodal values
- h-refinement: Split elements with high residual error
- r-refinement: Redistribute nodes to minimize error gradients
The utils/simulations/ directory contains reference FEM solvers using FEniCS for validation:
from utils.simulations.solve_poisson_problem import solve_poisson_1d
# Get reference solution
u_fem, x_fem = solve_poisson_1d(num_elements=100)
# Compare with FENN solution
error = torch.norm(u_fenn - u_fem) / torch.norm(u_fem)
print(f"Relative Lยฒ error: {error:.2e}")- Extension to 2D/3D problems
- Time-dependent problems (parabolic PDEs)
- Nonlinear material models
- Automatic differentiation for material tangents
- Multi-physics coupling
- p-adaptivity (adaptive polynomial order)
If you use FENNs in your research, please consider citing:
@software{fenns2025,
title={FENNs: Finite Element Neural Networks for Explainable PDE Solutions},
author={Your Name},
year={2025},
url={https://github.com/yourusername/FENNs}
}This project is open source and available under the MIT License.
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
For questions or collaborations, please open an issue on GitHub.
Note: This is a research project under active development. API may change between versions.