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:
Physics Simulation: A semi-implicit (symplectic Euler) time integrator for tetrahedral finite element method (FEM) using the Stable Neo-Hookean hyperelastic material model
Automatic Differentiation: Gradient computation through the simulation using PyTorch’s autograd, enabling gradient-based optimization of material properties, initial conditions, and other parameters
- Simulation
- Differentiation
- Automatic Differentiation Overview
- Differentiable Simulation
- Gradient Flow Through Time Steps
- Energy-Based Force Computation
- Material Parameter Gradients
- Differentiable Contact
- Smooth Operations for Differentiation
- Memory-Efficient Backpropagation
- Spatially Varying Materials
- Optimization Example
- Practical Considerations
- Verification
Core Principles#
Symplectic Integration
The simulator uses a first-order symplectic method (semi-implicit Euler) that provides better energy conservation than explicit methods.
Smooth Operations
All operations are smooth (differentiable) - no hard constraints, projections, or conditional branching that would break gradient flow.
Energy-Based Formulation
Forces are derived as negative gradients of the total elastic energy, which naturally integrates with PyTorch’s autograd system.
Barrier Functions
Contact handling uses smooth barrier potentials instead of hard constraints, maintaining \(C^2\) continuity for gradient computation.
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) |