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 |
import random import math import time import numpy as np #import scipy.misc as scm from multiprocessing import Pool np.set_printoptions(threshold=np.inf) random.seed(1) XX = 0 YY = 1 ZZ = 2 #total particle num PN = 1000 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 tmp_index = np.arange(xyzP.shape[0]) xx, yy = np.meshgrid(tmp_index, tmp_index) distances = np.linalg.norm(xyzP[xx]-xyzP[yy], axis=2) 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() |