How does torch-diffsim work?

How does torch-diffsim work?#

This section provides a description of how torch-diffsim implements differentiable physics simulation. We cover both the forward simulation (physics) and the backward differentiation (gradients).

Overview#

torch-diffsim combines two key components:

  1. Physics Simulation: Tetrahedral Stable Neo-Hookean FEM with either a semi-implicit (symplectic Euler) step or an optional backward-Euler IPC solve

  2. Differentiation: Unrolled PyTorch autograd for the explicit solver and an implicit adjoint for each converged IPC step

Core Principles#

  • Semi-Implicit Integration

The explicit solver uses a first-order semi-implicit Euler update. That update is symplectic before the solver’s damping, velocity limiting, constraints, and contact response are applied.

  • Smooth Operations

The differentiable solver uses unrolled PyTorch operations. Gradients are local and piecewise around contact activation.

  • Energy-Based Formulation

Forces are derived as negative gradients of the total elastic energy, which naturally integrates with PyTorch’s autograd system.

  • Barrier Functions

Differentiable ground contact uses a compact \(C^2\) penalty instead of hard projection. The IPC solver instead uses the IPC barrier together with conservative CCD.

Mathematical Notation#

Throughout this documentation, we use the following notation:

Symbol

Meaning

\(\mathbf{x}^n\)

Vertex positions at time step \(n\), \((N \times 3)\) tensor

\(\mathbf{v}^n\)

Vertex velocities at time step \(n\), \((N \times 3)\) tensor

\(\mathbf{M}\)

Mass matrix (diagonal), \((N \times N)\) matrix

\(\mathbf{f}\)

Force vector, \((N \times 3)\) tensor

\(\mathbf{F}\)

Deformation gradient, \((M \times 3 \times 3)\) tensor

\(\Psi(\mathbf{F})\)

Strain energy density function

\(\Delta t\)

Time step size

\(N\)

Number of vertices

\(M\)

Number of tetrahedral elements

\(E\)

Young’s modulus (material parameter)

\(\nu\)

Poisson’s ratio (material parameter)