Skip to content

Commit 790277e

Browse files
authored
Apply suggestions from code review
1 parent 7a0081d commit 790277e

File tree

1 file changed

+45
-33
lines changed

1 file changed

+45
-33
lines changed

physics/n_body_simulation.py

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ def __init__(
4343
self.size = size
4444
self.color = color
4545

46+
@property
47+
def position(self) tuple[float, float]:
48+
return self.position_x, self.position_y
49+
50+
@property
51+
def velocity(self) tuple[float, float]:
52+
return self.position_x, self.position_y
53+
4654
def update_velocity(
4755
self, force_x: float, force_y: float, delta_time: float
4856
) -> None:
@@ -51,22 +59,22 @@ def update_velocity(
5159
5260
>>> body_1 = Body(0.,0.,0.,0.)
5361
>>> body_1.update_velocity(1.,0.,1.)
54-
>>> (body_1.velocity_x, body_1.velocity_y)
62+
>>> body_1.velocity
5563
(1.0, 0.0)
5664
5765
>>> body_1.update_velocity(1.,0.,1.)
58-
>>> (body_1.velocity_x, body_1.velocity_y)
66+
>>> body_1.velocity
67+
5968
(2.0, 0.0)
6069
6170
>>> body_2 = Body(0.,0.,5.,0.)
6271
>>> body_2.update_velocity(0.,-10.,10.)
63-
>>> (body_2.velocity_x, body_2.velocity_y)
72+
>>> body_2.velocity
6473
(5.0, -100.0)
6574
6675
>>> body_2.update_velocity(0.,-10.,10.)
67-
>>> (body_2.velocity_x, body_2.velocity_y)
76+
>>> body_2.velocity
6877
(5.0, -200.0)
69-
7078
"""
7179
self.velocity_x += force_x * delta_time
7280
self.velocity_y += force_y * delta_time
@@ -77,20 +85,20 @@ def update_position(self, delta_time: float) -> None:
7785
7886
>>> body_1 = Body(0.,0.,1.,0.)
7987
>>> body_1.update_position(1.)
80-
>>> (body_1.position_x, body_1.position_y)
88+
>>> body_1.position
8189
(1.0, 0.0)
8290
8391
>>> body_1.update_position(1.)
84-
>>> (body_1.position_x, body_1.position_y)
92+
>>> body_1.position
8593
(2.0, 0.0)
8694
8795
>>> body_2 = Body(10.,10.,0.,-2.)
8896
>>> body_2.update_position(1.)
89-
>>> (body_2.position_x, body_2.position_y)
97+
>>> body_2.position
9098
(10.0, 8.0)
9199
92100
>>> body_2.update_position(1.)
93-
>>> (body_2.position_x, body_2.position_y)
101+
>>> body_2.position
94102
(10.0, 6.0)
95103
"""
96104
self.position_x += self.velocity_x * delta_time
@@ -118,23 +126,28 @@ def __init__(
118126
self.time_factor = time_factor
119127
self.softening_factor = softening_factor
120128

129+
def __len__() -> int:
130+
return len(self.bodies)
131+
121132
def update_system(self, delta_time: float) -> None:
122133
"""
123134
For each body, loop through all other bodies to calculate the total
124135
force they exert on it. Use that force to update the body's velocity.
125136
126137
>>> body_system_1 = BodySystem([Body(0,0,0,0), Body(10,0,0,0)])
138+
>>> len(body_system_1)
139+
2
127140
>>> body_system_1.update_system(1)
128-
>>> (body_system_1.bodies[0].position_x, body_system_1.bodies[0].position_y)
141+
>>> body_system_1.bodies[0].position
129142
(0.01, 0.0)
130-
>>> (body_system_1.bodies[0].velocity_x, body_system_1.bodies[0].velocity_y)
143+
>>> body_system_1.bodies[0].velocity
131144
(0.01, 0.0)
132145
133146
>>> body_system_2 = BodySystem([Body(-10,0,0,0), Body(10,0,0,0, mass=4)], 1, 10)
134147
>>> body_system_2.update_system(1)
135-
>>> (body_system_2.bodies[0].position_x, body_system_2.bodies[0].position_y)
148+
>>> body_system_2.bodies[0].position
136149
(-9.0, 0.0)
137-
>>> (body_system_2.bodies[0].velocity_x, body_system_2.bodies[0].velocity_y)
150+
>>> body_system_2.bodies[0]velocity
138151
(0.1, 0.0)
139152
"""
140153
for body1 in self.bodies:
@@ -239,13 +252,15 @@ def update(frame: int) -> list[plt.Circle]:
239252
plt.show()
240253

241254

242-
def example_1() -> None:
255+
def example_1() -> BodySystem:
243256
"""
244257
Example 1: figure-8 solution to the 3-body-problem
245258
This example can be seen as a test of the implementation: given the right
246259
initial conditions, the bodies should move in a figure-8.
247260
(initial conditions taken from http://www.artcompsci.org/vol_1/v1_web/node56.html)
248-
No doctest provided since this function does not have a return value.
261+
>>> body_system = example_1()
262+
>>> len(body_system)
263+
3
249264
"""
250265

251266
position_x = 0.9700436
@@ -258,11 +273,10 @@ def example_1() -> None:
258273
Body(-position_x, -position_y, velocity_x, velocity_y, size=0.2, color="green"),
259274
Body(0, 0, -2 * velocity_x, -2 * velocity_y, size=0.2, color="blue"),
260275
]
261-
body_system1 = BodySystem(bodies1, time_factor=3)
262-
plot("Figure-8 solution to the 3-body-problem", body_system1, -2, 2, -2, 2)
276+
return BodySystem(bodies1, time_factor=3)
263277

264278

265-
def example_2() -> None:
279+
def example_2() -> BodySystem:
266280
"""
267281
Example 2: Moon's orbit around the earth
268282
This example can be seen as a test of the implementation: given the right
@@ -285,18 +299,10 @@ def example_2() -> None:
285299

286300
moon = Body(-earth_moon_distance, 0, 0, moon_velocity, moon_mass, 10000000, "grey")
287301
earth = Body(0, 0, 0, earth_velocity, earth_mass, 50000000, "blue")
288-
body_system2 = BodySystem([earth, moon], gravitation_constant, time_factor=1000000)
289-
plot(
290-
"Moon's orbit around the earth",
291-
body_system2,
292-
-430000000,
293-
430000000,
294-
-430000000,
295-
430000000,
296-
)
302+
return BodySystem([earth, moon], gravitation_constant, time_factor=1000000)
297303

298304

299-
def example_3() -> None:
305+
def example_3() -> BodySystem:
300306
"""
301307
Example 3: Random system with many bodies.
302308
No doctest provided since this function does not have a return value.
@@ -327,11 +333,17 @@ def example_3() -> None:
327333
size=0.05,
328334
)
329335
)
330-
body_system3 = BodySystem(bodies, 0.01, 10, 0.1)
331-
plot("Random system with many bodies", body_system3, -1.5, 1.5, -1.5, 1.5)
336+
return BodySystem(bodies, 0.01, 10, 0.1)
332337

333338

334339
if __name__ == "__main__":
335-
example_1()
336-
example_2()
337-
example_3()
340+
plot("Figure-8 solution to the 3-body-problem", example_1(), -2, 2, -2, 2)
341+
plot(
342+
"Moon's orbit around the earth",
343+
example_2(),
344+
-430000000,
345+
430000000,
346+
-430000000,
347+
430000000,
348+
)
349+
plot("Random system with many bodies", example_3(), -1.5, 1.5, -1.5, 1.5)

0 commit comments

Comments
 (0)