Cornell University ECE4760
Gravity particle simulation

Pi Pico RP2350

Gravity
Gravitating particles interact over long distances via a inverse-square law force. If we write the force equation to factor out the particle mass to make an acceleration,
then in vector format we get the vector acceleration of the ith particle as:

where G is the gravitational constant, mj is the mass of the jth particle, epsilon is a small number to prevent infinite forces, and rij is the vector pointing
from particle i to particle j. This is effectively the inverse square law, muliiplied by the cos and  sin components of the connecting vector.
The equation and some of the code is based on an ECE5760 project by Mark Eiding and Brian Curless in 2015.

The goal is to compute this as fast as possible in floating point on the rp2350. You can get almost a factor of 2 speed up by
noting that the equation is symmetric between particle i and j, except for the sign of the connecting vector and the mass.
The following snippet computes the acceleration for the ith particle, while also adding up the effects of the ith particle
on all the other particles of higher index j.


A short video shows 150 gravitating particles computed in floating pont at a frame rate of 140 frames/second.
Display, of course, is 60 frames/second.
The red mass is 800, the green mass 15,  the white masses are small, 0.001.
Mass units are arbitrary, since big G was set to unity.

Code, ZIP of project


Parallelize across two cores and overclock the rp2350
The main n-squared loop that calculates the gravitaional forces between n objects can be easily parallelized across the two cores. Combining parallelization with overclocking gives almost a factor of four speed-up. To get stable execution at 300 MHz clock rate, the core voltage needed to be bumped up to 1.3 volts. The first two line of main do this:
vreg_set_voltage(VREG_VOLTAGE_1_30 );
set_sys_clock_khz (300000, false);

The inter-core FIFOs are used to sync the calculations. Core0 sends a FIFO message to core1 when drawing is done and the next computation cycle should begin. Core1 sends a FIFO message back to core0 when it is done with its half of the computation. Core0 then combines the forces from the two cores and draws the particles.

 

Code. Insert code into the zipped folder above and update the cmakefiles.txt


Copyright Cornell University April 18, 2025