Skip to main content

a very minimal paralllelizable physics simulator supporting differentiation entirely in torch.

Project description

https://github.com/user-attachments/assets/e7d80ce7-af74-4deb-bb5a-aa3f93bb7a6d

⚙️ torch-diffsim

PyPI

Documentation is hosted at: https://rishit-dagli.github.io/torch-diffsim/

torch-diffsim is an extremely minimal parallelizable differentiable finite element (FEM) simulator written entirely in PyTorch. It uses a semi-implicit (symplectic Euler) integrator, a stable Neo-Hookean material model, and smooth barrier-based contact. All operations preserve gradients to enable optimization of materials and states.

Install

pip install torch-diffsim

# or from source
git clone https://github.com/Rishit-dagli/torch-diffsim
cd torch-diffsim
pip install -e .

Quick start (standard simulation)

from diffsim import TetrahedralMesh, StableNeoHookean, SemiImplicitSolver, Simulator

# Load the Stanford Bunny tetrahedral mesh (path in this repository)
mesh = TetrahedralMesh.from_file("assets/tetmesh/bunny0.msh")

material = StableNeoHookean(youngs_modulus=1e5, poissons_ratio=0.45)
solver = SemiImplicitSolver(dt=0.01, damping=0.99, substeps=4)
sim = Simulator(mesh, material, solver)

for _ in range(200):
    sim.step()

https://github.com/user-attachments/assets/8e4c1e52-6baf-4806-b8d7-9f87f74c1af5

Quick start (differentiable simulation)

import torch
from diffsim import TetrahedralMesh
from diffsim.diff_physics import DifferentiableMaterial
from diffsim.diff_simulator import DifferentiableSolver, DifferentiableSimulator

device = "cuda" if torch.cuda.is_available() else "cpu"
mesh = TetrahedralMesh.create_cube(resolution=3, size=0.5, device=device)
mesh._compute_rest_state()

material = DifferentiableMaterial(youngs_modulus=1e5, poissons_ratio=0.4, requires_grad=True).to(device)
solver = DifferentiableSolver(dt=0.01, damping=0.99, substeps=4)
sim = DifferentiableSimulator(mesh, material, solver, device=device)

# Example loss: pull center of mass toward a target
target = torch.tensor([0.0, -0.3, 0.0], device=device)
for _ in range(20):
    sim.step()
loss = (sim.positions.mean(dim=0) - target).pow(2).sum()
loss.backward()

Learn more

Citation

If you use torch-diffsim in academic work, please include a citation or link to the project repository.

@misc{torch-diffsim,
  title  = {torch-diffsim},
  author = {Rishit Dagli},
  year   = {2025},
  howpublished = {\url{https://github.com/Rishit-dagli/torch-diffsim}}
}

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_diffsim-0.1.0.tar.gz (25.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

torch_diffsim-0.1.0-py3-none-any.whl (29.0 kB view details)

Uploaded Python 3

File details

Details for the file torch_diffsim-0.1.0.tar.gz.

File metadata

  • Download URL: torch_diffsim-0.1.0.tar.gz
  • Upload date:
  • Size: 25.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for torch_diffsim-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6ebffd8735774bc882bf3ca402ad501b36d081ffdedad324140b093bd2ac4625
MD5 2ddb7bc488fea6682eb8ff75e6c85e3b
BLAKE2b-256 aacddd05b07a39d9ed5680f0309fbed79c8204937468dca1a09d7a4de447c1f0

See more details on using hashes here.

File details

Details for the file torch_diffsim-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: torch_diffsim-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 29.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for torch_diffsim-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fae23cd03d32784b899b13d81755658b1d3c58f8b435e4149d6a5ba74509338e
MD5 44e320eb4db264c33b0a3f49612db968
BLAKE2b-256 980b6bdab1803777424f17d7dd94543e3c264f1d4cbfd78c0926a0f8ef475613

See more details on using hashes here.

Supported by

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