examples
examples copied to clipboard
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!