@@ -43,6 +43,14 @@ def __init__(
43
43
self .size = size
44
44
self .color = color
45
45
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
+
46
54
def update_velocity (
47
55
self , force_x : float , force_y : float , delta_time : float
48
56
) - > None :
@@ -51,22 +59,22 @@ def update_velocity(
51
59
52
60
>>> body_1 = Body(0.,0.,0.,0.)
53
61
>>> body_1.update_velocity(1.,0.,1.)
54
- >>> ( body_1.velocity_x, body_1.velocity_y)
62
+ >>> body_1.velocity
55
63
(1.0, 0.0)
56
64
57
65
>>> body_1.update_velocity(1.,0.,1.)
58
- >>> (body_1.velocity_x, body_1.velocity_y)
66
+ >>> body_1.velocity
67
+
59
68
(2.0, 0.0)
60
69
61
70
>>> body_2 = Body(0.,0.,5.,0.)
62
71
>>> body_2.update_velocity(0.,-10.,10.)
63
- >>> ( body_2.velocity_x, body_2.velocity_y)
72
+ >>> body_2.velocity
64
73
(5.0, -100.0)
65
74
66
75
>>> body_2.update_velocity(0.,-10.,10.)
67
- >>> ( body_2.velocity_x, body_2.velocity_y)
76
+ >>> body_2.velocity
68
77
(5.0, -200.0)
69
-
70
78
"""
71
79
self .velocity_x += force_x * delta_time
72
80
self .velocity_y += force_y * delta_time
@@ -77,20 +85,20 @@ def update_position(self, delta_time: float) -> None:
77
85
78
86
>>> body_1 = Body(0.,0.,1.,0.)
79
87
>>> body_1.update_position(1.)
80
- >>> ( body_1.position_x, body_1.position_y)
88
+ >>> body_1.position
81
89
(1.0, 0.0)
82
90
83
91
>>> body_1.update_position(1.)
84
- >>> ( body_1.position_x, body_1.position_y)
92
+ >>> body_1.position
85
93
(2.0, 0.0)
86
94
87
95
>>> body_2 = Body(10.,10.,0.,-2.)
88
96
>>> body_2.update_position(1.)
89
- >>> ( body_2.position_x, body_2.position_y)
97
+ >>> body_2.position
90
98
(10.0, 8.0)
91
99
92
100
>>> body_2.update_position(1.)
93
- >>> ( body_2.position_x, body_2.position_y)
101
+ >>> body_2.position
94
102
(10.0, 6.0)
95
103
"""
96
104
self .position_x += self .velocity_x * delta_time
@@ -118,23 +126,28 @@ def __init__(
118
126
self .time_factor = time_factor
119
127
self .softening_factor = softening_factor
120
128
129
+ def __len__ () -> int :
130
+ return len (self .bodies )
131
+
121
132
def update_system (self , delta_time : float ) -> None :
122
133
"""
123
134
For each body, loop through all other bodies to calculate the total
124
135
force they exert on it. Use that force to update the body's velocity.
125
136
126
137
>>> body_system_1 = BodySystem([Body(0,0,0,0), Body(10,0,0,0)])
138
+ >>> len(body_system_1)
139
+ 2
127
140
>>> 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
129
142
(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
131
144
(0.01, 0.0)
132
145
133
146
>>> body_system_2 = BodySystem([Body(-10,0,0,0), Body(10,0,0,0, mass=4)], 1, 10)
134
147
>>> 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
136
149
(-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
138
151
(0.1, 0.0)
139
152
"""
140
153
for body1 in self .bodies :
@@ -239,13 +252,15 @@ def update(frame: int) -> list[plt.Circle]:
239
252
plt .show ()
240
253
241
254
242
- def example_1 () -> None :
255
+ def example_1 () -> BodySystem :
243
256
"""
244
257
Example 1: figure-8 solution to the 3-body-problem
245
258
This example can be seen as a test of the implementation: given the right
246
259
initial conditions, the bodies should move in a figure-8.
247
260
(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
249
264
"""
250
265
251
266
position_x = 0.9700436
@@ -258,11 +273,10 @@ def example_1() -> None:
258
273
Body (- position_x , - position_y , velocity_x , velocity_y , size = 0.2 , color = "green" ),
259
274
Body (0 , 0 , - 2 * velocity_x , - 2 * velocity_y , size = 0.2 , color = "blue" ),
260
275
]
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 )
263
277
264
278
265
- def example_2 () -> None :
279
+ def example_2 () -> BodySystem :
266
280
"""
267
281
Example 2: Moon's orbit around the earth
268
282
This example can be seen as a test of the implementation: given the right
@@ -285,18 +299,10 @@ def example_2() -> None:
285
299
286
300
moon = Body (- earth_moon_distance , 0 , 0 , moon_velocity , moon_mass , 10000000 , "grey" )
287
301
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 )
297
303
298
304
299
- def example_3 () -> None :
305
+ def example_3 () -> BodySystem :
300
306
"""
301
307
Example 3: Random system with many bodies.
302
308
No doctest provided since this function does not have a return value.
@@ -327,11 +333,17 @@ def example_3() -> None:
327
333
size = 0.05 ,
328
334
)
329
335
)
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 )
332
337
333
338
334
339
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