examples icon indicating copy to clipboard operation
examples copied to clipboard

Add Differentiable Physics: Mass-Spring System example

Open AbhiLegend opened this issue 5 months ago • 10 comments

Summary

This PR adds a simple Differentiable Physics example implementing a Mass-Spring System using PyTorch.

  • Particles are connected by springs and evolve under the influence of spring forces and gravity.
  • The system is fully differentiable, allowing optimization of particle positions via gradient descent.
  • Semi-implicit Euler integration is used for better numerical stability.
  • The example does not require any external dependencies beyond PyTorch.

Files Added

  • examples/differentiable_physics/mass_spring.py
  • examples/differentiable_physics/README.md
  • examples/differentiable_physics/requirements.txt

Features

  • Training mode: Optimize particle positions to match a target configuration.
  • Evaluation mode: Forward simulate without optimization.
  • Particle 0 is pinned at (0,0) to prevent system drift.
  • Gravity strength is customizable.
  • Fully differentiable simulation with simple, clean code structure.

Testing

  • Verified mass_spring.py runs successfully in both train and eval modes.
  • Added a bash function to run_python_examples.sh for CI testing (5 epochs, 3 steps for quick test).
  • Local testing was performed with Git Bash on Windows (python -m uv is used for compatibility).
  • Confirmed loss convergence and expected particle behavior.

Notes

  • Minor residual errors are expected due to the elasticity of springs and gravitational force.
  • Example is designed to prioritize educational clarity and maintain a low dependency footprint.

Thank you for reviewing!

AbhiLegend avatar Apr 29 '25 13:04 AbhiLegend