Skip to main content

Simple finite element assemblers with torch.

Project description

License: MIT PyPI - Python Version PyPI - Version Black Binder

torch-fem: differentiable finite elements in PyTorch

Simple finite element assemblers for small-deformation mechanics with PyTorch. The advantage of using PyTorch is the ability to efficiently compute sensitivities and use them in optimization tasks.

Installation

Your may install torch-fem via pip with

pip install torch-fem

Features

  • Elements
    • 1D: Bar1 (linear), Bar2 (quadratic)
    • 2D: Quad1 (linear), Quad2 (quadratic), Tria1 (linear), Tria2 (quadratic)
    • 3D: Hexa1 (linear), Hexa2 (quadratic), Tetra1 (linear), Tetra2 (quadratic)
    • Shell: Flat-facet triangle (linear)
  • Material models
    • Isotropic linear elasticity (3D, 2D plane stress, 2D plane strain, 1D)
    • Orthotropic linear elasticity (3D, 2D plane stress, 2D plane strain)
    • Isotropic plasticity (3D, 2D plane stress, 1D)
  • Utilities
    • Homogenization of orthotropic stiffness (mean field)
    • 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.


Simple cantilever beam: There are examples with linear and quadratic triangles and quads.


Plasticity in a plate with hole: Isotropic linear hardening model for plane-stress.

Optimization examples

The subdirectory examples->optimization demonstrates the use of torch-fem for optimization of structures (e.g. topology optimization, composite orientation optimization).


Simple shape optimization of a truss: The top nodes are moved and MMA + autograd is used to minimize the compliance.


Simple topology optimization of a MBB beam: You can switch between analytical sensitivities and autograd sensitivities.


3D topology optimization of a jet engine bracket: The model is exported to Paraview for visualization.


Simple shape optimization of a fillet: The shape is morphed with shape basis vectors and MMA + autograd is used to minimize the maximum stress.


Simple fiber orientation optimization of a plate with a hole: Compliance is minimized by optimizing the fiber orientation of an anisotropic material using automatic differentiation w.r.t. element-wise fiber angles.

Minimal code

This is a minimal example of how to use torch-fem to solve a simple cantilever problem.

from torchfem import Planar
from torchfem.materials import IsotropicPlaneStress

# Material
material = IsotropicPlaneStress(E=1000.0, nu=0.3)

# Nodes and elements
nodes = torch.tensor([[0.0, 0.0], [1.0, 0.0], [2.0, 0.0], [0.0, 1.0], [1.0, 1.0], [2.0, 1.0]])
elements = torch.tensor([[0, 1, 4, 3], [1, 2, 5, 4]])

# Create model
cantilever = Planar(nodes, elements, material)

# Load at tip
cantilever.forces[5, 1] = -1.0

# Constrained displacement at left end
cantilever.constraints[[0, 3], :] = True

# Show model
cantilever.plot(node_markers="o", node_labels=True)

This creates a minimal planar FEM model:

minimal

# Solve
u, f, σ, ε, α = cantilever.solve()

# Plot
cantilever.plot(u, node_property=torch.norm(u, dim=1))

This solves the model and plots the result:

minimal

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
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 with Apple Accelerate

N DOFs FWD Time FWD Memory BWD Time BWD Memory
10 3000 0.84s 0.53 MB 0.66s 0.12 MB
20 24000 5.92s 268.69 MB 5.45s 235.25 MB
30 81000 2.94s 670.89 MB 1.58s 0.62 MB
40 192000 7.86s 1681.77 MB 4.08s 350.62 MB
50 375000 16.45s 3056.41 MB 9.30s 834.12 MB
60 648000 31.62s 4049.66 MB 19.37s 1296.44 MB
70 1029000 56.33s 4495.06 MB 34.10s 2405.62 MB
80 1536000 93.71s 6787.83 MB 56.53s 3716.17 MB
90 2187000 146.70s 8282.39 MB 109.08s 6407.16 MB

AMD Ryzen Threadripper PRO 5995WX (64 Cores, 512 GB RAM)

Python 3.12 with openBLAS

N DOFs FWD Time FWD Memory BWD Time BWD Memory
10 3000 1.42s 17.27 MB 1.87s 0.00 MB
20 24000 1.30s 160.49 MB 0.98s 62.64 MB
30 81000 2.76s 480.52 MB 2.16s 305.76 MB
40 192000 6.68s 1732.11 MB 5.15s 762.89 MB
50 375000 12.51s 3030.36 MB 11.29s 1044.85 MB
60 648000 22.94s 5813.95 MB 25.54s 3481.15 MB
70 1029000 38.81s 7874.30 MB 45.06s 4704.93 MB
80 1536000 63.07s 14278.46 MB 63.70s 8505.52 MB
90 2187000 93.47s 16803.27 MB 142.94s 10995.63 MB

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

torch_fem-0.3.0.tar.gz (2.7 MB view details)

Uploaded Source

Built Distribution

torch_fem-0.3.0-py3-none-any.whl (2.7 MB view details)

Uploaded Python 3

File details

Details for the file torch_fem-0.3.0.tar.gz.

File metadata

  • Download URL: torch_fem-0.3.0.tar.gz
  • Upload date:
  • Size: 2.7 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for torch_fem-0.3.0.tar.gz
Algorithm Hash digest
SHA256 aa4ba48918f0a1302458e705a0e8f46ef09678b1603959e2251fbe8b0206486a
MD5 d6b3bc8deed254af7c5bdce4cbe3ea00
BLAKE2b-256 64a8a135af809e14246db411e877cb0de616d20cb2f8b23df96a75707126457a

See more details on using hashes here.

File details

Details for the file torch_fem-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: torch_fem-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 2.7 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for torch_fem-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e04338a472a27fd034255c80b756f37476d06ed6eab7ba52f5382d7348727385
MD5 d0c36cb7ad134eadc51e56bb115520f3
BLAKE2b-256 e3c71d1236b8d465736f1a922de724a3a93343e1cf334419370c3c96ad3a3182

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page