###########################
# threading test ##########
###########################
import random
import math
import itertools
import time
import scipy.misc as scm
import numpy as np
import threading
from Queue import Queue
random.seed(1)
PX = 0;PY = 1;PZ = 2;
VX = 3;VY = 4;VZ = 5;
FX = 6;FY = 7;FZ = 8;
#number of particles in a line
line_num = 15
#total particle num
PN = line_num * line_num * line_num
#ready to 9 parameters for particle
#(PX, PY, PZ, VX, VY, VZ, FX, FY, FZ)
xyz = [[0 for i in range(9)] for j in range(PN)]
#Number of combinations of coulomb force calculation
combinum = int(scm.comb(PN, 2))
#thread number(local thread num)
core = 20
def find_pair_sub(prep,pend,thread,q):
global xyz
#local results array
xyzF = [[0 for i in range(3)] for j in range(PN)]
fx = 0; fy = 1; fz = 2
for i in xrange(prep,pend):
for j in xrange(i + 1, PN):
dx = xyz[i][PX] - xyz[j][PX]
dy = xyz[i][PY] - xyz[j][PY]
dz = xyz[i][PZ] - xyz[j][PZ]
r = math.sqrt(dx*dx + dy*dy + dz*dz)
xyzF[i][fx] = xyzF[i][fx] + dx/(r*r*r)
xyzF[i][fy] = xyzF[i][fy] + dy/(r*r*r)
xyzF[i][fz] = xyzF[i][fz] + dz/(r*r*r)
xyzF[j][fx] = xyzF[j][fx] - dx/(r*r*r)
xyzF[j][fy] = xyzF[j][fy] - dy/(r*r*r)
xyzF[j][fz] = xyzF[j][fz] - dz/(r*r*r)
q.put(xyzF)
def find_pair():
global PN
global combinum
q = Queue()
pw = combinum // core
pl = combinum % core
localt = 0
thread = 0
pre = 0
worklist = []
ppp = pw
for i in range(PN) :
if core == 1:
worklist.append([pre,PN,thread])
break
localt = localt + (PN - i - 1)
if localt >= ppp:
worklist.append([pre,i,thread])
ppp += pw
thread += 1
pre = i
if i != pre:
prep = worklist[thread-1][0]
worklist[thread-1] = [prep,PN,thread-1]
results = []
for i in range(core):
thread = threading.Thread(target=find_pair_sub, args=(worklist[i][0],worklist[i][1],worklist[i][2],q))
thread.start()
thread_list = threading.enumerate()
main_thread = threading.currentThread()
thread_list.remove(main_thread)
for thread in thread_list:
thread.join()
results.append(q.get())
for j in range(core):
for i in range(PN):
xyz[i][FX] += results[j][i][0]
xyz[i][FY] += results[j][i][1]
xyz[i][FZ] += results[j][i][2]
def init_lattice():
global xyz
pnum = 0
while pnum < PN:
xyz[pnum][PX] = random.uniform(-1,1)
xyz[pnum][PY] = random.uniform(-1,1)
xyz[pnum][PZ] = random.uniform(-1,1)
xyz[pnum][FX] = random.uniform(-1,1)
xyz[pnum][FY] = random.uniform(-1,1)
xyz[pnum][FZ] = random.uniform(-1,1)
pnum += 1
if __name__ == "__main__":
init_lattice()
find_pair()