Simulating Double Pendulum with Rust and WebAssembly

A double pendulum is a pendulum with another pendulum attached to its end, and is a simple physical system that exhibits rich dynamic behavior with a strong sensitivity to initial conditions. Despite its apparent simplicity, the double pendulum demonstrates chaotic motion that makes it fascinating to study and simulate.

Why Double Pendulum?

The double pendulum is a classic example of a chaotic system in classical mechanics. What makes it particularly interesting is that even tiny differences in initial conditions can lead to dramatically different outcomes. This property, known as sensitive dependence on initial conditions, is a hallmark of chaotic systems.

When you release multiple double pendulums from nearly identical starting positions, they initially move together, but quickly diverge into completely different patterns. This beautiful visualization of chaos theory makes the double pendulum a perfect candidate for simulation.

Double Pendulum Chaos Visualization

Visualizing sensitive dependence on initial conditions

Technology Stack

For this project, I chose to use Rust compiled to WebAssembly (WASM) for several reasons:

  • Performance: Rust provides near-native performance, which is crucial for real-time physics simulations with multiple pendulums
  • Memory Safety: Rust's ownership system prevents common bugs without runtime overhead
  • Web Integration: WebAssembly allows the simulation to run directly in the browser with excellent performance
  • Portability: The same simulation code can run anywhere WebAssembly is supported

Setting Up the Development Environment

To get started with this project, you'll need three main tools:

  1. Rust: The programming language we'll use for the simulation logic
  2. wasm-pack: A tool for building and packaging Rust code for WebAssembly
  3. Node.js: For running the development server and managing the JavaScript side of the application

You can install Rust using rustup:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Then install wasm-pack:

cargo install wasm-pack

The Physics Behind It

The motion of a double pendulum is governed by a system of coupled differential equations derived from Lagrangian mechanics. The system has four degrees of freedom: two angles (θ₁ and θ₂) and their corresponding angular velocities.

Without going into the full mathematical derivation, the key insight is that we need to numerically integrate these equations of motion at each time step to update the positions and velocities of both pendulum arms.

Implementation Approach

The simulation works by:

  1. Initializing multiple pendulums with slightly different starting angles
  2. At each time step, calculating the forces and accelerations using the equations of motion
  3. Updating the positions and velocities using numerical integration (Runge-Kutta method)
  4. Rendering the pendulums and their trails on a canvas element

The Rust code handles all the physics calculations and state management, while a thin JavaScript layer handles the rendering and browser interaction.

Double Pendulum Simulation Trails

Live Demo

You can see the simulation in action at dpvis.herokuapp.com. Try adjusting the initial conditions and watch how dramatically the paths diverge!

What's Next

In future posts, I plan to dive deeper into:

  • The mathematical derivation of the equations of motion
  • Numerical integration methods and their trade-offs
  • Optimizing Rust code for WebAssembly
  • Adding interactive controls for parameters like length, mass, and damping

The double pendulum is just one example of chaotic systems that can be explored through simulation. The combination of Rust's performance and WebAssembly's portability opens up exciting possibilities for creating interactive physics simulations that run smoothly in the browser.

Conclusion

Building this simulation was a great way to explore both the fascinating physics of chaotic systems and the practical aspects of using Rust and WebAssembly for performance-critical applications. The ability to compile Rust to run in the browser with near-native performance makes it an excellent choice for computationally intensive web applications.

If you're interested in the source code or want to experiment with your own modifications, check out the project repository on GitHub.