def unFractional(posfile): [basis,atypes,atoms,head,poscar] = poscarIO.read(open(posfile,"r").readlines(),frac_coord=True) xs,ys,zs = zip(*atoms) if sum(xs)/len(xs)>1.0 or sum(ys)/len(ys)>1.0 or sum(zs)/len(zs)>1.0: print "Error: it appears these are already unfractional coordinates. %s"%posfile exit(0) [basis,atypes,atoms,head,poscar] = poscarIO.read(open(posfile,"r").readlines(),frac_coord=False) poscarIO.write(posfile,basis, atoms,atypes,head,frac=True,autoFrac=False)
def fractional(posfile): [basis,atypes,atoms,head,poscar] = poscarIO.read(open(posfile,"r").readlines(),frac_coord=True) fraced=False xs,ys,zs = zip(*atoms) if sum(xs)/len(xs)>1.0 or sum(ys)/len(ys)>1.0 or sum(zs)/len(zs)>1.0: fraced=True if not fraced: print "Error: it appears these are already fractional coordiantes. %s"%posfile exit(0) poscarIO.write(posfile,basis,atoms,atypes,head,frac=True)
def fractional(posfile): [basis, atypes, atoms, head, poscar] = poscarIO.read(open(posfile, "r").readlines(), frac_coord=True) fraced = False xs, ys, zs = zip(*atoms) if sum(xs) / len(xs) > 1.0 or sum(ys) / len(ys) > 1.0 or sum(zs) / len( zs) > 1.0: fraced = True if not fraced: print "Error: it appears these are already fractional coordiantes. %s" % posfile exit(0) poscarIO.write(posfile, basis, atoms, atypes, head, frac=True)
def unFractional(posfile): [basis, atypes, atoms, head, poscar] = poscarIO.read(open(posfile, "r").readlines(), frac_coord=True) xs, ys, zs = zip(*atoms) if sum(xs) / len(xs) > 1.0 or sum(ys) / len(ys) > 1.0 or sum(zs) / len( zs) > 1.0: print "Error: it appears these are already unfractional coordinates. %s" % posfile exit(0) [basis, atypes, atoms, head, poscar] = poscarIO.read(open(posfile, "r").readlines(), frac_coord=False) poscarIO.write(posfile, basis, atoms, atypes, head, frac=True, autoFrac=False)
def outcar2poscar(outcar,outposcar,wantconfig): basis,atypes,atoms,head,poscar = outcarIO.outcar2poscar(outcar,wantconfig) poscarIO.write(outposcar,basis,atoms,atypes,head)
def prompt_iface(md, count, stresskb, T, PE, KE, ax, ay, az, afx, afy, afz, v1, v2, v3, types): print "=" * 50 print "Step Number: %d" % (count) print "Stress (in kB) XX YY ZZ XY YZ XZ:" print stresskb if md == True: print "T=%g, PE=%g, TE=%g" % (T, PE, KE) else: print "PE=%g" % (PE) print "=" * 50 print "" if not (plotdisabled): athread = threading.Thread(target=plot3, args=(md, ax, ay, az, afx, afy, afz, v1, v2, v3, types)) athread.start() athread.join() while True: try: val = raw_input( "Input coords and radius of sphere (x y z r) or (Enter) to continue:\n" ) [rx, ry, rz, rr] = [float(k) for k in val.split()] N = len(ax) atoms = [[ax[i], ay[i], az[i], types[i]] for i in range(N) if sqrt((ax[i] - rx)**2 + (ay[i] - ry)**2 + (az[i] - rz)**2) <= rr] atoms.sort(key=lambda x: x[3]) d1, d2, d3, types = zip(*atoms) cs = [types.count(i) for i in range(max(types) + 1)] while True: try: val = float( raw_input( "Scale factor (increases vacuum around cluster): \n" )) except ValueError: print "Scaling factor should be a floating point value between 1.2 and 3.0." else: break for atom in atoms: print "% 5.5g % 5.5g % 5.5g" % (tuple(atom[:3])) poscarIO.write("POSCAR.CLUSTER%d" % len(atoms), array([v1, v2, v3]) * val, [atom[:3] for atom in atoms], cs, "Cluster genearted by grabcluster.py") print "POSCAR.CLUSTER%d written to." % len(atoms) exit(0) except ValueError: if len(val) == 0: break else: if not (plotdisabled): bthread = threading.Thread(target=plot3sphere, args=(md, ax, ay, az, afx, afy, afz, v1, v2, v3, rx, ry, rz, rr)) bthread.start() bthread.join() try: val = raw_input("Good? (y/n):") if val == "y" or val == "Y": N = len(ax) atoms = [[ax[i], ay[i], az[i], types[i]] for i in range(N) if sqrt((ax[i] - rx)**2 + (ay[i] - ry)**2 + (az[i] - rz)**2) <= rr] atoms.sort(key=lambda x: x[3]) d1, d2, d3, types = zip(*atoms) cs = [types.count(i) for i in range(N)] #cut off types list after last non-zero number last = 0 for i, v in enumerate(cs): if v > 0: last = i cs = cs[:last + 1] print cs[:] for atom in atoms: print "% 5.5g % 5.5g % 5.5g" % (tuple(atom[:3])) exit(0) except ValueError: pass
atype = [0] * natom ax = [0.0] * natom ay = [0.0] * natom az = [0.0] * natom afx = [0.0] * natom afy = [0.0] * natom afz = [0.0] * natom for i in range(natom): line = forcedb.readline().split() atype[i] = int(line[0]) [ax[i], ay[i], az[i], afx[i], afy[i], afz[i]] = [float(j) for j in line[1:7]] if (count == confignum): pcar = sys.argv[3] poscarIO.write(pcar, array(basis), zip(ax, ay, az), atype, head) """ wrt=pcar+" "+msg+"\n" wrt+="1.0\n" wrt+=boundx+"\n" wrt+=boundy+"\n" wrt+=boundz+"\n" types=[atype.count(u) for u in set(atype)] wrt+=" ".join(map(str,types))+"\n"#type information wrt+="Direct\n" a=array([bndx,bndy,bndz]) for b in zip(ax,ay,az): x=linalg.solve(a.transpose(),b) wrt+="%9.9f %9.9f %9.9f\n"%(x[0],x[1],x[2])
def poscarGrow(poscarName,outputName,cx,cy,cz): poscar=open(poscarName,"r").readlines() nCopies=cx*cy*cz [basis,atypes,atoms,head,poscar] = poscarIO.read(poscar,frac_coord=True) natoms=len(atoms) v1,v2,v3=map(array,basis) ax,ay,az=map(array,zip(*atoms)) #duplicate and tile ax=array(split(tile(ax,nCopies),nCopies)) ay=array(split(tile(ay,nCopies),nCopies)) az=array(split(tile(az,nCopies),nCopies)) #Shift tiles in x dirn shift=array([1]*natoms) shiftfull=array([shift*j for j in range(cx)]) for i in range(cy*cz): ax[i*cx+array(range(cx))]+=shiftfull #Shift tiles in y dirn shift=array([1]*natoms) shiftfull=array([shift*(j/cx) for j in range(cy*cx)]) for i in range(cz): ay[i*cx*cy+array(range(cy*cx))]+=shiftfull #Shift tiles in z dirn shift=array([1]*natoms) shiftfull=array([shift*(j/cx/cy) for j in range(cy*cx*cz)]) az+=shiftfull ax=ax.reshape([nCopies*natoms]) ay=ay.reshape([nCopies*natoms]) az=az.reshape([nCopies*natoms]) #need to normalize with new positions with respect to duplicated lattice params v1*=cx v2*=cy v3*=cz basis=[v1,v2,v3] A=matrix([[float(cx),0.0,0.0],[0.0,float(cy),0.0],[0.0,0.0,float(cz)]]) ax,ay,az=zip(*[linalg.solve(A,array(p).T)[:] for p in zip(ax,ay,az)]) # if max(ax)>1.0 or max(ay)>1.0 or max(az)>1.0: # continue # exit(0) atoms=zip(ax,ay,az) #Sort atoms by type, as required by POSCAR types=list() j=0 for i in atypes: types+=[j+1]*i j+=1 newtypes=types*cx*cy*cz atoms,types=zip(*sorted(zip(atoms,newtypes),key=lambda x:x[1])) atypes=[i*nCopies for i in atypes] poscarIO.write(outputName,basis,atoms,atypes,head) return len(atoms),basis
forcedb.readline() #The #F atype=[0]*natom ax=[0.0]*natom ay=[0.0]*natom az=[0.0]*natom afx=[0.0]*natom afy=[0.0]*natom afz=[0.0]*natom for i in range(natom): line=forcedb.readline().split() atype[i]=int(line[0]) [ax[i],ay[i],az[i],afx[i],afy[i],afz[i]]=[float(j) for j in line[1:7]] if(count==confignum): pcar=sys.argv[3] poscarIO.write(pcar,array(basis),zip(ax,ay,az),atype,head) """ wrt=pcar+" "+msg+"\n" wrt+="1.0\n" wrt+=boundx+"\n" wrt+=boundy+"\n" wrt+=boundz+"\n" types=[atype.count(u) for u in set(atype)] wrt+=" ".join(map(str,types))+"\n"#type information wrt+="Direct\n" a=array([bndx,bndy,bndz]) for b in zip(ax,ay,az): x=linalg.solve(a.transpose(),b)
exit(0) #Gather poscar information for last set of atoms basis, atypes, atoms, head, poscar = outcarIO.outcar2poscar( outcarFile, configRequest) #Convert from fractional units to cartesian def u2a(u): a = list() a.append(u[0] * basis[0][0] + u[0] * basis[1][0] + u[0] * basis[2][0]) a.append(u[0] * basis[0][1] + u[1] * basis[1][1] + u[1] * basis[2][1]) a.append(u[0] * basis[0][2] + u[2] * basis[1][2] + u[2] * basis[2][2]) return a stepb0 = map(u2a, atoms) stepb1 = map(u2a, outcarIO.outcar2poscar(outcarFile, configRequest - 1)[2]) stepb2 = map(u2a, outcarIO.outcar2poscar(outcarFile, configRequest - 2)[2]) stepb3 = map(u2a, outcarIO.outcar2poscar(outcarFile, configRequest - 3)[2]) stepb4 = map(u2a, outcarIO.outcar2poscar(outcarFile, configRequest - 4)[2]) velo5 = list() for b0, b1, b2, b3, b4 in zip(stepb0, stepb1, stepb2, stepb3, stepb4): velo5.append([ (25 * b0[i] - 48 * b1[i] + 36 * b2[i] - 16 * b3[i] + 3 * b4[i]) / (12.0 * timestep) for i in range(3) ]) poscarIO.write("VDATCAR", basis, atoms, atypes, head, velocities=velo5)
print "%s <lammps dump> <config #> <POSCAR output>" % sys.argv[0].split("/")[-1] print "If only the dump file is provided, the number of configurations will be reported" print "Converts a lammps dump to POSCAR" # If only the filename is provided, count the number of configurations and spit that out if len(sys.argv) == 2: dumpdata = open(sys.argv[1], "r").readlines() usage() print "\nNumber of configurations:", len([i for i in dumpdata if "ITEM: TIMESTEP" in i]), "\n" exit(0) if len(sys.argv) != 4: usage() exit(0) dumpdata = sys.argv[1] # open(sys.argv[1],"r").readlines() configN = int(sys.argv[2]) posname = sys.argv[3] # Read in lammps data basis, types, atoms = lammpsIO.readConfig(dumpdata, configN) # atoms,types = zip(*[[[i[0],i[1],i[2]-basis[2][2]/2.0],t] for i,t in zip(atoms,types) if i[2]>basis[2][2]/2.0]) # basis[2][2]/=2.0 header = "POSCAR generated by %s on %s" % (sys.argv[0].split("/")[-1], time.asctime()) # Write out POSCAR data atypes = [types.count(j) for j in sorted(list(set(types)))] poscarIO.write(posname, basis, atoms, atypes, header, autoFrac=False, frac=False)
def prompt_iface(md,count,stresskb,T,PE,KE,ax,ay,az,afx,afy,afz,v1,v2,v3,types): print "="*50 print "Step Number: %d"%(count) print "Stress (in kB) XX YY ZZ XY YZ XZ:" print stresskb if md==True: print "T=%g, PE=%g, TE=%g" % (T,PE,KE) else: print "PE=%g" % (PE) print "="*50 print "" if not(plotdisabled): athread=threading.Thread(target=plot3,args=(md,ax,ay,az,afx,afy,afz,v1,v2,v3,types)) athread.start() athread.join() while True: try: val=raw_input("Input coords and radius of sphere (x y z r) or (Enter) to continue:\n") [rx,ry,rz,rr]=[float(k) for k in val.split()] N=len(ax) atoms=[[ax[i],ay[i],az[i],types[i]] for i in range(N) if sqrt((ax[i]-rx)**2+(ay[i]-ry)**2+(az[i]-rz)**2)<=rr] atoms.sort(key=lambda x:x[3]) d1,d2,d3,types=zip(*atoms) cs=[types.count(i) for i in range(max(types)+1)] while True: try: val=float(raw_input("Scale factor (increases vacuum around cluster): \n")) except ValueError: print "Scaling factor should be a floating point value between 1.2 and 3.0." else: break for atom in atoms: print "% 5.5g % 5.5g % 5.5g" % (tuple(atom[:3])) poscarIO.write("POSCAR.CLUSTER%d"%len(atoms),array([v1,v2,v3])*val,[atom[:3] for atom in atoms],cs,"Cluster genearted by grabcluster.py") print "POSCAR.CLUSTER%d written to."%len(atoms) exit(0) except ValueError: if len(val)==0: break else: if not(plotdisabled): bthread=threading.Thread(target=plot3sphere,args=(md,ax,ay,az,afx,afy,afz,v1,v2,v3,rx,ry,rz,rr)) bthread.start() bthread.join() try: val=raw_input("Good? (y/n):") if val=="y" or val=="Y": N=len(ax) atoms=[[ax[i],ay[i],az[i],types[i]] for i in range(N) if sqrt((ax[i]-rx)**2+(ay[i]-ry)**2+(az[i]-rz)**2)<=rr] atoms.sort(key=lambda x:x[3]) d1,d2,d3,types=zip(*atoms) cs=[types.count(i) for i in range(N)] #cut off types list after last non-zero number last=0 for i,v in enumerate(cs): if v>0: last=i cs=cs[:last+1] print cs[:] for atom in atoms: print "% 5.5g % 5.5g % 5.5g" % (tuple(atom[:3])) exit(0) except ValueError: pass
def outcar2poscar(outcar, outposcar, wantconfig): basis, atypes, atoms, head, poscar = outcarIO.outcar2poscar( outcar, wantconfig) poscarIO.write(outposcar, basis, atoms, atypes, head)
usage() print "\nNumber of configurations:", len( [i for i in dumpdata if "ITEM: TIMESTEP" in i]), "\n" exit(0) if len(sys.argv) != 4: usage() exit(0) dumpdata = sys.argv[1] #open(sys.argv[1],"r").readlines() configN = int(sys.argv[2]) posname = sys.argv[3] #Read in lammps data basis, types, atoms = lammpsIO.readConfig(dumpdata, configN) #atoms,types = zip(*[[[i[0],i[1],i[2]-basis[2][2]/2.0],t] for i,t in zip(atoms,types) if i[2]>basis[2][2]/2.0]) #basis[2][2]/=2.0 header = "POSCAR generated by %s on %s" % (sys.argv[0].split("/")[-1], time.asctime()) #Write out POSCAR data atypes = [types.count(j) for j in sorted(list(set(types)))] poscarIO.write(posname, basis, atoms, atypes, header, autoFrac=False, frac=False)
if configRequest > nSteps: print "%d configurations available"%nSteps exit(0) if configRequest < 5: print "Need more than 5 steps for backwards - finite difference method." exit(0) #Gather poscar information for last set of atoms basis,atypes,atoms,head,poscar = outcarIO.outcar2poscar(outcarFile,configRequest) #Convert from fractional units to cartesian def u2a(u): a=list() a.append(u[0]*basis[0][0]+u[0]*basis[1][0]+u[0]*basis[2][0]) a.append(u[0]*basis[0][1]+u[1]*basis[1][1]+u[1]*basis[2][1]) a.append(u[0]*basis[0][2]+u[2]*basis[1][2]+u[2]*basis[2][2]) return a stepb0 = map(u2a,atoms) stepb1 = map(u2a,outcarIO.outcar2poscar(outcarFile,configRequest-1)[2]) stepb2 = map(u2a,outcarIO.outcar2poscar(outcarFile,configRequest-2)[2]) stepb3 = map(u2a,outcarIO.outcar2poscar(outcarFile,configRequest-3)[2]) stepb4 = map(u2a,outcarIO.outcar2poscar(outcarFile,configRequest-4)[2]) velo5=list() for b0,b1,b2,b3,b4 in zip(stepb0,stepb1,stepb2,stepb3,stepb4): velo5.append( [(25*b0[i]-48*b1[i]+36*b2[i]-16*b3[i]+3*b4[i])/(12.0*timestep) for i in range(3)] ) poscarIO.write("VDATCAR",basis,atoms,atypes,head,velocities=velo5)