Add Differentiable Physics: Mass-Spring System example
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.pyexamples/differentiable_physics/README.mdexamples/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.pyruns successfully in bothtrainandevalmodes. - Added a bash function to
run_python_examples.shfor CI testing (5 epochs, 3 steps for quick test). - Local testing was performed with Git Bash on Windows (
python -m uvis 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!
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 |
@msaroufim I have just updated the example with PR, request you to check if it fits in the "differentiable physics" section
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 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
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.
@msaroufim , can you please review the PR? Please lemme know if something else is required for this example. Thanks :)
@jafraustro I made the changes.
@dvrogozh made the changes as perrequest.
@dvrogozh @jafraustro I have made the changes.
@dvrogozh @jafraustro I have made the changes.
Any update on this.