def read(self,filepath): print 'reading Procar', lines = nstrip(readfile(filepath)) self.nk = int(lines[1].split(':')[1].split()[0]) self.nbands = int(lines[1].split(':')[2].split()[0]) self.nions = int(lines[1].split(':')[3]) #initialize self.kvecs = zeros((3,self.nk),dtype = float) self.kweights = zeros((self.nk),dtype = float) self.ener = zeros((self.nk, self.nbands, self.nspin),dtype = float) self.occ = zeros((self.nk, self.nbands,self.nspin),dtype = float) self.weights = zeros((self.nk,self.nbands,self.nions,len(self.orbs),self.nspin),dtype = float) #read weights ipos = 2 for ispin in range(self.nspin): ipos += 1; # print 'ipos for spin %i: %i' % (ispin,ipos) for ik in range(self.nk): self.kvecs[:,ik] = lines[ipos].split()[3:6] self.kweights[ik] = float(lines[ipos].split()[8]) ipos += 2 for ib in range(self.nbands): self.ener[ik,ib,ispin] = float(lines[ipos].split()[4]) self.occ[ik,ib,ispin] = float(lines[ipos].split()[7]) ipos += 3 for i in range(self.nions): if mod(ipos,1e4)==0: print '-', #progress bar tempw = lines[ipos].split()[1:self.norbs+1] self.weights[ik,ib,i,:,ispin] = tempw ipos += 1 ipos += 2 ipos += 1 print
def readSym(dir): lines = readfile('{}/sym.out'.format(dir)) nops = int(lines[0].split(':')[1]) IBZvolCut = float(lines[1].split(':')[1]) # IBZvol = float(lines[2].split(':')[1]) IBZvol = None return nops, IBZvolCut, IBZvol
def rd_ibzkpt(self,dir): #only extracts the kpoint vectors self.vecs = zeros((3,self.nk),dtype = float64) lines = nstrip(readfile(dir+'IBZKPT')) nk2 = int(lines[1].split()[0]) if nk2 != self.nk: print nk2,self.nk, 'Warning! Nkpts in IBZKPTS differs from this program''s' for ik in range(self.nk): self.vecs[:,ik] = lines[ik+3].split()[:3] #don't read weights'
def get_eigen(self,dir): lines = nstrip(readfile(dir+'eigenv')) self.nk = int(lines[0].split()[0]) self.nbands = int(lines[0].split()[1]) self.ener = zeros((self.nk,self.nbands),dtype = float64) for ik in range(self.nk): for ib in range(self.nbands): self.ener[ik,ib] = lines[ik+1].split()[ib+1]
def get_tetweights(self,dir): lines = nstrip(readfile(dir+'tetwgt')) self.ntet = int(lines[0].split()[0]) self.nbands = int(lines[0].split()[1]) # nspin = int(lines[0][2]) #haven't added spin yet self.fwgt = zeros((self.ntet,self.nbands),dtype = float64) for it in range(self.ntet): for ib in range(self.nbands): self.fwgt[it,ib] = lines[it+1].split()[ib+1] #First line and first column are not weights
def rd_ibzkpt(self,dir): #only extracts the tets ids self.idk = zeros((4,self.ntet),dtype = int) self.vwgt = zeros(self.ntet,dtype = float64) lines = nstrip(readfile(dir+'IBZKPT')) nk = int(lines[1].split()[0]) ntet2 = int(lines[4+nk].split()[0]) self.volscale = float(lines[4+nk].split()[1]) print 'VASP volume scale', self.volscale if ntet2 != self.ntet: print 'Warning! NTET in IBZKPTS differs from this program''s' for it in range(self.ntet): self.vwgt[it] = lines[5+nk+it].split()[0] self.idk[:,it] = lines[5+nk+it].split()[1:] self.idk = self.idk -1 #(converting from Fortran counting to python)
def get_tetvecs(self,dir): lines = nstrip(readfile(dir+'tetvecs')) self.vecs = zeros((3,4,self.ntet),dtype = float64) self.vol = zeros(self.ntet,dtype = float64) print shape(self.vecs) for it in range(self.ntet): for ic in range(4): print it, ic,lines[5*it+ic+1] self.vecs[:,ic,it] = lines[5*it+ic+1].split() va = self.vecs[:,1,it]-self.vecs[:,0,it] vb = self.vecs[:,2,it]-self.vecs[:,0,it] vc = self.vecs[:,3,it]-self.vecs[:,0,it] self.vol[it] = abs(dot(va,cross(vb,vc))/2.0)
def incar(self,dir): print 'reading ISPIN and LORBIT from INCAR' lines = nstrip(readfile(dir+'INCAR')) for line in lines: if ('ISPIN' in line) or ('ispin' in line): self.nspin = int(line.split('=')[1]) if ('LORBIT' in line) or ('lorbit' in line): lorbit = int(line.split('=')[1]) if lorbit in [0,10]: #no lm decomposition self.orbs = ['s','p','d'] elif lorbit in [1,2,11,12]: self.orbs = ['s','py','pz','px','dxy','dyz','dz2','dxz','dx2'] else: sys.exit('Unrecognized LORBIT in INCAR') self.norbs = len(self.orbs)
def read_eigenval(dir): '''Read in k vectors and eigenvalues from vasp file''' os.chdir(dir) eigs = readfile('EIGENVAL') nb = int(eigs[5].split()[2]) nk = int(eigs[5].split()[1]) print nb, nk ks = zeros((nk,3)) eners = zeros((nk,nb)) for ik in range(nk): istart = 7 + ik*(nb+2) ks[ik,:] = [float(eigs[istart].split()[0]), float(eigs[istart].split()[1]), float(eigs[istart].split()[2])] for ib in range(nb): eners[ik,ib] = float(eigs[istart+ib+1].split()[1]) return [nb,nk,ks,eners]
def incar(self, dir): print 'reading ISPIN and LORBIT from INCAR' lines = nstrip(readfile(dir + 'INCAR')) for line in lines: if ('ISPIN' in line) or ('ispin' in line): self.nspin = int(line.split('=')[1]) if ('LORBIT' in line) or ('lorbit' in line): lorbit = int(line.split('=')[1]) if lorbit in [0, 10]: #no lm decomposition self.orbs = ['s', 'p', 'd'] elif lorbit in [1, 2, 11, 12]: self.orbs = [ 's', 'py', 'pz', 'px', 'dxy', 'dyz', 'dz2', 'dxz', 'dx2' ] else: sys.exit('Unrecognized LORBIT in INCAR') self.norbs = len(self.orbs)
def read(self, filepath): print 'reading Procar', lines = nstrip(readfile(filepath)) self.nk = int(lines[1].split(':')[1].split()[0]) self.nbands = int(lines[1].split(':')[2].split()[0]) self.nions = int(lines[1].split(':')[3]) #initialize self.kvecs = zeros((3, self.nk), dtype=float) self.kweights = zeros((self.nk), dtype=float) self.ener = zeros((self.nk, self.nbands, self.nspin), dtype=float) self.occ = zeros((self.nk, self.nbands, self.nspin), dtype=float) self.weights = zeros( (self.nk, self.nbands, self.nions, len(self.orbs), self.nspin), dtype=float) #read weights ipos = 2 for ispin in range(self.nspin): ipos += 1 # print 'ipos for spin %i: %i' % (ispin,ipos) for ik in range(self.nk): self.kvecs[:, ik] = lines[ipos].split()[3:6] self.kweights[ik] = float(lines[ipos].split()[8]) ipos += 2 for ib in range(self.nbands): self.ener[ik, ib, ispin] = float(lines[ipos].split()[4]) self.occ[ik, ib, ispin] = float(lines[ipos].split()[7]) ipos += 3 for i in range(self.nions): if mod(ipos, 1e4) == 0: print '-', #progress bar tempw = lines[ipos].split()[1:self.norbs + 1] self.weights[ik, ib, i, :, ispin] = tempw ipos += 1 ipos += 2 ipos += 1 print
] N = len(dirs) #x = zeros[len(dirs)] #y = zeros[len(dirs)] labels = ['No correction', 'Blochl correction'] if len(labels) != len(dirs): sys.exit( 'Number of graphing labels is different from the number of datasets!\n Stopping\n' ) fig = figure() ax1 = fig.add_subplot(111) for idir, dir in enumerate(dirs): print idir lines = readfile(dir + file) x = [lines[i].strip().split()[0] for i in range(len(lines))] y = [lines[i].strip().split()[1] for i in range(len(lines))] ax1.semilogy(x, y, color=cm.jet(1. * (idir + 1) / N), linestyle='None', marker='o', label=labels[idir]) #ylim((1e-12,1e0)) legend(loc='upper right', prop={'size': 12}) title('Al:Al Tetrahedral methods\n Cubic mesh') xlabel('n') ylabel('error (eV)') os.chdir(dirs[0]) os.chdir('../')
writefile([str(j)+'\n' for j in dets],'dets%s' % structi) writefile([str(j)+'\n' for j in err],'err%s' % structi) writefile([str(j)+'\n' for j in ef_err],'ef_err%s' % structi) # log err vs n for all structs os.chdir(tempdir) fig = figure() ax1 = fig.add_subplot(111) # ax1.set_color_cycle(['r','b','g','c', 'm', 'y', 'k']) xlabel('n in cubic grid') ylabel('Error (eV)') title('Structure noise '+title_detail+':\nTheoretical values: max n on struct 1; 1e-8 mark') xlim((0,55)) #ylim((1e-12,1e0)) for i,structi in enumerate(structselect): nlist = readfile('ns%s' % structi) errlist = readfile('err%s' % structi) ax1.semilogy(nlist, errlist,label=structi,linestyle='None',color=cm.jet(1.*(i+1)/len(structselect)), marker = 'o') # marker = 'o', plt.legend(loc='upper right',prop={'size':14}); show() fig.savefig('log_err_vs_n_structs') # for structi in structselect: # for arr in [] # # #en_per_atom vs ns # titleadd = ''+ title_detail
from analysisToolsVasp import readfile file = 'structs.cubmesh/errlist_vs_n' dirs = [ '/fslhome/bch/cluster_expansion/alal/equivk_f-16.tetra.noBlochl/', '/fslhome/bch/cluster_expansion/alal/equivk_f1-6.tetra/' ] N = len(dirs) #x = zeros[len(dirs)] #y = zeros[len(dirs)] labels = ['No correction','Blochl correction'] if len(labels) != len(dirs): sys.exit('Number of graphing labels is different from the number of datasets!\n Stopping\n') fig = figure() ax1 = fig.add_subplot(111) for idir,dir in enumerate(dirs): print idir lines = readfile(dir+file) x = [lines[i].strip().split()[0] for i in range(len(lines))] y = [lines[i].strip().split()[1] for i in range(len(lines))] ax1.semilogy(x, y,color=cm.jet(1.*(idir+1)/N),linestyle = 'None', marker = 'o', label=labels[idir]) #ylim((1e-12,1e0)) legend(loc='upper right',prop={'size':12}); title('Al:Al Tetrahedral methods\n Cubic mesh') xlabel('n') ylabel('error (eV)') os.chdir(dirs[0]);os.chdir('../') print dirs fig.savefig('err_vs_n_multi') print 'done'
if os.path.isdir(d) and d == filter2 ]) for struct in structs: if collateMeshMat: meshPlots.write('(* {} *)\n'.format(struct)) os.chdir(struct) iplot += 1 calcs = sorted( [d for d in os.listdir(os.getcwd()) if os.path.isdir(d)]) if len(calcs) > maxCalcs: maxCalcs = len(calcs) if collateMeshMat: for ic, calc in enumerate(calcs): os.chdir(calc) if ic == 0: os.system('cp bounds ../') if os.path.exists('cell_IBZmesh.m'): meshPlots.write('\t(* {} *)\n'.format(calc)) lines = readfile('cell_IBZmesh.m') lines.append('\n\n') meshPlots.writelines(lines) os.chdir('../') os.chdir(path) if collateMeshMat: meshPlots.close() #external run paths are of the form extmethodpath/atom_convergence/11_atom if not extpaths is None: for ipath, extpath in enumerate(extpaths): os.chdir(extpath) method = extpath.split('/')[0] if collateMeshMat: meshPlots = open('IBZmeshPlots', 'w') atomdirs = sorted([
latticeType, packingFraction, mink_reduce, lattvec_u,arenormal,\ unique_anorms,points_in_ppiped #,three_perp, from numpy import array, arccos, dot, cross, pi, floor, sum, sqrt, exp, log, asarray from numpy import transpose, rint, inner, multiply, size, argmin, argmax, nonzero, float64, identity from numpy import ceil, real, unravel_index from scipy.optimize import minimize from copy import copy, deepcopy fprec = float64 from numpy import zeros #use arrays, not "matrix" class #from numpy.matlib import zeros, matrix #creates np.matrix rather than array, but limited to 2-D!!!! uses *, but array uses matrixmultiply from numpy.linalg import norm, det, inv, eig from numpy.random import randint, random from itertools import combinations sys.path.append( '/bluehome2/bch/pythonscripts/cluster_expansion/analysis_scripts/') from analysisToolsVasp import writeEnergiesOszicar, writedirnames, nstrip, writeNk, writeNkIBZ, \ writeElConverge, writeElSteps, writeCPUtime, enerparts, getdata, readfile, writefile, \ getms, writefermi, removezeros,getEf '''reads chgcar from dir1 and multiplies by 2 (for superlattice of volume 2 along z axis), and writes it twice into CHGCAR for dir2 ''' dir1 = '/fslhome/bch/cluster_expansion/alal/cubic_al/mp_c1,3/c1_8x8x8/BANDS/' dir2 = '/fslhome/bch/cluster_expansion/alal/cubic_al/mp_c1,3/c3_8x8x4/BANDS/' lines1 = readfile(dir1 + 'CHGCAR') lines2 = readfile(dir2 + 'CHGCAR') print 'Done'
def analyze(paths): #as used with the parameter search, paths will have only one entry. But keep consistent with interactive vaspoutCombineRunsExtData extpath = None useSym = False coloring = 'method' # coloring = 'indiv' doLegend = True doLabel = True smoothFactor = 2.0 filter = '_' #string must be in dir name to be included filter2 = None #'Cu_1' #for single structures. set to None if using filter1 only summaryPath = paths[0] #count the number of plots: iplot = 0 maxCalcs = 0 maxNk = 0 methods = [] for ipath,path in enumerate(paths): method = path.split('_')[-1].split('/')[0] methods.append(method) os.chdir(path) if filter2 == None: structs = sorted([d for d in os.listdir(os.getcwd()) if os.path.isdir(d) and filter in d]) else: structs = sorted([d for d in os.listdir(os.getcwd()) if os.path.isdir(d) and d==filter2]) for struct in structs: os.chdir(struct) iplot += 1 calcs = sorted([d for d in os.listdir(os.getcwd()) if os.path.isdir(d)]) if len(calcs)>maxCalcs: maxCalcs = len(calcs) os.chdir(path) #external data is of the form extpath/atom_method/struct.csv. The csv has energies vs nK if not extpath is None: os.chdir(extpath) atoms_methods = sorted([d for d in os.listdir(extpath) if os.path.isdir(d) and filter in d])# os.chdir(extpath) for atom_method in atoms_methods: atom = atom_method.split('_')[0] os.chdir(atom_method) os.system('rm -r .*lock*') for structfile in os.listdir(os.getcwd()): if atom not in structfile: os.system('mv {} {}_{}'.format(structfile,atom,structfile)) #so that file has atom name at beginning if filter2 == None: structfiles = sorted([d for d in os.listdir(os.getcwd()) if os.path.getsize(d)>0]) else: structfiles = sorted([d for d in os.listdir(os.getcwd()) if '_'.join(d.split('_')[:2])==filter2 and os.path.getsize(d)>0]) for structfile in structfiles: iplot += 1 #count number of points in this structfile lines = readfile(structfile) if len(lines)>maxCalcs: maxCalcs = len(lines) os.chdir(extpath) nplots = iplot if nplots < len(paths): sys.exit('Stop. Structures do not match filter') data = zeros(nplots,dtype = [('ID', 'S25'),('color', 'S15'),('method', 'S15'),\ ('nDone','int32'),('nAtoms','int32'),('nops','int8'),\ ('IBZvolcut','float'),('IBZvol','float'),\ ('eners', '{}float'.format(maxCalcs)), ('errs', '{}float'.format(maxCalcs)),\ ('nKs', '{}int16'.format(maxCalcs)),('ns', '{}int8'.format(maxCalcs))]) # style.use('bmh') # for i, item in enumerate(rcParams['axes.prop_cycle']): # colorsList.append(item['color']) style.use('fivethirtyeight') # for i, item in enumerate(rcParams['axes.prop_cycle'][:-2]): # colorsList.append(item['color']) colorsList = [u'#30a2da', u'#fc4f30', u'#e5ae38', u'#6d904f', u'#8b8b8b', u'#348ABD', u'#A60628', u'#7A68A6', u'#467821', u'#D55E00', u'#CC79A7', u'#56B4E9', u'#009E73', u'#F0E442', u'#0072B2'] colorsList = colorsList + ['b','m','y','c','k'] rcParams.update({'figure.autolayout': True}) rcParams['axes.facecolor'] = 'white' rcParams['axes.linewidth'] = 1.0 rcParams['axes.edgecolor'] = 'black' # axisbg=axescolor rcParams['savefig.facecolor'] = 'white' # axisbg=axescolor rcParams['lines.markersize'] = 4.5 #read all the data iplot = -1 for ipath, path in enumerate(paths): #my data tag = path.split('/')[-1][-7:] os.chdir(path) if filter2 == None: structs = sorted([d for d in os.listdir(os.getcwd()) if os.path.isdir(d) and filter in d]) else: structs = sorted([d for d in os.listdir(os.getcwd()) if os.path.isdir(d) and d==filter2]) nStructs = len(structs) # print structs,path for istruct,struct in enumerate(structs): # print 'test', istruct, struct # print 'struct',struct os.chdir(struct) if coloring == 'indiv': # if iplot < nplots -1: color = rgb2hex(cm.jet(1.*(iplot+1)/float(nplots))) # else: # color = 'k' elif coloring == 'method': # color = colorsList[ipath] color = None calcs = sorted([d for d in os.listdir(os.getcwd()) if os.path.isdir(d) and os.path.exists('{}/OUTCAR'.format(d))]) energies = [] nKs = [] ns = [] #the base n of the run run nDone = 0 if useSym: try: nops,IBZvolcut,IBZvol = readSym(calcs[0]) except: sys.exit('Stopping. readSym failed. Set useSym to False') for calc in calcs: if electronicConvergeFinish(calc): ener = getEnergy(calc) #in energy/atom if not areEqual(ener,0,1e-5): nDone +=1 energies.append(ener) if 'vc' in path: nK = getNkIBZ(calc,'KPOINTS') else: nK = getNkIBZ(calc,'IBZKPT') if nK > maxNk: maxNk = nK nKs.append(nK) ns.append(int(calc.split('_')[-1])) #sort by increasing number of kpoints if len(energies)>0: iplot += 1 nKs = array(nKs) energies = array(energies) ns = array(ns) order = argsort(nKs) # print 'struct',struct # print 'energies',energies energies = energies[order] ns = ns[order] nKs = sort(nKs) eref = energies[-1]#the last energy of each struct is that of the most kpoints errs = abs(energies-eref)*1000 + 1e-4 #now in meV data[iplot]['ID'] = '{} {}'.format(struct,tag) nAtoms = getNatoms('{}/POSCAR'.format(calc)) data[iplot]['nAtoms'] = nAtoms if useSym: data[iplot]['nops'] = nops data[iplot]['IBZvolcut'] = IBZvolcut data[iplot]['nDone'] = nDone data[iplot]['eners'][:nDone] = energies data[iplot]['errs'][:nDone] = errs data[iplot]['nKs'][:nDone] = nKs data[iplot]['ns'][:nDone] = ns data[iplot]['color'] = color method = path.split('_')[-1].split('/')[0] data[iplot]['method'] = method os.chdir(path) # os.chdir(extpath) if not extpath is None: os.chdir(extpath) # print; print atoms_methods for atom_method in atoms_methods: os.chdir(atom_method) if coloring == 'method': color = None if 'MP' in atom_method: # color = colorsList[len(paths)] method = 'MP' elif 'Mueller' in atom_method: # color = colorsList[len(paths)+1] method = 'Mueller' if method not in methods: methods.append(method) if filter2 == None: structfiles = sorted([d for d in os.listdir(os.getcwd()) if os.path.getsize(d)>0]) else: structfiles = sorted([d for d in os.listdir(os.getcwd()) if '_'.join(d.split('_')[:2])==filter2 and os.path.getsize(d)>0]) for structfile in structfiles: if useSym: nops,IBZvolcut,nAtoms = copyData(structfile,data) if coloring == 'indiv': if iplot < nplots -1: color = cm.jet(1.*(iplot+1)/float(nplots)) else: color = 'k' iplot += 1 energies = [] nKs = [] lines = readfile(structfile) for line in lines: nK = int(line.split('\t')[0]) if nK > maxNk: maxNk = nK nKs.append(nK) energies.append(-float(line.split('\t')[1].split('\r')[0])) nKs = array(nKs) energies = array(energies) nDone = len(energies) order = argsort(nKs) energies = energies[order] eref = energies[-1]#the last energy of each struct is that of the most kpoints nKs = sort(nKs) errs = abs(energies-eref)*1000 + 1e-4 #now in meV struct = '_'.join(structfile.split('_')[:2]) data[iplot]['ID'] = atom_method + struct data[iplot]['nAtoms'] = nAtoms if useSym: data[iplot]['nops'] = nops data[iplot]['IBZvolcut'] = IBZvolcut data[iplot]['nDone'] = len(energies) data[iplot]['eners'][:nDone] = energies data[iplot]['errs'][:nDone] = errs data[iplot]['nKs'][:nDone] = nKs data[iplot]['color'] = color data[iplot]['method'] = method os.chdir(extpath) nplots = iplot+1 lines = [' ID , nKIBZ , ener , err, nAtoms, nops,IBZcut\n'] for iplot in range(nplots): n = data[iplot]['nDone'] for icalc in range(n):#data[iplot]['eners'][:n].tolist() lines.append('{}_n{},{},{:15.12f},{:15.12f},{},{},{}\n'.format(data[iplot]['ID'],\ data[iplot]['ns'][icalc], data[iplot]['nKs'][icalc],\ data[iplot]['eners'][icalc],data[iplot]['errs'][icalc],\ data[iplot]['nAtoms'],data[iplot]['nops'],data[iplot]['IBZvolcut'])) writefile(lines,'{}/summary.csv'.format(summaryPath)) #plots if maxNk > 1: if filter[0] == '_':filter = '' #labels can't begin with _ # plotTypes = ['linear','loglog', 'loglinear'];ylabels = ['Vasp error energy/atom (eV)','Error (meV)','Error (meV)'] # print 'plot only loglog' plotTypes = ['loglog']; ylabels = ['Error (meV)'] # plotTypes = [] xtext = 'N k-points' for it,plotType in enumerate(plotTypes): fig = figure() ax1 = fig.add_subplot(111) xlabel(xtext) ylabel(ylabels[it]) # title('Convergence vs mesh method') #ylim((1e-12,1e0)) oldmethod = '' methods2 = [] for iplot in range(nplots): labelStr = None n = data[iplot]['nDone'] if coloring == 'method': method = data[iplot]['method'] data[iplot]['color'] = colorsList[methods.index(method)] if method != oldmethod and method not in methods2: if doLabel: labelStr = '{} {}'.format(filter,data[iplot]['method']) plotData(fig,summaryPath,data[iplot],n,plotType,filter,doLegend,labelStr) oldmethod = method;labelStr = None methods2.append(method) else: plotData(fig,summaryPath,data[iplot],n,plotType,filter,doLegend,labelStr) elif coloring == 'indiv': if doLabel: labelStr = '{} {}'.format(filter,data[iplot]['ID']) plotData(data[iplot],n,plotType,filter,doLegend,labelStr) #Method averaging if coloring == 'method': # print 'Averaging, plotting method errors' nbins = int(10*ceil(log10(maxNk)))# 10 bins per decade nKbins = array([(10.0**(1/10.0))**i for i in range(nbins)]) fig = figure() ax1 = fig.add_subplot(111) xlabel('N k-points (smoothed by factor {})'.format(int(smoothFactor))) ylabel('Error (meV)') methodCostsLogs = [] for im,method in enumerate(methods): methnKmax = 0 binCounts = zeros(nbins,dtype = int32) binErrs = zeros(nbins,dtype = float) costLogs = zeros(nbins,dtype = float) # "Costs" relative to excellent Si Monkhorst Pack, which has err = 10^3/nK^3 + 10^-3 meV. for iplot in range(nplots): if data[iplot]['method'] == method: for icalc in range(data[iplot]['nDone']-1): nK = data[iplot]['nKs'][icalc] if nK>methnKmax: methnKmax = nK if nK>1: for ibin in range(nbins): if abs(log10(nK/nKbins[ibin])) <= log10(smoothFactor)\ and nKbins[ibin]<= maxNk: binErrs[ibin] += data[iplot]['errs'][icalc] costLogs[ibin] += log10(data[iplot]['errs'][icalc]/(10**3/(nK**3.0)+0.001)) binCounts[ibin] += 1 mask = where(binCounts>0) binErrs2 = binErrs[mask[0]] binCounts2 = binCounts[mask[0]] nKbins2 = nKbins[mask[0]] costLogs2 = costLogs[mask[0]] nbins2 = len(nKbins2) avgErrs = [binErrs2[ibin]/binCounts2[ibin] for ibin in range(nbins2)] avgcostLogs = [costLogs2[ibin]/binCounts2[ibin] for ibin in range(nbins2)] avgcostLins = [10**avgcostLogs[ibin] for ibin in range(nbins2)] methodCostsLogs.append(mean(avgcostLogs)) loglog(nKbins2,avgErrs,label = method,\ color = colorsList[im], marker = None) loglog(nKbins2,avgcostLins,label = None,\ color = colorsList[im], marker = None,linestyle=':') # print 'Method',method, 'nKmax',methnKmax, 'avgLogCost', mean(avgcostLogs) legend(loc='lower left',prop={'size':12}); fig.savefig('{}/methodErrs'.format(summaryPath)) close('all') if maxNk > 1: return [methodCostsLogs[0],mean(data['nDone'])] #there is only one method when running this routine else: return [100,0]
def weightfunc(FE,FEmax,FEmin): maxratio = 1.0 return str(1.0 + (maxratio-1)*(FEmax-FE)/(FEmax-FEmin)) '''Reads formation energies in structures.in. If weighted, it changes the weights. If not, it adds a line for each weight''' #dir = '/fslhome/bch/cluster_expansion/graphene/csv1-8W/' dir = '/fslhome/bch/cluster_expansion/graphene/test1/' infile = dir+'structures.in.441.FEsorted' #infile = dir+'structures.in' outfile = dir+'structures.in' FEs = [] indices = [] lines = readfile(infile) if 'noweights' in lines[1]: weights_in = False lines[1] = 'weights\n' else: weights_in = True #print 'weights_in', weights_in #Find maximum and min formation energies for i, line in enumerate(lines): if 'FE' in line: energy = float(line.split()[line.split().index('FE')+2].split(',')[0]) #FE = 1.2578, etc. remove any "," FEs.append(energy) if '#Energy' in line: indices.append(i+2) #where weights should appear FEmax = amax(FEs) FEmin = amin(FEs)
def analyze( paths ): #as used with the parameter search, paths will have only one entry. But keep consistent with interactive vaspoutCombineRunsExtData extpath = None useSym = False coloring = 'method' # coloring = 'indiv' doLegend = True doLabel = True smoothFactor = 2.0 filter = '_' #string must be in dir name to be included filter2 = None #'Cu_1' #for single structures. set to None if using filter1 only summaryPath = paths[0] #count the number of plots: iplot = 0 maxCalcs = 0 maxNk = 0 methods = [] for ipath, path in enumerate(paths): method = path.split('_')[-1].split('/')[0] methods.append(method) os.chdir(path) if filter2 == None: structs = sorted([ d for d in os.listdir(os.getcwd()) if os.path.isdir(d) and filter in d ]) else: structs = sorted([ d for d in os.listdir(os.getcwd()) if os.path.isdir(d) and d == filter2 ]) for struct in structs: os.chdir(struct) iplot += 1 calcs = sorted( [d for d in os.listdir(os.getcwd()) if os.path.isdir(d)]) if len(calcs) > maxCalcs: maxCalcs = len(calcs) os.chdir(path) #external data is of the form extpath/atom_method/struct.csv. The csv has energies vs nK if not extpath is None: os.chdir(extpath) atoms_methods = sorted([ d for d in os.listdir(extpath) if os.path.isdir(d) and filter in d ]) # os.chdir(extpath) for atom_method in atoms_methods: atom = atom_method.split('_')[0] os.chdir(atom_method) os.system('rm -r .*lock*') for structfile in os.listdir(os.getcwd()): if atom not in structfile: os.system('mv {} {}_{}'.format( structfile, atom, structfile)) #so that file has atom name at beginning if filter2 == None: structfiles = sorted([ d for d in os.listdir(os.getcwd()) if os.path.getsize(d) > 0 ]) else: structfiles = sorted([ d for d in os.listdir(os.getcwd()) if '_'.join(d.split('_')[:2]) == filter2 and os.path.getsize(d) > 0 ]) for structfile in structfiles: iplot += 1 #count number of points in this structfile lines = readfile(structfile) if len(lines) > maxCalcs: maxCalcs = len(lines) os.chdir(extpath) nplots = iplot if nplots < len(paths): sys.exit('Stop. Structures do not match filter') data = zeros(nplots,dtype = [('ID', 'S25'),('color', 'S15'),('method', 'S15'),\ ('nDone','int32'),('nAtoms','int32'),('nops','int8'),\ ('IBZvolcut','float'),('IBZvol','float'),\ ('eners', '{}float'.format(maxCalcs)), ('errs', '{}float'.format(maxCalcs)),\ ('nKs', '{}int16'.format(maxCalcs)),('ns', '{}int8'.format(maxCalcs))]) # style.use('bmh') # for i, item in enumerate(rcParams['axes.prop_cycle']): # colorsList.append(item['color']) style.use('fivethirtyeight') # for i, item in enumerate(rcParams['axes.prop_cycle'][:-2]): # colorsList.append(item['color']) colorsList = [ u'#30a2da', u'#fc4f30', u'#e5ae38', u'#6d904f', u'#8b8b8b', u'#348ABD', u'#A60628', u'#7A68A6', u'#467821', u'#D55E00', u'#CC79A7', u'#56B4E9', u'#009E73', u'#F0E442', u'#0072B2' ] colorsList = colorsList + ['b', 'm', 'y', 'c', 'k'] rcParams.update({'figure.autolayout': True}) rcParams['axes.facecolor'] = 'white' rcParams['axes.linewidth'] = 1.0 rcParams['axes.edgecolor'] = 'black' # axisbg=axescolor rcParams['savefig.facecolor'] = 'white' # axisbg=axescolor rcParams['lines.markersize'] = 4.5 #read all the data iplot = -1 for ipath, path in enumerate(paths): #my data tag = path.split('/')[-1][-7:] os.chdir(path) if filter2 == None: structs = sorted([ d for d in os.listdir(os.getcwd()) if os.path.isdir(d) and filter in d ]) else: structs = sorted([ d for d in os.listdir(os.getcwd()) if os.path.isdir(d) and d == filter2 ]) nStructs = len(structs) # print structs,path for istruct, struct in enumerate(structs): # print 'test', istruct, struct # print 'struct',struct os.chdir(struct) if coloring == 'indiv': # if iplot < nplots -1: color = rgb2hex(cm.jet(1. * (iplot + 1) / float(nplots))) # else: # color = 'k' elif coloring == 'method': # color = colorsList[ipath] color = None calcs = sorted([ d for d in os.listdir(os.getcwd()) if os.path.isdir(d) and os.path.exists('{}/OUTCAR'.format(d)) ]) energies = [] nKs = [] ns = [] #the base n of the run run nDone = 0 if useSym: try: nops, IBZvolcut, IBZvol = readSym(calcs[0]) except: sys.exit('Stopping. readSym failed. Set useSym to False') for calc in calcs: if electronicConvergeFinish(calc): ener = getEnergy(calc) #in energy/atom if not areEqual(ener, 0, 1e-5): nDone += 1 energies.append(ener) if 'vc' in path: nK = getNkIBZ(calc, 'KPOINTS') else: nK = getNkIBZ(calc, 'IBZKPT') if nK > maxNk: maxNk = nK nKs.append(nK) ns.append(int(calc.split('_')[-1])) #sort by increasing number of kpoints if len(energies) > 0: iplot += 1 nKs = array(nKs) energies = array(energies) ns = array(ns) order = argsort(nKs) # print 'struct',struct # print 'energies',energies energies = energies[order] ns = ns[order] nKs = sort(nKs) eref = energies[ -1] #the last energy of each struct is that of the most kpoints errs = abs(energies - eref) * 1000 + 1e-4 #now in meV data[iplot]['ID'] = '{} {}'.format(struct, tag) nAtoms = getNatoms('{}/POSCAR'.format(calc)) data[iplot]['nAtoms'] = nAtoms if useSym: data[iplot]['nops'] = nops data[iplot]['IBZvolcut'] = IBZvolcut data[iplot]['nDone'] = nDone data[iplot]['eners'][:nDone] = energies data[iplot]['errs'][:nDone] = errs data[iplot]['nKs'][:nDone] = nKs data[iplot]['ns'][:nDone] = ns data[iplot]['color'] = color method = path.split('_')[-1].split('/')[0] data[iplot]['method'] = method os.chdir(path) # os.chdir(extpath) if not extpath is None: os.chdir(extpath) # print; print atoms_methods for atom_method in atoms_methods: os.chdir(atom_method) if coloring == 'method': color = None if 'MP' in atom_method: # color = colorsList[len(paths)] method = 'MP' elif 'Mueller' in atom_method: # color = colorsList[len(paths)+1] method = 'Mueller' if method not in methods: methods.append(method) if filter2 == None: structfiles = sorted([ d for d in os.listdir(os.getcwd()) if os.path.getsize(d) > 0 ]) else: structfiles = sorted([ d for d in os.listdir(os.getcwd()) if '_'.join(d.split('_')[:2]) == filter2 and os.path.getsize(d) > 0 ]) for structfile in structfiles: if useSym: nops, IBZvolcut, nAtoms = copyData(structfile, data) if coloring == 'indiv': if iplot < nplots - 1: color = cm.jet(1. * (iplot + 1) / float(nplots)) else: color = 'k' iplot += 1 energies = [] nKs = [] lines = readfile(structfile) for line in lines: nK = int(line.split('\t')[0]) if nK > maxNk: maxNk = nK nKs.append(nK) energies.append(-float(line.split('\t')[1].split('\r')[0])) nKs = array(nKs) energies = array(energies) nDone = len(energies) order = argsort(nKs) energies = energies[order] eref = energies[ -1] #the last energy of each struct is that of the most kpoints nKs = sort(nKs) errs = abs(energies - eref) * 1000 + 1e-4 #now in meV struct = '_'.join(structfile.split('_')[:2]) data[iplot]['ID'] = atom_method + struct data[iplot]['nAtoms'] = nAtoms if useSym: data[iplot]['nops'] = nops data[iplot]['IBZvolcut'] = IBZvolcut data[iplot]['nDone'] = len(energies) data[iplot]['eners'][:nDone] = energies data[iplot]['errs'][:nDone] = errs data[iplot]['nKs'][:nDone] = nKs data[iplot]['color'] = color data[iplot]['method'] = method os.chdir(extpath) nplots = iplot + 1 lines = [' ID , nKIBZ , ener , err, nAtoms, nops,IBZcut\n'] for iplot in range(nplots): n = data[iplot]['nDone'] for icalc in range(n): #data[iplot]['eners'][:n].tolist() lines.append('{}_n{},{},{:15.12f},{:15.12f},{},{},{}\n'.format(data[iplot]['ID'],\ data[iplot]['ns'][icalc], data[iplot]['nKs'][icalc],\ data[iplot]['eners'][icalc],data[iplot]['errs'][icalc],\ data[iplot]['nAtoms'],data[iplot]['nops'],data[iplot]['IBZvolcut'])) writefile(lines, '{}/summary.csv'.format(summaryPath)) #plots if maxNk > 1: if filter[0] == '_': filter = '' #labels can't begin with _ # plotTypes = ['linear','loglog', 'loglinear'];ylabels = ['Vasp error energy/atom (eV)','Error (meV)','Error (meV)'] # print 'plot only loglog' plotTypes = ['loglog'] ylabels = ['Error (meV)'] # plotTypes = [] xtext = 'N k-points' for it, plotType in enumerate(plotTypes): fig = figure() ax1 = fig.add_subplot(111) xlabel(xtext) ylabel(ylabels[it]) # title('Convergence vs mesh method') #ylim((1e-12,1e0)) oldmethod = '' methods2 = [] for iplot in range(nplots): labelStr = None n = data[iplot]['nDone'] if coloring == 'method': method = data[iplot]['method'] data[iplot]['color'] = colorsList[methods.index(method)] if method != oldmethod and method not in methods2: if doLabel: labelStr = '{} {}'.format(filter, data[iplot]['method']) plotData(fig, summaryPath, data[iplot], n, plotType, filter, doLegend, labelStr) oldmethod = method labelStr = None methods2.append(method) else: plotData(fig, summaryPath, data[iplot], n, plotType, filter, doLegend, labelStr) elif coloring == 'indiv': if doLabel: labelStr = '{} {}'.format(filter, data[iplot]['ID']) plotData(data[iplot], n, plotType, filter, doLegend, labelStr) #Method averaging if coloring == 'method': # print 'Averaging, plotting method errors' nbins = int(10 * ceil(log10(maxNk))) # 10 bins per decade nKbins = array([(10.0**(1 / 10.0))**i for i in range(nbins)]) fig = figure() ax1 = fig.add_subplot(111) xlabel('N k-points (smoothed by factor {})'.format( int(smoothFactor))) ylabel('Error (meV)') methodCostsLogs = [] for im, method in enumerate(methods): methnKmax = 0 binCounts = zeros(nbins, dtype=int32) binErrs = zeros(nbins, dtype=float) costLogs = zeros( nbins, dtype=float ) # "Costs" relative to excellent Si Monkhorst Pack, which has err = 10^3/nK^3 + 10^-3 meV. for iplot in range(nplots): if data[iplot]['method'] == method: for icalc in range(data[iplot]['nDone'] - 1): nK = data[iplot]['nKs'][icalc] if nK > methnKmax: methnKmax = nK if nK > 1: for ibin in range(nbins): if abs(log10(nK/nKbins[ibin])) <= log10(smoothFactor)\ and nKbins[ibin]<= maxNk: binErrs[ibin] += data[iplot]['errs'][ icalc] costLogs[ibin] += log10( data[iplot]['errs'][icalc] / (10**3 / (nK**3.0) + 0.001)) binCounts[ibin] += 1 mask = where(binCounts > 0) binErrs2 = binErrs[mask[0]] binCounts2 = binCounts[mask[0]] nKbins2 = nKbins[mask[0]] costLogs2 = costLogs[mask[0]] nbins2 = len(nKbins2) avgErrs = [ binErrs2[ibin] / binCounts2[ibin] for ibin in range(nbins2) ] avgcostLogs = [ costLogs2[ibin] / binCounts2[ibin] for ibin in range(nbins2) ] avgcostLins = [10**avgcostLogs[ibin] for ibin in range(nbins2)] methodCostsLogs.append(mean(avgcostLogs)) loglog(nKbins2,avgErrs,label = method,\ color = colorsList[im], marker = None) loglog(nKbins2,avgcostLins,label = None,\ color = colorsList[im], marker = None,linestyle=':') # print 'Method',method, 'nKmax',methnKmax, 'avgLogCost', mean(avgcostLogs) legend(loc='lower left', prop={'size': 12}) fig.savefig('{}/methodErrs'.format(summaryPath)) close('all') if maxNk > 1: return [methodCostsLogs[0], mean(data['nDone']) ] #there is only one method when running this routine else: return [100, 0]
structs = sorted([d for d in os.listdir(os.getcwd()) if os.path.isdir(d) and filter in d]) else: structs = sorted([d for d in os.listdir(os.getcwd()) if os.path.isdir(d) and d==filter2]) for struct in structs: if collateMeshMat:meshPlots.write('(* {} *)\n'.format(struct)) os.chdir(struct) iplot += 1 calcs = sorted([d for d in os.listdir(os.getcwd()) if os.path.isdir(d)]) if len(calcs) > maxCalcs: maxCalcs = len(calcs) if collateMeshMat: for ic, calc in enumerate(calcs): os.chdir(calc) if ic == 0: os.system('cp bounds ../') if os.path.exists('cell_IBZmesh.m'): meshPlots.write('\t(* {} *)\n'.format(calc)) lines = readfile('cell_IBZmesh.m') lines.append( '\n\n') meshPlots.writelines(lines) os.chdir('../') os.chdir(path) if collateMeshMat: meshPlots.close() #external run paths are of the form extmethodpath/atom_convergence/11_atom if not extpaths is None: for ipath,extpath in enumerate(extpaths): os.chdir(extpath) method = extpath.split('/')[0] if collateMeshMat: meshPlots = open('IBZmeshPlots','w') atomdirs = sorted([d for d in os.listdir(extpath) if os.path.isdir(d) and filter in d])# os.chdir(extpath)
'''Reads formation energies in structures.in or .holdout Ignores weights, and writes for energy a scaled formation energy that spreads differences in FE for low energy, and compresses differences in FE for high FE''' #dir = '/fslhome/bch/cluster_expansion/graphene/csv1-8W/' dir = '/fslhome/bch/cluster_expansion/graphene/test1/' #infile = dir+'structures.in.441.FEsorted' infile = dir + 'structures.holdout' #infile = dir+'structures.in' #outfile = dir+'structures.in' outfile = dir + 'structures.holdout' FEs = [] indices = [] lines = readfile(infile) #Find maximum and min formation energies for i, line in enumerate(lines): if 'FE' in line: energy = float( line.split()[line.split().index('FE') + 2].split(',')[0]) #FE = 1.2578, etc. remove any "," FEs.append(energy) if '#Energy' in line: indices.append(i) #where label 'Energy' appeared FEmax = amax(FEs) FEmin = amin(FEs) indices = array(indices) #Write new Fes
latticeType, packingFraction, mink_reduce, lattvec_u,arenormal,\ unique_anorms,points_in_ppiped #,three_perp, from numpy import array, arccos, dot, cross, pi, floor, sum, sqrt, exp, log, asarray from numpy import transpose,rint,inner,multiply,size,argmin,argmax,nonzero,float64, identity from numpy import ceil,real,unravel_index from scipy.optimize import minimize from copy import copy,deepcopy fprec=float64 from numpy import zeros #use arrays, not "matrix" class #from numpy.matlib import zeros, matrix #creates np.matrix rather than array, but limited to 2-D!!!! uses *, but array uses matrixmultiply from numpy.linalg import norm, det, inv, eig from numpy.random import randint,random from itertools import combinations sys.path.append('/bluehome2/bch/pythonscripts/cluster_expansion/analysis_scripts/') from analysisToolsVasp import writeEnergiesOszicar, writedirnames, nstrip, writeNk, writeNkIBZ, \ writeElConverge, writeElSteps, writeCPUtime, enerparts, getdata, readfile, writefile, \ getms, writefermi, removezeros,getEf '''reads chgcar from dir1 and multiplies by 2 (for superlattice of volume 2 along z axis), and writes it twice into CHGCAR for dir2 ''' dir1 = '/fslhome/bch/cluster_expansion/alal/cubic_al/mp_c1,3/c1_8x8x8/BANDS/' dir2 = '/fslhome/bch/cluster_expansion/alal/cubic_al/mp_c1,3/c3_8x8x4/BANDS/' lines1 = readfile(dir1 + 'CHGCAR') lines2 = readfile(dir2 + 'CHGCAR') print 'Done'