GPU accelerated differential finite elements for solid mechanics with PyTorch.
Project description
torch-fem
Simple GPU accelerated differentiable finite elements for solid mechanics with PyTorch. PyTorch enables efficient computation of sensitivities via automatic differentiation and using them in optimization tasks.
Installation
Your may install torch-fem via pip with
pip install torch-fem
Optional: For GPU support, install CUDA, PyTorch for CUDA, and the corresponding CuPy version.
For CUDA 11.8:
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
pip install cupy-cuda11x # v11.2 - 11.8
For CUDA 12.6:
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu126
pip install cupy-cuda12x # v12.x
Features
-
Elements
- 1D: Bar1, Bar2
- 2D: Quad1, Quad2, Tria1, Tria2
- 3D: Hexa1, Hexa2, Tetra1, Tetra2
- Shell: Flat-facet triangle (linear only)
-
Material models
- Isotropic linear elasticity
- Orthotropic linear elasticity
- Isotropic small strain plasticity
- Isotropic small strain damage
- Hyperelasticity (via automatic differentiation of their energy function)
- Isotropic thermal conductivity
- Orthotropic thermal conductivity
- Custom user material interface
-
Utilities
- Homogenization of orthotropic elasticity for composites
- Simple structured meshing
- I/O to and from other mesh formats via meshio
Basic examples
The subdirectory examples->basic contains a couple of Jupyter Notebooks demonstrating the use of torch-fem for trusses, planar problems, shells and solids. You may click on the examples to check out the notebooks online.
Optimization examples
The subdirectory examples->optimization demonstrates the use of torch-fem for optimization of structures (e.g. topology optimization, composite orientation optimization). You may click on the examples to check out the notebooks online.
Minimal example
This is a minimal example of how to use torch-fem to solve a very simple planar cantilever problem.
import torch
from torchfem import Planar
from torchfem.materials import IsotropicElasticityPlaneStress
torch.set_default_dtype(torch.float64)
# Material
material = IsotropicElasticityPlaneStress(E=1000.0, nu=0.3)
# Nodes and elements
nodes = torch.tensor([[0., 0.], [1., 0.], [2., 0.], [0., 1.], [1., 1.], [2., 1.]])
elements = torch.tensor([[0, 1, 4, 3], [1, 2, 5, 4]])
# Create model
cantilever = Planar(nodes, elements, material)
# Load at tip [Node_ID, DOF]
cantilever.forces[5, 1] = -1.0
# Constrained displacement at left end [Node_IDs, DOFs]
cantilever.constraints[[0, 3], :] = True
# Show model
cantilever.plot(node_markers="o", node_labels=True)
This creates a minimal planar FEM model:
# Solve
u, f, σ, F, α = cantilever.solve()
# Plot displacement magnitude on deformed state
cantilever.plot(u, node_property=torch.norm(u, dim=1))
This solves the model and plots the result:
If we want to compute gradients through the FEM model, we simply need to define the variables that require gradients. Automatic differentiation is performed through the entire FE solver.
# Enable automatic differentiation
cantilever.thickness.requires_grad = True
u, f, _, _, _ = cantilever.solve()
# Compute sensitivity of compliance w.r.t. element thicknesses
compliance = torch.inner(f.ravel(), u.ravel())
torch.autograd.grad(compliance, cantilever.thickness)[0]
Benchmarks
The following benchmarks were performed on a cube subjected to a one-dimensional extension. The cube is discretized with N x N x N linear hexahedral elements, has a side length of 1.0 and is made of a material with Young's modulus of 1000.0 and Poisson's ratio of 0.3. The cube is fixed at one end and a displacement of 0.1 is applied at the other end. The benchmark measures the forward time to assemble the stiffness matrix and the time to solve the linear system. In addition, it measures the backward time to compute the sensitivities of the sum of displacements with respect to forces.
Apple M1 Pro (10 cores, 16 GB RAM)
Python 3.10, SciPy 1.14.1, Apple Accelerate, float64
| N | DOFs | FWD Time | BWD Time | Peak RAM |
|---|---|---|---|---|
| 10 | 3000 | 0.16s | 0.10s | 870.0MB |
| 20 | 24000 | 0.99s | 0.18s | 1268.9MB |
| 30 | 81000 | 3.44s | 0.60s | 1953.6MB |
| 40 | 192000 | 8.78s | 1.19s | 2749.4MB |
| 50 | 375000 | 17.90s | 2.29s | 3920.5MB |
| 60 | 648000 | 32.46s | 3.69s | 5103.4MB |
| 70 | 1029000 | 53.75s | 6.23s | 7243.5MB |
| 80 | 1536000 | 85.68s | 9.61s | 10532.1MB |
AMD Ryzen Threadripper PRO 5995WX (64 Cores, 512 GB RAM) and NVIDIA GeForce RTX 4090
Python 3.12, CuPy 13.3.0, CUDA 11.8
| N | DOFs | FWD Time | BWD Time | Peak RAM |
|---|---|---|---|---|
| 10 | 3000 | 0.66s | 0.15s | 1371.7MB |
| 20 | 24000 | 1.00s | 0.43s | 1358.9MB |
| 30 | 81000 | 1.14s | 0.65s | 1371.1MB |
| 40 | 192000 | 1.37s | 0.83s | 1367.3MB |
| 50 | 375000 | 1.51s | 1.04s | 1356.4MB |
| 60 | 648000 | 1.94s | 1.43s | 1342.1MB |
| 70 | 1029000 | 5.19s | 4.31s | 1366.8MB |
| 80 | 1536000 | 7.48s | 18.88s | 5105.6MB |
Alternatives
There are many alternative FEM solvers in Python that you may also consider:
- Non-differentiable
- Differentiable
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file torch_fem-0.5.1.tar.gz.
File metadata
- Download URL: torch_fem-0.5.1.tar.gz
- Upload date:
- Size: 2.9 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
617fb3937961f676b2e20e6fed8d26de26539c3675928c4e03f64926d1a976f1
|
|
| MD5 |
0d33cd31b8dad85eb9c2030d64ea4b51
|
|
| BLAKE2b-256 |
11af9eebaa769b1ab331b9b75aaef4dce86c6d01c0fabed8e95203d4edf58ce4
|
File details
Details for the file torch_fem-0.5.1-py3-none-any.whl.
File metadata
- Download URL: torch_fem-0.5.1-py3-none-any.whl
- Upload date:
- Size: 2.9 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2df9e6e14b5d6e483e7eede3cd4193274fb4ee387547d40efecec0130e411938
|
|
| MD5 |
82b4ce87321b3515464d66a49eccb975
|
|
| BLAKE2b-256 |
d7634d8d9ea1504ecbbcb17c6fac361391bbc7b52e51eaa1005fd5ab10d6c361
|