Too slow
import random
import math
import time
import numpy as np
np.set_printoptions(threshold=np.inf)
random.seed(1)
XX = 0
YY = 1
ZZ = 2
#total particle num
PN = 300
xyzV = [[0 for i in range(3)] for j in range(PN)]
xyzP = np.zeros((PN,3))
xyzF = np.zeros((PN,3))
def find_pair():
global PN
global xyzF
global xyzP
x1 = np.empty(0)
y1 = np.empty(0)
for mm in range(1,PN):
for nn in range(mm,PN):
x1 = np.append(x1,[nn],axis=0)
y1 = np.append(y1,[mm-1],axis=0)
x1 = x1.astype(np.int32)
x1 = x1.reshape(x1.shape[0],1)
y1 = y1.astype(np.int32)
y1 = y1.reshape(y1.shape[0],1)
dd = np.linalg.norm(xyzP[x1]-xyzP[y1], axis=2)
distances = np.zeros((PN,PN))
ly = 0
lx = ly + 1
for kk in range(0,dd.shape[0]):
distances[lx][ly] = dd[kk]
distances[ly][lx] = dd[kk]
lx = lx + 1
if lx == PN:
ly = ly + 1
lx = ly + 1
tmp_index = np.arange(xyzP.shape[0])
xx, yy = np.meshgrid(tmp_index, tmp_index)
dxyz = xyzP[xx] - xyzP[yy]
distances = distances[:,:,np.newaxis]
tmp_force = dxyz/distances
tmp_force = np.nan_to_num(tmp_force,0)
mmm = tmp_force.sum(axis=1)
xyzF = xyzF + mmm
def init_lattice():
global xyzF
global xyzP
pnum = 0
while pnum < PN:
xyzP[pnum][XX] = random.uniform(-1,1)
xyzP[pnum][YY] = random.uniform(-1,1)
xyzP[pnum][ZZ] = random.uniform(-1,1)
xyzF[pnum][XX] = random.uniform(-1,1)
xyzF[pnum][YY] = random.uniform(-1,1)
xyzF[pnum][ZZ] = random.uniform(-1,1)
pnum += 1
def results_sum():
#global xyzF
pnum = 0
total_F = 0
while pnum < PN:
total_F = total_F + xyzF[pnum][XX]
total_F = total_F + xyzF[pnum][YY]
total_F = total_F + xyzF[pnum][ZZ]
pnum += 1
print (total_F)
if __name__ == "__main__":
init_lattice()
find_pair()
results_sum()
