-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathN-body simulation.py
280 lines (183 loc) · 8.48 KB
/
N-body simulation.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#==========================================================================================================================================
# Script written in Python to integrate the equations of motion of N particles interacting with each other gravitationally with high precision.
# The script computes the equations of motion and use scipy.integrate to integrate them.
# Then it uses matplotlib to visualize the solution
#==========================================================================================================================================
import numpy as np
import sympy as sp
# Define a Vector2D class
class Vec2:
def __init__(self, x, y):
self.x = x
self.y = y
# Used for debugging. This method is called when you print an instance
def __str__(self):
return f"({self.x}, {self.y})"
def __add__(self, v):
return Vec2(self.x + v.x, self.y + v.y)
def __radd__(self, v):
return Vec2(self.x + v.x, self.y + v.y)
def __sub__(self, v):
return Vec2(self.x - v.x, self.y - v.y)
def __rsub__(self, v):
return Vec2(v.x- self.x , v.y - self.y)
def __mul__(self, n):
return Vec2(self.x * n, self.y * n)
def __rmul__(self, n):
return Vec2(self.x * n, self.y * n)
def dot(self, v):
return self.x*v.x + self.y*v.y
def get_length(self):
return np.sqrt(self.dot(self) )
# Define a Particle class. The particles are the bodies attracting each other
class Particle():
# n = number of particles
n = 0
def __init__(self,initial_pos,initial_vel, mass):
# i = particle index
self.i = Particle.n
Particle.n += 1
self.m = mass
self.G = 1 # change this to 6.67408 × 1e-11 if you want real world measuring units.
# pos, vel, acc = symbolic variables
self.pos = Vec2(sp.symbols("x_"+str(self.i)),sp.symbols("y_"+str(self.i)))
self.vel = Vec2(sp.symbols("vx_"+str(self.i)),sp.symbols("vy_"+str(self.i)))
self.acc = Vec2(0,0)
# lamb_vel, lamd_acc = lambdify functions.
self.lamb_vel = Vec2(None,None)
self.lamd_acc = Vec2(None,None)
# initial_pos, initial_vel = intial position and velocity
self.initial_pos = initial_pos
self.initial_vel = initial_vel
# vf_vel, vf_acc = functions used in vectorfield() function
self.vf_vel = Vec2(0,0)
self.vf_acc = Vec2(0,0)
# sol_pos, sol_vel = position and velocity solution list obtained after the integration of the equations of motion
self.sol_pos = Vec2(None,None)
self.sol_vel = Vec2(None,None)
# compute particle acceleration using Newton's law of universal gravitation
def calculate_acc(self,particles):
for j in range(len(particles)):
if self.i !=j:
self.acc += (particles[j].pos - self.pos)*particles[j].m*self.G*(1/(((self.pos.x-particles[j].pos.x)**2 + (self.pos.y-particles[j].pos.y)**2)**(3/2)))
# lambdified symbolic functions are faster for numerical calculations.
# I used this approach (compute first symbolic equations of motion and then compile the function with lambdify)
# to avoid python loops in the vectorfield function which needs to be run thousands of times and that is slow.
def lambdify_vel(self,particles):
self.lamb_vel.x = sp.lambdify(self.vel.x, self.vel.x)
self.lamb_vel.y = sp.lambdify(self.vel.y, self.vel.y)
def lambdify_acc(self,particles):
var = []
for j in range(len(particles)):
var.append(particles[j].pos.x)
var.append(particles[j].pos.y)
self.lamd_acc.x = sp.lambdify([var], self.acc.x)
self.lamd_acc.y = sp.lambdify([var], self.acc.y)
#Input here the initial conditions of the particles and their masses
################################################################################################################################
#particle list
par = []
#create the particles
par.append(Particle(initial_pos = Vec2(2,5), initial_vel = Vec2(0.5,0.5) , mass = 1.))
par.append(Particle(initial_pos = Vec2(5,2), initial_vel = Vec2(0.5,0.2) , mass = 1.))
par.append(Particle(initial_pos = Vec2(3,3), initial_vel = Vec2(0.1,0.5) , mass = 1.))
par.append(Particle(initial_pos = Vec2(0.6,2.5), initial_vel = Vec2(0.5,0.5) , mass = 1.))
# Simulation time and number of steps
t_end = 60.0
steps = 800
################################################################################################################################
n = len(par)
#create the functions to integrate
for i in range(n):
par[i].calculate_acc(par)
for i in range(n):
par[i].lambdify_vel(par)
par[i].lambdify_acc(par)
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
def vectorfield(var, t):
'''
integrate function
the function calculates f, a list with all differential equations of motion in the order
diff(x0), diff(y0), diff(x1), diff(y1)...diff(xn-1), diff(yn-1), diff(vx0), diff(vy0)...diff(vxn-1), diff(vyn-1)
it can be optimized, but it's done to be readable
'''
pos = var[0:2*n]
vel = var[2*n:4*n]
f = []
for i in range(0,n):
par[i].vf_vel.x = par[i].lamb_vel.x(vel[2*i])
par[i].vf_vel.y = par[i].lamb_vel.y(vel[2*i + 1])
f.append(par[i].vf_vel.x)
f.append(par[i].vf_vel.y)
for i in range(0,n):
par[i].vf_acc.x = par[i].lamd_acc.x(pos)
par[i].vf_acc.y = par[i].lamd_acc.y(pos)
f.append(par[i].vf_acc.x)
f.append(par[i].vf_acc.y)
return f
from scipy.integrate import odeint
# set the initial conditions
var = []
for i in range(len(par)):
var.append(par[i].initial_pos.x)
var.append(par[i].initial_pos.y)
for i in range(len(par)):
var.append(par[i].initial_vel.x)
var.append(par[i].initial_vel.y)
# ODE solver parameters
t = np.linspace(0,t_end,steps+1)
sol = odeint(vectorfield, var, t)
sol = np.transpose(sol)
# order the solution for clarity
for i in range(n):
par[i].sol_pos.x = sol[2*i]
par[i].sol_pos.y = sol[2*i+1]
for i in range(n):
par[i].sol_vel.x = sol[2*n + 2*i]
par[i].sol_vel.y = sol[2*n + 2*i+1]
# Calculate the total Energy of the system. The energy should be constant.
# Potential Energy
Energy = 0
for i in range(0,n):
for j in range(i+1,n):
Energy += (-1/(((par[i].sol_pos.x-par[j].sol_pos.x)**2 + (par[i].sol_pos.y-par[j].sol_pos.y)**2)**(1/2)))
# Kinetic Energy
for i in range(0,n):
Energy += 0.5*(par[i].sol_vel.x*par[i].sol_vel.x + par[i].sol_vel.y*par[i].sol_vel.y)
# Visualization of the solution with matplotlib. It uses a slider to change the time
################################################################################################################################
plt.style.use('dark_background')
fig = plt.figure(figsize=(7, 7))
ax = fig.add_subplot(1,1,1)
plt.subplots_adjust(bottom=0.2,left=0.15)
ax.axis('equal')
ax.axis([-1, 30, -1, 30])
ax.set_title('Energy =' + str(Energy[0]))
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
circle = [None]*n
line = [None]*n
for i in range(n):
circle[i] = plt.Circle((par[i].sol_pos.x[0], par[i].sol_pos.y[0]), 0.08, ec="w", lw=2.5, zorder=20)
ax.add_patch(circle[i])
line[i] = ax.plot(par[i].sol_pos.x[:0],par[i].sol_pos.y[:0])[0]
from matplotlib.widgets import Slider
slider_ax = plt.axes([0.1, 0.05, 0.8, 0.05])
slider = Slider(slider_ax, # the axes object containing the slider
't', # the name of the slider parameter
0, # minimal value of the parameter
t_end, # maximal value of the parameter
valinit=0, # initial value of the parameter
color = '#5c05ff'
)
def update(time):
i = int(np.rint(time*steps/t_end))
ax.set_title('Energy =' + str(Energy[i]))
for j in range(n):
circle[j].center = par[j].sol_pos.x[i], par[j].sol_pos.y[i]
line[j].set_xdata(par[j].sol_pos.x[:i+1])
line[j].set_ydata(par[j].sol_pos.y[:i+1])
slider.on_changed(update)
plt.show()