The functionality of the buttons is described as follows.
Button1 shows the scene. It does not suspend calculations. Button2 stops the calculations, clears the scene, and displays a text message on the screen. Button3 reactivates computations to show the evolution of the masses orbiting in real-time. Finally, Button4 suspends the calculations so that the last scene remains on the screen.
In the following section, we describe the N-body problem.
14.1 The Gravitational N-body Problem
Figure 14-2 depicts three punctual masses, M1, M2, and M3, pointed by position vectors r1, r2, and r3, respectively. The relative position vectors between them are r1, 2, r1, 3, and r2, 3.
In Equation (14.1), the value of n ranges between 1 and 3.
In Equation (14.2) n, m can take the values 1, 2, 3, and n ≠ m.
In Equation (14.5), m ≠ n should hold; otherwise, we would have the trivial case in which the distance from one particle to itself is zero.
In Equation (14.6), we have explicitly expressed r1 as a function of time. The second derivative of this vector gives the three components of the acceleration vector that correspond to M1.
14.2 Motion Equations
In Equation (14.7), the second derivative of the position vector represents the acceleration vector acting on M1. For programming purposes, it will be useful to note that this expression is independent of M1. Therefore, we conclude that, in general, the formula for calculating the acceleration vector acting on a given mass is a function of all the others present in the system and, in turn, independent of its value.
In the following section, we describe the method to numerically solve the dynamic equations.
14.3 Numerical Solution to the Dynamic Equations
Now, if the initial spatial coordinates and component velocities at state n=0 are known, we can use Equations (14.17) through (14.19) to calculate the position of mass M1 at the state n=1. However, for the state n=2, we need the values of the component velocities of state n=1. These values can be calculated as follows.
This procedure allows us to calculate the dynamic components of M1. To obtain the dynamic equations for the other masses, we proceed in a similar way. The method can be applied, in principle, to any number of masses.
In the following section, we describe the numerical approach to calculating the dynamic parameters of the N-body problem.
14.4 Capturing Numerical Data
To visualize how we perform the calculations, we present an example with five planets, which corresponds to the screenshot in Figure 14-1.
To track all of the parameters described in the previous section, we need a memory region for storing the values that correspond to the position, velocity, and acceleration components that correspond to each mass. We also need to track the three-dimensional values calculated with Equations (14.17) through (14.25). Finally, we also need the value of each mass. All this data can be manageable if we associate a one-dimensional array to each mass.
Creating Array Planets
The first three entries, 0 to 2, correspond to the position of the planet, given by coordinates (x,y,z).
The second three entries, 3 to 5, correspond to the three velocity components, given by (vx,vy,vz).
Entry 6 corresponds to the mass of the planet.
Entries 7 to 9 correspond to the (xP, yP, zP) values calculated using Equations (14.17)-(14.19). These values will replace (x, y, z), entries 0 to 2. However, we will perform this action only after finishing all the calculations over all the planets. Otherwise, we would disturb the system, and the results would be incorrect.
Entries 10 to 12 store the new velocity components, (vxP,vyP,vzP), calculated with Equations (14.23)-(14.25). These values will replace the values stored in entries 3 to 5, after completing the calculations over all the planets.
Entries 13 to 15 store the three acceleration components, (ax,ay,az), calculated by Equations (14.8) through (14.10). These values will be used in Equations (14.23) through (14.25) to calculate the new velocity components.
5x5 Array Storing Sn, m Values for the Case N=5
In Table 14-1, there are N=5 masses present in the system, numbered from 0 to 4. The 5x5 array stores all possible combinations of parameter Sn, m. Since Sn, m = Sm, n, the array stores redundant entries that we will keep to simplify the code. Additionally, we will not use the entries marked with the X symbol.
The following section discusses the working example.
14.5 Five Planets Working Example
In principle, there is no restriction on the number of punctual masses our N-body program can handle. In our working example, N=5.
The initial values that we provide to our planets must be based on our geometrical situation. We have assigned to the horizontal and vertical axes the coordinates (x and y), respectively. The z-axis corresponds to the depth.
For simplicity, in our working example, we will position the planets on the x-z plane. The initial velocities will only have components in the z-direction. Additionally, in trying to mimic the solar system, we will position one of the planets at the center and the rest of them will orbit around this one. The masses of the orbiting planets will be small compared with the one placed at the center.
In writing Equation (14.26), we assume that M0 ≫ M1, and as mentioned, that the small mass orbits around the biggest one following approximately a circular path. The left side of the equation represents the attractive force between the two masses. The right side of the equation represents the centrifugal force acting on mass M1.
Setting the Initial Conditions for the Planets
In this code, we have assigned to the percentage variable P the value 0.5, as in previous chapters. This value will place the initial z-coordinate of the planets centered between the screen and the plane of projection. The first planet, Planet, is placed at the center of the screen and it is initially static, with zero velocity. The other four, Planet to Planet, have been assigned initial speeds different from zero. Their masses are small compared to Planet.
Calculating the Initial Distances Between Planets
This directive draws a circle with radius equal to M, filled with the corresponding red or cyan color.
Code for Representing Planets with Ellipses
In this code, the function utilizes the coordinates of one of the two points of projection given in (VX, VY, VZ) together with the x-, y-, and z-coordinates available in the Planet array to draw the corresponding circle in Draw1 or Draw2, according to the value of the parameter Which.
The MovePlanets() function , at every clock tick, updates all the parameters of the planets. The function uses a normalized gravitational constant with a value of 1 and a positive parameter dt with a value equal to 0.02. This parameter corresponds to Δt in the analytical equations in Section 14.3.
The value of dt must be as small as possible to attain accurate solutions of the differential equations. However, lowering this value too much will reduce the rate of change in the planet’s position, so that the displacements may not be appreciated.
Calculating Planets with the New Position
Calculating Dynamic Distances Between Planets
Calculating Acceleration Terms ax, ay, and az for Each Planet Using the Gravitational Formula (Positions 13, 14, and 15)
Calculating the New Velocities: vxP=vx+ax*dt, vyP=vy+ay*dt, and vzP=vz+az*dt, (Positions 10, 11, and 12)
Actualizing the Positions and Velocities of the Planets
Function to Process the Movements of the Planets
Code for the main.py File
Code for the file.kv File
In this chapter, we described a simple numerical approach to the gravitational N-body problem. We derived basic equations for calculating the interaction between masses under the influence of Newton’s gravitational law. We presented numerical solutions of the dynamic equations and illustrated their use by employing a working five-planet example. This approach allows us to appreciate the dynamics of the system in stereoscopic view.