-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumba_project.py
370 lines (304 loc) · 11.6 KB
/
numba_project.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import numpy as np
from scipy.integrate import solve_ivp
from matplotlib import pyplot as plt
# noinspection PyUnresolvedReferences
from mpl_toolkits.mplot3d import Axes3D # <--- This is important for 3d plotting
# import matplotlib
# matplotlib.use('Qt5Agg')
import project_helper
from runga_kuta import make_solver
from pytictoc import TicToc
import os
from numba import jit, njit, jitclass
from numba import int16, float64
from numba import deferred_type, optional
from numba.core import types
from numba.typed import Dict, List
plt.rcParams["font.size"] = "14"
# constant
N = 50. # 5000
M_TOT = 10 ** 11 # Sun masses
R_INITIAL = 50e3 # Parsec
SOFT_PARAM = 1e3 # Parsec
MAX_BOX_SIZE = 666e3 # Parsec
T_FINAL = 20e9 # years, = 20453 in units time of the exercise
T_FINAL = 20453 # in units time of the exercise
STEP_T = T_FINAL / 20e4
THETA = 1
GRAVITATIONAL_CONSTANT = 4.30091e-3
node_type = deferred_type()
spec = [
('leafs', optional(node_type)),
('father', optional(node_type)),
('borders', float64[:]),
('center_of_mass', float64[:]),
('mass_count', int16),
('main_diagonal', float64[:]),
]
@jitclass(spec)
class Node:
"""
Tree structure for saving all the data in the mass system
"""
def __init__(self, borders, father=None):
# leaf are the children of some node
self.leafs = None
self.father = father
# initialize the basics attributes
self.borders = borders
self.center_of_mass = np.array([0., 0., 0.])
self.masses_indices = []
self.mass_count = 0
# we can compute already the main diagonal of the box
two_point = self.borders.T
self.main_diagonal = np.linalg.norm(two_point[0] - two_point[1])
# node_type.define(Node.class_type.instance_type)
# @jitclass(spec)
def MassSystem(velocity):
"""
Object that have all the properties of the system.
It save the positions, velocities and have also the root of the Tree.
Some of the important methods are:
build_tree, calculate_force ode_to_solve
"""
# The Dict.empty() constructs a typed dictionary.
# The key and value typed must be explicitly declared.
self_float = Dict.empty(
key_type=types.unicode_type,
value_type=types.float64,
)
# self_node = Dict.empty(
# key_type=types.unicode_type,
# value_type=node_type,
# )
self_2D_array = Dict.empty(
key_type=types.unicode_type,
value_type=types.float64[:,:],
)
# self = {}
# each mass is the total mass divided by the number of the point_like mass
self_float['N'] = N
self_float['each_mass'] = M_TOT / self_float['N']
# calculate positions
u, cos_theta, phi = np.random.rand(3, int(self_float['N']))
phi = 2 * np.pi * phi
cos_theta = 2 * cos_theta - 1
sin_theta = np.sqrt(1 - cos_theta ** 2)
r = R_INITIAL * u ** (1 / 3)
self_2D_array['positions'] = (r * np.array([sin_theta * np.cos(phi), sin_theta * np.sin(phi), cos_theta])).T
# calculate velocities
self_2D_array['velocities'] = np.random.normal(0, velocity / np.sqrt(3), (self_float['N'], 3))
# build tree
self = List()
# self = [self_float, self_node, self_2D_array]
# self_node['root'] = build_tree(self)
# calculate forces
# self_2D_array['forces'] = np.zeros_like(self['positions'])
# self_float['count_img'] = 0.
return self
def remove_exceeds_masses(self):
ind_to_del = np.argwhere((mass_system['positions'] < -MAX_BOX_SIZE) | (mass_system['positions'] > MAX_BOX_SIZE))[:, 0]
if len(ind_to_del) > 0:
ind_to_del = np.unique(ind_to_del, axis=0)
self['N'] = self['N'] - len(ind_to_del)
self['positions'] = np.delete(self['positions'], ind_to_del, axis=0)
self['velocities'] = np.delete(self['velocities'], ind_to_del, axis=0)
def get_current_values(self):
r = self['positions'].flatten()
v = self['velocities'].flatten()
return np.hstack((r, v))
def set_values_from_flat(self, y):
dof = int(self['N'] * 3)
self['positions'] = np.reshape(y[:dof], (self['N'], 3))
self['velocities'] = np.reshape(y[dof:], (self['N'], 3))
def plot(self, num, show=False, save=True, camera_pos=False):
"""
Plot the current state of the system in 3D figure plot
"""
fig = plt.figure(num)
ax = fig.add_subplot(111, projection='3d')
ax.scatter(*self['positions'].T)
if show:
plt.show()
# fig.canvas.draw()
# fig.canvas.flush_events()
if save:
plt.savefig(folder + f'{self["count_img"]}.png', bbox_inches='tight', dpi=100)
self['count_img'] += 1
@njit
def build_tree(system):
borders = get_current_box_size(system) # x_lim, y_lim, z_lim
root = Node(borders, father=system)
root.masses_indices = list(range(system['N']))
root.center_of_mass = np.mean(system['positions'], axis=0)
root.mass_count = system['N']
build_tree_helper(system, root)
return root
@njit
def build_tree_helper(system, node):
middle_limits = [np.mean(lim) for lim in node.borders]
x_lim, y_lim, z_lim = node.borders
borders8 = [[sorted([x_lim[i], middle_limits[0]]),
sorted([y_lim[j], middle_limits[1]]),
sorted([z_lim[k], middle_limits[2]])]
for i in range(2) for j in range(2) for k in range(2)]
leafs = []
for border in borders8:
leaf = Node(np.array(border), father=node)
fill_attributes(system, leaf)
if leaf.mass_count > 0:
leafs.append(leaf)
if leaf.mass_count > 1:
build_tree_helper(system, leaf)
node.leafs = leafs
@jit
def fill_attributes(system, node):
masses_indices = node.father.masses_indices[:]
for i in masses_indices:
point = system['positions'][i]
if point_in_box(point, node.borders):
node.masses_indices.append(i)
node.father.masses_indices.remove(i)
if len(node.masses_indices) > 0:
node.center_of_mass = np.mean(system['positions'][node.masses_indices, :], axis=0)
node.mass_count = len(node.masses_indices)
@jit
def get_current_box_size(system):
x_lim = np.array([np.min(system['positions'][:, 0]) - 1, np.max(system['positions'][:, 0]) + 1])
y_lim = np.array([np.min(system['positions'][:, 1]) - 1, np.max(system['positions'][:, 1]) + 1])
z_lim = np.array([np.min(system['positions'][:, 2]) - 1, np.max(system['positions'][:, 2]) + 1])
return np.array([x_lim, y_lim, z_lim])
@jit
def calculate_force(system):
"""
Initiate the calculation of the force for each point mass we are saving the force that act on it.
:return:
"""
system['forces'] = np.zeros((system['N'], 3))
for i, point in enumerate(system['positions']):
system['forces'][i] = calculate_force_helper(system, system['root'], point)
@jit
def calculate_force_helper(system, node, point):
"""
Recursive function return the for acting on "point" from all the masses in "node"
"""
force = np.array([0., 0., 0.])
if node.mass_count == 0:
# exit condition from the recursion
return force
# define the vector between two point
distance_vec = -(point - node.center_of_mass) # attractive force
distance = np.linalg.norm(distance_vec)
if node.mass_count == 1:
# if just 1 mass so the force is simply the force between them
if distance == 0:
# unless we are calculating for the same point
return force # np.array([0., 0., 0.])
# compute and return the force
force_amplitude = GRAVITATIONAL_CONSTANT * system['each_mass'] ** 2 / (distance + SOFT_PARAM) ** 2
force_direction = distance_vec / distance
return force_amplitude * force_direction
else:
# mass_count >= 2
if distance / node.main_diagonal < THETA or point_in_box(point, node.borders):
# if too close we are need to get inside the recursion
for leaf in node.leafs:
force = force + calculate_force_helper(system, leaf, point)
else:
# we don't need to go further just multiply the force by the number of masses inside this node
force_amplitude = node.mass_count * GRAVITATIONAL_CONSTANT * system['each_mass'] ** 2 / (
distance + SOFT_PARAM) ** 2
force_direction = distance_vec / distance
return force_amplitude * force_direction
# I don't think this line is ever execute
return force
@jit
def ode_to_solve(t, y):
dof = int(mass_system['N'] * 3)
set_values_from_flat(mass_system, y)
mass_system['root'] = build_tree(mass_system)
calculate_force(mass_system)
drdt = y[dof:]
dvdt = mass_system['forces'].flatten() / mass_system['each_mass']
return np.hstack((drdt, dvdt))
@jit
def point_in_box(point, borders):
return borders[0][0] <= point[0] < borders[0][1] and \
borders[1][0] <= point[1] < borders[1][1] and \
borders[2][0] <= point[2] < borders[2][1]
@jit
def start_cal(n):
save_positions = [mass_system['positions']]
for i in range(n):
print(i)
y_0 = get_current_values(mass_system)
if len(y_0) < N * 6:
print('The len of y_0/6 is ' + str(len(y_0) // 6))
_, sol = my_solver(y_0)
# sol = solve_ivp(ode_to_solve(mass_system), t_span, y_0, rtol=0.1, atol=eps)
# set_values_from_flat(mass_system,sol)
remove_exceeds_masses(mass_system)
if i % 2 == 0:
save_positions.append(mass_system['positions'])
if mass_system['N'] == 0:
return save_positions
return save_positions
def start_cal_no_jit(n):
save_positions = [mass_system['positions']]
try:
for i in range(n):
print(i)
y_0 = get_current_values(mass_system)
if len(y_0) < N * 6:
print(f'The len of y_0/6 is {len(y_0) // 6}')
_, sol = my_solver(y_0)
# sol = solve_ivp(ode_to_solve(mass_system), t_span, y_0, rtol=0.1, atol=eps)
# set_values_from_flat(mass_system,sol)
remove_exceeds_masses(mass_system)
if i % 2 == 0:
save_positions.append(mass_system['positions'])
if mass_system['N'] == 0:
return save_positions
except KeyboardInterrupt:
remove_exceeds_masses(mass_system)
save_positions.append(mass_system['positions'])
print('KeyboardInterrupt')
except Exception as e:
global emergency
emergency = save_positions
raise e
return save_positions
if __name__ == '__main__':
np.random.seed(12345)
plt.close('all')
tic = TicToc()
# define the system
v = 80
mass_system = MassSystem(v)
# define parameters of calculation
y_0 = get_current_values(mass_system)
# t_span = (0, T_FINAL / n)
tf = 80
n = T_FINAL // tf + 1
t_span = (0, tf)
eps = 1000
my_solver = make_solver(ode_to_solve, t_span, 'RK4', eps)
if '#@jit'.startswith('#'):
start_cal = start_cal_no_jit
# start simulation
tic.tic()
all_positions = start_cal(n)
tic.toc()
time = tic.tocvalue()
folder = project_helper.create_folder()
np.save(folder + 'all_pos', all_positions)
new_folder_rename = folder[:-1] + f'_eps_{eps}_N_mass_{N}_repeat_ode_{n}_v_{v}_time_{time // 60}_m_/'
if '#@jit'.startswith('@'):
new_folder_rename = new_folder_rename[:-1] + 'jit/'
os.rename(folder, new_folder_rename)
# all_positions = list(np.load('all_pos.npy', allow_pickle=True))
tic.tic()
project_helper.save_figures(2, all_positions, folder)
project_helper.gif(folder, 'animate')
tic.toc()
project_helper.beep()