physics.n_body_simulation¶
In physics and astronomy, a gravitational N-body simulation is a simulation of a dynamical system of particles under the influence of gravity. The system consists of a number of bodies, each of which exerts a gravitational force on all other bodies. These forces are calculated using Newton’s law of universal gravitation. The Euler method is used at each time-step to calculate the change in velocity and position brought about by these forces. Softening is used to prevent numerical divergences when a particle comes too close to another (and the force goes to infinity). (Description adapted from https://en.wikipedia.org/wiki/N-body_simulation ) (See also http://www.shodor.org/refdesk/Resources/Algorithms/EulersMethod/ )
Attributes¶
Classes¶
This class is used to hold the bodies, the gravitation constant, the time |
Functions¶
|
Example 1: figure-8 solution to the 3-body-problem |
|
Example 2: Moon's orbit around the earth |
|
Example 3: Random system with many bodies. |
|
Utility function to plot how the given body-system evolves over time. |
|
Updates the body-system and applies the change to the patch-list used for plotting |
Module Contents¶
- class physics.n_body_simulation.Body(position_x: float, position_y: float, velocity_x: float, velocity_y: float, mass: float = 1.0, size: float = 1.0, color: str = 'blue')¶
- update_position(delta_time: float) None ¶
Euler algorithm for position
>>> body_1 = Body(0.,0.,1.,0.) >>> body_1.update_position(1.) >>> body_1.position (1.0, 0.0)
>>> body_1.update_position(1.) >>> body_1.position (2.0, 0.0)
>>> body_2 = Body(10.,10.,0.,-2.) >>> body_2.update_position(1.) >>> body_2.position (10.0, 8.0)
>>> body_2.update_position(1.) >>> body_2.position (10.0, 6.0)
- update_velocity(force_x: float, force_y: float, delta_time: float) None ¶
Euler algorithm for velocity
>>> body_1 = Body(0.,0.,0.,0.) >>> body_1.update_velocity(1.,0.,1.) >>> body_1.velocity (1.0, 0.0)
>>> body_1.update_velocity(1.,0.,1.) >>> body_1.velocity (2.0, 0.0)
>>> body_2 = Body(0.,0.,5.,0.) >>> body_2.update_velocity(0.,-10.,10.) >>> body_2.velocity (5.0, -100.0)
>>> body_2.update_velocity(0.,-10.,10.) >>> body_2.velocity (5.0, -200.0)
- color = 'blue'¶
- mass = 1.0¶
- property position: tuple[float, float]¶
- position_x¶
- position_y¶
- size = 1.0¶
- property velocity: tuple[float, float]¶
- velocity_x¶
- velocity_y¶
- class physics.n_body_simulation.BodySystem(bodies: list[Body], gravitation_constant: float = 1.0, time_factor: float = 1.0, softening_factor: float = 0.0)¶
This class is used to hold the bodies, the gravitation constant, the time factor and the softening factor. The time factor is used to control the speed of the simulation. The softening factor is used for softening, a numerical trick for N-body simulations to prevent numerical divergences when two bodies get too close to each other.
- __len__() int ¶
- update_system(delta_time: float) None ¶
For each body, loop through all other bodies to calculate the total force they exert on it. Use that force to update the body’s velocity.
>>> body_system_1 = BodySystem([Body(0,0,0,0), Body(10,0,0,0)]) >>> len(body_system_1) 2 >>> body_system_1.update_system(1) >>> body_system_1.bodies[0].position (0.01, 0.0) >>> body_system_1.bodies[0].velocity (0.01, 0.0)
>>> body_system_2 = BodySystem([Body(-10,0,0,0), Body(10,0,0,0, mass=4)], 1, 10) >>> body_system_2.update_system(1) >>> body_system_2.bodies[0].position (-9.0, 0.0) >>> body_system_2.bodies[0].velocity (0.1, 0.0)
- bodies¶
- gravitation_constant = 1.0¶
- softening_factor = 0.0¶
- time_factor = 1.0¶
- physics.n_body_simulation.example_1() BodySystem ¶
Example 1: figure-8 solution to the 3-body-problem This example can be seen as a test of the implementation: given the right initial conditions, the bodies should move in a figure-8. (initial conditions taken from http://www.artcompsci.org/vol_1/v1_web/node56.html) >>> body_system = example_1() >>> len(body_system) 3
- physics.n_body_simulation.example_2() BodySystem ¶
Example 2: Moon’s orbit around the earth This example can be seen as a test of the implementation: given the right initial conditions, the moon should orbit around the earth as it actually does. (mass, velocity and distance taken from https://en.wikipedia.org/wiki/Earth and https://en.wikipedia.org/wiki/Moon) No doctest provided since this function does not have a return value.
- physics.n_body_simulation.example_3() BodySystem ¶
Example 3: Random system with many bodies. No doctest provided since this function does not have a return value.
- physics.n_body_simulation.plot(title: str, body_system: BodySystem, x_start: float = -1, x_end: float = 1, y_start: float = -1, y_end: float = 1) None ¶
Utility function to plot how the given body-system evolves over time. No doctest provided since this function does not have a return value.
- physics.n_body_simulation.update_step(body_system: BodySystem, delta_time: float, patches: list[matplotlib.pyplot.Circle]) None ¶
Updates the body-system and applies the change to the patch-list used for plotting
>>> body_system_1 = BodySystem([Body(0,0,0,0), Body(10,0,0,0)]) >>> patches_1 = [plt.Circle((body.position_x, body.position_y), body.size, ... fc=body.color)for body in body_system_1.bodies] >>> update_step(body_system_1, 1, patches_1) >>> patches_1[0].center (0.01, 0.0)
>>> body_system_2 = BodySystem([Body(-10,0,0,0), Body(10,0,0,0, mass=4)], 1, 10) >>> patches_2 = [plt.Circle((body.position_x, body.position_y), body.size, ... fc=body.color)for body in body_system_2.bodies] >>> update_step(body_system_2, 1, patches_2) >>> patches_2[0].center (-9.0, 0.0)
- physics.n_body_simulation.DELTA_TIME = 0.02¶
- physics.n_body_simulation.INTERVAL = 20¶