Documentation · Examples · Changelog
torch-fem
torch-fem is a simple GPU-accelerated differentiable finite element solver for solid mechanics built on PyTorch. Automatic differentiation provides exact sensitivities of simulation results with respect to material parameters, geometry, loads, etc. without hand-derived adjoint formulations. It is aimed at researchers in computational mechanics who need gradients through FEM solvers for tasks such as optimization, inverse problems, and machine-learning-augmented simulation.
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
- Composite laminates for shells
- Simple structured meshing
- I/O to and from other mesh formats via meshio
Installation
You may install torch-fem via pip with
pip install torch-fem
To run the example notebooks, install with the notebook extra (pip install torch-fem[notebook]). For GPU acceleration, install PyTorch with CUDA support and the matching CuPy version - see the installation guide for details.
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. Rather than differentiating through individual solver iterations or Newton iterations (this would explode in memory and autograd graph size) though, the implicit function theorem is used to formulate an adjoint backward for solve().
# Enable automatic differentiation
cantilever.thickness.requires_grad = True
u, f, _, _, _ = cantilever.solve(differentiable_parameters=cantilever.thickness)
# Compute sensitivity of compliance w.r.t. element thicknesses
compliance = torch.inner(f.ravel(), u.ravel())
torch.autograd.grad(compliance, cantilever.thickness)[0]
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.
| Plasticity in a plate with hole: Isotropic linear hardening model for plane-stress or plane-strain. |
| Finite strain cantilever: Hyperelastic model in Total Lagrangian Formulation. |
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.
Performance
torch-fem solves problems with millions of degrees of freedom: a linear elastic hexahedral cube model with 1.5 million DOFs assembles and solves in about four seconds on a consumer GPU (RTX 4090, float64). Detailed CPU and GPU benchmarks for timing and memory are reported in the performance documentation and can be reproduced with the scripts in benchmarks/.
Citing torch-fem
If you use torch-fem in your research, please cite it as follows:
@software{torchfem,
author = {Meyer, Nils},
title = {torch-fem: GPU accelerated differentiable finite elements for solid mechanics with PyTorch},
doi = {10.5281/zenodo.20306384},
url = {https://github.com/meyer-nils/torch-fem},
}
Contributing
Contributions are welcome! Please check out the contributing guide for the development workflow. Bug reports, feature requests, and usage questions are all welcome in the issue tracker - see the support guide for what to include.
Alternatives
There are many alternative Python FEM tools that you may also consider, depending on your needs:
- General-purpose FEM/PDE frameworks
- Differentiable or adjoint-capable FEM
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.7.3.tar.gz.
File metadata
- Download URL: torch_fem-0.7.3.tar.gz
- Upload date:
- Size: 3.0 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e951092ef599b5cef548628dccf886885540e28fd2d84aa344adf4b0aaae1749
|
|
| MD5 |
cca5320a6919cc6faef5d64d02a029d7
|
|
| BLAKE2b-256 |
1365eafd3d3b8cd74334f8a6e6fcf337448bf6ea4ef3dedc9fe47c035372e3d1
|
Provenance
The following attestation bundles were made for torch_fem-0.7.3.tar.gz:
Publisher:
publish-to-pypi.yml on meyer-nils/torch-fem
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
torch_fem-0.7.3.tar.gz -
Subject digest:
e951092ef599b5cef548628dccf886885540e28fd2d84aa344adf4b0aaae1749 - Sigstore transparency entry: 2167045466
- Sigstore integration time:
-
Permalink:
meyer-nils/torch-fem@61ec9fd633ff46886ffc6f6a767a0526d6c824e1 -
Branch / Tag:
refs/tags/v0.7.3 - Owner: https://github.com/meyer-nils
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@61ec9fd633ff46886ffc6f6a767a0526d6c824e1 -
Trigger Event:
push
-
Statement type:
File details
Details for the file torch_fem-0.7.3-py3-none-any.whl.
File metadata
- Download URL: torch_fem-0.7.3-py3-none-any.whl
- Upload date:
- Size: 3.0 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2c25c13e199cb7fb0f33628660c764c86a47d5c63c4a8a4fb0b33fb1d4ea86f5
|
|
| MD5 |
934d62fded9094db2bc5a92dc4ad1e86
|
|
| BLAKE2b-256 |
551279cdbef7a36c6a7d80a10ca4710b0fc6b0586013b044013fff9b6c12f7fc
|
Provenance
The following attestation bundles were made for torch_fem-0.7.3-py3-none-any.whl:
Publisher:
publish-to-pypi.yml on meyer-nils/torch-fem
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
torch_fem-0.7.3-py3-none-any.whl -
Subject digest:
2c25c13e199cb7fb0f33628660c764c86a47d5c63c4a8a4fb0b33fb1d4ea86f5 - Sigstore transparency entry: 2167045472
- Sigstore integration time:
-
Permalink:
meyer-nils/torch-fem@61ec9fd633ff46886ffc6f6a767a0526d6c824e1 -
Branch / Tag:
refs/tags/v0.7.3 - Owner: https://github.com/meyer-nils
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@61ec9fd633ff46886ffc6f6a767a0526d6c824e1 -
Trigger Event:
push
-
Statement type: