The program simulates the gravitational interactions between celestial bodies using numerical methods.
This section initializes the constants, defines the solar system's parameters, and organizes the celestial bodies into a matrix.
days-per-year: Used to convert velocities from per day to per year for ease of computation.
π: Approximation of pi, critical for calculating circular orbital mechanics.
solar-mass: Normalized mass of the Sun, used as a reference unit.
Δt: Defines the simulation's time granularity.
Each celestial body is represented as a vector:
Components 1 to 3: Position (x, y, z).
Components 4 to 6: Velocity (vx, vy, vz).
Component 7: Mass (m).
Planets are initialized with a position position, velocity (scaled by days-per-year), and mass (solar-mass fraction). The planets matrix stores the positions, velocities, and masses of all celestial bodies.
The index pairs ix1 and ix2 are used to represent all unique interactions between celestial bodies without repeating any calculations. Each pair of values from ix1 and ix2 identifies two bodies that interact with each other, ensuring that every combination of bodies is considered exactly once.
For example, if one pair specifies that body 1 interacts with body 2, there is no separate calculation for body 2 interacting with body 1, as the interaction is symmetric. This method simplifies the simulation by avoiding duplicate computations, making it efficient and easy to manage.
This section calculates the momentum and energy of the system, ensuring accurate simulation of physical laws.
Squared differences in positions for all body pairs.
Caculate the system's total energy by summing kinetic and potential energies. Kinetic energy comes from each body's mass and velocity, while potential energy depends on gravitational interactions between body pairs based on their masses and distances. The total energy ensures the simulation conserves energy, reflecting physical accuracy.
Calculates the total momentum of the system to ensure the center of mass remains stationary. It computes momentum as the product of each body's mass and velocity, summing across all bodies. An offset is applied to the Sun's velocity to balance the system, maintaining a stable reference frame.
Updates the positions and velocities of celestial bodies over time based on gravitational forces.
Gravitational interactions between body pairs are calculated using their distances and masses. A scaling factor (magnitude) determines how much the velocities change, based on the inverse square of the distance. Velocities are adjusted for each pair, ensuring forces are applied equally and oppositely, maintaining symmetry.
After updating velocities, positions are recalculated by moving each body according to its velocity and the time step. This step advances the simulation, showing how bodies evolve over time under gravitational influence.