Skip to main content

Simple finite element assemblers with torch.

Project description

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

torch-fem: differentiable linear elastic finite elements

Simple finite element assemblers for linear elasticity with PyTorch. The advantage of using PyTorch is the ability to efficiently compute sensitivities and use them in structural optimization.

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.

Optimization examples

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


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


Simple topology optimization of a 3D beam: 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.

Installation

Your may install torch-fem via pip with

pip install torch-fem

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

# Define a (minimal) mesh 
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]])

# Apply a load at the tip
tip = (nodes[:, 0] == 2.0) & (nodes[:, 1] == 1.0)
forces = torch.zeros_like(nodes)
forces[tip, 1] = -1.0

# Constrained displacement at left end
left = nodes[:, 0] == 0.0
displacements = torch.zeros_like(nodes)
constraints = torch.zeros_like(nodes, dtype=bool)
constraints[left, :] = True

# Thickness
thickness = torch.ones(len(elements))

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

# Create model
cantilever = Planar(nodes, elements, forces, displacements, constraints, thickness, material.C())

This creates a minimal planar FEM model:

minimal

# Solve
u, f = cantilever.solve()

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

This solves the model and plots the result:

minimal

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.1.14.tar.gz (48.5 kB view details)

Uploaded Source

Built Distribution

torch_fem-0.1.14-py3-none-any.whl (52.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: torch_fem-0.1.14.tar.gz
  • Upload date:
  • Size: 48.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.5

File hashes

Hashes for torch_fem-0.1.14.tar.gz
Algorithm Hash digest
SHA256 cbddc444b652799e617b039bbb76243e0e966bdea58d5825ac6928c62516cd08
MD5 6582d2cc26f0986cfa747663c5d475f5
BLAKE2b-256 29ebed10d45695e98504f3fa912df1fd8bd4a645e3878cebfde58c2407cd0750

See more details on using hashes here.

File details

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

File metadata

  • Download URL: torch_fem-0.1.14-py3-none-any.whl
  • Upload date:
  • Size: 52.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.5

File hashes

Hashes for torch_fem-0.1.14-py3-none-any.whl
Algorithm Hash digest
SHA256 9772c288fbdbee9bc7fe74a29d01996d5c0a897c2cb86e4e72492aa4aba442c4
MD5 d90d9be7b1e7cf46338dca63779029fb
BLAKE2b-256 cda269ee93efca6dd182c3a1c08b1e3e5ce7f087859bb51b9f5c3dda2f6c9364

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