Metadata-Version: 2.4
Name: torch-diffsim
Version: 0.1.0
Summary: a very minimal paralllelizable physics simulator supporting differentiation entirely in torch.
Author-email: Rishit Dagli <rishit.dagli@gmail.com>
License: MIT
Project-URL: Homepage, https://github.com/Rishit-Dagli/torch-diffsim
Project-URL: Documentation, https://rishit-dagli.github.io/torch-diffsim/
Project-URL: Repository, https://github.com/Rishit-Dagli/torch-diffsim
Keywords: physics,simulation,differentiable,pytorch,neo-hookean
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Physics
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: torch>=2.0.0
Requires-Dist: numpy>=1.20.0
Requires-Dist: polyscope>=1.3.0
Requires-Dist: meshio>=5.0.0
Requires-Dist: scipy>=1.7.0
Requires-Dist: matplotlib>=3.5.0
Provides-Extra: dev
Requires-Dist: pytest>=7.0.0; extra == "dev"
Requires-Dist: black>=22.0.0; extra == "dev"
Requires-Dist: ruff>=0.1.0; extra == "dev"
Dynamic: license-file

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

# ⚙️ torch-diffsim

![PyPI](https://img.shields.io/pypi/v/torch-diffsim?style=flat-square)

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


```bash
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)

```python
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)

```python
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

- User Guide and API: https://rishit-dagli.github.io/torch-diffsim/
- 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.

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


