def both_connections(con_A,con_B): l1 = [] l2 = [] #get the values for linekrs which have connectison #in an agreeable format for k in range(len(con_A)): a = [] b = [] for i in range(len(con_A[k])): a.append(con_A[k][i][1]) for i in range(len(con_B[k])): b.append(con_B[k][i][1]) l1.append(a) l2.append(b) #count the number that are unique and the same one=[] two=[] two_list=[] one_list=[] for k in range(len(l1)): two.append(len(points.intersection(l1[k],l2[k]))) one.append(len(points.difference(l1[k],l2[k]))) for k in range(len(l1)): two_list.append((points.intersection(l1[k],l2[k]))) one_list.append((points.difference(l1[k],l2[k]))) return one,two,one_list, two_list
def find_defects_mod(CV,CW,VW,V,W,L,n_finish=1,n_start=0,delta=20,filters=0.2): from MD.analysis.nearest_neighbor import ws_neighbors_point from MD.analysis.nearest_neighbor import close_neighbors_point ####### debug = False x = np.arange(n_start,n_finish,delta) lattice = [] for i in range(CV.shape[0]): lattice.append(int(points.dist(CV[0],CV[i],L)[0])) for i in range(CW.shape[0]): lattice.append(int(points.dist(CV[0],CW[i],L)[0])) points.unique(lattice) lattice.sort() print lattice rmin = lattice[1] / 2.0 rmax = lattice[2] / 2.0 +1 #for interstitial s = min(W.shape[1],V.shape[1]) #for vacancy #s = max(W.shape[1],V.shape[1]) DW = np.zeros((len(x),CW.shape[0])) DV = np.zeros((len(x),CV.shape[0])) vac_count = np.zeros((len(x),1)) int_count = np.zeros((len(x),1)) fid = open('defects.txt','w') master = [[],[]] particle_frame = [] for index,k in enumerate(x): print 'frame',k # Identify the points on the bcc Lattice which belong to A and B type #lets look at one point first # Assign each point to its place on the lattice #find the closest point ##################### N = [] N_V = [] N_W = [] Vn = [] Wn = [] for i in V[k]: if abs(i[0]) < L[0]: Vn.append(i) for i in W[k]: if abs(i[0]) < L[0]: Wn.append(i) print 'Wn',len(Wn) print 'Vn',len(Vn) particle_frame.append(len(Wn)+len(Vn)) Wn = np.array(Wn) Vn = np.array(Vn) for i in range(CV.shape[0]): N = ws_neighbors_point(Vn,CV[i],L,i,rmin=rmin,rmax=rmax)[0] N_V.extend(N) num = len(N) if num == 0: N2 = ws_neighbors_point(Wn,CV[i],L,i,rmin=rmin,rmax=rmax)[0] N_W.extend(N2) num = -len(N2) DV[index][i] = num ########################### for i in range(CW.shape[0]): N = ws_neighbors_point(Wn,CW[i],L,i+W.shape[1],rmin=rmin,rmax=rmax)[0] N_W.extend(N) num = len(N) if num == 0: N2 = ws_neighbors_point(Vn,CW[i],L,i+V.shape[1],rmin=rmin,rmax=rmax)[0] N_V.extend(N2) num = -len(N2) DW[index][i] = num #find the atoms that haven't been placed on the lattice yet IV = points.difference(range(Vn.shape[0]),N_V) IW = points.difference(range(Wn.shape[0]),N_W) ## if debug: print 'atoms not added listed after first sorting' print IV, IW print DW[index] print DV[index] for i in IV: #find closest lattice point nw, dw = close_neighbors_point(CW,Vn[i],L) nv, dv = close_neighbors_point(CV,Vn[i],L) if dw <= dv: #check to see if there is already an atom at that point if DW[index][nw] == 1 or DW[index][nw] == -1: if DV[index][nv] == 0: DV[index][nv] = 1 N_V.extend([i]) else: DW[index][nw] = -2 N_V.extend([i]) if DW[index][nw] == 0: DW[index][nw] = -1 N_V.extend([i]) #check to see if there is already an atom at that point else: if DV[index][nv] == 1 or DV[index][nv] == -1: #if there isn't one at the other point add it if DW[index][nw] == 0: DW[index][nw] = -1 N_V.extend([i]) else: if DV[index][nv] == 1: DV[index][nv] = 2 if DV[index][nv] == -1: DV[index][nv] = -2 N_V.extend([i]) if DV[index][nv] == 0: DV[index][nv] = 1 N_V.extend([i]) for i in IW: nw, dw = close_neighbors_point(CW,Wn[i],L) nv, dv = close_neighbors_point(CV,Wn[i],L) if dv <= dw: if DV[index][nv] == 1 or DV[index][nv] == -1: if DW[index][nw] == 0: DW[index][nw] = 1 N_W.extend([i]) else: DV[index][nv] = -2 N_W.extend([i]) if DV[index][nv] == 0: DV[index][nv] = -1 N_W.extend([i]) else: if DW[index][nw] == 1 or DW[index][nw] == -1: if DV[index][nv] == 0: DV[index][nv] = -1 N_W.extend([i]) else: DW[index][nw] = 2 N_W.extend([i]) if DW[index][nw] == 0: DW[index][nw] = 1 N_W.extend([i]) #find the atoms that haven't been placed on the lattice yet IV = points.difference(range(Vn.shape[0]),N_V) IW = points.difference(range(Wn.shape[0]),N_W) ## if debug: print 'atoms not added list for debugging' print IV, IW print DW[index] print DV[index] print 'Defect list for lattice' #print out the vacency, substitutions def out_defect(A, index, fid, def_list, C=0): for i in range(A.shape[1]): if A[index][i] == 0: pr = 'vacecy at '+ str(i+C)+ '\n' try: def_list[0].extend(i+C) except: def_list[0].append(i+C) if debug: print pr fid.write(pr) if A[index][i] == -1: pr = 'substitution ' + str(i+C)+ '\n' if debug: print pr fid.write(pr) if A[index][i] == -2: pr = 'interstitial ' + str(i + C)+ '\n' try: def_list[1].extend(i+C) except: def_list[1].append(i+C) if debug: print pr fid.write(pr) if A[index][i] == 2: pr = 'interstitial ' + str(i + C)+ '\n' try: def_list[1].extend(i+C) except: def_list[1].append(i+C) if debug: print pr fid.write(pr) frame = 'Frame ' + str(k) + '\n' fid.write(frame) def_list = [[],[]] out_defect(DV, index, fid, def_list) out_defect(DW, index, fid, def_list, C = DV.shape[1]) if len(points.difference(def_list[0], master[0])) != 0: vac_count[index] += len(points.difference(def_list[1],master[1])) master[0] = def_list[0] if len(points.difference(def_list[1],master[1])) != 0: int_count[index] += len(points.difference(def_list[1],master[1])) master[1] = def_list[1] #find the atoms that haven't been placed on the lattice yet IV = points.difference(range(V.shape[1]),N_V) IW = points.difference(range(W.shape[1]),N_W) # Identify the defects surrounding each point #The total number of frames we are going to look at # Find the number of defects in each frame count = 0 substitutions = [] vacancies = [] intersticial = [] def count_def(A,check): count = 0 for i in A: if i == check: count +=1 return count def count_ldef(A,check): count = 0 for i in A: if i < check: count +=1 return count def count_adef(A,check): count = 0 for i in A: if abs(i) == check: count +=1 return count for k in range(DW.shape[0]): #find the points that are nearest neighbor that are different substitutions.append(count_ldef(DW[k],0)+count_ldef(DV[k],0)) vacancies.append(count_def(DW[k],0)+count_def(DV[k],0)) intersticial.append(count_adef(DW[k],2.0)+count_adef(DV[k],2.0)) print 'substitions' print substitutions print 'vacancies' print vacancies print 'interstitials' print intersticial util.pickle_dump(DV,'DV_s.pkl') util.pickle_dump(DW,'DW_s.pkl') util.pickle_dump([substitutions, vacancies, intersticial, x],'plot_def_s.pkl') pyplot.plot3(x, substitutions, x, particle_frame, x, intersticial, label1='substitutions',label2='number of particles',label3='intersticial', save='defects_per_frame_surface',showleg=True) #pyplot.plot(x, vac_count, save='defect_vac_diffusion_count') #pyplot.plot(x, int_count, save='defect_int_diffusion_count') return DV, DW, [substitutions, vacancies, intersticial, x]
def int_msd(DV,DW,V,W,CV,CW,L): #we create an active_int which keeps track of the index at the current frame #we creat an all_int which keeps track of the intersticals #whenever the number of intersticials decerases we find the one that #vanished and add its trajectory to all_int #whenever teh number of intersticials increases we add a new trajectory #to the active ints interstitials = np.zeros((DV.shape[0],1,3)) last_index = 0 active_int = [] all_int = [] ########################## # Functions ######################## def find_nearest(point,CW,CV,L): current = 100 for i in CW: d = points.dist(point,i,L)[0] if d < current: current = d new_point = i for i in CV: d = points.dist(point,i,L)[0] if d < current: current = d new_point = i return new_point #place the intersticial into the active_int array def place_int(new_point, active_int,list_index,L): current = 100 for i in range(len(active_int)): if i not in list_index: d = points.dist(new_point,active_int[i][-1],L)[0] if d < current: current = d index = i try: index active_int[index].append(new_point) list_index.append(index) return index except: pass #find the distance between the new points and the active_int array def find_int_dist(new_point, active_int,L): current = 100 for i in range(len(active_int)): d = points.dist(new_point,active_int[i][-1],L)[0] if d < current: current = d return current def find_point(DV,CV,CW,i): if i < DV.shape[1]: new_point = CV[i] if i >= DV.shape[1]: new_point = CW[i-DV.shape[1]] return new_point ########################################### # Logic: Run through once ########################################### index = np.where(abs(DV[0]) == 2)[0] index = np.append(index, np.where(abs(DW[0]) == 2)[0] + DW.shape[1]) #xtrack keeps track of the frame when an interstitial is added x_track = [] for i in index: new_point = find_point(DV,CV,CW,i) active_int.append([new_point]) x_track.append(0) #find the point in the activt_interstitals and append it to the #the correct index last_index = index.shape[0] ########################################### #loop through the rest ########################################### for k in range(1,len(DW)): print k #find the points that are nearest neighbor that are different index = np.where(abs(DV[k]) == 2)[0] index = np.append(index, np.where(abs(DW[k]) == 2)[0] + DW.shape[1]) list_index = [] #sometimes there are more than one interstitial #we only want to make sure we follow the same one #so we look for the closest to that last position #if there is the same amount of intersticials if index.shape[0] == last_index: for i in index: new_point = find_point(DV,CV,CW,i) #find the point in the activt_interstitals and append it to the #the correct index place_int(new_point, active_int, list_index, L) #if an interstitical has been created if index.shape[0] > last_index: new_int = [] dist_int = [] for i in index: new_point = find_point(DV,CV,CW,i) #find the new interstitical dist_int.append(find_int_dist(new_point,active_int,L)) new_int.append(new_point) #find the index of the max point max_index = dist_int.index(max(dist_int)) for i in range(len(new_int)): if i != max_index: place_int(new_int[i], active_int,list_index, L) active_int.append([new_int[max_index]]) x_track.append(k) #if an intersticial has been destroyed if index.shape[0] < last_index: ann_int = [] dist_int = [] placed = [] for i in index: new_point = find_point(DV,CV,CW,i) #find the annihlated interstitical dist_int.append(find_int_dist(new_point,active_int,L)) ann_int.append(new_point) #find the index of the max point max_index = dist_int.index(max(dist_int)) for i in range(len(ann_int)): placed.append(place_int(ann_int[i],active_int,list_index,L)) #find the point to be removed dif = points.difference(range(len(active_int)),placed) #add the point to the list of all intersticials for i in dif: all_int.append(active_int[i]) #remove the point from the active list count = 0 for i in dif: del active_int[i-count] count+=1 last_index = index.shape[0] for i in active_int: all_int.append(i) print 'len int' print len(all_int) final_int = [] final_x = [] #lets filter out the short lived interstitials for i in range(len(all_int)): if len(all_int[i]) > 1: final_int.append(all_int[i]) final_x.append(x_track[i]) print len(final_int) print 'print x' print len(final_x) from MD.analysis.msd import msd_no_drift as msd #Find the msd of each interstitial msd_all = [] x_all = [] for i in final_int: A = np.zeros((len(i),1,3)) for j in range(len(i)): A[j][0][0] = i[j][0] A[j][0][1] = i[j][1] A[j][0][2] = i[j][2] x, msds = msd(A, L) msd_all.append(msds) x_all.append(x) #now we need to relate the msd to the point in time the interstital was #active msd_total = np.zeros(DV.shape[0]) for i,k in enumerate(final_x): print k for j in range(len(msd_all[i])): if k+j < msd_total.shape[0]: msd_total[k+j] += msd_all[i][j] #for i in range(1,msd_total.shape[0]): # msd_total[i] += msd_total[i-1] msd_total = msd_total**0.5 x = range(msd_total.shape[0]) util.pickle_dump([x,msd_total],'msd_int.pkl') pyplot.plot(x,msd_total,xlabel='time',ylabel='msd',save='MSD_int')