forked from Arpitt-18/3-Body-Problem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyT.py
More file actions
141 lines (96 loc) · 4.25 KB
/
yT.py
File metadata and controls
141 lines (96 loc) · 4.25 KB
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
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import solve_ivp
import time
# ------------------------------------------------------------------- #
m1 = 1.0
m2 = 1.0
m3 = 1.0
# Position
inital_position_1 = [-0.5, 0.0, 0.0]
inital_position_2 = [0.5, 0.0, 0.0]
inital_position_3 = [0.0, 0.001, 1.0]
# Velocity
inital_velocity_1 = [0.0, 0.347111, 0]
inital_velocity_2 = [0.0, -0.347111, 0.0]
inital_velocity_3 = [0.0, 0.0, -0.1]
initial_conditions = np.array([
inital_position_1, inital_position_2, inital_position_3,
inital_velocity_1, inital_velocity_2, inital_velocity_3
]).ravel()
# ------------------------------------------------------------------- #
def system_odes(t, S, m1, m2, m3):
p1, p2, p3 = S[0:3], S[3:6], S[6:9]
dp1_dt, dp2_dt, dp3_dt = S[9:12], S[12:15], S[15:18]
f1, f2, f3 = dp1_dt, dp2_dt, dp3_dt
df1_dt = m3*(p3 - p1)/np.linalg.norm(p3 - p1)**3 + m2*(p2 - p1)/np.linalg.norm(p2 - p1)**3
df2_dt = m3*(p3 - p2)/np.linalg.norm(p3 - p2)**3 + m1*(p1 - p2)/np.linalg.norm(p1 - p2)**3
df3_dt = m1*(p1 - p3)/np.linalg.norm(p1 - p3)**3 + m2*(p2 - p3)/np.linalg.norm(p2 - p3)**3
return np.array([f1, f2, f3, df1_dt, df2_dt, df3_dt]).ravel()
# ------------------------------------------------------------------- #
time_s, time_e = 0, 7
t_points = np.linspace(time_s, time_e, 2001)
t1 = time.time()
solution = solve_ivp(
fun=system_odes,
t_span=(time_s, time_e),
y0=initial_conditions,
t_eval=t_points,
args=(m1, m2, m3)
)
t2 = time.time()
print(f"Solved in: {t2-t1:.3f} [s]")
t_sol = solution.t
p1x_sol = solution.y[0]
p1y_sol = solution.y[1]
p1z_sol = solution.y[2]
p2x_sol = solution.y[3]
p2y_sol = solution.y[4]
p2z_sol = solution.y[5]
p3x_sol = solution.y[6]
p3y_sol = solution.y[7]
p3z_sol = solution.y[8]
# ------------------------------------------------------------------- #
fig, ax = plt.subplots(subplot_kw={"projection":"3d"})
planet1_plt, = ax.plot(p1x_sol, p1y_sol, p1z_sol, 'green', label='Planet 1', linewidth=1)
planet2_plt, = ax.plot(p2x_sol, p2y_sol, p2z_sol, 'red', label='Planet 2', linewidth=1)
planet3_plt, = ax.plot(p3x_sol, p3y_sol, p3z_sol, 'blue',label='Planet 3', linewidth=1)
planet1_dot, = ax.plot([p1x_sol[-1]], [p1y_sol[-1]], [p1z_sol[-1]], 'o', color='green', markersize=6)
planet2_dot, = ax.plot([p2x_sol[-1]], [p2y_sol[-1]], [p2z_sol[-1]], 'o', color='red', markersize=6)
planet3_dot, = ax.plot([p3x_sol[-1]], [p3y_sol[-1]], [p3z_sol[-1]], 'o', color='blue', markersize=6)
ax.set_title("The 3-Body Problem")
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")
plt.grid()
plt.legend()
# ------------------------------------------------------------------- #
from matplotlib.animation import FuncAnimation
# ------- Animating the solutions ------- #
def update(frame):
lower_lim = max(0, frame - 300)
print(f"Progress: {(frame+1)/len(t_points):.1%} | 100.0 %", end='\r')
x_current_1 = p1x_sol[lower_lim:frame+1]
y_current_1 = p1y_sol[lower_lim:frame+1]
z_current_1 = p1z_sol[lower_lim:frame+1]
x_current_2 = p2x_sol[lower_lim:frame+1]
y_current_2 = p2y_sol[lower_lim:frame+1]
z_current_2 = p2z_sol[lower_lim:frame+1]
x_current_3 = p3x_sol[lower_lim:frame+1]
y_current_3 = p3y_sol[lower_lim:frame+1]
z_current_3 = p3z_sol[lower_lim:frame+1]
planet1_plt.set_data(x_current_1, y_current_1)
planet1_plt.set_3d_properties(z_current_1)
planet1_dot.set_data([x_current_1[-1]], [y_current_1[-1]])
planet1_dot.set_3d_properties([z_current_1[-1]])
planet2_plt.set_data(x_current_2, y_current_2)
planet2_plt.set_3d_properties(z_current_2)
planet2_dot.set_data([x_current_2[-1]], [y_current_2[-1]])
planet2_dot.set_3d_properties([z_current_2[-1]])
planet3_plt.set_data(x_current_3, y_current_3)
planet3_plt.set_3d_properties(z_current_3)
planet3_dot.set_data([x_current_3[-1]], [y_current_3[-1]])
planet3_dot.set_3d_properties([z_current_3[-1]])
return planet1_plt, planet1_dot, planet2_plt, planet2_dot, planet3_plt, planet3_dot
animation = FuncAnimation(fig, update, frames=range(0, len(t_points), 2), interval=10, blit=True)
plt.show()