-
Notifications
You must be signed in to change notification settings - Fork 11
/
orbits_formation_example.py
275 lines (175 loc) · 7 KB
/
orbits_formation_example.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
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 "(" + str(self.x) + ", " + str(self.y) + ")"
def get_length(self):
return np.sqrt(self.x ** 2 + self.y ** 2)
def __add__(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 __mul__(self, n):
return Vec2(self.x * n, self.y * n)
# Define a Particle class. The particles are the bodies attracting each other
class Particle():
# n = number of particles
n = 0
def __init__(self,ipos,ivel):
# i = particle index
self.i = Particle.n
Particle.n += 1
self.m = 1
self.G = 1
# 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)
# fvel, facc = lambdify functions
self.fvel = Vec2(None,None)
self.facc = Vec2(None,None)
# fpos, fvel = intial position and velocity
self.ipos = ipos
self.ivel = ivel
# fnvel, fnacc = functions used in vectorfield() function
self.fnvel = Vec2(0,0)
self.fnacc = Vec2(0,0)
# solpos, solvel = position and velocity list obtained after the integration
self.solpos = Vec2(None,None)
self.solvel = Vec2(None,None)
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)))
def lambdify_vel(self,particles):
self.fvel.x = sp.lambdify(self.vel.x, self.vel.x)
self.fvel.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.facc.x = sp.lambdify([var], self.acc.x)
self.facc.y = sp.lambdify([var], self.acc.y)
#particle list
par = []
#create the particles
par.append(Particle(Vec2(0,0.),Vec2(0.0,0.0)))
par[0].m = 50
par.append(Particle(Vec2(5,5),Vec2(-1.0,1.5)))
par[1].m = 1.5
par.append(Particle(Vec2(-85,85),Vec2(-0.2,-0.6)))
par[2].m = 2.
par.append(Particle(Vec2(20,20),Vec2(-0.9,0.9)))
par[3].m = 0.5
par.append(Particle(Vec2(208,17),Vec2(-0.0,0.5)))
par[4].m = 1.5
par.append(Particle(Vec2(-122.6,-10.5),Vec2(0.0,-0.5)))
par[5].m = 1.0
par.append(Particle(Vec2(0.0,-350.5),Vec2(0.4,-0.0)))
par[6].m = 1.0
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].fnvel.x = par[i].fvel.x(vel[2*i])
par[i].fnvel.y = par[i].fvel.y(vel[2*i + 1])
f.append(par[i].fnvel.x)
f.append(par[i].fnvel.y)
for i in range(0,n):
par[i].fnacc.x = par[i].facc.x(pos)
par[i].fnacc.y = par[i].facc.y(pos)
f.append(par[i].fnacc.x)
f.append(par[i].fnacc.y)
return f
################################################################################################################################
from scipy.integrate import odeint
# set the initial conditions
var = []
for i in range(len(par)):
var.append(par[i].ipos.x)
var.append(par[i].ipos.y)
for i in range(len(par)):
var.append(par[i].ivel.x)
var.append(par[i].ivel.y)
# ODE solver parameters
tfin = 10000.0
steps = 124000
t = np.linspace(0,tfin,steps+1)
sol = odeint(vectorfield, var, t)
sol = np.transpose(sol)
# order the solution for clarity
for i in range(n):
par[i].solpos.x = sol[2*i]
par[i].solpos.y = sol[2*i+1]
for i in range(n):
par[i].solvel.x = sol[2*n + 2*i]
par[i].solvel.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 += (-par[i].m*par[j].m/(((par[i].solpos.x-par[j].solpos.x)**2 + (par[i].solpos.y-par[j].solpos.y)**2)**(1/2)))
# Kinetic Energy
for i in range(0,n):
Energy += 0.5*par[i].m*(par[i].solvel.x*par[i].solvel.x + par[i].solvel.y*par[i].solvel.y)
# Visualization of the solution with matplotlib. It use 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([-800, 800, -800, 800])
ax.set_title('Energy =' + str(Energy[0]))
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
circle = [None]*n
line = [None]*n
circle[0] = plt.Circle((par[0].solpos.x[0], par[0].solpos.y[0]), 1.18, ec="w", lw=2.5, zorder=20)
ax.add_patch(circle[0])
line[0] = ax.plot(par[0].solpos.x[:0],par[0].solpos.y[:0])[0]
for i in range(1,n):
circle[i] = plt.Circle((par[i].solpos.x[0], par[i].solpos.y[0]), 0.08, ec="w", lw=2.5, zorder=20)
ax.add_patch(circle[i])
line[i] = ax.plot(par[i].solpos.x[:0],par[i].solpos.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
tfin, # maximal value of the parameter
valinit=0, # initial value of the parameter
color = '#5c05ff'
)
def update(time):
i = int(np.rint(time*steps/tfin))
ax.set_title('Energy =' + str(Energy[i]))
for j in range(n):
circle[j].center = par[j].solpos.x[i], par[j].solpos.y[i]
line[j].set_xdata(par[j].solpos.x[:i+1])
line[j].set_ydata(par[j].solpos.y[:i+1])
slider.on_changed(update)
plt.show()