我做了一个模拟。我有50000个粒子在一个盒子中结晶,其中有几个晶体(具有周期性边界条件)。我试图找到盒子里最大的水晶。这些代码效果很好,但是在我的计算机上大约需要100秒。速率确定步骤是下面的函数(find_neighbors)。我想知道是否有使用并行计算的方法来缩短计算时间?
我的电脑有12个核心。也许我们可以将阵列(中心)分成12个部分?
我的函数的输出是一个二维列表。
这是我编写的没有并行计算的函数。
#Calculate if two particles are neighbors (Periodic Boundary Condition)
#center is the coordinates of the particles
#d is the distance threshold for two clusters to be neighbors
# Maxima_Range and Lattice_Range are for periodic boundary conditions
#Pos_3d is a list.
# Suppose particle i has neighbors j,k,l.
# Pos_3d[i] = [j,k,l]
def find_neighbors(center,d,Maxima_Range,Lattice_Range):
a,b = np.shape(center) #get the dimension of the array
flag_3d = np.zeros(a) #count the number of neighbors for particle i
Pos_3d = [[] for i in range(a)]
# Pos_3d is a two dimmensional list.
#Pos_3d[i] includes the indices of all the neighbors of particle i
diff_sqrt = d ** 0.5
for i in range(a):
if i % 6 == 5: #for some reason, when i % 6 == 5, it is the center coordinates, this cannot be changed.
for j in range(i+1,a):
if j % 6 == 5:
diff_x = center[i][0]-center[j][0]
diff_y = center[i][1]-center[j][1]
diff_z = center[i][2]-center[j][2]
if diff_x < diff_sqrt and diff_y < diff_sqrt and diff_z < diff_sqrt and diff_x > -diff_sqrt and diff_y > -diff_sqrt and diff_z > -diff_sqrt:
#if one of the x,y,z component is already bigger than d, we don't need to calculate their distance in 3-D. They are not neighbors.
diff = diff_x * diff_x +diff_y * diff_y +diff_z * diff_z
if diff <= d: #without period boundary condition
flag_3d[i] +=1
flag_3d[j] +=1
Pos_3d[i].append(j)
Pos_3d[j].append(i)
continue
#With periodic boundary condition
#in x
do something..
return Pos_3d