-
Notifications
You must be signed in to change notification settings - Fork 0
/
nbody.pyx
270 lines (251 loc) · 8.33 KB
/
nbody.pyx
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
# -*- coding: utf-8 -*-
# The Computer Language Benchmarks Game
# http://benchmarksgame.alioth.debian.org/
#
# originally by Kevin Carson
# modified by Tupteq, Fredrik Johansson, and Daniel Nanz
# modified by Maciej Fijalkowski
# 2to3
#
#PI = 3.14159265358979323
#SOLAR_MASS = 4*PI**2
#DAYS_PER_YEAR = 365.24
#BODIES = {
# 'sun': ([0.0, 0.0, 0.0], [0.0, 0.0, 0.0], SOLAR_MASS),
#
# 'jupiter': ([4.84143144246472090e+00,
# -1.16032004402742839e+00,
# -1.03622044471123109e-01],
# [1.66007664274403694e-03 * DAYS_PER_YEAR,
# 7.69901118419740425e-03 * DAYS_PER_YEAR,
# -6.90460016972063023e-05 * DAYS_PER_YEAR],
# 9.54791938424326609e-04 * SOLAR_MASS),
#
# 'saturn': ([8.34336671824457987e+00,
# 4.12479856412430479e+00,
# -4.03523417114321381e-01],
# [-2.76742510726862411e-03 * DAYS_PER_YEAR,
# 4.99852801234917238e-03 * DAYS_PER_YEAR,
# 2.30417297573763929e-05 * DAYS_PER_YEAR],
# 2.85885980666130812e-04 * SOLAR_MASS),
#
# 'uranus': ([1.28943695621391310e+01,
# -1.51111514016986312e+01,
# -2.23307578892655734e-01],
# [2.96460137564761618e-03 * DAYS_PER_YEAR,
# 2.37847173959480950e-03 * DAYS_PER_YEAR,
# -2.96589568540237556e-05 * DAYS_PER_YEAR],
# 4.36624404335156298e-05 * SOLAR_MASS),
#
# 'neptune': ([1.53796971148509165e+01,
# -2.59193146099879641e+01,
# 1.79258772950371181e-01],
# [2.68067772490389322e-03 * DAYS_PER_YEAR,
# 1.62824170038242295e-03 * DAYS_PER_YEAR,
# -9.51592254519715870e-05 * DAYS_PER_YEAR],
# 5.15138902046611451e-05 * SOLAR_MASS)
# }
#
#class conserve(object):
# def __init__(self):
# pass
# #self.body = body
# #self.ref = ref
# def combinations(self, l):
# result = []
# for x in range(len(l) - 1):
# ls = l[x+1:]
# for y in ls:
# result.append((l[x],y))
# return result
#
#
# def offset_momentum(self, ref, bodies):
#
# px = py = pz = 0.0
#
# for (r, [vx, vy, vz], m) in bodies:
# px -= vx * m
# py -= vy * m
# pz -= vz * m
# (r, v, m) = ref
# v[0] = px / m
# v[1] = py / m
# v[2] = pz / m
#
# def advance(self, dt, n, bodies, pairs):
#
# for i in range(n):
# for (([x1, y1, z1], v1, m1),
# ([x2, y2, z2], v2, m2)) in pairs:
# dx = x1 - x2
# dy = y1 - y2
# dz = z1 - z2
# mag = dt * ((dx * dx + dy * dy + dz * dz) ** (-1.5))
# b1m = m1 * mag
# b2m = m2 * mag
# v1[0] -= dx * b2m
# v1[1] -= dy * b2m
# v1[2] -= dz * b2m
# v2[0] += dx * b1m
# v2[1] += dy * b1m
# v2[2] += dz * b1m
# for (r, [vx, vy, vz], m) in bodies:
# r[0] += dt * vx
# r[1] += dt * vy
# r[2] += dt * vz
#
#
# def report_energy(self, bodies, pairs):
# e = 0.0
# for (((x1, y1, z1), v1, m1),
# ((x2, y2, z2), v2, m2)) in pairs:
# dx = x1 - x2
# dy = y1 - y2
# dz = z1 - z2
# e -= (m1 * m2) / ((dx * dx + dy * dy + dz * dz) ** 0.5)
# for (r, [vx, vy, vz], m) in bodies:
# e += m * (vx * vx + vy * vy + vz * vz) / 2.
# return e
#
#a = conserve()
#bodies = BODIES
#n = 50000
#ref = 'sun'
#system = list(bodies.values())
#pairs = a.combinations(system)
#a.offset_momentum(bodies[ref], system)
#print "{:.9f}".format(a.report_energy(system, pairs))
#a.advance(0.01, n, system, pairs)
#print "{:.9f}".format(a.report_energy(system, pairs))
#
#===========The uper code, I just write if for class type=========
#===I will use cython syntax to rewrite the code===
from numba import jit
DEF PI = 3.14159265358979323
DEF SOLAR_MASS = 4*PI**2
DEF DAYS_PER_YEAR = 365.24
#(position, velocity, mass)
BODIES = {
'sun': ([0.0, 0.0, 0.0], [0.0, 0.0, 0.0], SOLAR_MASS),
'jupiter': ([4.84143144246472090e+00,
-1.16032004402742839e+00,
-1.03622044471123109e-01],
[1.66007664274403694e-03 * DAYS_PER_YEAR,
7.69901118419740425e-03 * DAYS_PER_YEAR,
-6.90460016972063023e-05 * DAYS_PER_YEAR],
9.54791938424326609e-04 * SOLAR_MASS),
'saturn': ([8.34336671824457987e+00,
4.12479856412430479e+00,
-4.03523417114321381e-01],
[-2.76742510726862411e-03 * DAYS_PER_YEAR,
4.99852801234917238e-03 * DAYS_PER_YEAR,
2.30417297573763929e-05 * DAYS_PER_YEAR],
2.85885980666130812e-04 * SOLAR_MASS),
'uranus': ([1.28943695621391310e+01,
-1.51111514016986312e+01,
-2.23307578892655734e-01],
[2.96460137564761618e-03 * DAYS_PER_YEAR,
2.37847173959480950e-03 * DAYS_PER_YEAR,
-2.96589568540237556e-05 * DAYS_PER_YEAR],
4.36624404335156298e-05 * SOLAR_MASS),
'neptune': ([1.53796971148509165e+01,
-2.59193146099879641e+01,
1.79258772950371181e-01],
[2.68067772490389322e-03 * DAYS_PER_YEAR,
1.62824170038242295e-03 * DAYS_PER_YEAR,
-9.51592254519715870e-05 * DAYS_PER_YEAR],
5.15138902046611451e-05 * SOLAR_MASS)
}
#combination each planet into pair
cpdef combinations(x):
cdef:
list result = [ ]
for i in xrange(len(x)-1):
ls = x[1+i:]
for y in ls:
result.append([x[i],y])
return result
ctypedef struct tempbody:
double x[3]
double v[3]
double m
DEF NBODIES = 5
cdef advance(dt, n, bodies):
cdef:
tempbody *body1
tempbody *body2
double dx, dy, dz, b1m, b2m, mag
tempbody cbody[NBODIES]
list pylist = [ ]
tempbody *pointer
#c array can't be output
for i in xrange(NBODIES):
pointer = &cbody[i]
(x, y, z) = bodies[i]
pointer.x[0],pointer.x[1],pointer.x[2] = x
pointer.v[0],pointer.v[1],pointer.v[2] = y
pointer.m = z
for i in xrange(n):
for ii in xrange(NBODIES):
body1 = &cbody[ii]
for jj in xrange(ii+1, NBODIES):
body2 = &cbody[jj]
dx = body1.x[0]-body2.x[0]
dy = body1.x[1]-body2.x[1]
dz = body1.x[2]-body2.x[2]
mag = dt * ((dx * dx + dy * dy + dz * dz) ** (-1.5))
b1m = body1.m * mag
b2m = body2.m * mag
body1.v[0] -= dx * b2m
body1.v[1] -= dy * b2m
body1.v[2] -= dz * b2m
body2.v[0] += dx * b1m
body2.v[1] += dy * b1m
body2.v[2] += dz * b1m
for ii in xrange(NBODIES):
body2 = &cbody[ii]
body2.x[0] += dt * body2.v[0]
body2.x[1] += dt * body2.v[1]
body2.x[2] += dt * body2.v[2]
#change to the python list
for i in xrange(NBODIES):
x = [cbody[i].x[0],cbody[i].x[1],cbody[i].x[2]]
v = [cbody[i].v[0],cbody[i].v[1],cbody[i].v[2]]
m = cbody[i].m
pylist.append((x, v, m))
return pylist
cdef report_energy(list bodies):
cdef:
double e = 0.0
double dx = 0.0
double dy = 0.0
double dz = 0.0
pairs = combinations(bodies)
for (((x1, y1, z1), v1, m1), ((x2, y2, z2), v2, m2)) in pairs:
dx = x1 - x2
dy = y1 - y2
dz = z1 - z2
e -= (m1 * m2) / ((dx * dx + dy * dy + dz * dz) ** 0.5)
for (r, [vx, vy, vz], m) in bodies:
e += m * (vx * vx + vy * vy + vz * vz) / 2.
print("%.9f" % e)
cdef offset_momentum(ref, list bodies):
cdef:
double px = 0.0
double py = 0.0
double pz = 0.0
for (r, [vx, vy, vz], m) in bodies:
px -= vx * m
py -= vy * m
pz -= vz * m
(r, v, m) = ref
v[0] = px / m
v[1] = py / m
v[2] = pz / m
def main(n, bodies=BODIES):
system = list(bodies.values())
offset_momentum(bodies['sun'], system)
report_energy(system)
system = advance(0.01, n, system)
report_energy(system)