Skip to content

Commit 579adfc

Browse files
authored
doctest & adjusted time_delta
1 parent 7fe7295 commit 579adfc

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

other/n-body_simulation.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ def __init__(
2727
position_y: float,
2828
velocity_x: float,
2929
velocity_y: float,
30-
mass: float = 1,
31-
size: float = 1,
30+
mass: float = 1.0,
31+
size: float = 1.0,
3232
color: str = "blue",
3333
) -> None:
3434
"""
@@ -48,13 +48,23 @@ def update_velocity(
4848
) -> None:
4949
"""
5050
Euler algorithm for velocity
51+
52+
>>> body = Body(0.,0.,0.,0.)
53+
>>> body.update_velocity(1.,0.,1.)
54+
>>> body.velocity_x
55+
1.0
5156
"""
5257
self.velocity_x += force_x * delta_time
5358
self.velocity_y += force_y * delta_time
5459

5560
def update_position(self: Body, delta_time: float) -> None:
5661
"""
5762
Euler algorithm for position
63+
64+
>>> body = Body(0.,0.,1.,0.)
65+
>>> body.update_position(1.)
66+
>>> body.position_x
67+
1.0
5868
"""
5969
self.position_x += self.velocity_x * delta_time
6070
self.position_y += self.velocity_y * delta_time
@@ -72,9 +82,9 @@ class BodySystem:
7282
def __init__(
7383
self: BodySystem,
7484
bodies: list[Body],
75-
gravitation_constant: float = 1,
76-
time_factor: float = 1,
77-
softening_factor: float = 0,
85+
gravitation_constant: float = 1.0,
86+
time_factor: float = 1.0,
87+
softening_factor: float = 0.0,
7888
) -> None:
7989
self.bodies = bodies
8090
self.gravitation_constant = gravitation_constant
@@ -85,6 +95,11 @@ def update_system(self: BodySystem, delta_time: float) -> None:
8595
"""
8696
For each body, loop through all other bodies to calculate the total
8797
force they exert on it. Use that force to update the body's velocity.
98+
99+
>>> body_system = BodySystem([Body(0,0,0,0), Body(10,0,0,0)])
100+
>>> body_system.update_system(1)
101+
>>> body_system.bodies[0].position_x
102+
0.01
88103
"""
89104
for body1 in self.bodies:
90105
force_x = 0.0
@@ -118,13 +133,14 @@ def update_system(self: BodySystem, delta_time: float) -> None:
118133

119134
def plot(
120135
title: str,
121-
bodySystem: BodySystem,
136+
body_system: BodySystem,
122137
x_start: float = -1,
123138
x_end: float = 1,
124139
y_start: float = -1,
125140
y_end: float = 1,
126141
) -> None:
127142
INTERVAL = 20 # Frame rate of the animation
143+
DELTA_TIME = INTERVAL / 1000 # Time between time steps in seconds
128144

129145
fig = plt.figure()
130146
fig.canvas.set_window_title(title)
@@ -134,7 +150,7 @@ def plot(
134150

135151
# Each body is drawn as a patch by the plt-function
136152
patches = []
137-
for body in bodySystem.bodies:
153+
for body in body_system.bodies:
138154
patches.append(
139155
plt.Circle((body.position_x, body.position_y), body.size, fc=body.color)
140156
)
@@ -151,10 +167,10 @@ def init() -> list[patches.Circle]: # type: ignore
151167
# Function called at each step of the animation
152168
def animate(i: int) -> list[patches.Circle]: # type: ignore
153169
# Update the positions of the bodies
154-
bodySystem.update_system(INTERVAL)
170+
body_system.update_system(DELTA_TIME)
155171

156172
# Update the positions of the patches
157-
for patch, body in zip(patches, bodySystem.bodies):
173+
for patch, body in zip(patches, body_system.bodies):
158174
patch.center = (body.position_x, body.position_y)
159175
return patches
160176

@@ -177,7 +193,7 @@ def animate(i: int) -> list[patches.Circle]: # type: ignore
177193
Body(-position_x, -position_y, velocity_x, velocity_y, size=0.2, color="green"),
178194
Body(0, 0, -2 * velocity_x, -2 * velocity_y, size=0.2, color="blue"),
179195
]
180-
body_system1 = BodySystem(bodies1, time_factor=0.003)
196+
body_system1 = BodySystem(bodies1, time_factor=3)
181197
plot("Figure-8 solution to the 3-body-problem", body_system1, -2, 2, -2, 2)
182198

183199
# Example 2: Moon's orbit around the earth
@@ -194,7 +210,7 @@ def animate(i: int) -> list[patches.Circle]: # type: ignore
194210

195211
moon = Body(-earth_moon_distance, 0, 0, moon_velocity, moon_mass, 10000000, "grey")
196212
earth = Body(0, 0, 0, earth_velocity, earth_mass, 50000000, "blue")
197-
body_system2 = BodySystem([earth, moon], gravitation_constant, time_factor=1000)
213+
body_system2 = BodySystem([earth, moon], gravitation_constant, time_factor=1000000)
198214
plot(
199215
"Moon's orbit around the earth",
200216
body_system2,
@@ -230,5 +246,5 @@ def animate(i: int) -> list[patches.Circle]: # type: ignore
230246
size=0.05,
231247
)
232248
)
233-
body_system3 = BodySystem(bodies, 0.01, 0.01, 0.1)
249+
body_system3 = BodySystem(bodies, 0.01, 10, 0.1)
234250
plot("Random system with many bodies", body_system3, -1.5, 1.5, -1.5, 1.5)

0 commit comments

Comments
 (0)