-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfusion.py
More file actions
executable file
·458 lines (375 loc) · 18.3 KB
/
fusion.py
File metadata and controls
executable file
·458 lines (375 loc) · 18.3 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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
import math
import random
import numpy as np
from scipy.spatial.distance import pdist, squareform
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as p3
from matplotlib import animation
"""
tritium mass = 3
deuterium mass = 2
neutron mass = 1
Helium mass = 4
The particle state is a list of dictionaries:
example_state = [{'type': 'tritium', 'id': 0, 'size': 0.3, 'mass': 3,
'position': [1, 0, 0], 'velocity': [0, 0,-1], 'acceleration': [1, 1, 0]},
{'type': 'tritium', 'id': 1, 'size': 0.3, 'mass': 3,
'position': [1, 0, 1], 'velocity': [0, -1, -1], 'acceleration': [0, 0, 0]},
{'type': 'deuterium', 'id': 2, 'size': 0.2, 'mass': 2,
'position': [0, 0.5, 1], 'velocity': [1, 0.2, 0.1], 'acceleration': [0, 0, 0]},
{'type': 'neutron', 'id': 3, 'size': 0.1, 'mass': 1,
'position': [1, 1, 0], 'velocity': [5, 0, 0], 'acceleration': [0, 1, 1]}]
"""
class ParticleBox:
def __init__(self, init_state, boundary_min=-2, boundary_max=2):
"""
initializes the cube with particles, begins time, and sets the boundary of the simulation
:param init_state: list:
[{'type': string 'id': int, 'mass': scalar, 'position': [x, y, z], 'velocity': [vx, vy, vz]}]
:param boundary: [x_min, x_max, y_min, y_max, z_min, z_max]
"""
self.init_state = init_state
self.state = self.init_state.copy()
self.time_elapsed = 0
self.boundary_min = boundary_min
self.boundary_max = boundary_max
self.velocity_threshold = 10 * boundary_max #
self.r = 0.005 # inflection radius where nuclear force becomes greater than the EM forces
self.massHe = 4
self.massD = 2
self.massT = 3
self.massN = 1
self.sizeHe = 0.004
self.sizeD = 0.002
self.sizeT = 0.002
self.sizeN = 0.001
def update(self, dt):
"""
update frame once by dt seconds
:param self: object
:param dt: float
"""
self.time_elapsed += dt
# update positions
# for i, particle in enumerate(self.state):
# particle['position'] += dt * np.array(particle['velocity']) # forward Euler method
# collect data about the temperature of the state
particles = np.array([self.state[i] for i in np.arange(len(self.state))])
temperature(particles, self.time_elapsed)
"""
DESCRIPTION OF DISTANCE MATRIX:
`distance' is a distance matrix between all the particles, where its indices correspond to particle ids
e.g. if matrix element (1,2)=1.4 this means that particles with ids 1 and 2 are 1.4 units apart
NOTE: this assumes only that the particle ids start from 0 and that the init_state dictionary is ordered
"""
distance = squareform(pdist(np.array([self.state[i]['position'] for i in np.arange(len(self.state))])))
inflection_r = self.r
ind_short1, ind_short2= np.where(distance < inflection_r) # particles interacting under the short-range force
unique_short = (ind_short1 < ind_short2)
ind_short1 = ind_short1[unique_short]
ind_short2 = ind_short2[unique_short]
ind_long1, ind_long2 = np.where((distance > inflection_r))
# ind_long1, ind_long2 = np.where(distance < 100 * inflection_r)
unique_long = (ind_long1 < ind_long2)
ind_long1 = ind_long1[unique_long]
ind_long2 = ind_long2[unique_long]
for i1, i2 in zip(ind_short1, ind_short2):
particle1_type = self.state[i1]['type']
particle1_size = self.state[i1]['size']
particle1_id = self.state[i1]['id']
particle2_type = self.state[i2]['type']
particle2_size = self.state[i2]['size']
particle2_id = self.state[i2]['id']
m1 = self.state[particle1_id]['mass']
pos1 = np.array(self.state[particle1_id]['position'])
vel1 = np.array(self.state[particle1_id]['velocity'])
m2 = self.state[particle2_id]['mass']
pos2 = np.array(self.state[particle2_id]['position'])
vel2 = np.array(self.state[particle2_id]['velocity'])
# determine relative position and velocity
rel_pos1 = pos1 - pos2
rel_pos2 = pos2 - pos1
rel_vel1 = vel1 - vel2
rel_vel2 = vel2 - vel1
rel_pos1_squared = abs(np.dot(rel_pos1, rel_pos1))
rel_pos2_squared = abs(np.dot(rel_pos2, rel_pos2))
if rel_pos1_squared == 0:
rel_pos1_squared = 0.001
if rel_pos2_squared == 0:
rel_pos2_squared = 0.001
rel_pos1_unit = np.array([rel_pos1[i] / math.sqrt(rel_pos1_squared) for i in range(3)])
rel_pos2_unit = np.array([rel_pos1[i] / math.sqrt(rel_pos2_squared) for i in range(3)])
# the attractive force due to the Yukawa potential is applied
acel_scalar1 = -(1 + 1 / math.sqrt(rel_pos1_squared)) * \
(math.exp(-m1 * math.sqrt(rel_pos1_squared)) / math.sqrt(rel_pos1_squared)) / m1
acel_scalar2 = -(1 + 1 / math.sqrt(rel_pos2_squared)) * \
(math.exp(-m2 * math.sqrt(rel_pos2_squared)) / math.sqrt(rel_pos2_squared)) / m2
acel1 = np.array([acel_scalar1 * rel_pos1_unit[i] for i in range(3)]) # forward Euler
acel2 = np.array([acel_scalar2 * rel_pos2_unit[i] for i in range(3)]) # forward Euler
# move the particle according to the timestep
vel1 = np.array([vel1[i] + acel1[i] * dt for i in range(3)]) # forward Euler
vel2 = np.array([vel2[i] + acel2[i] * dt for i in range(3)]) # forward Euler
pos1 = np.array([pos1[i] + vel1[i] * dt for i in range(3)]) # forward Euler
pos2 = np.array([pos2[i] + vel2[i] * dt for i in range(3)]) # forward Euler
# update the states of the particles
self.state[particle1_id]['position'] = np.array(pos1)
self.state[particle1_id]['velocity'] = np.array(vel1)
self.state[particle1_id]['acceleration'] = np.array(acel1)
self.state[particle2_id]['position'] = np.array(pos2)
self.state[particle2_id]['velocity'] = np.array(vel2)
self.state[particle2_id]['acceleration'] = np.array(acel2)
# momentum vector of the center of mass frame
vel_cm = (m1 * vel1 + m2 * vel2) / (m1 + m2)
# collisions of spheres reflect rel_vel over rel_pos
three_momentum1 = np.dot(rel_vel1, rel_pos1)
three_momentum2 = np.dot(rel_vel2, rel_pos2)
rel_vel1 = 2 * rel_pos1 * three_momentum1 / rel_pos1_squared - rel_vel1
rel_vel2 = 2 * rel_pos2 * three_momentum2 / rel_pos2_squared - rel_vel2
if math.sqrt(rel_pos1_squared) <= particle1_size + particle2_size:
if (particle1_type == 'tritium' and particle2_type == 'deuterium') or \
(particle1_type == 'deuterium' and particle2_type == 'tritium'):
record_fusion(1, self.time_elapsed)
print('fusion occured at: ' + str(self.time_elapsed))
# a D-T reaction involves producing a new Helium particle and a neutron
new_init_acceleration = np.array([0, 0, 0])
new_he_velocity = (vel1 + vel2) / self.massHe
new_he_particle = {'type': 'helium', 'id': particle1_id, 'size': self.sizeHe,
'mass': self.massHe, 'position': pos1, 'velocity': new_he_velocity,
'acceleration': new_init_acceleration}
new_neutron = {'type': 'neutron', 'id': particle2_id, 'size': self.sizeN,
'mass': self.massN, 'position': pos2, 'velocity': random_vector(),
'acceleration': new_init_acceleration}
# remove old deuterium - tritium pair from state
if particle1_id > particle2_id:
self.state.pop(particle1_id)
self.state.pop(particle2_id)
else:
self.state.pop(particle2_id)
self.state.pop(particle1_id)
# add new particles to state
self.state.append(new_he_particle)
self.state.append(new_neutron)
# correct the order of the new state: sort by particle id
self.state.sort(key=lambda d: d['id'])
else:
# any other reaction involves a normal elastic collision between the particles
self.state[particle1_id]['velocity'] = vel_cm + rel_vel1 * m2 / (m1 + m2)
self.state[particle2_id]['velocity'] = vel_cm - rel_vel2 * m1 / (m1 + m2)
for i1, i2 in zip(ind_long1, ind_long2):
particle1_id = self.state[i1]['id']
particle1_type = self.state[i1]['type']
particle2_id = self.state[i2]['id']
particle2_type = self.state[i2]['type']
if (particle1_type == 'neutron') or (particle2_type == 'neutron'):
continue # neutrons do not experience the long-range electromagnetic forces
m1 = self.state[particle1_id]['mass']
pos1 = np.array(self.state[particle1_id]['position'])
vel1 = np.array(self.state[particle1_id]['velocity'])
m2 = self.state[particle2_id]['mass']
pos2 = np.array(self.state[particle2_id]['position'])
vel2 = np.array(self.state[particle2_id]['velocity'])
# determine relative position
rel_pos1 = pos1 - pos2
rel_pos2 = pos2 - pos1
rel_pos1_squared = abs(np.dot(rel_pos1, rel_pos1))
rel_pos2_squared = abs(np.dot(rel_pos2, rel_pos2))
rel_pos_unit1 = [rel_pos1[i] / math.sqrt(rel_pos1_squared) for i in range(3)]
rel_pos_unit2 = [rel_pos2[i] / math.sqrt(rel_pos2_squared) for i in range(3)]
# a simple 1/r^2 repulsive Coloumb force is applied
acel_scalar1 = (1 / rel_pos1_squared) / m1
acel_scalar2 = (1 / rel_pos2_squared) / m2
acel1 = [acel_scalar1 * rel_pos_unit1[i] for i in range(3)]
acel2 = [acel_scalar2 * rel_pos_unit2[i] for i in range(3)]
# move the particle according to the timestep
vel1 = [vel1[i] + acel1[i] * dt for i in range(3)]
vel2 = [vel2[i] + acel2[i] * dt for i in range(3)]
pos1 = [pos1[i] + vel1[i] * dt for i in range(3)]
pos2 = [pos2[i] + vel2[i] * dt for i in range(3)]
# update the states of the particles
self.state[particle1_id]['position'] = pos1
self.state[particle1_id]['velocity'] = vel1
self.state[particle1_id]['acceleration'] = acel1
self.state[particle2_id]['position'] = pos2
self.state[particle2_id]['velocity'] = vel2
self.state[particle2_id]['acceleration'] = acel2
# check for crossing boundary
for particle in self.state:
for component in np.arange(len(particle['position'])):
crossed_min = (particle['position'][component] < self.boundary_min + particle['size'])
crossed_max = (particle['position'][component] > self.boundary_max - particle['size'])
if crossed_min:
particle['position'][component] = self.boundary_min + particle['size']
particle['velocity'][component] *= -1
if crossed_max:
particle['position'][component] = self.boundary_max - particle['size']
particle['velocity'][component] *= -1
def random_vector():
return np.array([random.random() * random_sign(),
random.random() * random_sign(),
random.random() * random_sign()])
def random_sign():
if random.random() < 0.5:
return -1
else:
return 1
times = []
av_tritium_temps = []
av_deuterium_temps = []
av_helium_temps = []
av_neutron_temps = []
def temperature(particles, timestamp):
"""
Function receives particle dict for every particle for each timestep dt
:param particles: array: array of particle dictionaries in the state
:param timestamp: int: time corresponding to particles
:return: 0
# """
tritium_temp = []
deuterium_temp = []
helium_temp = []
neutron_temp = []
for particle in particles:
if particle['type'] == 'tritium':
tritium_temp.append(np.dot(particle['velocity'], particle['velocity']))
else:
tritium_temp.append(0)
if particle['type'] == 'deuterium':
deuterium_temp.append(np.dot(particle['velocity'], particle['velocity']))
else:
deuterium_temp.append(0)
if particle['type'] == 'helium':
helium_temp.append(np.dot(particle['velocity'], particle['velocity']))
else:
helium_temp.append(0)
if particle['type'] == 'neutron':
neutron_temp.append(np.dot(particle['velocity'], particle['velocity']))
else:
neutron_temp.append(0)
times.append(timestamp)
av_tritium_temps.append(sum(tritium_temp) / len(tritium_temp))
av_deuterium_temps.append(sum(deuterium_temp) / len(deuterium_temp))
av_helium_temps.append(sum(helium_temp) / len(helium_temp))
av_neutron_temps.append(sum(neutron_temp) / len(neutron_temp))
return 0
def plot_temperature(axis1, axis2):
"""
plots the data
:param axis1: array: times
:param axis2: quadruple: (tritium_temp, deuterium_temp, helium_temp, neutron_temp)
:return: 0
"""
fig2, ax_temp = plt.subplots()
ax_temp.plot(axis1, axis2[0], 'r-')
ax_temp.plot(axis1, axis2[1], 'b-')
ax_temp.plot(axis1, axis2[2], 'g-')
ax_temp.plot(axis1, axis2[3], 'y-')
ax_temp.set_xlabel('Time')
ax_temp.set_ylabel('Average Velocity of Particles')
ax_temp.legend(['Tritium', 'Deuterium', 'Helium', 'Neutron'])
plt.savefig('AverageVelocitiesOverTime.png')
return 0
# set up initial state
counts = []
count_times = []
rates = []
def record_fusion(count, timestep):
counts.append(count)
count_times.append(timestep)
rates.append(sum(counts) / timestep)
return 0
def plot_fusion_counts(axis1, axis2):
fig3, ax_counts = plt.subplots()
ax_counts.plot(axis1, axis2, 'g-')
ax_counts.set_xlabel('Time')
ax_counts.set_ylabel('Number of Fusion Reactions per Second')
plt.savefig('RateOfFusion.png')
return 0
np.random.seed(1)
plot_boundary = 2
# fill the init_state dictionary with N randomly-positioned particles moving at random velocity
N = 5
types = ['tritium', 'deuterium']
mass = {'tritium': 2, 'deuterium': 2}
size = {'tritium': 0.2, 'deuterium': 0.2}
N_types = {'tritium': 0, 'helium': 0, 'deuterium': 0, 'neutron': 0}
particles = np.random.choice(types, N)
init_state = []
for i, particle in enumerate(particles):
init_state.append(
{'type': particle, 'id': i, 'mass': mass[particle], 'size': size[particle],
'position': random_vector() * plot_boundary, 'velocity': random_vector(), 'acceleration': [0, 0, 0]}
)
N_types[particle] += 1
box = ParticleBox(init_state)
dt = 1 / 100
# set up figure and animation
fig = plt.figure()
ax = fig.add_axes([0, 0, 1, 1], projection='3d')
# ax.axis('off')
# initialize particle points
tritium = sum([ax.plot([], [], [], 'ro', ms=2) for i in np.arange(N)], [])
deuterium = sum([ax.plot([], [], [], 'bo', ms=2) for i in np.arange(N)], [])
helium = sum([ax.plot([], [], [], 'go', ms=4) for i in np.arange(N)], [])
neutron = sum([ax.plot([], [], [], 'yo', ms=1) for i in np.arange(N)], [])
# prepare the axis limits and labels
ax.set_xlim3d([-plot_boundary, plot_boundary])
ax.set_xlabel('x')
ax.set_ylim3d([-plot_boundary, plot_boundary])
ax.set_ylabel('y')
ax.set_zlim3d([-plot_boundary, plot_boundary])
ax.set_zlabel('z')
# set a point-of-view: specified by (altitude degrees, azimuth degrees)
ax.view_init(30, 0)
# set title
ax.set_title('My Fusion Reactor')
def init():
"""
initializes the animation
:return: particles, cube
"""
for trit, deut, hel, neut in zip(tritium, deuterium, helium, neutron):
trit.set_data([], [])
trit.set_3d_properties([])
deut.set_data([], [])
deut.set_3d_properties([])
hel.set_data([], [])
hel.set_3d_properties([])
neut.set_data([], [])
neut.set_3d_properties([])
return tritium + deuterium + helium + neutron
def animate(i):
"""
perform animation step
:param i:
:return:
"""
global box, dt, ax, fig
box.update(dt)
# ms = int(fig.dpi * 2 * box.size * fig.get_figwidth() / np.diff(ax.get_xbound())[0])
for trit, deut, hel, neut, particle in zip(tritium, deuterium, helium, neutron, box.state):
x = particle['position'][0]
y = particle['position'][1]
z = particle['position'][2]
if particle['type'] == 'tritium':
trit.set_data(x, y)
trit.set_3d_properties(z)
if particle['type'] == 'deuterium':
deut.set_data(x, y)
deut.set_3d_properties(z)
if particle['type'] == 'helium':
hel.set_data(x, y)
hel.set_3d_properties(z)
if particle['type'] == 'neutron':
neut.set_data(x, y)
neut.set_3d_properties(z)
ax.view_init(30, 0.3 * i)
fig.canvas.draw()
return tritium + deuterium + helium + neutron
# create animation object
ani = animation.FuncAnimation(fig, animate, init_func=init, frames=100, interval=30, blit=True)
plt.show()
# ani.save('fusion.html', fps=30, extra_args=['-vcodec', 'libx264'])
# plot temperature
plot_temperature(times, (av_tritium_temps, av_deuterium_temps, av_helium_temps, av_neutron_temps))
# plot rate of fuson reactions
plot_fusion_counts(count_times, rates)