Skip to content

Add Differentiable Physics: Mass-Spring System example #1332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

AbhiLegend
Copy link

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!

Copy link

netlify bot commented Apr 29, 2025

Deploy Preview for pytorch-examples-preview canceled.

Name Link
🔨 Latest commit 7d3d2a1
🔍 Latest deploy log https://app.netlify.com/sites/pytorch-examples-preview/deploys/6810ce4e46ef87000846902c

@AbhiLegend
Copy link
Author

@msaroufim I have just updated the example with PR, request you to check if it fits in the "differentiable physics" section

@AbhiLegend AbhiLegend mentioned this pull request May 1, 2025
12 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants