Too slow
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 |
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() |