@@ -27,8 +27,8 @@ def __init__(
27
27
position_y : float ,
28
28
velocity_x : float ,
29
29
velocity_y : float ,
30
- mass : float = 1 ,
31
- size : float = 1 ,
30
+ mass : float = 1.0 ,
31
+ size : float = 1.0 ,
32
32
color : str = "blue" ,
33
33
) -> None :
34
34
"""
@@ -48,13 +48,23 @@ def update_velocity(
48
48
) -> None :
49
49
"""
50
50
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
51
56
"""
52
57
self .velocity_x += force_x * delta_time
53
58
self .velocity_y += force_y * delta_time
54
59
55
60
def update_position (self : Body , delta_time : float ) -> None :
56
61
"""
57
62
Euler algorithm for position
63
+
64
+ >>> body = Body(0.,0.,1.,0.)
65
+ >>> body.update_position(1.)
66
+ >>> body.position_x
67
+ 1.0
58
68
"""
59
69
self .position_x += self .velocity_x * delta_time
60
70
self .position_y += self .velocity_y * delta_time
@@ -72,9 +82,9 @@ class BodySystem:
72
82
def __init__ (
73
83
self : BodySystem ,
74
84
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 ,
78
88
) -> None :
79
89
self .bodies = bodies
80
90
self .gravitation_constant = gravitation_constant
@@ -85,6 +95,11 @@ def update_system(self: BodySystem, delta_time: float) -> None:
85
95
"""
86
96
For each body, loop through all other bodies to calculate the total
87
97
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
88
103
"""
89
104
for body1 in self .bodies :
90
105
force_x = 0.0
@@ -118,13 +133,14 @@ def update_system(self: BodySystem, delta_time: float) -> None:
118
133
119
134
def plot (
120
135
title : str ,
121
- bodySystem : BodySystem ,
136
+ body_system : BodySystem ,
122
137
x_start : float = - 1 ,
123
138
x_end : float = 1 ,
124
139
y_start : float = - 1 ,
125
140
y_end : float = 1 ,
126
141
) -> None :
127
142
INTERVAL = 20 # Frame rate of the animation
143
+ DELTA_TIME = INTERVAL / 1000 # Time between time steps in seconds
128
144
129
145
fig = plt .figure ()
130
146
fig .canvas .set_window_title (title )
@@ -134,7 +150,7 @@ def plot(
134
150
135
151
# Each body is drawn as a patch by the plt-function
136
152
patches = []
137
- for body in bodySystem .bodies :
153
+ for body in body_system .bodies :
138
154
patches .append (
139
155
plt .Circle ((body .position_x , body .position_y ), body .size , fc = body .color )
140
156
)
@@ -151,10 +167,10 @@ def init() -> list[patches.Circle]: # type: ignore
151
167
# Function called at each step of the animation
152
168
def animate (i : int ) -> list [patches .Circle ]: # type: ignore
153
169
# Update the positions of the bodies
154
- bodySystem .update_system (INTERVAL )
170
+ body_system .update_system (DELTA_TIME )
155
171
156
172
# 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 ):
158
174
patch .center = (body .position_x , body .position_y )
159
175
return patches
160
176
@@ -177,7 +193,7 @@ def animate(i: int) -> list[patches.Circle]: # type: ignore
177
193
Body (- position_x , - position_y , velocity_x , velocity_y , size = 0.2 , color = "green" ),
178
194
Body (0 , 0 , - 2 * velocity_x , - 2 * velocity_y , size = 0.2 , color = "blue" ),
179
195
]
180
- body_system1 = BodySystem (bodies1 , time_factor = 0.003 )
196
+ body_system1 = BodySystem (bodies1 , time_factor = 3 )
181
197
plot ("Figure-8 solution to the 3-body-problem" , body_system1 , - 2 , 2 , - 2 , 2 )
182
198
183
199
# Example 2: Moon's orbit around the earth
@@ -194,7 +210,7 @@ def animate(i: int) -> list[patches.Circle]: # type: ignore
194
210
195
211
moon = Body (- earth_moon_distance , 0 , 0 , moon_velocity , moon_mass , 10000000 , "grey" )
196
212
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 )
198
214
plot (
199
215
"Moon's orbit around the earth" ,
200
216
body_system2 ,
@@ -230,5 +246,5 @@ def animate(i: int) -> list[patches.Circle]: # type: ignore
230
246
size = 0.05 ,
231
247
)
232
248
)
233
- body_system3 = BodySystem (bodies , 0.01 , 0.01 , 0.1 )
249
+ body_system3 = BodySystem (bodies , 0.01 , 10 , 0.1 )
234
250
plot ("Random system with many bodies" , body_system3 , - 1.5 , 1.5 , - 1.5 , 1.5 )
0 commit comments