examples icon indicating copy to clipboard operation
examples copied to clipboard

Add Differentiable Physics: Mass-Spring System example

Open AbhiLegend opened this issue 7 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

Deploy Preview for pytorch-examples-preview canceled.

Name Link
Latest commit f1a806e435756348b577aeace15dae5254fd914c
Latest deploy log https://app.netlify.com/projects/pytorch-examples-preview/deploys/682ce2476390c90008af215c

netlify[bot] avatar Apr 29 '25 13:04 netlify[bot]

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

AbhiLegend avatar Apr 30 '25 20:04 AbhiLegend

I don't see any testing changes that were made, you probably want to update this file https://github.com/pytorch/examples/blob/main/run_python_examples.sh

I do also think it's worth making some visualization for the work and putting it in the main README

msaroufim avatar May 13 '25 21:05 msaroufim

I don't see any testing changes that were made, you probably want to update this file https://github.com/pytorch/examples/blob/main/run_python_examples.sh

I do also think it's worth making some visualization for the work and putting it in the main README

I have updated the PR.Thanks

AbhiLegend avatar May 13 '25 22:05 AbhiLegend

I don't see any testing changes that were made, you probably want to update this file https://github.com/pytorch/examples/blob/main/run_python_examples.sh

I do also think it's worth making some visualization for the work and putting it in the main README

Updated all the changes.

AbhiLegend avatar May 13 '25 22:05 AbhiLegend

@msaroufim , can you please review the PR? Please lemme know if something else is required for this example. Thanks :)

AbhiLegend avatar May 16 '25 12:05 AbhiLegend

@jafraustro I made the changes.

AbhiLegend avatar May 18 '25 21:05 AbhiLegend

@dvrogozh made the changes as perrequest.

AbhiLegend avatar May 19 '25 22:05 AbhiLegend

@dvrogozh @jafraustro I have made the changes.

AbhiLegend avatar May 20 '25 20:05 AbhiLegend

@dvrogozh @jafraustro I have made the changes.

Any update on this.

AbhiLegend avatar May 26 '25 21:05 AbhiLegend