def make_bcc111(latconst=1.0): """ Make a cell of bcc structure with z along [111]. """ s= NAPSystem(specorder=_default_specorder) #...lattice a1= np.array([ 1.414, 0.0, 0.0 ]) a2= np.array([ 0.0, 2.449, 0.0 ]) a3= np.array([ 0.0, 0.0, 1.732 ]) s.set_lattice(latconst,a1,a2,a3) positions=[(0.00, 0.00, 0.00), (0.00, 0.00, 0.50), (0.00, 0.333, 0.167), (0.00, 0.333, 0.667), (0.00, 0.667, 0.333), (0.00, 0.667, 0.833), (0.50, 0.167, 0.333), (0.50, 0.167, 0.833), (0.50, 0.50, 0.00), (0.50, 0.50, 0.50), (0.50, 0.833, 0.167), (0.50, 0.833, 0.667)] for p in positions: atom= Atom() atom.set_pos(p[0],p[1],p[2]) atom.set_symbol(_default_specorder[0]) s.add_atom(atom) return s
def main(args): files = args['FILES'] files.sort(key=get_key, reverse=True) nskip = int(args['--skip']) del files[:nskip] prefix = args['--prefix'] out4fp = args['--out4fp'] nsum = 0 volsum = 0.0 asum= bsum= csum= 0.0 alpsum= betsum= gmmsum= 0.0 for i,fi in enumerate(files): try: nsys = NAPSystem(fname=fi) volsum += nsys.volume() a,b,c,alpha,beta,gamma = nsys2lat(nsys) asum += a bsum += b csum += c alpsum += alpha betsum += beta gmmsum += gamma nsum += 1 except Exception as e: print('Failed {0:s} '.format(fi)) pass if nsum < 1: raise ValueError('Something went wrong! nsum<1') vol = volsum /nsum a = asum/nsum b = bsum/nsum c = csum/nsum alpha = alpsum/nsum beta = betsum/nsum gamma = gmmsum/nsum if out4fp: with open(prefix+'.vol','w') as f: f.write('# Volume\n') f.write(' 1 1.0\n') f.write('{0:15.3f}\n'.format(vol)) with open(prefix+'.lat','w') as f: f.write('# Lattice parameters\n') f.write(' 6 1.0\n') f.write(' {0:10.3f} {1:10.3f} {2:10.3f}'.format(a,b,c) +' {0:10.3f} {1:10.3f} {2:10.3f}\n'.format(alpha,beta,gamma)) else: with open(prefix+'.vol','w') as f: f.write('{0:15.3f}\n'.format(vol)) with open(prefix+'.lat','w') as f: f.write(' {0:10.3f} {1:10.3f} {2:10.3f}'.format(a,b,c) +' {0:10.3f} {1:10.3f} {2:10.3f}\n'.format(alpha,beta,gamma)) print(' Wrote {0:s}.vol {0:s}.lat'.format(prefix))
def main(args): infname = args['FILE'] aSys = NAPSystem(fname=infname) natm = len(aSys.atoms) outfname = infname + ".voro" f = open(outfname, 'w') alc = aSys.alc ax = aSys.a1[0] ay = aSys.a2[1] az = aSys.a3[2] for ia in range(natm): pos = aSys.atoms[ia].pos f.write(' {0:4d}'.format(ia + 1)) f.write(' {0:10.3f}'.format(alc * ax * pos[0])) f.write(' {0:10.3f}'.format(alc * ay * pos[1])) f.write(' {0:10.3f}'.format(alc * az * pos[2])) f.write('\n') f.close() print('Use voro++ as follows:') cmd= 'voro++ -p -c " %i %q %v %s %A"' \ +' 0.0 {0:5.1f}'.format(alc*ax) \ +' 0.0 {0:5.1f}'.format(alc*ay) \ +' 0.0 {0:5.1f}'.format(alc*az) \ +' {0:s}'.format(outfname) print('$ ' + cmd) os.system(cmd)
def make_sc(latconst=1.0): """ Make a cell of simple cubic structure. """ s= NAPSystem(specorder=_default_specorder) #...lattice a1= np.array([ 1.0, 0.0, 0.0 ]) a2= np.array([ 0.0, 1.0, 0.0 ]) a3= np.array([ 0.0, 0.0, 1.0 ]) s.set_lattice(latconst,a1,a2,a3) p=[0.00, 0.00, 0.00] atom= Atom() atom.set_pos(p[0],p[1],p[2]) atom.set_symbol(_default_specorder[0]) s.add_atom(atom) return s
def make_bcc(latconst=1.0,specorder=_default_specorder): """ Make a cell of bcc structure with z along [001]. """ s= NAPSystem(specorder=specorder) #...lattice a1= np.array([ 1.0, 0.0, 0.0 ]) a2= np.array([ 0.0, 1.0, 0.0 ]) a3= np.array([ 0.0, 0.0, 1.0 ]) s.set_lattice(latconst,a1,a2,a3) positions=[(0.00, 0.00, 0.00), (0.50, 0.50, 0.50)] for p in positions: atom= Atom() atom.set_pos(p[0],p[1],p[2]) atom.set_symbol(specorder[0]) s.add_atom(atom) return s
def make_hcp(latconst=1.0): """ Make a cell of hcp structure. """ s= NAPSystem(specorder=_default_specorder) #...lattice a1= np.array([ 1.0, 0.0, 0.0 ]) a2= np.array([-0.5, np.sqrt(3.0)/2, 0.0 ]) a3= np.array([ 0.0, 0.0, 1.633 ]) s.set_lattice(latconst,a1,a2,a3) positions=[(0.00, 0.00, 0.00), (1.0/3, 2.0/3, 0.50)] for p in positions: atom= Atom() atom.set_pos(p[0],p[1],p[2]) atom.set_symbol(_default_specorder[0]) s.add_atom(atom) return s
def get_list_high_energy(gsmpls,threshold): emin = 1e+30 highsmpls = [] ergs = [] for i,s in enumerate(gsmpls): smpldir = s[0] erg = s[1] #atoms = read(smpldir+'/POSCAR',format='vasp') atoms = NAPSystem(fname=smpldir+"/pos",format='pmd') natm = atoms.num_atoms() erg /= natm ergs.append(erg) emin = min(erg,emin) for i,s in enumerate(gsmpls): smpldir = s[0] erg = ergs[i] if erg-emin > threshold: highsmpls.append(smpldir) return highsmpls
def get_list_high_energy(gsmpls,threshold): emin = 1e+30 highsmpls = [] ergs = [] for i,s in enumerate(gsmpls): smpldir = s[0] erg = s[1] #atoms = read(smpldir+'/POSCAR',format='vasp') atoms = NAPSystem(fname=smpldir+"/pos",ffmt='pmd') natm = atoms.num_atoms() erg /= natm ergs.append(erg) emin = min(erg,emin) for i,s in enumerate(gsmpls): smpldir = s[0] erg = ergs[i] if erg-emin > threshold: highsmpls.append(smpldir) return highsmpls
def xdatcar2poscars(fname='XDATCAR', nskip=1): f = open(fname, 'r') isys = 0 while True: #....."Direct configuration= #" try: line = f.readline() #...Check configuration if not "Direct configuration=" in line: alc, a1, a2, a3, species, num_atoms = read_header(f) # print(species) # print(num_atoms) natm = 0 for n in num_atoms: natm += n continue elif len(line.split()) == 0: break except: break #...Skip reading this configuration if requested if not isys % nskip == 0: for i in range(natm): f.readline() isys += 1 continue #...Following lines: atom positions #...Make a NAPSystem from these information and write to POSCAR_##### nsys = NAPSystem(specorder=species) nsys.set_lattice(alc, a1, a2, a3) inc = 0 for inum, ni in enumerate(num_atoms): for j in range(ni): inc += 1 ai = Atom() ai.set_id(inc) ai.set_symbol(species[inum]) data = f.readline().split() # print(data) if not is_number(data[0]): raise ValueError('Wrong format?') x1, x2, x3 = [float(x) for x in data[0:3]] ai.set_pos(x1, x2, x3) ai.set_vel(0.0, 0.0, 0.0) nsys.add_atom(ai) foutname = 'POSCAR_{0:05d}'.format(isys) print(' >>> ' + foutname) nsys.write_POSCAR(fname=foutname) isys += 1 f.close() return None
def make_honeycomb(latconst=1.0): """ Make a cell of 2D honeycomb structure. """ s= NAPSystem(specorder=_default_specorder) #...lattice a1= np.array([ 1.0, 0.0, 0.0 ]) a2= np.array([ 0.0, 1.5, 0.0 ]) a3= np.array([ 0.0, 0.0, np.sqrt(3.0) ]) s.set_lattice(latconst,a1,a2,a3) positions=[(0.00, 0.50, 0.00), (0.50, 0.50, 1./6), (0.50, 0.50, 0.50), (0.00, 0.50, 0.5 +1.0/6)] for p in positions: atom= Atom() atom.set_pos(p[0],p[1],p[2]) atom.set_symbol(_default_specorder[0]) s.add_atom(atom) return s
def make_wurtzite(latconst=1.0, specorder=None, celltype='conventional'): """ Make a cell of wurtzite structure. - celltype: conventional or primitive """ if specorder is None: specorder = ['Ga', 'N'] if len(specorder) < 2: specorder = ['Ga', 'N'] print('Since len(specorder) < 2, specorder is reset to ', specorder) s = NAPSystem(specorder=specorder) if celltype[0] == 'c': #...conventional cell a1 = np.array([1.00, 0.00, 0.00]) a2 = np.array([0.00, np.sqrt(3.0), 0.00]) a3 = np.array([0.00, 0.00, 1.633]) s.set_lattice(latconst, a1, a2, a3) poss = [ [0.00, 0.00, 0.00], [0.50, 0.50, 0.00], [0.50, 0.5 / 3, 0.50], [0.00, 0.5 / 3 + 0.5, 0.50], [0.50, 0.5 / 3, 0.125], [0.00, 0.5 / 3 + 0.5, 0.125], [0.00, 0.00, 0.625], [0.50, 0.50, 0.625], ] symbols = [ specorder[0] if i < 4 else specorder[1] for i in range(len(poss)) ] elif cenlltype[0] == 'p': #...primitive cell a1 = np.array([1.0, 0.0, 0.0]) a2 = np.array([-0.5, np.sqrt(3.0) / 2, 0.0]) a3 = np.array([0.0, 0.0, 1.633]) s.set_lattice(latconst, a1, a2, a3) poss = [ [0.00, 0.00, 0.00], [2.0 / 3, 1.0 / 3, 0.125], [2.0 / 3, 1.0 / 3, 0.50], [0.00, 0.00, 0.625], ] symbols = [ specorder[0] if i < 2 else specorder[1] for i in range(len(poss)) ] vels = [[0., 0., 0.] for i in range(len(poss))] frcs = [[0., 0., 0.] for i in range(len(poss))] s.add_atoms(symbols, poss, vels, frcs) return s
def make_nacl(latconst=1.0): specorder = ['Na', 'Cl'] s = NAPSystem(specorder=specorder) #...lattice a1 = np.array([1.0, 0.0, 0.0]) a2 = np.array([0.0, 1.0, 0.0]) a3 = np.array([0.0, 0.0, 1.0]) s.set_lattice(latconst, a1, a2, a3) positions = [ (0.00, 0.00, 0.00), (0.50, 0.00, 0.00), (0.00, 0.50, 0.00), (0.00, 0.00, 0.50), (0.50, 0.50, 0.00), (0.50, 0.00, 0.50), (0.00, 0.50, 0.50), (0.50, 0.50, 0.50), ] species = ['Na', 'Cl', 'Cl', 'Cl', 'Na', 'Na', 'Na', 'Cl'] for i, p in enumerate(positions): atom = Atom() atom.set_pos(p[0], p[1], p[2]) atom.set_symbol(species[i]) s.add_atom(atom) return s
def structure2aSys(structure,idoffset=1): """ Converts Structure object of pymatgen to NAPSystem object in nap. Args: structure (Structure): pymatgen Structure object to be converted to NAPSystem object.. Returns: aSys (NAPSystem): """ lattice= structure.lattice alc= 1.0 a1= np.array(lattice.matrix[0]) a2= np.array(lattice.matrix[1]) a3= np.array(lattice.matrix[2]) #... rescale a? vectors a1= a1/alc a2= a2/alc a3= a3/alc aSys= NAPSystem() aSys.set_lattice(alc,a1,a2,a3) for ia in range(structure.num_sites): ai= Atom() si= structure[ia] crd= si.frac_coords ai.set_pos(crd[0],crd[1],crd[2]) sid= structure.symbol_set.index(si.species_string)+idoffset ai.set_sid(sid) ai.set_id(ia+1) aSys.add_atom(ai) return aSys
def make_zincblend(latconst=1.0, specorder=None): """ Make a cell of diamond structure. """ if specorder is None: specorder = ['Ga', 'N'] if len(specorder) < 2: specorder = ['Ga', 'N'] print('Since len(specorder) < 2, specorder is reset to ', specorder) s = NAPSystem(specorder=specorder) #...lattice a1 = np.array([1.0, 0.0, 0.0]) a2 = np.array([0.0, 1.0, 0.0]) a3 = np.array([0.0, 0.0, 1.0]) s.set_lattice(latconst, a1, a2, a3) poss = [[0.00, 0.00, 0.00], [0.50, 0.50, 0.00], [0.50, 0.00, 0.50], [0.00, 0.50, 0.50], [0.25, 0.25, 0.25], [0.75, 0.75, 0.25], [0.75, 0.25, 0.75], [0.25, 0.75, 0.75]] symbols = [ specorder[0] if i < 4 else specorder[1] for i in range(len(poss)) ] vels = [[0., 0., 0.] for i in range(len(poss))] frcs = [[0., 0., 0.] for i in range(len(poss))] s.add_atoms(symbols, poss, vels, frcs) return s
def make_nacl(latconst=1.0, specorder=None): if specorder is None: specorder = ['Na', 'Cl'] if len(specorder) < 2: specorder = ['Na', 'Cl'] print('Since len(specorder) < 2, specorder is reset to ', specorder) s = NAPSystem(specorder=specorder) #...lattice a1 = np.array([1.0, 0.0, 0.0]) a2 = np.array([0.0, 1.0, 0.0]) a3 = np.array([0.0, 0.0, 1.0]) s.set_lattice(latconst, a1, a2, a3) poss = [ [0.00, 0.00, 0.00], [0.50, 0.00, 0.00], [0.00, 0.50, 0.00], [0.00, 0.00, 0.50], [0.50, 0.50, 0.00], [0.50, 0.00, 0.50], [0.00, 0.50, 0.50], [0.50, 0.50, 0.50], ] symbols = ['Na', 'Cl', 'Cl', 'Cl', 'Na', 'Na', 'Na', 'Cl'] vels = [[0., 0., 0.] for i in range(len(poss))] frcs = [[0., 0., 0.] for i in range(len(poss))] s.add_atoms(symbols, poss, vels, frcs) return s
def from_ase(atoms, specorder=None): """ Convert ASE Atoms object to NAPSystem object. """ spcorder = [] if specorder is not None: spcorder = specorder symbols = atoms.get_chemical_symbols() spos = atoms.get_scaled_positions() vels = atoms.get_velocities() cell = atoms.get_cell() celli = np.linalg.inv(cell) if spos is None: raise ValueError('ASE atoms object has no atom in it.') #...Initialize and remake self.specorder for s in symbols: if s not in spcorder: spcorder.append(s) nsys = NAPSystem(specorder=spcorder) # nsys = cls(specorder=spcorder) nsys.alc = 1.0 nsys.a1[:] = atoms.cell[0] nsys.a2[:] = atoms.cell[1] nsys.a3[:] = atoms.cell[2] #...First, initialize arrays natm = len(atoms) sids = [0 for i in range(natm)] poss = np.array(spos) if vels is None: vels = np.zeros((natm, 3)) else: vels = np.array(vels) frcs = np.zeros((natm, 3)) #...Create arrays to be installed into nsys.atoms sids = [nsys.specorder.index(si) + 1 for si in symbols] nsys.atoms.sid = sids nsys.atoms[['x', 'y', 'z']] = poss for i in range(len(vels)): vels[i] = np.dot(celli, vels[i]) nsys.atoms[['vx', 'vy', 'vz']] = vels nsys.atoms[['fx', 'fy', 'fz']] = frcs return nsys
def make_2D_triangle(latconst=3.8, size=(1, 1, 1)): """ Make 2D triangle lattice on x-z plane. Note that it is not x-y plane. """ specorder = ['Ar'] s = NAPSystem(specorder=specorder) #...lattice a1 = np.array([1.0, 0.0, 0.0]) a2 = np.array([0.0, 10.0, 0.0]) a3 = np.array([0.0, 0.0, np.sqrt(3.0)]) s.set_lattice(latconst, a1, a2, a3) poss = [[0.00, 0.50, 0.00], [0.50, 0.50, 0.50]] symbol = _default_specorder[0] symbols = [symbol for i in range(len(poss))] vels = [[0., 0., 0.] for i in range(len(poss))] frcs = [[0., 0., 0.] for i in range(len(poss))] s.add_atoms(symbols, poss, vels, frcs) s.repeat(*size) s.add_vacuum(2. * latconst, 0.0, 10. * latconst * np.sqrt(3)) return s
def make_2D_triangle(latconst=3.8, size=(1, 1, 1)): """ Make 2D triangle lattice on x-z plane. Note that it is not x-y plane. """ specorder = ['Ar'] s = NAPSystem(specorder=specorder) #...lattice a1 = np.array([1.0, 0.0, 0.0]) a2 = np.array([0.0, 10.0, 0.0]) a3 = np.array([0.0, 0.0, np.sqrt(3.0)]) s.set_lattice(latconst, a1, a2, a3) positions = [(0.00, 0.50, 0.00), (0.50, 0.50, 0.50)] for p in positions: atom = Atom() atom.set_pos(p[0], p[1], p[2]) atom.set_symbol(specorder[0]) s.add_atom(atom) s.repeat(*size) s.add_vacuum(2. * latconst, 0.0, 10. * latconst * np.sqrt(3)) return s
def make_nacl(latconst=1.0): specorder = ['Na','Cl'] s = NAPSystem(specorder=specorder) #...lattice a1= np.array([ 1.0, 0.0, 0.0 ]) a2= np.array([ 0.0, 1.0, 0.0 ]) a3= np.array([ 0.0, 0.0, 1.0 ]) s.set_lattice(latconst,a1,a2,a3) positions=[(0.00, 0.00, 0.00), (0.50, 0.00, 0.00), (0.00, 0.50, 0.00), (0.00, 0.00, 0.50), (0.50, 0.50, 0.00), (0.50, 0.00, 0.50), (0.00, 0.50, 0.50), (0.50, 0.50, 0.50),] species = ['Na','Cl','Cl','Cl','Na','Na','Na','Cl'] for i,p in enumerate(positions): atom= Atom() atom.set_pos(p[0],p[1],p[2]) atom.set_symbol(species[i]) s.add_atom(atom) return s
def xdatcar2poscars(fname='XDATCAR',nskip=1): f= open(fname,'r') isys = 0 while True: #....."Direct configuration= #" try: line = f.readline() #...Check configuration if not "Direct configuration=" in line: alc,a1,a2,a3,species,num_atoms = read_header(f) # print(species) # print(num_atoms) natm = 0 for n in num_atoms: natm += n continue elif len(line.split()) == 0: break except: break #...Skip reading this configuration if requested if not isys%nskip == 0: for i in range(natm): f.readline() isys += 1 continue #...Following lines: atom positions #...Make a NAPSystem from these information and write to POSCAR_##### nsys = NAPSystem(specorder=species) nsys.set_lattice(alc,a1,a2,a3) inc = 0 for inum,ni in enumerate(num_atoms): for j in range(ni): inc += 1 ai = Atom() ai.set_id(inc) ai.set_symbol(species[inum]) data= f.readline().split() # print(data) if not is_number(data[0]): raise ValueError('Wrong format?') x1,x2,x3 = [ float(x) for x in data[0:3] ] ai.set_pos(x1,x2,x3) ai.set_vel(0.0,0.0,0.0) nsys.add_atom(ai) foutname = 'POSCAR_{0:05d}'.format(isys) print(' >>> '+foutname) nsys.write_POSCAR(fname=foutname) isys += 1 f.close() return None
def prepare(infname='POSCAR', dlt1max=0.01, dlt2max=0.06): #...original system nsys0 = NAPSystem(fname=infname) #orig_atoms = read(infname,format='vasp') orig_atoms = nsys0.to_ase_atoms() #...get deformations fmats = get_deformations(dlt1max, dlt2max) #...deform original system and save to _prefix_##/POSCAR for i, fmat in enumerate(fmats): atoms = orig_atoms.copy() #nsys = copy.deepcopy(nsys0) dname = _prefix + "{0:02d}".format(i) os.system('mkdir -p {}'.format(dname)) print(dname) cell0 = atoms.get_cell() emat = 0.5 * (np.dot(fmat.T, fmat) - np.identity(3)) + np.identity(3) cell = np.dot(emat, cell0) #print(i,emat,cell) # cell = np.dot(cell0,fmat.T) atoms.set_cell(cell, scale_atoms=True) atoms.write(dname + '/POSCAR', format='vasp', vasp5=True, direct=True, sort=False) print('prepare done') print('') print('After performing VASP or pmd calculations in these directories, ' + 'run the following command:') print(' $ python elasticity.py analyze str.ref') print('or') print(' $ python elasticity.py analyze strs.pmd') print('')
def rdf_average(infiles,nr,specorder,dr=0.1,rmax=3.0,pairwise=False): nspcs = len(specorder) agr= np.zeros((nspcs+1,nspcs+1,nr),dtype=float) nsum= 0 for infname in infiles: if not os.path.exists(infname): print("[Error] File, {0}, does not exist !!!".format(infname)) sys.exit() nsys= NAPSystem(fname=infname,specorder=specorder) print(' File =',infname) rd,gr= rdf(nsys,nspcs,dr,rmax,pairwise=pairwise) agr += gr nsum += 1 agr /= nsum return rd,agr
def make_diamond(latconst=1.0): """ Make a cell of diamond structure. """ s= NAPSystem(specorder=_default_specorder) #...lattice a1= np.array([ 1.0, 0.0, 0.0 ]) a2= np.array([ 0.0, 1.0, 0.0 ]) a3= np.array([ 0.0, 0.0, 1.0 ]) s.set_lattice(latconst,a1,a2,a3) positions=[(0.00, 0.00, 0.00), (0.50, 0.50, 0.00), (0.50, 0.00, 0.50), (0.00, 0.50, 0.50), (0.25, 0.25, 0.25), (0.75, 0.75, 0.25), (0.75, 0.25, 0.75), (0.25, 0.75, 0.75)] for p in positions: atom= Atom() atom.set_pos(p[0],p[1],p[2]) atom.set_symbol(_default_specorder[0]) s.add_atom(atom) return s
def read_sample(dirname): """ Read system, energy, forces and stress information of given DIRNAME. """ #...The directory must have erg.ref, frc.ref, strs.ref, and pos files. files = ('pos', 'erg.ref', 'frc.ref', 'strs.ref') for f in files: if not os.path.exists(dirname + '/' + f): raise RuntimeError('The file ' + f + ' does not exist in ' + dirname) #...Read pos first nsys = NAPSystem(fname=dirname + '/pos', ffmt='pmd') erg = read_erg(fname=dirname + '/erg.ref') frcs = read_frc(fname=dirname + '/frc.ref') strs = read_strs(fname=dirname + '/strs.ref') return nsys, erg, frcs, strs
def make_2D_triangle(latconst=3.8,size=(1,1,1)): """ Make 2D triangle lattice on x-z plane. Note that it is not x-y plane. """ specorder = ['Ar'] s = NAPSystem(specorder=specorder) #...lattice a1= np.array([ 1.0, 0.0, 0.0 ]) a2= np.array([ 0.0, 10.0, 0.0 ]) a3= np.array([ 0.0, 0.0, np.sqrt(3.0) ]) s.set_lattice(latconst,a1,a2,a3) positions=[(0.00, 0.50, 0.00), (0.50, 0.50, 0.50)] for p in positions: atom= Atom() atom.set_pos(p[0],p[1],p[2]) atom.set_symbol(specorder[0]) s.add_atom(atom) s.repeat(*size) s.add_vacuum(2.*latconst, 0.0, 10.*latconst*np.sqrt(3)) return s
def write_input(self, atoms, properties=None, system_changes=None): """Write input parameters to in.pmd file.""" FileIOCalculator.write_input(self, atoms, properties, system_changes) if self.label == 'pmd': infname = 'in.pmd' # write_pmd(atoms,fname='pmdini',specorder=self.specorder) nsys = NAPSystem.from_ase_atoms(atoms, specorder=self.specorder) nsys.write_pmd(fname='pmdini') with open(infname, 'w') as f: fmvs, ifmvs = get_fmvs(atoms) for i in range(len(fmvs)): for ii in range(3): if fmvs[i][ii] > 0.1 and not self.dimension[ii]: fmvs[i][ii] = 0.0 f.write(get_input_txt(self.parameters, fmvs))
def make_dimer(distance, latconst, spcs): if distance > latconst / 2: raise ValueError('Lattice size is too small for given distance.') s = NAPSystem(specorder=spcs) a1 = np.array([1.0, 0.0, 0.0]) a2 = np.array([0.0, 1.0, 0.0]) a3 = np.array([0.0, 0.0, 1.0]) s.set_lattice(latconst, a1, a2, a3) pos = [(0.0, 0.0, 0.0), (distance / latconst, 0.0, 0.0)] #...Atom 1 atom = Atom() p = pos[0] atom.set_pos(p[0], p[1], p[2]) atom.set_symbol(spcs[0]) s.add_atom(atom) #...Atom 2 atom = Atom() p = pos[1] atom.set_pos(p[0], p[1], p[2]) atom.set_symbol(spcs[1]) s.add_atom(atom) return s
def make_sc(latconst=1.0): """ Make a cell of simple cubic structure. """ s = NAPSystem(specorder=_default_specorder) #...lattice a1 = np.array([1.0, 0.0, 0.0]) a2 = np.array([0.0, 1.0, 0.0]) a3 = np.array([0.0, 0.0, 1.0]) s.set_lattice(latconst, a1, a2, a3) p = [0.00, 0.00, 0.00] atom = Atom() atom.set_pos(p[0], p[1], p[2]) atom.set_symbol(_default_specorder[0]) s.add_atom(atom) return s
def adf_average(infiles, dang=1.0, rcut=3.0, triplets=[], no_average=False, specorder=None): na = int(180.0 / dang) + 1 aadf = np.zeros((len(triplets), na), dtype=float) nsum = 0 for infname in infiles: if not os.path.exists(infname): print("[Error] File, {0}, does not exist !!!".format(infname)) sys.exit() nsys = NAPSystem(fname=infname, specorder=specorder) print(' File = ', infname) angd, df, n = adf(nsys, dang, rcut, triplets) aadf += df nsum += n if not no_average: aadf /= nsum return angd, aadf
def make_hcp(latconst=1.0): """ Make a cell of hcp structure. """ s = NAPSystem(specorder=_default_specorder) #...lattice a1 = np.array([1.0, 0.0, 0.0]) a2 = np.array([-0.5, np.sqrt(3.0) / 2, 0.0]) a3 = np.array([0.0, 0.0, 1.633]) s.set_lattice(latconst, a1, a2, a3) positions = [(0.00, 0.00, 0.00), (1.0 / 3, 2.0 / 3, 0.50)] for p in positions: atom = Atom() atom.set_pos(p[0], p[1], p[2]) atom.set_symbol(_default_specorder[0]) s.add_atom(atom) return s
def make_sc(latconst=1.0): """ Make a cell of simple cubic structure. """ s = NAPSystem(specorder=_default_specorder) #...lattice a1 = np.array([1.0, 0.0, 0.0]) a2 = np.array([0.0, 1.0, 0.0]) a3 = np.array([0.0, 0.0, 1.0]) s.set_lattice(latconst, a1, a2, a3) symbol = _default_specorder[0] symbols = [symbol] poss = [[0.00, 0.00, 0.00]] vels = [[0., 0., 0.]] frcs = [[0., 0., 0.]] s.add_atoms(symbols, poss, vels, frcs) return s
def make_bcc(latconst=1.0, specorder=_default_specorder): """ Make a cell of bcc structure with z along [001]. """ s = NAPSystem(specorder=specorder) #...lattice a1 = np.array([1.0, 0.0, 0.0]) a2 = np.array([0.0, 1.0, 0.0]) a3 = np.array([0.0, 0.0, 1.0]) s.set_lattice(latconst, a1, a2, a3) positions = [(0.00, 0.00, 0.00), (0.50, 0.50, 0.50)] for p in positions: atom = Atom() atom.set_pos(p[0], p[1], p[2]) atom.set_symbol(specorder[0]) s.add_atom(atom) return s
def make_hcp(latconst=1.0): """ Make a cell of hcp structure. """ s = NAPSystem(specorder=_default_specorder) #...lattice a1 = np.array([1.0, 0.0, 0.0]) a2 = np.array([-0.5, np.sqrt(3.0) / 2, 0.0]) a3 = np.array([0.0, 0.0, 1.633]) s.set_lattice(latconst, a1, a2, a3) poss = [[0.00, 0.00, 0.00], [1.0 / 3, 2.0 / 3, 0.50]] symbol = _default_specorder[0] symbols = [symbol for i in range(len(poss))] vels = [[0., 0., 0.] for i in range(len(poss))] frcs = [[0., 0., 0.] for i in range(len(poss))] s.add_atoms(symbols, poss, vels, frcs) return s
def make_honeycomb(latconst=1.0): """ Make a cell of 2D honeycomb structure. """ s = NAPSystem(specorder=_default_specorder) #...lattice a1 = np.array([1.0, 0.0, 0.0]) a2 = np.array([0.0, 1.5, 0.0]) a3 = np.array([0.0, 0.0, np.sqrt(3.0)]) s.set_lattice(latconst, a1, a2, a3) positions = [(0.00, 0.50, 0.00), (0.50, 0.50, 1. / 6), (0.50, 0.50, 0.50), (0.00, 0.50, 0.5 + 1.0 / 6)] for p in positions: atom = Atom() atom.set_pos(p[0], p[1], p[2]) atom.set_symbol(_default_specorder[0]) s.add_atom(atom) return s
def make_bcc110(latconst=1.0): """ Make a cell of bcc structure with z along [110]. """ s = NAPSystem(specorder=_default_specorder) #...lattice a1 = np.array([1.0, 0.0, 0.0]) a2 = np.array([0.0, 1.414, 0.0]) a3 = np.array([0.0, 0.0, 1.414]) s.set_lattice(latconst, a1, a2, a3) symbol = _default_specorder[0] symbols = [symbol, symbol, symbol, symbol] poss = [[0.00, 0.00, 0.00], [0.00, 0.50, 0.50], [0.50, 0.50, 0.00], [0.50, 0.00, 0.50]] vels = [[0., 0., 0.] for i in range(4)] frcs = [[0., 0., 0.] for i in range(4)] s.add_atoms(symbols, poss, vels, frcs) return s
def make_sc(latconst=1.0, specorder=None): """ Make a cell of simple cubic structure. """ if specorder is None: raise ValueError('specorder must be given.') s = NAPSystem(specorder=specorder) #...lattice a1 = np.array([1.0, 0.0, 0.0]) a2 = np.array([0.0, 1.0, 0.0]) a3 = np.array([0.0, 0.0, 1.0]) s.set_lattice(latconst, a1, a2, a3) symbols = [specorder[0]] poss = [[0.00, 0.00, 0.00]] vels = [[0., 0., 0.]] frcs = [[0., 0., 0.]] s.add_atoms(symbols, poss, vels, frcs) return s
def make_diamond(latconst=1.0): """ Make a cell of diamond structure. """ s = NAPSystem(specorder=_default_specorder) #...lattice a1 = np.array([1.0, 0.0, 0.0]) a2 = np.array([0.0, 1.0, 0.0]) a3 = np.array([0.0, 0.0, 1.0]) s.set_lattice(latconst, a1, a2, a3) positions = [(0.00, 0.00, 0.00), (0.50, 0.50, 0.00), (0.50, 0.00, 0.50), (0.00, 0.50, 0.50), (0.25, 0.25, 0.25), (0.75, 0.75, 0.25), (0.75, 0.25, 0.75), (0.25, 0.75, 0.75)] for p in positions: atom = Atom() atom.set_pos(p[0], p[1], p[2]) atom.set_symbol(_default_specorder[0]) s.add_atom(atom) return s
def make_bcc(latconst=1.0, specorder=None): """ Make a cell of bcc structure with z along [001]. """ if specorder is None: specorder = ['Fe'] s = NAPSystem(specorder=specorder) #...lattice a1 = np.array([1.0, 0.0, 0.0]) a2 = np.array([0.0, 1.0, 0.0]) a3 = np.array([0.0, 0.0, 1.0]) s.set_lattice(latconst, a1, a2, a3) poss = [[0.00, 0.00, 0.00], [0.50, 0.50, 0.50]] symbol = _default_specorder[0] symbols = [symbol for i in range(len(poss))] vels = [[0., 0., 0.] for i in range(len(poss))] frcs = [[0., 0., 0.] for i in range(len(poss))] s.add_atoms(symbols, poss, vels, frcs) return s
def make_diamond(latconst=1.0): """ Make a cell of diamond structure. """ s = NAPSystem(specorder=_default_specorder) #...lattice a1 = np.array([1.0, 0.0, 0.0]) a2 = np.array([0.0, 1.0, 0.0]) a3 = np.array([0.0, 0.0, 1.0]) s.set_lattice(latconst, a1, a2, a3) poss = [[0.00, 0.00, 0.00], [0.50, 0.50, 0.00], [0.50, 0.00, 0.50], [0.00, 0.50, 0.50], [0.25, 0.25, 0.25], [0.75, 0.75, 0.25], [0.75, 0.25, 0.75], [0.25, 0.75, 0.75]] symbol = _default_specorder[0] symbols = [symbol for i in range(len(poss))] vels = [[0., 0., 0.] for i in range(len(poss))] frcs = [[0., 0., 0.] for i in range(len(poss))] s.add_atoms(symbols, poss, vels, frcs) return s
def make_dimer(distance,latconst,spcs): if distance > latconst/2: raise ValueError('Lattice size is too small for given distance.') s = NAPSystem(specorder=spcs) a1= np.array([ 1.0, 0.0, 0.0 ]) a2= np.array([ 0.0, 1.0, 0.0 ]) a3= np.array([ 0.0, 0.0, 1.0 ]) s.set_lattice(latconst,a1,a2,a3) pos=[(0.0, 0.0, 0.0), (distance/latconst, 0.0, 0.0)] #...Atom 1 atom = Atom() p = pos[0] atom.set_pos(p[0],p[1],p[2]) atom.set_symbol(spcs[0]) s.add_atom(atom) #...Atom 2 atom = Atom() p = pos[1] atom.set_pos(p[0],p[1],p[2]) atom.set_symbol(spcs[1]) s.add_atom(atom) return s
def make_bcc111(latconst=1.0): """ Make a cell of bcc structure with z along [111]. """ s = NAPSystem(specorder=_default_specorder) #...lattice a1 = np.array([1.414, 0.0, 0.0]) a2 = np.array([0.0, 2.449, 0.0]) a3 = np.array([0.0, 0.0, 1.732]) s.set_lattice(latconst, a1, a2, a3) symbol = _default_specorder[0] poss = [[0.00, 0.00, 0.00], [0.00, 0.00, 0.50], [0.00, 0.333, 0.167], [0.00, 0.333, 0.667], [0.00, 0.667, 0.333], [0.00, 0.667, 0.833], [0.50, 0.167, 0.333], [0.50, 0.167, 0.833], [0.50, 0.50, 0.00], [0.50, 0.50, 0.50], [0.50, 0.833, 0.167], [0.50, 0.833, 0.667]] symbols = [symbol for i in range(len(poss))] vels = [[0., 0., 0.] for i in range(len(poss))] frcs = [[0., 0., 0.] for i in range(len(poss))] s.add_atoms(symbols, poss, vels, frcs) return s
files = args['FILES'] sid = int(args['--sid']) width = float(args['-w']) sgm = float(args['--sigma']) if sgm < 0.0: sgm = abs(sgm)*width specorder = args['--specorder'].split(',') if 'None' in specorder and not files[0].find('POSCAR') > -1: raise ValueError('ERROR: specorder must be specified, unless files are POSCAR format.') if sid == 0: raise ValueError('ERROR: sid should be specified and should not be 0.') files.sort(key=get_key,reverse=True) nsys0 = NAPSystem(fname=files[0],specorder=specorder) a,b,c = nsys0.get_lattice_angles() if abs(a-np.pi/2) > 180.0/np.pi or abs(b-np.pi/2) > 180.0/np.pi \ or abs(c-np.pi/2) > 180.0/np.pi: raise ValueError('ERROR: Currently only available for orthogonal lattice, a,b,c=',a,b,c) ndivs = get_num_division(nsys0,width) if np.dot(ndivs,ndivs) > 10000000: raise ValueError('ERROR: ndivs too large.',ndivs) else: print(' # of divisions = {0:d} {1:d} {2:d}'.format(*ndivs)) pdist = np.zeros(ndivs,dtype=float) for f in files: print(' Reading {0:s}...'.format(f)) nsys = NAPSystem(fname=f,specorder=specorder) pdist += get_prob_dist(ndivs,nsys,sid,sgm)
def convert(fname,specorder,index): """ Convert data in fname to fitpot format. """ ry2ev = 13.605698066 au2ang = 0.529177249 ryau2evang = ry2ev/au2ang if not os.path.exists(fname): raise RuntimeError(fname+' not found.') #atoms= read('POSCAR',index=0,format='vasp') try: natm,nspcs,spcs,cell,pos,elems,erg,frcs,pos_unit,strs \ = read_espresso_out(fname) except IOError as e: print('IOError({0:s}): {1:s}'.format(e.errno,e.strerror)) raise erg = erg *Ry_to_eV psys = NAPSystem(specorder=specorder) psys.set_hmat(cell) hi = unitvec_to_hi(cell[0,:],cell[1,:],cell[2,:]) # converting force here, not when reading the file frcs[:,:] *= ryau2evang for ia in range(natm): ai = Atom() pi = pos[ia,:] if pos_unit != "crystal": sx,sy,sz = cartesian_to_scaled(hi,pi[0],pi[1],pi[2]) else: sx,sy,sz = pi[:] ai.set_pos(sx,sy,sz) ai.set_frc(frcs[ia,0],frcs[ia,1],frcs[ia,2]) ai.set_symbol(elems[ia]) psys.add_atom(ai) psys.assign_pbc() if os.path.exists('POSCAR'): print(' cp original POSCAR to POSCAR.orig') os.system('cp POSCAR POSCAR.orig') psys.write_POSCAR() psys.write_pmd(fname='pos') write_ergref(fname='erg.ref',erg=erg) write_frcref(fname='frc.ref',frcs=frcs) #...write stress with open('strs.ref','w') as f: for s in strs: f.write(' {0:8.2f}'.format(s)) f.write('\n')
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
pi[1]= random() if two_dim: pi[2]= 0.0 ai[0]= 0.0 ai[1]= 0.0 ai[2]= random()*np.pi*2 -np.pi else: pi[2]= random() ai[0]= random()*np.pi*2 -np.pi ai[1]= random()*np.pi/2 -np.pi/2 ai[2]= random()*np.pi*2 -np.pi print(' point,angle =',pi,ai) gi= Grain(pi,ai) grains.append(gi) uc= makestruct(latconst) uc.write('POSCAR_uc') gsys = NAPSystem(specorder=['H']) gsys.set_lattice(uc.alc, uc.a1*nx, uc.a2*ny, uc.a3*nz) for g in grains: pi = g.point a = Atom() a.set_pos(pi[0],pi[1],pi[2]) a.set_symbol('H') gsys.add_atom(a) gsys.write('POSCAR_gpoints') system= make_polycrystal(grains,uc,nx,ny,nz,two_dim) system.write(ofname) print(' Elapsed time = {0:12.2f}'.format(time.time()-t0)) print(' Wrote a file: {0:s}'.format(ofname))
def reduce_cij(atoms0,cij0,eps=1.e-4): """ Reduce number of independent Cij according to the crystal system of original cell. It is not Cij=Cji. """ cij = cij0 symdata = spglib.get_symmetry_dataset(atoms0) napsys = NAPSystem(ase_atoms=atoms0) sgnum = symdata['number'] print('Spacegroup number = ',sgnum,' ==> ',end='') a,b,c = napsys.get_lattice_lengths() alpha,beta,gamma = napsys.get_lattice_angles() aeqb = abs(a-b) < eps*min(a,b) beqc = abs(b-c) < eps*min(b,c) ceqa = abs(c-a) < eps*min(c,a) aleqpi2 = abs(alpha-np.pi/2) < eps*np.pi/2 bteqpi2 = abs(beta -np.pi/2) < eps*np.pi/2 gmeqpi2 = abs(gamma-np.pi/2) < eps*np.pi/2 if 0 < sgnum <= 2: # Triclinic print('Triclinic') pass elif sgnum <= 15: # Monoclinic print('Monoclinic') pass elif sgnum <= 74: # Orthorhombic print('Orthorhombic') pass elif sgnum <= 142: # Tetragonal print('Tetragonal') pass elif sgnum <= 194: # Hexagonal print('Hexagonal') print('Number of independent C_ij elements are reduced to 6.') print('C_66 should be 1/2(C_11-C_12) but this is not enforced now.') if not aleqpi2: c22 = (cij[1,1] +cij[2,2])/2 c13 = (cij[0,1] +cij[0,2])/2 c55 = (cij[4,4] +cij[5,5])/2 cij[1,1] = cij[2,2] = c22 cij[0,1] = cij[0,2] = c13 cij[4,4] = cij[5,5] = c55 elif not bteqpi2: c11 = (cij[0,0] +cij[2,2])/2 c12 = (cij[0,1] +cij[1,2])/2 c44 = (cij[3,3] +cij[5,5])/2 cij[0,0] = cij[2,2] = c11 cij[0,1] = cij[1,2] = c12 cij[3,3] = cij[5,5] = c44 elif not gmeqpi2: c11 = (cij[0,0] +cij[1,1])/2 c12 = (cij[0,2] +cij[1,2])/2 c44 = (cij[3,3] +cij[4,4])/2 cij[0,0] = cij[1,1] = c11 cij[0,2] = cij[1,2] = c12 cij[3,3] = cij[4,4] = c44 elif sgnum <= 230: # Cubic print('Cubic') print('Number of independent C_ij elements are reduced to 3.') c11 = (cij[0,0] +cij[1,1] +cij[2,2])/3 c12 = (cij[0,1] +cij[0,2] +cij[1,2])/3 c44 = (cij[3,3] +cij[4,4] +cij[5,5])/3 cij[0,0] = cij[1,1] = cij[2,2] = c11 cij[0,1] = cij[0,2] = cij[1,2] = c12 cij[3,3] = cij[4,4] = cij[5,5] = c44 else: raise ValueError('Invalid space group number, ',sgnum) # Just symmetrize Cij for i in range(6): for j in range(i,6): cij[j,i] = cij[i,j] return cij