N-Body Gravity System

A multibody gravity simultaion system

This project is maintained by mpinto70

Multiparticle gravity system

This system was inspired by these articles

Introduction

Given two bodies and of masses and ; separated by a distance , Newton’s law of universal gravitation establishes that the gravitational force that attracts one body to the other is:

where is the gravitational force of two bodies; is the distance between them and is the gravitational constant: . The gravitational force is the same in both bodies and point from one body to the other.

Given the positions of , , and , , the vector pointing from to is

And the unitary vector in the direction from to is

The vector form of eq. 1 is:

As can be seen in the figure above: .

Other equations

Newton’s second law of motion:

Constant linear acceleration equations

Considering multi-body interaction illustrated in above figure, eq. 3 and eq. 4 become:

Approximations

It should be noted that, from eq. 2, and consequently (eq. 4). The simulation will progress in small time increments and consider that is constant during these increments. The consequence of this procedure is shown in the figure below.

The dashed circle is where the body would really be after time elapsed, but since I consider that force is constant during interval, the calculated position is the one shown by the filled green disk. The deviation has the following relations:

Of all factors affecting , is the only one that can be manipulated during the simulation.

Reducing the number of operations

In order to reduce the number of operations performed, I use some variations of equations of previous sections:

The velocity change (eq. 14) has to be calculated to determine next velocity (eq. 16) and can be reused in next position calculation (eq. 15). For acceleration calculation (eq. 17) I use the following matrix product:

Using Hadamard product, eq. 18 becomes

Note that is constant, , and .

One of the properties of the Hadamard product is

If we have , then , eq. 20 becomes

Implementation

Since is constant (eq. 19), it can be calculated at the beginning of the simulation and then reused in every iteration to calculate .

Scale

For solar system simulations:

In order to avoid numbers that are too big or too small, we should strive to combine big numbers with small numbers (like I did with ). Also, eq. 17 could be written as

I could have made and , but in that case , which is a very small number. So I preferred to formulate the problem as I did, because , which is the unitary vector, and which makes , that are not so small as the other.

Simulations

In order to assess the effect of using a bigger , I ran two simulations for the inner solar system, for 300 years with two different values for : and . The commands ran were:

bin/exe files/inner-solar-system 300 2 >/tmp/solar-system.2.out
bin/exe files/inner-solar-system 300 10 >/tmp/solar-system.20.out

In order to generate the graphs for these simulations I ran the following commands:

gnuplot -c files/inner-solar-system.gnuplot /tmp/solar-system.2.out 2
gnuplot -c files/inner-solar-system.gnuplot /tmp/solar-system.20.out 20

The results are shown below.

Note that, for the bigger , Mercury orbit is expanding a lot. For the smaller the expansion is more limited.