def make_polycrystal(grns,uc,n1,n2,n3,two_dim=False): """ THIS ROUTINE IS NOT THAT UNIVERSAL. Each grain has to have neighboring grains within a supercell, otherwise there will be some unexpecting grain boundries. In order to do so, the system should be large enough and the number of grains should be large enough. """ #...Calc the minimum bond distance in unit cell and use it as penetration depth dmin = 1.0e+30 for i in range(uc.num_atoms()-1): for j in range(i+1,uc.num_atoms()): dij = uc.get_distance(i,j) dmin = min(dij,dmin) print(' Minimum bond distance in the unitcell: ',dmin) dmin = dmin *DMIN_RATE penetration_depth = dmin*2 print(' Minimum bond distance allowed in the new system: ',dmin) sv,nsv= shift_vector(two_dim) # print(' nsv =',nsv) # for i in range(nsv): # print(' i,sv[i]=',i,sv[i]) nsys= NAPSystem(specorder=uc.specorder) nsys.set_lattice(uc.alc,uc.a1*n1,uc.a2*n2,uc.a3*n3) hmat = nsys.get_hmat() hmati = nsys.get_hmat_inv() nmax = n1*n2*n3 *uc.num_atoms() sidsl = np.zeros(nmax,dtype=int) symsl = [] possl = np.zeros((nmax,3)) velsl = np.zeros((nmax,3)) frcsl = np.zeros((nmax,3)) ix0 = -n1/2-1 ix1 = n1/2+2 iy0 = -n2/2-1 iy1 = n2/2+2 iz0 = -n3/2-1 iz1 = n3/2+2 if two_dim: if n3 != 1: raise ValueError('n3 should be 1 in case two_dim is ON.') iz0 = 0 iz1 = 1 print(' x range = ',ix0,ix1) print(' y range = ',iy0,iy1) print(' z range = ',iz0,iz1) inc = 0 for ig in range(len(grns)): grain= grns[ig] rmat= grain.rmat # Rotation matrix of the grain pi= grain.point # Grain center in reduced coordinate api= np.dot(hmat,pi) # Grain center in Cartessian coordinate print(' grain-ID = ',ig+1) for ix in range(ix0,ix1): # print('ix=',ix) for iy in range(iy0,iy1): for iz in range(iz0,iz1): for m in range(uc.num_atoms()): sidt = uc.get_atom_attr(m,'sid') rt= np.zeros((3,)) pm = uc.get_atom_attr(m,'pos') rt[0]= (pm[0]+ix)/n1 rt[1]= (pm[1]+iy)/n2 rt[2]= (pm[2]+iz)/n3 #...rt to absolute position art= np.dot(hmat,rt) #...Rotate ari= np.dot(rmat,art) #...Shift origin to the grain center ari[0]= ari[0]+api[0] ari[1]= ari[1]+api[1] ari[2]= ari[2]+api[2] #...check distance from all the grain points di= distance(ari,api,two_dim) isOutside= False for jg in range(len(grns)): gj= grns[jg] for isv in range(nsv): pj= gj.point if jg == ig: if not two_dim and isv == 13: continue elif two_dim and isv == 4: continue svi= sv[isv] pj= pj +svi apj = np.dot(hmat,pj) dj= distance(ari,apj,two_dim) if dj +penetration_depth < di: # Allow some penetration here isOutside= True break if isOutside: break if isOutside: break #...here ri is inside this grain, register it #...Cartessian coord to reduced coord ri = np.dot(hmati,ari) ri[0]= pbc(ri[0]) ri[1]= pbc(ri[1]) ri[2]= pbc(ri[2]) sidsl[inc] = sidt possl[inc] = ri velsl[inc,:] = 0.0 frcsl[inc,:] = 0.0 symsl.append(nsys.specorder[sidt-1]) inc += 1 if inc > nmax: raise ValueError('inc > nmax') #...Create filled arrays from non-filled ones poss = np.array(possl[:inc]) vels = np.array(velsl[:inc]) frcs = np.array(frcsl[:inc]) nsys.add_atoms(symsl,poss,vels,frcs) #...remove too-close atoms at the grain boundaries print(' Making pair list in order to remove close atoms...') print(' Number of atoms: ',nsys.num_atoms()) nsys.make_pair_list(RCUT) nsys.write('POSCAR_orig') short_pairs = [] # dmin2= dmin**2 # xij= np.zeros((3,)) print(' Making the list of SHORT pairs...') for ia in range(nsys.num_atoms()): lst= nsys.get_atom_attr(ia,'lspr') for j in range(len(lst)): ja= lst[j] if ja > ia: continue dij = nsys.get_distance(ia,ja) if dij < dmin: short_pairs.append((ia,ja,dij)) print(' Number of short pairs: ',len(short_pairs)) #...Remove only relevant atoms, not all the atoms in the short_pairs. ls_remove = [] ls_not_remove = [] for pair in short_pairs: ia = pair[0] ja = pair[1] if ia not in ls_not_remove and ja not in ls_not_remove: ls_remove.append(ia) ls_not_remove.append(ja) elif ia not in ls_not_remove: ls_remove.append(ia) elif ja not in ls_not_remove: ls_remove.append(ja) else: # Both atoms are already in not_remove list, which should be avoided. ls_not_remove.remove(ia) ls_remove.append(ia) ls_not_remove.append(ja) #...Remove double registered IDs ls_remove = uniq(ls_remove) print(' Number of to be removed atoms: ',len(ls_remove)) nsys.remove_atoms(*ls_remove) return nsys
def make_polycrystal(grns, uc, n1, n2, n3, two_dim=False): """ THIS ROUTINE IS NOT THAT UNIVERSAL. Each grain has to have neighboring grains within a supercell, otherwise there will be some unexpecting grain boundries. In order to do so, the system should be large enough and the number of grains should be large enough. """ #...Calc the minimum bond distance in unit cell and use it as penetration depth dmin = 1.0e+30 for i in range(uc.num_atoms() - 1): for j in range(i + 1, uc.num_atoms()): dij = uc.get_distance(i, j) dmin = min(dij, dmin) print(' Minimum bond distance in the unitcell: ', dmin) dmin = dmin * DMIN_RATE penetration_depth = dmin * 2 print(' Minimum bond distance allowed in the new system: ', dmin) sv, nsv = shift_vector(two_dim) # print(' nsv =',nsv) # for i in range(nsv): # print(' i,sv[i]=',i,sv[i]) system = NAPSystem(specorder=uc.specorder) system.set_lattice(uc.alc, uc.a1 * n1, uc.a2 * n2, uc.a3 * n3) hmat = np.zeros((3, 3)) hmat[0] = system.a1 * system.alc hmat[1] = system.a2 * system.alc hmat[2] = system.a3 * system.alc hmati = np.linalg.inv(hmat) ix0 = -n1 / 2 - 1 ix1 = n1 / 2 + 2 iy0 = -n2 / 2 - 1 iy1 = n2 / 2 + 2 iz0 = -n3 / 2 - 1 iz1 = n3 / 2 + 2 if two_dim: if n3 != 1: raise ValueError('n3 should be 1 in case two_dim is ON.') iz0 = 0 iz1 = 1 print(' x range = ', ix0, ix1) print(' y range = ', iy0, iy1) print(' z range = ', iz0, iz1) for ig in range(len(grns)): grain = grns[ig] rmat = grain.rmat # Rotation matrix of the grain pi = grain.point # Grain center in reduced coordinate api = np.dot(hmat, pi) # Grain center in Cartessian coordinate print(' grain-ID = ', ig + 1) for ix in range(ix0, ix1): # print('ix=',ix) for iy in range(iy0, iy1): for iz in range(iz0, iz1): for m in range(len(uc.atoms)): rt = np.zeros((3, )) rt[0] = (uc.atoms[m].pos[0] + ix) / n1 rt[1] = (uc.atoms[m].pos[1] + iy) / n2 rt[2] = (uc.atoms[m].pos[2] + iz) / n3 #...rt to absolute position art = np.dot(hmat, rt) #...Rotate ari = np.dot(rmat, art) #...Shift origin to the grain center ari[0] = ari[0] + api[0] ari[1] = ari[1] + api[1] ari[2] = ari[2] + api[2] #...check distance from all the grain points di = distance(ari, api, two_dim) isOutside = False for jg in range(len(grns)): gj = grns[jg] for isv in range(nsv): pj = gj.point if jg == ig: if not two_dim and isv == 13: continue elif two_dim and isv == 4: continue svi = sv[isv] pj = pj + svi apj = np.dot(hmat, pj) dj = distance(ari, apj, two_dim) if dj + penetration_depth < di: # Allow some penetration here isOutside = True break if isOutside: break if isOutside: break #...here ri is inside this grain, register it atom = Atom() #...Cartessian coord to reduced coord ri = np.dot(hmati, ari) ri[0] = pbc(ri[0]) ri[1] = pbc(ri[1]) ri[2] = pbc(ri[2]) atom.set_pos(ri[0], ri[1], ri[2]) atom.set_symbol(uc.atoms[m].symbol) system.add_atom(atom) #...remove too-close atoms at the grain boundaries print(' Making pair list in order to remove close atoms...') print(' Number of atoms: ', system.num_atoms()) system.make_pair_list(RCUT) system.write('POSCAR_orig') short_pairs = [] # dmin2= dmin**2 # xij= np.zeros((3,)) print(' Making the list of SHORT pairs...') for ia in range(system.num_atoms()): # ai= system.atoms[ia] # pi= ai.pos nlst = system.nlspr[ia] lst = system.lspr[ia] for j in range(nlst): ja = lst[j] if ja > ia: continue dij = system.get_distance(ia, ja) if dij < dmin: short_pairs.append((ia, ja, dij)) # aj= system.atoms[ja] # pj= aj.pos # xij[0]= pj[0]-pi[0] -anint(pj[0]-pi[0]) # xij[1]= pj[1]-pi[1] -anint(pj[1]-pi[1]) # xij[2]= pj[2]-pi[2] -anint(pj[2]-pi[2]) # xij= np.dot(hmat,xij) # d2= xij[0]**2 +xij[1]**2 +xij[2]**2 # if d2 < dmin2: # if not ia in ls_remove: # ls_remove.append(ia) # elif not ja in ls_remove: # ls_remove.append(ja) # print('ia,len(ls_remove)=',ia,len(ls_remove)) print(' Number of short pairs: ', len(short_pairs)) #...Remove only relevant atoms, not all the atoms in the short_pairs. ls_remove = [] ls_not_remove = [] for pair in short_pairs: ia = pair[0] ja = pair[1] if ia not in ls_not_remove and ja not in ls_not_remove: ls_remove.append(ia) ls_not_remove.append(ja) elif ia not in ls_not_remove: ls_remove.append(ia) elif ja not in ls_not_remove: ls_remove.append(ja) else: # Both atoms are already in not_remove list, which should be avoided. ls_not_remove.remove(ia) ls_remove.append(ia) ls_not_remove.append(ja) #...Remove double registered IDs ls_remove = uniq(ls_remove) print(' Number of to be removed atoms: ', len(ls_remove)) # print(' Number of to be removed atoms: ',len(ls_remove)) #...one of two will survive # print(' One of two too-close atoms will survive...') # count= [ ls_remove.count(ls_remove[i]) for i in range(len(ls_remove))] # for i in range(0,len(ls_remove),2): # if count[i] > count[i+1]: # ls_remove[i+1]= -1 # elif count[i] < count[i+1]: # ls_remove[i]= -1 # else: # n= int(random()*2.0) # 0 or 1 # ls_remove[i+n]= -1 ls_remove.sort() # for ia in range(len(ls_remove)-1,-1,-1): # n= ls_remove[ia] # if ia != len(ls_remove)-1: # if n == nprev: continue # system.atoms.pop(n) # nprev= n for i in reversed(range(len(ls_remove))): ia = ls_remove[i] system.atoms.pop(ia) return system
def make_polycrystal(grns,uc,n1,n2,n3,two_dim=False): """ THIS ROUTINE IS NOT THAT UNIVERSAL. Each grain has to have neighboring grains within a supercell, otherwise there will be some unexpecting grain boundries. In order to do so, the system should be large enough and the number of grains should be large enough. """ #...Calc the minimum bond distance in unit cell and use it as penetration depth dmin = 1.0e+30 for i in range(uc.num_atoms()-1): for j in range(i+1,uc.num_atoms()): dij = uc.get_distance(i,j) dmin = min(dij,dmin) print(' Minimum bond distance in the unitcell: ',dmin) dmin = dmin *DMIN_RATE penetration_depth = dmin*2 print(' Minimum bond distance allowed in the new system: ',dmin) sv,nsv= shift_vector(two_dim) # print(' nsv =',nsv) # for i in range(nsv): # print(' i,sv[i]=',i,sv[i]) system= NAPSystem(specorder=uc.specorder) system.set_lattice(uc.alc,uc.a1*n1,uc.a2*n2,uc.a3*n3) hmat= np.zeros((3,3)) hmat[0]= system.a1 *system.alc hmat[1]= system.a2 *system.alc hmat[2]= system.a3 *system.alc hmati= np.linalg.inv(hmat) ix0 = -n1/2-1 ix1 = n1/2+2 iy0 = -n2/2-1 iy1 = n2/2+2 iz0 = -n3/2-1 iz1 = n3/2+2 if two_dim: if n3 != 1: raise ValueError('n3 should be 1 in case two_dim is ON.') iz0 = 0 iz1 = 1 print(' x range = ',ix0,ix1) print(' y range = ',iy0,iy1) print(' z range = ',iz0,iz1) for ig in range(len(grns)): grain= grns[ig] rmat= grain.rmat # Rotation matrix of the grain pi= grain.point # Grain center in reduced coordinate api= np.dot(hmat,pi) # Grain center in Cartessian coordinate print(' grain-ID = ',ig+1) for ix in range(ix0,ix1): # print('ix=',ix) for iy in range(iy0,iy1): for iz in range(iz0,iz1): for m in range(len(uc.atoms)): rt= np.zeros((3,)) rt[0]= (uc.atoms[m].pos[0]+ix)/n1 rt[1]= (uc.atoms[m].pos[1]+iy)/n2 rt[2]= (uc.atoms[m].pos[2]+iz)/n3 #...rt to absolute position art= np.dot(hmat,rt) #...Rotate ari= np.dot(rmat,art) #...Shift origin to the grain center ari[0]= ari[0]+api[0] ari[1]= ari[1]+api[1] ari[2]= ari[2]+api[2] #...check distance from all the grain points di= distance(ari,api,two_dim) isOutside= False for jg in range(len(grns)): gj= grns[jg] for isv in range(nsv): pj= gj.point if jg == ig: if not two_dim and isv == 13: continue elif two_dim and isv == 4: continue svi= sv[isv] pj= pj +svi apj = np.dot(hmat,pj) dj= distance(ari,apj,two_dim) if dj +penetration_depth < di: # Allow some penetration here isOutside= True break if isOutside: break if isOutside: break #...here ri is inside this grain, register it atom= Atom() #...Cartessian coord to reduced coord ri = np.dot(hmati,ari) ri[0]= pbc(ri[0]) ri[1]= pbc(ri[1]) ri[2]= pbc(ri[2]) atom.set_pos(ri[0],ri[1],ri[2]) atom.set_symbol(uc.atoms[m].symbol) system.add_atom(atom) #...remove too-close atoms at the grain boundaries print(' Making pair list in order to remove close atoms...') print(' Number of atoms: ',system.num_atoms()) system.make_pair_list(RCUT) system.write('POSCAR_orig') short_pairs = [] # dmin2= dmin**2 # xij= np.zeros((3,)) print(' Making the list of SHORT pairs...') for ia in range(system.num_atoms()): # ai= system.atoms[ia] # pi= ai.pos nlst= system.nlspr[ia] lst= system.lspr[ia] for j in range(nlst): ja= lst[j] if ja > ia: continue dij = system.get_distance(ia,ja) if dij < dmin: short_pairs.append((ia,ja,dij)) # aj= system.atoms[ja] # pj= aj.pos # xij[0]= pj[0]-pi[0] -anint(pj[0]-pi[0]) # xij[1]= pj[1]-pi[1] -anint(pj[1]-pi[1]) # xij[2]= pj[2]-pi[2] -anint(pj[2]-pi[2]) # xij= np.dot(hmat,xij) # d2= xij[0]**2 +xij[1]**2 +xij[2]**2 # if d2 < dmin2: # if not ia in ls_remove: # ls_remove.append(ia) # elif not ja in ls_remove: # ls_remove.append(ja) # print('ia,len(ls_remove)=',ia,len(ls_remove)) print(' Number of short pairs: ',len(short_pairs)) #...Remove only relevant atoms, not all the atoms in the short_pairs. ls_remove = [] ls_not_remove = [] for pair in short_pairs: ia = pair[0] ja = pair[1] if ia not in ls_not_remove and ja not in ls_not_remove: ls_remove.append(ia) ls_not_remove.append(ja) elif ia not in ls_not_remove: ls_remove.append(ia) elif ja not in ls_not_remove: ls_remove.append(ja) else: # Both atoms are already in not_remove list, which should be avoided. ls_not_remove.remove(ia) ls_remove.append(ia) ls_not_remove.append(ja) #...Remove double registered IDs ls_remove = uniq(ls_remove) print(' Number of to be removed atoms: ',len(ls_remove)) # print(' Number of to be removed atoms: ',len(ls_remove)) #...one of two will survive # print(' One of two too-close atoms will survive...') # count= [ ls_remove.count(ls_remove[i]) for i in range(len(ls_remove))] # for i in range(0,len(ls_remove),2): # if count[i] > count[i+1]: # ls_remove[i+1]= -1 # elif count[i] < count[i+1]: # ls_remove[i]= -1 # else: # n= int(random()*2.0) # 0 or 1 # ls_remove[i+n]= -1 ls_remove.sort() # for ia in range(len(ls_remove)-1,-1,-1): # n= ls_remove[ia] # if ia != len(ls_remove)-1: # if n == nprev: continue # system.atoms.pop(n) # nprev= n for i in reversed(range(len(ls_remove))): ia = ls_remove[i] system.atoms.pop(ia) return system