-
Notifications
You must be signed in to change notification settings - Fork 0
/
N_Bodies_Problem.py
213 lines (158 loc) · 7.9 KB
/
N_Bodies_Problem.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# =====================================================================================================================================================================================
# Numerical resolution of the N-bodies problem
# =====================================================================================================================================================================================
#--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Importing Libraries
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.animation as animation
import time
#--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Functions
def import_files(system , time_day , step , t) :
if system == 'Voyager 2' :
name = ['Voyager 2','Sun','Earth','Saturn','Jupiter']
elif system == 'solar system' :
name = ['Sun','Mercury','Venus','Earth','Mars','Jupiter','Saturn','Uranus','Neptun','Moon','Ceres']
elif system == 'halley' :
name = ['Halley comet','Sun']
elif system == 'Voyager 1' :
name = ['Voyager 1','Sun','Earth','Saturn','Jupiter']
n = len(name)
M = list()
T = np.zeros(( 6 * n , len(t)))
with open(system + '_values.txt','r') as file :
L = file.readlines()
for i in range(n):
M.append(float(L[i]))
for i in range(n , 6 * n + n , 1) :
T[i - n , 0] = float(L[i])
file.close()
return T , M , name , n
def Euler_explicite(T , t , pas , M , n , G) :
for i in range(1,len(t) , 1) :
T[: , i] = T[: , i - 1] + pas * F(T[: , i - 1] , M , n , G) # Recurrence formula of the explicit Euler method
return T
def Runge_Kutta_2(T , t , pas , M , n , G) :
for i in range(1 , len(t) , 1) :
k1 = T[: , i - 1] + pas * F(T[: , i - 1] , M , n , G)
T[: , i] = T[: , i - 1] + (pas / 2) * (F(T[: , i - 1] , M , n , G) + F(k1 , M , n , G)) # Recurrence formula of the Runge-Kutta method 2
return T
def Runge_Kutta_4( T , t , pas , M , n , G) :
for i in range(1,len(t) , 1) :
k1 = pas * F(T[: , i - 1] , M , n , G)
k2 = pas * F(T[: , i - 1] + (1 / 2) * k1 , M , n , G)
k3 = pas * F(T[: , i - 1] + (1 / 2) * k2 , M , n , G)
k4 = pas * F(T[: , i - 1] + k3 , M , n , G)
T[:,i] = T[: , i - 1]+ (1 / 6) *(k1 + 2 * k2 + 2 * k3 + k4) # Recurrence formula of the Runge-Kutta method 4
return T
def F(Z , M , n , G) :
K = np.zeros(6 * n) # Creation of an array of 6n rows of zeros that we will come after
# Complete with the Tn + 1 values
for i in range(0 , n , 1) :
a = 0
b = 0
c = 0
for j in range(0 , n , 1) :
if i != j :
a += (M[j] * (Z[6 * i + 3] - Z[6 * j + 3])) / ((Z[6 * i + 3] - Z[6 * j + 3]) ** 2 + (Z[6 * i + 4] - Z[6 * j + 4]) ** 2 + (Z[6 * i + 5] - Z[6 * j + 5]) ** 2) ** (3 / 2)
b += (M[j] * (Z[6 * i + 4] - Z[6 * j + 4])) / ((Z[6 * i + 3] - Z[6 * j + 3]) ** 2 + (Z[6 * i + 4] - Z[6 * j + 4]) ** 2 + (Z[6 * i + 5] - Z[6 * j + 5]) ** 2) ** (3 / 2)
c += (M[j] * (Z[6 * i + 5] - Z[6 * j + 5])) / ((Z[6 * i + 3] - Z[6 * j + 3]) ** 2 + (Z[6 * i + 4] - Z[6 * j + 4]) ** 2 + (Z[6 * i + 5] - Z[6 * j + 5]) ** 2) ** (3 / 2)
a = - G * a
b = - G * b
c = - G * c
K[6 * i] = a
K[6 * i + 3] = Z[6 * i]
K[6 * i + 1] = b
K[6 * i + 4] = Z[6 * i + 1]
K[6 * i + 2] = c
K[6 * i + 5] = Z[ 6 * i + 2]
return K
def show_2D(n , T , name , methode) :
plt.figure("Graphic Representation")
for i in range(0 , n , 1) :
plt.plot(T[6 * i + 3 , :] , T[6 * i + 4 , :] , label = 'trajectory of {}'.format(name[i]))
plt.plot(T[6 * i + 3 , 0] , T[6 * i + 4 , 0] , 'ko') # we trace the initial positions of each body by a black point
plt.title('Numerical representation for the problem with {} bodies with the method of {}'.format(n , methode))
plt.xlabel('X (in astronomical units)')
plt.ylabel('Y (in astronomical units)')
plt.grid(linewidth = 0.5)
plt.axis('equal')
plt.legend()
plt.show()
def animation_2D(n , T , name , methode , time_annimation , step):
show_2D(n , T , name , methode)
nbr_var = []
begin = time.time()
end = time.time()
for i in range(0 , n , 1):
nbr_var.append(name[i])
nbr_var[i], = plt.plot([] , [] , 'ko' , linewidth = 5)
plt.axis('equal')
for z in range(0 , len(t) , int(len(t) / 100)) :
if time_annimation >= (end - begin) :
for i in range(0 , n , 1) :
nbr_var[i].set_data(T[6 * i + 3 , z] , T[6 * i + 4 , z])
plt.axis('equal')
plt.draw()
plt.pause(step)
end = time.time()
def show_3D(n, T, name, method):
# Create a 3D plot
fig = plt.figure("Graphic Representation")
ax = fig.add_subplot(111, projection='3d')
for i in range(n):
ax.plot(T[6 * i + 3, :], T[6 * i + 4, :], T[6 * i + 5, :], label=f'Trajectory of {name[i]}')
ax.scatter(T[6 * i + 3, 0], T[6 * i + 4, 0], T[6 * i + 5, 0], label=f'Start of {name[i]}')
ax.set_xlabel('X (in astronomical units)')
ax.set_ylabel('Y (in astronomical units)')
ax.set_zlabel('Z (in astronomical units)')
plt.title(f'Numerical representation for the problem with {n} bodies using {method}')
plt.legend()
plt.show()
def animation_3D(n , T , name , methode , time_annimation , step) :
show_3D(n , T , name , methode)
nbr_var = []
begin = time.time()
end = time.time()
for i in range(0 , n , 1) :
nbr_var.append(name[i])
nbr_var[i], = plt.plot([] , [] , [] , 'ko' , linewidth = 5)
for z in range(0 , len(t) , int(len(t) / 100)) :
if time_annimation >= (end - begin) :
for i in range(0 , n , 1) :
nbr_var[i].set_data(T[6 * i + 3 , z] , T[6 * i + 4 , z])
nbr_var[i].set_3d_properties(T[6 * i + 5 , z])
plt.draw()
plt.pause(step)
end=time.time()
#--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Constants
G = 1.4872 * 10 ** -34
methode = "Runge_Kutta_4"
S = ['Voyager 2' , 'solar system' , 'halley' , 'Voyager 1']
systeme = S[1]
time_day = 10000 # one year
step = 0.1
time_annimation = 20 # seconds
#--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Main program
# Time scale
t = np.arange(0 , time_day , step)
# Importing data
T , M , name , n = import_files(systeme , time_day , step , t)
# Explicit Euler Method
#Z = Euler_explicite(T , t , step , M , n , G)
# Runge Kutta 2 Method
#Z = Runge_Kutta_2(T, t, step , M , n , G)
# Runge Kutta 4 Method
Z = Runge_Kutta_4(T, t, step, M, n , G)
# Display of the result in 2D
show_2D(n , Z , name , methode)
# Display of the result in 2D with animations
#animation_2D(n , T , name , methode , time_annimation , step)
# Display of the result in 3D
#show_3D(n , Z , name , methode)
# Display of the result in 3D with animations
#animation_3D(n , T , name , methode , time_annimation , step)