https://github.com/user-attachments/assets/e7d80ce7-af74-4deb-bb5a-aa3f93bb7a6d
⚙️ torch-diffsim
Documentation is hosted at: https://rishit-dagli.github.io/torch-diffsim/
Checkout the Deepwiki: torch-diffsim for an in-depth conceptual description.
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
# IPC backend
pip install "torch-diffsim[ipc]"
# or from source
git clone https://github.com/Rishit-dagli/torch-diffsim
cd torch-diffsim
pip install -e .
Quick start: suspend a soft bunny
from pathlib import Path
import torch
from diffsim import SemiImplicitSolver, Simulator, StableNeoHookean, TetrahedralMesh
device = "cuda" if torch.cuda.is_available() else "cpu"
source = TetrahedralMesh.from_file(
Path("assets/tetmesh/stanford_bunny.msh"), device=device
)
mesh = TetrahedralMesh(
source.vertices + source.vertices.new_tensor([0.0, 0.60, 0.0]),
source.tetrahedra,
device=device,
)
sim = Simulator(
mesh,
StableNeoHookean(youngs_modulus=2e5, poissons_ratio=0.4),
SemiImplicitSolver(dt=0.002, damping=0.996, substeps=6),
density=1000.0,
device=device,
)
support = torch.where(
mesh.vertices[:, 1] >= torch.quantile(mesh.vertices[:, 1], 0.96)
)[0]
sim.set_fixed_vertices(support)
for _ in range(500):
sim.step()
Run python examples/suspended_bunny.py to see the fixed points, rest pose,
deformation, von Mises stress, and equivalent Green strain.
Quick start: train through the simulator
Here one torch.nn.Module learns feedback across four combinations of
ear-tip target and initial impulse. Adam differentiates the exact mean loss
through all 48 FEM steps in every condition.
import torch
from examples.train_neural_controller import (
EarController,
build_problem,
rollout,
)
device = "cuda" if torch.cuda.is_available() else "cpu"
torch.manual_seed(8)
if torch.cuda.is_available():
torch.cuda.manual_seed_all(8)
problem = build_problem(device)
controller = EarController().to(device)
optimizer = torch.optim.Adam(controller.parameters(), lr=0.018)
for _ in range(12):
optimizer.zero_grad()
for target_distance, initial_velocity in problem["training_conditions"]:
result = rollout(
problem,
controller,
steps=48,
target_distance=target_distance,
initial_tip_velocity=initial_velocity,
)
(result["loss"] / len(problem["training_conditions"])).backward()
torch.nn.utils.clip_grad_norm_(controller.parameters(), 1.0)
optimizer.step()
The example retains the best policy, fits the constant baseline on the same training set, and records both held-out trajectories:
python examples/train_neural_controller.py --plot neural_bunny.png
Learn more
- User Guide and API: https://torch-diffsim.github.io
- Examples: see the
examples/directory - How it works: simulation and differentiation details in the docs
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}}
}
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_diffsim-0.2.0.tar.gz.
File metadata
- Download URL: torch_diffsim-0.2.0.tar.gz
- Upload date:
- Size: 32.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
34843fcf34e2689954c78eb08bb000f593e6d8b920500dcfc8d3d518be964ee4
|
|
| MD5 |
1954ed62454b33c5df5a99f84e54de38
|
|
| BLAKE2b-256 |
95d780588f48d06bbbec2c57d13e7893253ebcf951f07304ef39e19dabe739ed
|
File details
Details for the file torch_diffsim-0.2.0-py3-none-any.whl.
File metadata
- Download URL: torch_diffsim-0.2.0-py3-none-any.whl
- Upload date:
- Size: 35.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
34c66909d3fb3d131fddf1a4628e9bbf8036db14be127aea0958bc43f761472e
|
|
| MD5 |
dc0e386a13e9f32dab3d5753ddb27d7b
|
|
| BLAKE2b-256 |
58283e7bf3f92a19d76c2dfa179ca23873e5527a8da7f0a0c21fce21df487240
|