From 262a99dce8310f0ad00f4612aa95b82aa83055a4 Mon Sep 17 00:00:00 2001 From: Junfeng Qiao Date: Mon, 7 Oct 2019 11:04:08 +0800 Subject: [PATCH 01/10] fix bugs for transforming spn file --- utility/gw2wannier90.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/utility/gw2wannier90.py b/utility/gw2wannier90.py index 4e42d3326..d51b47147 100755 --- a/utility/gw2wannier90.py +++ b/utility/gw2wannier90.py @@ -297,6 +297,7 @@ def reorder_uXu(ext,formatted=False): f_spn_out.write(header) nbnd,NK=np.array(f_spn_in.readline().split(),dtype=np.int32) f_spn_out.write(" ".join(str(x) for x in (NBND,NKPT) ) ) + f_spn_out.write("\n") else: f_spn_in = FortranFile(seedname+".spn", 'r') f_spn_out = FortranFile(seednameGW+".spn", 'w') @@ -313,24 +314,24 @@ def reorder_uXu(ext,formatted=False): indmQP,indnQP=np.tril_indices(NBND) if SPNformatted: - SPN=np.loadtxt(f_spn_in).reshape(-1) + SPN=np.loadtxt(f_spn_in).view(complex).reshape(-1) start=0 length=(3*nbnd*(nbnd+1))/2 for ik in xrange(NK): + A=np.zeros((3,nbnd,nbnd),dtype=np.complex) if SPNformatted: - A=SPN[start:start+length] + A[:,indn,indm]=SPN[start:start+length].reshape(3,nbnd*(nbnd+1)/2,order='F') start+=length else: - A=np.zeros((3,nbnd,nbnd),dtype=np.complex) - A[:,indn,indm]=f_spn_in.read_record(dtype=np.complex).reshape(3,nbnd*(nbnd+1)/2,order='F') + A[:,indn,indm]=f_spn_in.read_record(dtype=np.complex).reshape(3,nbnd*(nbnd+1)/2,order='F') A[:,indm,indn]=A[:,indn,indm].conj() check=np.einsum('ijj->',np.abs(A.imag)) if check> 1e-10: raise RuntimeError ( "REAL DIAG CHECK FAILED for spn: {0}".format(check) ) A=A[:,:,BANDSORT[ik]][:,BANDSORT[ik],:][:,indnQP,indmQP].reshape((3*NBND*(NBND+1)/2),order='F') - if formatted: - f_spn_out.write("".join("{0:26:16e} {1:26:16e}\n".format(x.real,x.imag) for x in A)) + if SPNformatted: + f_spn_out.write("".join("{0:26.16e} {1:26.16e}\n".format(x.real,x.imag) for x in A)) else: f_spn_out.write_record(A) From 981fcc74d25a097a92cd61c9e07290a173dfa87b Mon Sep 17 00:00:00 2001 From: Junfeng Qiao Date: Mon, 7 Oct 2019 11:39:33 +0800 Subject: [PATCH 02/10] fix mixed space and tab --- utility/gw2wannier90.py | 388 ++++++++++++++++++++-------------------- 1 file changed, 194 insertions(+), 194 deletions(-) diff --git a/utility/gw2wannier90.py b/utility/gw2wannier90.py index d51b47147..0b47e980a 100755 --- a/utility/gw2wannier90.py +++ b/utility/gw2wannier90.py @@ -8,16 +8,16 @@ # distribution, or http://www.gnu.org/copyleft/gpl.txt # # The webpage of the Wannier90 code is www.wannier.org -# +# # The Wannier90 code is hosted on GitHub: # # https://github.com/wannier-developers/wannier90 -# +# # Designed and tested with: Quantum Espresso and Yambo -# This interface should work with any G0W0 code +# This interface should work with any G0W0 code # Originally written by Stepan Tsirkin # Extended, developed and documented by Antimo Marrazzo -# +# # Last update February 19th, 2017 by Antimo Marrazzo (antimo.marrazzo@epfl.ch) # import numpy as np @@ -37,8 +37,8 @@ print "Options can be mmn, amn, spn, unk, uhu" print "Be careful with unformatted files, they are compiler-dependdent" print "A safer choice is to use (bigger) formatted files, in this case type:" - print "spn_formatted, uiu_formatted, uhu_formatted" - print "If no options is specified, all the matrices and files are considered" + print "spn_formatted, uiu_formatted, uhu_formatted" + print "If no options is specified, all the matrices and files are considered" exit() print '------------------------------\n' print '##############################\n' @@ -51,7 +51,7 @@ #In case of formatted spn,uIu and uHu (mmn,amn,eig are formatted by default) #NB: Formatted output is strongly reccommended! Fortran binaries are compilers dependent. -SPNformatted="spn_formatted" in targets +SPNformatted="spn_formatted" in targets UIUformatted="uiu_formatted" in targets UHUformatted="uhu_formatted" in targets UNKformatted="unk_formatted" in targets @@ -91,7 +91,7 @@ s=f.readline() if "begin nnkpts" in s: break NNB=int(f.readline()) - + KPNB=np.array([[int(f.readline().split()[1])-1 for inb in xrange(NNB)] for ikpt in xrange(NKPT)]) while True: @@ -105,7 +105,7 @@ corrections=np.loadtxt(seedname+".gw.unsorted.eig") corrections={(int(l[1])-1,int(l[0])-1):l[2] for l in corrections} -print "G0W0 QP corrections read from ",seedname+".gw.unsorted.eig" +print "G0W0 QP corrections read from ",seedname+".gw.unsorted.eig" #print corrections eigenDFT=np.loadtxt(seedname+".eig") nk=int(eigenDFT[:,1].max()) @@ -164,182 +164,182 @@ feig_out=open(seednameGW+".eig","w") for ik in xrange(NKPT): for ib in xrange(NBND): - feig_out.write(" {0:4d} {1:4d} {2:17.12f}\n".format(ib+1,ik+1,eigenDFTGW[ik,ib])) + feig_out.write(" {0:4d} {1:4d} {2:17.12f}\n".format(ib+1,ik+1,eigenDFTGW[ik,ib])) feig_out.close() print seednameGW+".eig", ' written.' ('------------------------------\n') if calcAMN: - try: - print "----------\n AMN module \n---------\n" - f_amn_out=open(seednameGW+".amn","w") - f_amn_in=open(seedname+".amn","r") - s=f_amn_in.readline().strip() - f_amn_out.write("{0}, sorted by GW quasi-particle energies on {1} \n".format(s,datetime.datetime.now().isoformat()) ) - s=f_amn_in.readline() - nb,nk,npr=np.array(s.split(),dtype=int) - assert nk==NKPT - assert nb==nbndDFT - f_amn_out.write(" {0} {1} {2} \n".format(NBND,nk,npr) ) - - AMN=np.loadtxt(f_amn_in,dtype=float)[:,3:5] - AMN=np.reshape(AMN[:,0]+AMN[:,1]*1j,(nb,npr,nk) ,order='F') - for ik in xrange(nk): - amn=AMN[BANDSORT[ik],:,ik] - for ipr in xrange(npr): - for ib in xrange(NBND): - f_amn_out.write(" {0:4d} {1:4d} {2:4d} {3:16.12f} {4:16.12f}\n".format(ib+1,ipr+1,ik+1,amn[ib,ipr].real,amn[ib,ipr].imag)) - f_amn_in.close() - f_amn_out.close() - print "----------\n AMN - OK \n---------\n" - except IOError as err: - print "WARNING: {0}.amn not written : ".format(seednameGW) ,err + try: + print "----------\n AMN module \n---------\n" + f_amn_out=open(seednameGW+".amn","w") + f_amn_in=open(seedname+".amn","r") + s=f_amn_in.readline().strip() + f_amn_out.write("{0}, sorted by GW quasi-particle energies on {1} \n".format(s,datetime.datetime.now().isoformat()) ) + s=f_amn_in.readline() + nb,nk,npr=np.array(s.split(),dtype=int) + assert nk==NKPT + assert nb==nbndDFT + f_amn_out.write(" {0} {1} {2} \n".format(NBND,nk,npr) ) + + AMN=np.loadtxt(f_amn_in,dtype=float)[:,3:5] + AMN=np.reshape(AMN[:,0]+AMN[:,1]*1j,(nb,npr,nk) ,order='F') + for ik in xrange(nk): + amn=AMN[BANDSORT[ik],:,ik] + for ipr in xrange(npr): + for ib in xrange(NBND): + f_amn_out.write(" {0:4d} {1:4d} {2:4d} {3:16.12f} {4:16.12f}\n".format(ib+1,ipr+1,ik+1,amn[ib,ipr].real,amn[ib,ipr].imag)) + f_amn_in.close() + f_amn_out.close() + print "----------\n AMN - OK \n---------\n" + except IOError as err: + print "WARNING: {0}.amn not written : ".format(seednameGW) ,err if calcMMN: - try: - print "----------\n MMN module \n---------\n" - - f_mmn_out=open(os.path.join(seednameGW+".mmn"),"w") - f_mmn_in=open(os.path.join(seedname+".mmn"),"r") - - s=f_mmn_in.readline().strip() - f_mmn_out.write("{0}, sorted by GW quasi-particle energies on {1} \n".format(s,datetime.datetime.now().isoformat()) ) - s=f_mmn_in.readline() - nb,nk,nnb=np.array(s.split(),dtype=int) - assert nb==nbndDFT - assert nk==NKPT - f_mmn_out.write(" {0} {1} {2} \n".format(NBND,nk,nnb) ) - - MMN=[] - for ik in xrange(nk): - MMN.append([]) - for ib in xrange(nnb): - s=f_mmn_in.readline() - f_mmn_out.write(s) - ik1,ik2=(int(i)-1 for i in s.split()[:2]) - assert(ik==ik1) - assert(KPNB[ik][ib]==ik2) - tmp=np.array([[f_mmn_in.readline().split() for m in xrange(nb)] for n in xrange(nb) ],dtype=str) - tmp=np.array(tmp[BANDSORT[ik2],:,:][:,BANDSORT[ik1],:],dtype=float) - tmp=(tmp[:,:,0]+1j*tmp[:,:,1]).T - MMN[ik].append(tmp) - for n in xrange(NBND): - for m in xrange(NBND): - f_mmn_out.write( " {0:16.12f} {1:16.12f}\n".format(MMN[ik][ib][m,n].real,MMN[ik][ib][m,n].imag) ) - print "----------\n MMN OK \n---------\n" - except IOError as err: - print "WARNING: {0}.mmn not written : ".format(seednameGW),err - if calcUHU: - print "WARNING: {0}.uHu file also will not be written : ".format(seednameGW) - calcUHU=False + try: + print "----------\n MMN module \n---------\n" + + f_mmn_out=open(os.path.join(seednameGW+".mmn"),"w") + f_mmn_in=open(os.path.join(seedname+".mmn"),"r") + + s=f_mmn_in.readline().strip() + f_mmn_out.write("{0}, sorted by GW quasi-particle energies on {1} \n".format(s,datetime.datetime.now().isoformat()) ) + s=f_mmn_in.readline() + nb,nk,nnb=np.array(s.split(),dtype=int) + assert nb==nbndDFT + assert nk==NKPT + f_mmn_out.write(" {0} {1} {2} \n".format(NBND,nk,nnb) ) + + MMN=[] + for ik in xrange(nk): + MMN.append([]) + for ib in xrange(nnb): + s=f_mmn_in.readline() + f_mmn_out.write(s) + ik1,ik2=(int(i)-1 for i in s.split()[:2]) + assert(ik==ik1) + assert(KPNB[ik][ib]==ik2) + tmp=np.array([[f_mmn_in.readline().split() for m in xrange(nb)] for n in xrange(nb) ],dtype=str) + tmp=np.array(tmp[BANDSORT[ik2],:,:][:,BANDSORT[ik1],:],dtype=float) + tmp=(tmp[:,:,0]+1j*tmp[:,:,1]).T + MMN[ik].append(tmp) + for n in xrange(NBND): + for m in xrange(NBND): + f_mmn_out.write( " {0:16.12f} {1:16.12f}\n".format(MMN[ik][ib][m,n].real,MMN[ik][ib][m,n].imag) ) + print "----------\n MMN OK \n---------\n" + except IOError as err: + print "WARNING: {0}.mmn not written : ".format(seednameGW),err + if calcUHU: + print "WARNING: {0}.uHu file also will not be written : ".format(seednameGW) + calcUHU=False def reorder_uXu(ext,formatted=False): - try: - print "----------\n {0} \n----------".format(ext) - - if formatted: - f_uXu_in = open(seedname+"."+ext, 'r') - f_uXu_out = open(seednameGW+"."+ext, 'w') - header=f_uXu_in.readline() - f_uXu_out.write(header) - nbnd,NK,nnb=np.array(f_uXu_in.readline().split(),dtype=int) - f_uXu_out.write(" ".join(str(x) for x in [NBND,NK,nnb])+"\n") - else: - f_uXu_in = FortranFile(seedname+"."+ext, 'r') - f_uXu_out = FortranFile(seednameGW+"."+ext, 'w') - header=f_uXu_in.read_record(dtype='c') - f_uXu_out.write_record(header) - nbnd,NK,nnb=np.array(f_uXu_in.read_record(dtype=np.int32)) - f_uXu_out.write_record(np.array([NBND,NK,nnb],dtype=np.int32)) - - assert nbnd==nbndDFT - print nbnd,NK,nnb - - if formatted: - uXu=np.loadtxt(f_uXu_in).reshape(-1) - start=0 - length=nbnd*nbnd - - for ik in xrange(NKPT): - for ib2 in xrange(nnb): - for ib1 in xrange(nnb): - if formatted: - A=uXu[start:start+length] - start+=length - else: - A=f_uXu_in.read_record(dtype=np.complex) - A=(A.reshape(nbnd,nbnd,order='F')[BANDSORT[KPNB[ik][ib2]],:][:,BANDSORT[KPNB[ik][ib1]]]+ - np.einsum('ln,lm,l->nm',MMN[ik][ib2].conj(),MMN[ik][ib1],eigenDE[ik]) ).reshape(-1,order='F') - if formatted: - f_uXu_out.write("".join("{0:26.16e} {1:26.16f}\n".format(x.real,x.imag) for x in A)) - else: - f_uXu_out.write_record(A) - f_uXu_out.close() - f_uXu_in.close() - print "----------\n {0} OK \n----------\n".format(ext) - except IOError as err: - print "WARNING: {0}.{1} not written : ".format(seednameGW,ext),err + try: + print "----------\n {0} \n----------".format(ext) + + if formatted: + f_uXu_in = open(seedname+"."+ext, 'r') + f_uXu_out = open(seednameGW+"."+ext, 'w') + header=f_uXu_in.readline() + f_uXu_out.write(header) + nbnd,NK,nnb=np.array(f_uXu_in.readline().split(),dtype=int) + f_uXu_out.write(" ".join(str(x) for x in [NBND,NK,nnb])+"\n") + else: + f_uXu_in = FortranFile(seedname+"."+ext, 'r') + f_uXu_out = FortranFile(seednameGW+"."+ext, 'w') + header=f_uXu_in.read_record(dtype='c') + f_uXu_out.write_record(header) + nbnd,NK,nnb=np.array(f_uXu_in.read_record(dtype=np.int32)) + f_uXu_out.write_record(np.array([NBND,NK,nnb],dtype=np.int32)) + + assert nbnd==nbndDFT + print nbnd,NK,nnb + + if formatted: + uXu=np.loadtxt(f_uXu_in).reshape(-1) + start=0 + length=nbnd*nbnd + + for ik in xrange(NKPT): + for ib2 in xrange(nnb): + for ib1 in xrange(nnb): + if formatted: + A=uXu[start:start+length] + start+=length + else: + A=f_uXu_in.read_record(dtype=np.complex) + A=(A.reshape(nbnd,nbnd,order='F')[BANDSORT[KPNB[ik][ib2]],:][:,BANDSORT[KPNB[ik][ib1]]]+ + np.einsum('ln,lm,l->nm',MMN[ik][ib2].conj(),MMN[ik][ib1],eigenDE[ik]) ).reshape(-1,order='F') + if formatted: + f_uXu_out.write("".join("{0:26.16e} {1:26.16f}\n".format(x.real,x.imag) for x in A)) + else: + f_uXu_out.write_record(A) + f_uXu_out.close() + f_uXu_in.close() + print "----------\n {0} OK \n----------\n".format(ext) + except IOError as err: + print "WARNING: {0}.{1} not written : ".format(seednameGW,ext),err if calcUHU: reorder_uXu("uHu",UHUformatted) if calcUIU: reorder_uXu("uIu",UIUformatted) if calcSPN: - try: - print "----------\n SPN \n---------\n" - - if SPNformatted: - f_spn_in = open(seedname+".spn", 'r') - f_spn_out = open(seednameGW+".spn", 'w') - header=f_spn_in.readline() - f_spn_out.write(header) - nbnd,NK=np.array(f_spn_in.readline().split(),dtype=np.int32) - f_spn_out.write(" ".join(str(x) for x in (NBND,NKPT) ) ) - f_spn_out.write("\n") - else: - f_spn_in = FortranFile(seedname+".spn", 'r') - f_spn_out = FortranFile(seednameGW+".spn", 'w') - header=f_spn_in.read_record(dtype='c') - f_spn_out.write_record(header) - nbnd,NK=f_spn_in.read_record(dtype=np.int32) - f_spn_out.write_record(np.array([NBND,NKPT],dtype=np.int32)) - - print "".join(header) - assert nbnd==nbndDFT - - - indm,indn=np.tril_indices(nbnd) - indmQP,indnQP=np.tril_indices(NBND) - - if SPNformatted: - SPN=np.loadtxt(f_spn_in).view(complex).reshape(-1) - start=0 - length=(3*nbnd*(nbnd+1))/2 - - for ik in xrange(NK): - A=np.zeros((3,nbnd,nbnd),dtype=np.complex) - if SPNformatted: - A[:,indn,indm]=SPN[start:start+length].reshape(3,nbnd*(nbnd+1)/2,order='F') - start+=length - else: - A[:,indn,indm]=f_spn_in.read_record(dtype=np.complex).reshape(3,nbnd*(nbnd+1)/2,order='F') - A[:,indm,indn]=A[:,indn,indm].conj() - check=np.einsum('ijj->',np.abs(A.imag)) - if check> 1e-10: - raise RuntimeError ( "REAL DIAG CHECK FAILED for spn: {0}".format(check) ) - A=A[:,:,BANDSORT[ik]][:,BANDSORT[ik],:][:,indnQP,indmQP].reshape((3*NBND*(NBND+1)/2),order='F') - if SPNformatted: - f_spn_out.write("".join("{0:26.16e} {1:26.16e}\n".format(x.real,x.imag) for x in A)) - else: - f_spn_out.write_record(A) - - f_spn_in.close() - f_spn_out.close() - print "----------\n SPN OK \n---------\n" - except IOError as err: - print "WARNING: {0}.spn not written : ".format(seednameGW) ,err + try: + print "----------\n SPN \n---------\n" + + if SPNformatted: + f_spn_in = open(seedname+".spn", 'r') + f_spn_out = open(seednameGW+".spn", 'w') + header=f_spn_in.readline() + f_spn_out.write(header) + nbnd,NK=np.array(f_spn_in.readline().split(),dtype=np.int32) + f_spn_out.write(" ".join(str(x) for x in (NBND,NKPT) ) ) + f_spn_out.write("\n") + else: + f_spn_in = FortranFile(seedname+".spn", 'r') + f_spn_out = FortranFile(seednameGW+".spn", 'w') + header=f_spn_in.read_record(dtype='c') + f_spn_out.write_record(header) + nbnd,NK=f_spn_in.read_record(dtype=np.int32) + f_spn_out.write_record(np.array([NBND,NKPT],dtype=np.int32)) + + print "".join(header) + assert nbnd==nbndDFT + + + indm,indn=np.tril_indices(nbnd) + indmQP,indnQP=np.tril_indices(NBND) + + if SPNformatted: + SPN=np.loadtxt(f_spn_in).view(complex).reshape(-1) + start=0 + length=(3*nbnd*(nbnd+1))/2 + + for ik in xrange(NK): + A=np.zeros((3,nbnd,nbnd),dtype=np.complex) + if SPNformatted: + A[:,indn,indm]=SPN[start:start+length].reshape(3,nbnd*(nbnd+1)/2,order='F') + start+=length + else: + A[:,indn,indm]=f_spn_in.read_record(dtype=np.complex).reshape(3,nbnd*(nbnd+1)/2,order='F') + A[:,indm,indn]=A[:,indn,indm].conj() + check=np.einsum('ijj->',np.abs(A.imag)) + if check> 1e-10: + raise RuntimeError ( "REAL DIAG CHECK FAILED for spn: {0}".format(check) ) + A=A[:,:,BANDSORT[ik]][:,BANDSORT[ik],:][:,indnQP,indmQP].reshape((3*NBND*(NBND+1)/2),order='F') + if SPNformatted: + f_spn_out.write("".join("{0:26.16e} {1:26.16e}\n".format(x.real,x.imag) for x in A)) + else: + f_spn_out.write_record(A) + + f_spn_in.close() + f_spn_out.close() + print "----------\n SPN OK \n---------\n" + except IOError as err: + print "WARNING: {0}.spn not written : ".format(seednameGW) ,err if calcUNK: unkgwdir="UNK_GW" @@ -355,32 +355,32 @@ def reorder_uXu(ext,formatted=False): pass for f_unk_name in files_list: - try: - shutil.move('./'+f_unk_name,'./'+unkdftdir+'/') - if UNKformatted: - f_unk_out=open(os.path.join(unkgwdir,f_unk_name),"w") - f_unk_in=open(os.path.join(unkdftdir,f_unk_name),"r") - nr1,nr2,nr3,ik,nbnd=np.array(f_unk_in.readline().split(),dtype=int) - NR=nr1*nr2*nr3 - f_unk_out.write(" ".join(str(x) for x in (nr1,nr2,nr3,ik,NBND))+"\n") - f_unk_out.write("\n".join( - np.array([l.strip() for l in f_unk_in],dtype=str).reshape((nbnd,NR),order='C')[BANDSORT[ik-1],:].reshape(-1,order='C') ) ) - else: - f_unk_out=FortranFile(os.path.join(unkgwdir,f_unk_name),"w") - f_unk_in=FortranFile(f_unk_name,"r") - nr1,nr2,nr3,ik,nbnd=f_unk_in.read_record(dtype=np.int32) - f_unk_out.write_record(np.array([nr1,nr2,nr3,ik,NBND],dtype=np.int32)) - unk=np.array([f_unk_in.read_record(dtype=np.complex) for ib in xrange(nbnd)] )[BANDSORT[ik-1],:] - for i in xrange(NBND): - f_unk_out.write_record(unk[ib]) - f_unk_in.close() - f_unk_out.close() - shutil.move('./'+unkgwdir+'/'+f_unk_name,'./') - except IOError as err: - if err.errno==21: - pass - else: - raise err + try: + shutil.move('./'+f_unk_name,'./'+unkdftdir+'/') + if UNKformatted: + f_unk_out=open(os.path.join(unkgwdir,f_unk_name),"w") + f_unk_in=open(os.path.join(unkdftdir,f_unk_name),"r") + nr1,nr2,nr3,ik,nbnd=np.array(f_unk_in.readline().split(),dtype=int) + NR=nr1*nr2*nr3 + f_unk_out.write(" ".join(str(x) for x in (nr1,nr2,nr3,ik,NBND))+"\n") + f_unk_out.write("\n".join( + np.array([l.strip() for l in f_unk_in],dtype=str).reshape((nbnd,NR),order='C')[BANDSORT[ik-1],:].reshape(-1,order='C') ) ) + else: + f_unk_out=FortranFile(os.path.join(unkgwdir,f_unk_name),"w") + f_unk_in=FortranFile(f_unk_name,"r") + nr1,nr2,nr3,ik,nbnd=f_unk_in.read_record(dtype=np.int32) + f_unk_out.write_record(np.array([nr1,nr2,nr3,ik,NBND],dtype=np.int32)) + unk=np.array([f_unk_in.read_record(dtype=np.complex) for ib in xrange(nbnd)] )[BANDSORT[ik-1],:] + for i in xrange(NBND): + f_unk_out.write_record(unk[ib]) + f_unk_in.close() + f_unk_out.close() + shutil.move('./'+unkgwdir+'/'+f_unk_name,'./') + except IOError as err: + if err.errno==21: + pass + else: + raise err os.rmdir(unkgwdir) print 'UNK files are being reordered, old files coming from DFT are available in UNK_DFT' f_raw.close() From 119778097d938d09a2455156d850ad1c84830192 Mon Sep 17 00:00:00 2001 From: Junfeng Qiao Date: Mon, 7 Oct 2019 13:33:24 +0800 Subject: [PATCH 03/10] transform to python3 by 2to3 --- utility/gw2wannier90.py | 126 ++++++++++++++++++++-------------------- utility/k_mapper.py | 20 +++---- 2 files changed, 73 insertions(+), 73 deletions(-) diff --git a/utility/gw2wannier90.py b/utility/gw2wannier90.py index 0b47e980a..5828f7f16 100755 --- a/utility/gw2wannier90.py +++ b/utility/gw2wannier90.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python3 # # gw2wannier90 interface # @@ -31,19 +31,19 @@ argv=sys.argv if len(argv)<2: - print "### gw2wannier90 interface ###" - print "You need to provide the seedname" - print "Usage: gw2wannier90.py seedname options" - print "Options can be mmn, amn, spn, unk, uhu" - print "Be careful with unformatted files, they are compiler-dependdent" - print "A safer choice is to use (bigger) formatted files, in this case type:" - print "spn_formatted, uiu_formatted, uhu_formatted" - print "If no options is specified, all the matrices and files are considered" + print("### gw2wannier90 interface ###") + print("You need to provide the seedname") + print("Usage: gw2wannier90.py seedname options") + print("Options can be mmn, amn, spn, unk, uhu") + print("Be careful with unformatted files, they are compiler-dependdent") + print("A safer choice is to use (bigger) formatted files, in this case type:") + print("spn_formatted, uiu_formatted, uhu_formatted") + print("If no options is specified, all the matrices and files are considered") exit() -print '------------------------------\n' -print '##############################\n' -print '### gw2wannier90 interface ###\n' -print '##############################\n' +print('------------------------------\n') +print('##############################\n') +print('### gw2wannier90 interface ###\n') +print('##############################\n') seedname=argv[1] # for instance "silicon" seednameGW=seedname+".gw" #for instance "silicon.gw" @@ -83,16 +83,16 @@ s=f.readline() if "begin kpoints" in s: break NKPT=int(f.readline()) -print "Kpoints number:",NKPT +print("Kpoints number:",NKPT) n1=np.array(NKPT,dtype=int) -IKP=[tuple(np.array(np.round(np.array(f.readline().split(),dtype=float)*n1),dtype=int)) for i in xrange (NKPT)] +IKP=[tuple(np.array(np.round(np.array(f.readline().split(),dtype=float)*n1),dtype=int)) for i in range (NKPT)] while True: s=f.readline() if "begin nnkpts" in s: break NNB=int(f.readline()) -KPNB=np.array([[int(f.readline().split()[1])-1 for inb in xrange(NNB)] for ikpt in xrange(NKPT)]) +KPNB=np.array([[int(f.readline().split()[1])-1 for inb in range(NNB)] for ikpt in range(NKPT)]) while True: s=f.readline() @@ -100,12 +100,12 @@ exbands=np.array(f.readline().split(),dtype=int) if len(exbands)>1 or exbands[0]!=0: #raise RuntimeError("exclude bands is not supported yet") # actually it is OK, see below - print 'Exclude bands option is used: be careful to be consistent' + print('Exclude bands option is used: be careful to be consistent') 'with the choice of bands for the GW QP corrections.' corrections=np.loadtxt(seedname+".gw.unsorted.eig") corrections={(int(l[1])-1,int(l[0])-1):l[2] for l in corrections} -print "G0W0 QP corrections read from ",seedname+".gw.unsorted.eig" +print("G0W0 QP corrections read from ",seedname+".gw.unsorted.eig") #print corrections eigenDFT=np.loadtxt(seedname+".eig") nk=int(eigenDFT[:,1].max()) @@ -120,16 +120,16 @@ f_raw.write(str(line)+'\n') f_raw.write('------------------------------ \n') -providedGW= [ ib for ib in xrange(nbndDFT) if all( (ik,ib) in corrections.keys() for ik in xrange(NKPT)) ] +providedGW= [ ib for ib in range(nbndDFT) if all( (ik,ib) in list(corrections.keys()) for ik in range(NKPT)) ] #print providedGW f_raw.write('------------------------------\n') f_raw.write('List of provided GW corrections (bands indeces)\n') f_raw.write(str(providedGW)) f_raw.write('------------------------------\n') NBND=len(providedGW) -print "Adding GW QP corrections to KS eigenvalues" -eigenDE= np.array([ [corrections[(ik,ib)] for ib in providedGW ] for ik in xrange(NKPT)]) -eigenDFTGW= np.array([ [eigenDFT[ik,ib]+corrections[(ik,ib)] for ib in providedGW ] for ik in xrange(NKPT)]) +print("Adding GW QP corrections to KS eigenvalues") +eigenDE= np.array([ [corrections[(ik,ib)] for ib in providedGW ] for ik in range(NKPT)]) +eigenDFTGW= np.array([ [eigenDFT[ik,ib]+corrections[(ik,ib)] for ib in providedGW ] for ik in range(NKPT)]) f_raw.write('------------------------------\n') f_raw.write('Writing GW eigenvalues unsorted (KS + QP correction) \n') @@ -137,8 +137,8 @@ f_raw.write(str(line)+'\n') f_raw.write('------------------------------\n') -print "Sorting" -bsort=np.array([np.argsort(eigenDFTGW[ik,:]) for ik in xrange(NKPT)]) +print("Sorting") +bsort=np.array([np.argsort(eigenDFTGW[ik,:]) for ik in range(NKPT)]) f_raw.write('------------------------------\n') f_raw.write('Writing sorting list\n') @@ -147,9 +147,9 @@ f_raw.write('------------------------------\n') -eigenDE=np.array([eigenDE[ik][bsort[ik]] for ik in xrange(NKPT)]) -eigenDFTGW=np.array([eigenDFTGW[ik][bsort[ik]] for ik in xrange(NKPT)]) -BANDSORT=np.array([np.array(providedGW)[bsort[ik]] for ik in xrange(NKPT)]) +eigenDE=np.array([eigenDE[ik][bsort[ik]] for ik in range(NKPT)]) +eigenDFTGW=np.array([eigenDFTGW[ik][bsort[ik]] for ik in range(NKPT)]) +BANDSORT=np.array([np.array(providedGW)[bsort[ik]] for ik in range(NKPT)]) f_raw.write('------------------------------\n') f_raw.write('Writing sorted GW eigenvalues\n') @@ -157,21 +157,21 @@ f_raw.write(str(line)+'\n') f_raw.write('------------------------------\n') -print "GW eigenvalues sorted" +print("GW eigenvalues sorted") #print eigenDFT -print '------------------------------\n' -print "writing "+seednameGW+".eig" +print('------------------------------\n') +print("writing "+seednameGW+".eig") feig_out=open(seednameGW+".eig","w") -for ik in xrange(NKPT): - for ib in xrange(NBND): +for ik in range(NKPT): + for ib in range(NBND): feig_out.write(" {0:4d} {1:4d} {2:17.12f}\n".format(ib+1,ik+1,eigenDFTGW[ik,ib])) feig_out.close() -print seednameGW+".eig", ' written.' +print(seednameGW+".eig", ' written.') ('------------------------------\n') if calcAMN: try: - print "----------\n AMN module \n---------\n" + print("----------\n AMN module \n---------\n") f_amn_out=open(seednameGW+".amn","w") f_amn_in=open(seedname+".amn","r") s=f_amn_in.readline().strip() @@ -184,21 +184,21 @@ AMN=np.loadtxt(f_amn_in,dtype=float)[:,3:5] AMN=np.reshape(AMN[:,0]+AMN[:,1]*1j,(nb,npr,nk) ,order='F') - for ik in xrange(nk): + for ik in range(nk): amn=AMN[BANDSORT[ik],:,ik] - for ipr in xrange(npr): - for ib in xrange(NBND): + for ipr in range(npr): + for ib in range(NBND): f_amn_out.write(" {0:4d} {1:4d} {2:4d} {3:16.12f} {4:16.12f}\n".format(ib+1,ipr+1,ik+1,amn[ib,ipr].real,amn[ib,ipr].imag)) f_amn_in.close() f_amn_out.close() - print "----------\n AMN - OK \n---------\n" + print("----------\n AMN - OK \n---------\n") except IOError as err: - print "WARNING: {0}.amn not written : ".format(seednameGW) ,err + print("WARNING: {0}.amn not written : ".format(seednameGW) ,err) if calcMMN: try: - print "----------\n MMN module \n---------\n" + print("----------\n MMN module \n---------\n") f_mmn_out=open(os.path.join(seednameGW+".mmn"),"w") f_mmn_in=open(os.path.join(seedname+".mmn"),"r") @@ -212,32 +212,32 @@ f_mmn_out.write(" {0} {1} {2} \n".format(NBND,nk,nnb) ) MMN=[] - for ik in xrange(nk): + for ik in range(nk): MMN.append([]) - for ib in xrange(nnb): + for ib in range(nnb): s=f_mmn_in.readline() f_mmn_out.write(s) ik1,ik2=(int(i)-1 for i in s.split()[:2]) assert(ik==ik1) assert(KPNB[ik][ib]==ik2) - tmp=np.array([[f_mmn_in.readline().split() for m in xrange(nb)] for n in xrange(nb) ],dtype=str) + tmp=np.array([[f_mmn_in.readline().split() for m in range(nb)] for n in range(nb) ],dtype=str) tmp=np.array(tmp[BANDSORT[ik2],:,:][:,BANDSORT[ik1],:],dtype=float) tmp=(tmp[:,:,0]+1j*tmp[:,:,1]).T MMN[ik].append(tmp) - for n in xrange(NBND): - for m in xrange(NBND): + for n in range(NBND): + for m in range(NBND): f_mmn_out.write( " {0:16.12f} {1:16.12f}\n".format(MMN[ik][ib][m,n].real,MMN[ik][ib][m,n].imag) ) - print "----------\n MMN OK \n---------\n" + print("----------\n MMN OK \n---------\n") except IOError as err: - print "WARNING: {0}.mmn not written : ".format(seednameGW),err + print("WARNING: {0}.mmn not written : ".format(seednameGW),err) if calcUHU: - print "WARNING: {0}.uHu file also will not be written : ".format(seednameGW) + print("WARNING: {0}.uHu file also will not be written : ".format(seednameGW)) calcUHU=False def reorder_uXu(ext,formatted=False): try: - print "----------\n {0} \n----------".format(ext) + print("----------\n {0} \n----------".format(ext)) if formatted: f_uXu_in = open(seedname+"."+ext, 'r') @@ -255,16 +255,16 @@ def reorder_uXu(ext,formatted=False): f_uXu_out.write_record(np.array([NBND,NK,nnb],dtype=np.int32)) assert nbnd==nbndDFT - print nbnd,NK,nnb + print(nbnd,NK,nnb) if formatted: uXu=np.loadtxt(f_uXu_in).reshape(-1) start=0 length=nbnd*nbnd - for ik in xrange(NKPT): - for ib2 in xrange(nnb): - for ib1 in xrange(nnb): + for ik in range(NKPT): + for ib2 in range(nnb): + for ib1 in range(nnb): if formatted: A=uXu[start:start+length] start+=length @@ -278,9 +278,9 @@ def reorder_uXu(ext,formatted=False): f_uXu_out.write_record(A) f_uXu_out.close() f_uXu_in.close() - print "----------\n {0} OK \n----------\n".format(ext) + print("----------\n {0} OK \n----------\n".format(ext)) except IOError as err: - print "WARNING: {0}.{1} not written : ".format(seednameGW,ext),err + print("WARNING: {0}.{1} not written : ".format(seednameGW,ext),err) if calcUHU: reorder_uXu("uHu",UHUformatted) if calcUIU: reorder_uXu("uIu",UIUformatted) @@ -288,7 +288,7 @@ def reorder_uXu(ext,formatted=False): if calcSPN: try: - print "----------\n SPN \n---------\n" + print("----------\n SPN \n---------\n") if SPNformatted: f_spn_in = open(seedname+".spn", 'r') @@ -306,7 +306,7 @@ def reorder_uXu(ext,formatted=False): nbnd,NK=f_spn_in.read_record(dtype=np.int32) f_spn_out.write_record(np.array([NBND,NKPT],dtype=np.int32)) - print "".join(header) + print("".join(header)) assert nbnd==nbndDFT @@ -318,7 +318,7 @@ def reorder_uXu(ext,formatted=False): start=0 length=(3*nbnd*(nbnd+1))/2 - for ik in xrange(NK): + for ik in range(NK): A=np.zeros((3,nbnd,nbnd),dtype=np.complex) if SPNformatted: A[:,indn,indm]=SPN[start:start+length].reshape(3,nbnd*(nbnd+1)/2,order='F') @@ -337,9 +337,9 @@ def reorder_uXu(ext,formatted=False): f_spn_in.close() f_spn_out.close() - print "----------\n SPN OK \n---------\n" + print("----------\n SPN OK \n---------\n") except IOError as err: - print "WARNING: {0}.spn not written : ".format(seednameGW) ,err + print("WARNING: {0}.spn not written : ".format(seednameGW) ,err) if calcUNK: unkgwdir="UNK_GW" @@ -370,8 +370,8 @@ def reorder_uXu(ext,formatted=False): f_unk_in=FortranFile(f_unk_name,"r") nr1,nr2,nr3,ik,nbnd=f_unk_in.read_record(dtype=np.int32) f_unk_out.write_record(np.array([nr1,nr2,nr3,ik,NBND],dtype=np.int32)) - unk=np.array([f_unk_in.read_record(dtype=np.complex) for ib in xrange(nbnd)] )[BANDSORT[ik-1],:] - for i in xrange(NBND): + unk=np.array([f_unk_in.read_record(dtype=np.complex) for ib in range(nbnd)] )[BANDSORT[ik-1],:] + for i in range(NBND): f_unk_out.write_record(unk[ib]) f_unk_in.close() f_unk_out.close() @@ -382,5 +382,5 @@ def reorder_uXu(ext,formatted=False): else: raise err os.rmdir(unkgwdir) - print 'UNK files are being reordered, old files coming from DFT are available in UNK_DFT' + print('UNK files are being reordered, old files coming from DFT are available in UNK_DFT') f_raw.close() diff --git a/utility/k_mapper.py b/utility/k_mapper.py index 3dc074775..e3116dd0c 100755 --- a/utility/k_mapper.py +++ b/utility/k_mapper.py @@ -1,4 +1,4 @@ -#! /usr/bin/env python +#! /usr/bin/env python3 # # This file is distributed as part of the Wannier90 code and # under the terms of the GNU General Public License. See the @@ -11,7 +11,7 @@ # # https://github.com/wannier-developers/wannier90 # -# Python2 script to find the indeces of a coarse mesh into a finer mesh +# Python3 script to find the indeces of a coarse mesh into a finer mesh # provided they are commensurate. # # Written by Antimo Marrazzo (EPFL) @@ -31,7 +31,7 @@ def prepare_mesh(coarse_grid,nscf_output_file): if "number of k points=" in line: numk_line=line.strip('\n').split() num_kpoints=int(numk_line[4]) - print 'Number of kpoints provided to Yambo through a NSCF calculation', num_kpoints + print('Number of kpoints provided to Yambo through a NSCF calculation', num_kpoints) if read_kpts==True: kline=line.strip('\n').split() if 'wk' in kline: @@ -68,18 +68,18 @@ def indeces_list(fine_mesh,coarse_mesh): count=count+1 return k_list -print '####################' -print '####Mesh mapper#####' -print '####################' +print('####################') +print('####Mesh mapper#####') +print('####################') coarse_grid = sys.argv[1:4] coarse_grid = [int(i) for i in coarse_grid] -print 'Input coarse mesh:', coarse_grid +print('Input coarse mesh:', coarse_grid) nscf_output_file = sys.argv[4] -print 'Path of the QE NSCF output file', nscf_output_file +print('Path of the QE NSCF output file', nscf_output_file) (k_fine_list,k_coarse_list)=prepare_mesh(coarse_grid,nscf_output_file) ind_list=indeces_list(np.array(k_fine_list),np.array(k_coarse_list)) -print 'List of k-indeces to pass to Yambo', ind_list +print('List of k-indeces to pass to Yambo', ind_list) for i in ind_list: - print str(i)+'|'+str(i)+'|'+'first band'+'|'+'last band'+'|' + print(str(i)+'|'+str(i)+'|'+'first band'+'|'+'last band'+'|') From dabe5fd5ca9303eee26209cb74351439fb4d229d Mon Sep 17 00:00:00 2001 From: Junfeng Qiao Date: Mon, 7 Oct 2019 13:36:23 +0800 Subject: [PATCH 04/10] formatted by yapf --- utility/gw2wannier90.py | 408 +++++++++++++++++++++++----------------- utility/k_mapper.py | 68 +++---- 2 files changed, 267 insertions(+), 209 deletions(-) diff --git a/utility/gw2wannier90.py b/utility/gw2wannier90.py index 5828f7f16..0a0822f75 100755 --- a/utility/gw2wannier90.py +++ b/utility/gw2wannier90.py @@ -21,317 +21,359 @@ # Last update February 19th, 2017 by Antimo Marrazzo (antimo.marrazzo@epfl.ch) # import numpy as np -import os,shutil +import os, shutil import datetime from scipy.io import FortranFile import sys import glob +argv = sys.argv -argv=sys.argv - -if len(argv)<2: +if len(argv) < 2: print("### gw2wannier90 interface ###") print("You need to provide the seedname") print("Usage: gw2wannier90.py seedname options") print("Options can be mmn, amn, spn, unk, uhu") print("Be careful with unformatted files, they are compiler-dependdent") - print("A safer choice is to use (bigger) formatted files, in this case type:") + print( + "A safer choice is to use (bigger) formatted files, in this case type:" + ) print("spn_formatted, uiu_formatted, uhu_formatted") - print("If no options is specified, all the matrices and files are considered") + print( + "If no options is specified, all the matrices and files are considered" + ) exit() print('------------------------------\n') print('##############################\n') print('### gw2wannier90 interface ###\n') print('##############################\n') -seedname=argv[1] # for instance "silicon" -seednameGW=seedname+".gw" #for instance "silicon.gw" -targets=[s.lower() for s in argv[2:]] #options read from command line +seedname = argv[1] # for instance "silicon" +seednameGW = seedname + ".gw" #for instance "silicon.gw" +targets = [s.lower() for s in argv[2:]] #options read from command line #In case of formatted spn,uIu and uHu (mmn,amn,eig are formatted by default) #NB: Formatted output is strongly reccommended! Fortran binaries are compilers dependent. -SPNformatted="spn_formatted" in targets -UIUformatted="uiu_formatted" in targets -UHUformatted="uhu_formatted" in targets -UNKformatted="unk_formatted" in targets - -if set(targets).intersection(set(["spn","uhu","mmn","amn","unk"])) : - calcAMN="amn" in targets - calcMMN="mmn" in targets - calcUHU="uhu" in targets - calcUIU="uiu" in targets - calcSPN="spn" in targets - calcUNK="unk" in targets +SPNformatted = "spn_formatted" in targets +UIUformatted = "uiu_formatted" in targets +UHUformatted = "uhu_formatted" in targets +UNKformatted = "unk_formatted" in targets + +if set(targets).intersection(set(["spn", "uhu", "mmn", "amn", "unk"])): + calcAMN = "amn" in targets + calcMMN = "mmn" in targets + calcUHU = "uhu" in targets + calcUIU = "uiu" in targets + calcSPN = "spn" in targets + calcUNK = "unk" in targets else: - calcAMN=True - calcMMN=True - calcUHU=True - calcUIU=True - calcSPN=True - calcUNK=True + calcAMN = True + calcMMN = True + calcUHU = True + calcUIU = True + calcSPN = True + calcUNK = True -if calcUHU : calcMMN=True +if calcUHU: calcMMN = True #Here we open a file to dump all the intermediate steps (mainly for debugging) -f_raw=open(seedname+'.gw2wannier90.raw','w') +f_raw = open(seedname + '.gw2wannier90.raw', 'w') #Opening seedname.nnkp file -f=open(seedname+".nnkp","r") +f = open(seedname + ".nnkp", "r") #It copies the seedname.win for GW, we should make this optional #shutil.copy(seedname+".win",seednameGW+".win") while True: - s=f.readline() + s = f.readline() if "begin kpoints" in s: break -NKPT=int(f.readline()) -print("Kpoints number:",NKPT) -n1=np.array(NKPT,dtype=int) -IKP=[tuple(np.array(np.round(np.array(f.readline().split(),dtype=float)*n1),dtype=int)) for i in range (NKPT)] +NKPT = int(f.readline()) +print("Kpoints number:", NKPT) +n1 = np.array(NKPT, dtype=int) +IKP = [ + tuple( + np.array(np.round(np.array(f.readline().split(), dtype=float) * n1), + dtype=int)) for i in range(NKPT) +] while True: - s=f.readline() + s = f.readline() if "begin nnkpts" in s: break -NNB=int(f.readline()) +NNB = int(f.readline()) -KPNB=np.array([[int(f.readline().split()[1])-1 for inb in range(NNB)] for ikpt in range(NKPT)]) +KPNB = np.array([[int(f.readline().split()[1]) - 1 for inb in range(NNB)] + for ikpt in range(NKPT)]) while True: - s=f.readline() + s = f.readline() if "begin exclude_bands" in s: break -exbands=np.array(f.readline().split(),dtype=int) -if len(exbands)>1 or exbands[0]!=0: +exbands = np.array(f.readline().split(), dtype=int) +if len(exbands) > 1 or exbands[0] != 0: #raise RuntimeError("exclude bands is not supported yet") # actually it is OK, see below print('Exclude bands option is used: be careful to be consistent') 'with the choice of bands for the GW QP corrections.' -corrections=np.loadtxt(seedname+".gw.unsorted.eig") -corrections={(int(l[1])-1,int(l[0])-1):l[2] for l in corrections} -print("G0W0 QP corrections read from ",seedname+".gw.unsorted.eig") +corrections = np.loadtxt(seedname + ".gw.unsorted.eig") +corrections = {(int(l[1]) - 1, int(l[0]) - 1): l[2] for l in corrections} +print("G0W0 QP corrections read from ", seedname + ".gw.unsorted.eig") #print corrections -eigenDFT=np.loadtxt(seedname+".eig") -nk=int(eigenDFT[:,1].max()) -assert nk==NKPT +eigenDFT = np.loadtxt(seedname + ".eig") +nk = int(eigenDFT[:, 1].max()) +assert nk == NKPT -nbndDFT=int(eigenDFT[:,0].max()) -eigenDFT=eigenDFT[:,2].reshape(NKPT,nbndDFT,order='C') +nbndDFT = int(eigenDFT[:, 0].max()) +eigenDFT = eigenDFT[:, 2].reshape(NKPT, nbndDFT, order='C') #print eigenDFT f_raw.write('------------------------------\n') f_raw.write('Writing DFT eigenvalues \n') for line in eigenDFT: - f_raw.write(str(line)+'\n') + f_raw.write(str(line) + '\n') f_raw.write('------------------------------ \n') -providedGW= [ ib for ib in range(nbndDFT) if all( (ik,ib) in list(corrections.keys()) for ik in range(NKPT)) ] +providedGW = [ + ib for ib in range(nbndDFT) + if all((ik, ib) in list(corrections.keys()) for ik in range(NKPT)) +] #print providedGW f_raw.write('------------------------------\n') f_raw.write('List of provided GW corrections (bands indeces)\n') f_raw.write(str(providedGW)) f_raw.write('------------------------------\n') -NBND=len(providedGW) +NBND = len(providedGW) print("Adding GW QP corrections to KS eigenvalues") -eigenDE= np.array([ [corrections[(ik,ib)] for ib in providedGW ] for ik in range(NKPT)]) -eigenDFTGW= np.array([ [eigenDFT[ik,ib]+corrections[(ik,ib)] for ib in providedGW ] for ik in range(NKPT)]) +eigenDE = np.array([[corrections[(ik, ib)] for ib in providedGW] + for ik in range(NKPT)]) +eigenDFTGW = np.array( + [[eigenDFT[ik, ib] + corrections[(ik, ib)] for ib in providedGW] + for ik in range(NKPT)]) f_raw.write('------------------------------\n') f_raw.write('Writing GW eigenvalues unsorted (KS + QP correction) \n') for line in eigenDFTGW: - f_raw.write(str(line)+'\n') + f_raw.write(str(line) + '\n') f_raw.write('------------------------------\n') print("Sorting") -bsort=np.array([np.argsort(eigenDFTGW[ik,:]) for ik in range(NKPT)]) +bsort = np.array([np.argsort(eigenDFTGW[ik, :]) for ik in range(NKPT)]) f_raw.write('------------------------------\n') f_raw.write('Writing sorting list\n') for line in bsort: - f_raw.write(str(line)+'\n') + f_raw.write(str(line) + '\n') f_raw.write('------------------------------\n') - -eigenDE=np.array([eigenDE[ik][bsort[ik]] for ik in range(NKPT)]) -eigenDFTGW=np.array([eigenDFTGW[ik][bsort[ik]] for ik in range(NKPT)]) -BANDSORT=np.array([np.array(providedGW)[bsort[ik]] for ik in range(NKPT)]) +eigenDE = np.array([eigenDE[ik][bsort[ik]] for ik in range(NKPT)]) +eigenDFTGW = np.array([eigenDFTGW[ik][bsort[ik]] for ik in range(NKPT)]) +BANDSORT = np.array([np.array(providedGW)[bsort[ik]] for ik in range(NKPT)]) f_raw.write('------------------------------\n') f_raw.write('Writing sorted GW eigenvalues\n') for line in eigenDFTGW: - f_raw.write(str(line)+'\n') + f_raw.write(str(line) + '\n') f_raw.write('------------------------------\n') print("GW eigenvalues sorted") #print eigenDFT print('------------------------------\n') -print("writing "+seednameGW+".eig") -feig_out=open(seednameGW+".eig","w") +print("writing " + seednameGW + ".eig") +feig_out = open(seednameGW + ".eig", "w") for ik in range(NKPT): for ib in range(NBND): - feig_out.write(" {0:4d} {1:4d} {2:17.12f}\n".format(ib+1,ik+1,eigenDFTGW[ik,ib])) + feig_out.write(" {0:4d} {1:4d} {2:17.12f}\n".format( + ib + 1, ik + 1, eigenDFTGW[ik, ib])) feig_out.close() -print(seednameGW+".eig", ' written.') +print(seednameGW + ".eig", ' written.') ('------------------------------\n') if calcAMN: try: print("----------\n AMN module \n---------\n") - f_amn_out=open(seednameGW+".amn","w") - f_amn_in=open(seedname+".amn","r") - s=f_amn_in.readline().strip() - f_amn_out.write("{0}, sorted by GW quasi-particle energies on {1} \n".format(s,datetime.datetime.now().isoformat()) ) - s=f_amn_in.readline() - nb,nk,npr=np.array(s.split(),dtype=int) - assert nk==NKPT - assert nb==nbndDFT - f_amn_out.write(" {0} {1} {2} \n".format(NBND,nk,npr) ) - - AMN=np.loadtxt(f_amn_in,dtype=float)[:,3:5] - AMN=np.reshape(AMN[:,0]+AMN[:,1]*1j,(nb,npr,nk) ,order='F') + f_amn_out = open(seednameGW + ".amn", "w") + f_amn_in = open(seedname + ".amn", "r") + s = f_amn_in.readline().strip() + f_amn_out.write( + "{0}, sorted by GW quasi-particle energies on {1} \n".format( + s, + datetime.datetime.now().isoformat())) + s = f_amn_in.readline() + nb, nk, npr = np.array(s.split(), dtype=int) + assert nk == NKPT + assert nb == nbndDFT + f_amn_out.write(" {0} {1} {2} \n".format(NBND, nk, npr)) + + AMN = np.loadtxt(f_amn_in, dtype=float)[:, 3:5] + AMN = np.reshape(AMN[:, 0] + AMN[:, 1] * 1j, (nb, npr, nk), order='F') for ik in range(nk): - amn=AMN[BANDSORT[ik],:,ik] + amn = AMN[BANDSORT[ik], :, ik] for ipr in range(npr): for ib in range(NBND): - f_amn_out.write(" {0:4d} {1:4d} {2:4d} {3:16.12f} {4:16.12f}\n".format(ib+1,ipr+1,ik+1,amn[ib,ipr].real,amn[ib,ipr].imag)) + f_amn_out.write( + " {0:4d} {1:4d} {2:4d} {3:16.12f} {4:16.12f}\n". + format(ib + 1, ipr + 1, ik + 1, amn[ib, ipr].real, + amn[ib, ipr].imag)) f_amn_in.close() f_amn_out.close() print("----------\n AMN - OK \n---------\n") except IOError as err: - print("WARNING: {0}.amn not written : ".format(seednameGW) ,err) - + print("WARNING: {0}.amn not written : ".format(seednameGW), err) if calcMMN: try: print("----------\n MMN module \n---------\n") - f_mmn_out=open(os.path.join(seednameGW+".mmn"),"w") - f_mmn_in=open(os.path.join(seedname+".mmn"),"r") - - s=f_mmn_in.readline().strip() - f_mmn_out.write("{0}, sorted by GW quasi-particle energies on {1} \n".format(s,datetime.datetime.now().isoformat()) ) - s=f_mmn_in.readline() - nb,nk,nnb=np.array(s.split(),dtype=int) - assert nb==nbndDFT - assert nk==NKPT - f_mmn_out.write(" {0} {1} {2} \n".format(NBND,nk,nnb) ) - - MMN=[] + f_mmn_out = open(os.path.join(seednameGW + ".mmn"), "w") + f_mmn_in = open(os.path.join(seedname + ".mmn"), "r") + + s = f_mmn_in.readline().strip() + f_mmn_out.write( + "{0}, sorted by GW quasi-particle energies on {1} \n".format( + s, + datetime.datetime.now().isoformat())) + s = f_mmn_in.readline() + nb, nk, nnb = np.array(s.split(), dtype=int) + assert nb == nbndDFT + assert nk == NKPT + f_mmn_out.write(" {0} {1} {2} \n".format(NBND, nk, nnb)) + + MMN = [] for ik in range(nk): MMN.append([]) for ib in range(nnb): - s=f_mmn_in.readline() + s = f_mmn_in.readline() f_mmn_out.write(s) - ik1,ik2=(int(i)-1 for i in s.split()[:2]) - assert(ik==ik1) - assert(KPNB[ik][ib]==ik2) - tmp=np.array([[f_mmn_in.readline().split() for m in range(nb)] for n in range(nb) ],dtype=str) - tmp=np.array(tmp[BANDSORT[ik2],:,:][:,BANDSORT[ik1],:],dtype=float) - tmp=(tmp[:,:,0]+1j*tmp[:,:,1]).T + ik1, ik2 = (int(i) - 1 for i in s.split()[:2]) + assert (ik == ik1) + assert (KPNB[ik][ib] == ik2) + tmp = np.array( + [[f_mmn_in.readline().split() for m in range(nb)] + for n in range(nb)], + dtype=str) + tmp = np.array(tmp[BANDSORT[ik2], :, :][:, BANDSORT[ik1], :], + dtype=float) + tmp = (tmp[:, :, 0] + 1j * tmp[:, :, 1]).T MMN[ik].append(tmp) for n in range(NBND): for m in range(NBND): - f_mmn_out.write( " {0:16.12f} {1:16.12f}\n".format(MMN[ik][ib][m,n].real,MMN[ik][ib][m,n].imag) ) + f_mmn_out.write(" {0:16.12f} {1:16.12f}\n".format( + MMN[ik][ib][m, n].real, MMN[ik][ib][m, n].imag)) print("----------\n MMN OK \n---------\n") except IOError as err: - print("WARNING: {0}.mmn not written : ".format(seednameGW),err) + print("WARNING: {0}.mmn not written : ".format(seednameGW), err) if calcUHU: - print("WARNING: {0}.uHu file also will not be written : ".format(seednameGW)) - calcUHU=False + print("WARNING: {0}.uHu file also will not be written : ".format( + seednameGW)) + calcUHU = False -def reorder_uXu(ext,formatted=False): +def reorder_uXu(ext, formatted=False): try: print("----------\n {0} \n----------".format(ext)) if formatted: - f_uXu_in = open(seedname+"."+ext, 'r') - f_uXu_out = open(seednameGW+"."+ext, 'w') - header=f_uXu_in.readline() + f_uXu_in = open(seedname + "." + ext, 'r') + f_uXu_out = open(seednameGW + "." + ext, 'w') + header = f_uXu_in.readline() f_uXu_out.write(header) - nbnd,NK,nnb=np.array(f_uXu_in.readline().split(),dtype=int) - f_uXu_out.write(" ".join(str(x) for x in [NBND,NK,nnb])+"\n") + nbnd, NK, nnb = np.array(f_uXu_in.readline().split(), dtype=int) + f_uXu_out.write(" ".join(str(x) for x in [NBND, NK, nnb]) + "\n") else: - f_uXu_in = FortranFile(seedname+"."+ext, 'r') - f_uXu_out = FortranFile(seednameGW+"."+ext, 'w') - header=f_uXu_in.read_record(dtype='c') + f_uXu_in = FortranFile(seedname + "." + ext, 'r') + f_uXu_out = FortranFile(seednameGW + "." + ext, 'w') + header = f_uXu_in.read_record(dtype='c') f_uXu_out.write_record(header) - nbnd,NK,nnb=np.array(f_uXu_in.read_record(dtype=np.int32)) - f_uXu_out.write_record(np.array([NBND,NK,nnb],dtype=np.int32)) + nbnd, NK, nnb = np.array(f_uXu_in.read_record(dtype=np.int32)) + f_uXu_out.write_record(np.array([NBND, NK, nnb], dtype=np.int32)) - assert nbnd==nbndDFT - print(nbnd,NK,nnb) + assert nbnd == nbndDFT + print(nbnd, NK, nnb) if formatted: - uXu=np.loadtxt(f_uXu_in).reshape(-1) - start=0 - length=nbnd*nbnd + uXu = np.loadtxt(f_uXu_in).reshape(-1) + start = 0 + length = nbnd * nbnd for ik in range(NKPT): for ib2 in range(nnb): for ib1 in range(nnb): if formatted: - A=uXu[start:start+length] - start+=length + A = uXu[start:start + length] + start += length else: - A=f_uXu_in.read_record(dtype=np.complex) - A=(A.reshape(nbnd,nbnd,order='F')[BANDSORT[KPNB[ik][ib2]],:][:,BANDSORT[KPNB[ik][ib1]]]+ - np.einsum('ln,lm,l->nm',MMN[ik][ib2].conj(),MMN[ik][ib1],eigenDE[ik]) ).reshape(-1,order='F') + A = f_uXu_in.read_record(dtype=np.complex) + A = (A.reshape(nbnd, nbnd, + order='F')[BANDSORT[KPNB[ik][ib2]], :] + [:, BANDSORT[KPNB[ik][ib1]]] + + np.einsum('ln,lm,l->nm', MMN[ik][ib2].conj(), + MMN[ik][ib1], eigenDE[ik])).reshape( + -1, order='F') if formatted: - f_uXu_out.write("".join("{0:26.16e} {1:26.16f}\n".format(x.real,x.imag) for x in A)) + f_uXu_out.write("".join( + "{0:26.16e} {1:26.16f}\n".format(x.real, x.imag) + for x in A)) else: f_uXu_out.write_record(A) f_uXu_out.close() f_uXu_in.close() print("----------\n {0} OK \n----------\n".format(ext)) except IOError as err: - print("WARNING: {0}.{1} not written : ".format(seednameGW,ext),err) + print("WARNING: {0}.{1} not written : ".format(seednameGW, ext), err) -if calcUHU: reorder_uXu("uHu",UHUformatted) -if calcUIU: reorder_uXu("uIu",UIUformatted) +if calcUHU: reorder_uXu("uHu", UHUformatted) +if calcUIU: reorder_uXu("uIu", UIUformatted) if calcSPN: try: print("----------\n SPN \n---------\n") if SPNformatted: - f_spn_in = open(seedname+".spn", 'r') - f_spn_out = open(seednameGW+".spn", 'w') - header=f_spn_in.readline() + f_spn_in = open(seedname + ".spn", 'r') + f_spn_out = open(seednameGW + ".spn", 'w') + header = f_spn_in.readline() f_spn_out.write(header) - nbnd,NK=np.array(f_spn_in.readline().split(),dtype=np.int32) - f_spn_out.write(" ".join(str(x) for x in (NBND,NKPT) ) ) + nbnd, NK = np.array(f_spn_in.readline().split(), dtype=np.int32) + f_spn_out.write(" ".join(str(x) for x in (NBND, NKPT))) f_spn_out.write("\n") else: - f_spn_in = FortranFile(seedname+".spn", 'r') - f_spn_out = FortranFile(seednameGW+".spn", 'w') - header=f_spn_in.read_record(dtype='c') + f_spn_in = FortranFile(seedname + ".spn", 'r') + f_spn_out = FortranFile(seednameGW + ".spn", 'w') + header = f_spn_in.read_record(dtype='c') f_spn_out.write_record(header) - nbnd,NK=f_spn_in.read_record(dtype=np.int32) - f_spn_out.write_record(np.array([NBND,NKPT],dtype=np.int32)) + nbnd, NK = f_spn_in.read_record(dtype=np.int32) + f_spn_out.write_record(np.array([NBND, NKPT], dtype=np.int32)) print("".join(header)) - assert nbnd==nbndDFT - + assert nbnd == nbndDFT - indm,indn=np.tril_indices(nbnd) - indmQP,indnQP=np.tril_indices(NBND) + indm, indn = np.tril_indices(nbnd) + indmQP, indnQP = np.tril_indices(NBND) if SPNformatted: - SPN=np.loadtxt(f_spn_in).view(complex).reshape(-1) - start=0 - length=(3*nbnd*(nbnd+1))/2 + SPN = np.loadtxt(f_spn_in).view(complex).reshape(-1) + start = 0 + length = (3 * nbnd * (nbnd + 1)) / 2 for ik in range(NK): - A=np.zeros((3,nbnd,nbnd),dtype=np.complex) + A = np.zeros((3, nbnd, nbnd), dtype=np.complex) if SPNformatted: - A[:,indn,indm]=SPN[start:start+length].reshape(3,nbnd*(nbnd+1)/2,order='F') - start+=length + A[:, indn, indm] = SPN[start:start + length].reshape( + 3, nbnd * (nbnd + 1) / 2, order='F') + start += length else: - A[:,indn,indm]=f_spn_in.read_record(dtype=np.complex).reshape(3,nbnd*(nbnd+1)/2,order='F') - A[:,indm,indn]=A[:,indn,indm].conj() - check=np.einsum('ijj->',np.abs(A.imag)) - if check> 1e-10: - raise RuntimeError ( "REAL DIAG CHECK FAILED for spn: {0}".format(check) ) - A=A[:,:,BANDSORT[ik]][:,BANDSORT[ik],:][:,indnQP,indmQP].reshape((3*NBND*(NBND+1)/2),order='F') + A[:, indn, indm] = f_spn_in.read_record( + dtype=np.complex).reshape(3, + nbnd * (nbnd + 1) / 2, + order='F') + A[:, indm, indn] = A[:, indn, indm].conj() + check = np.einsum('ijj->', np.abs(A.imag)) + if check > 1e-10: + raise RuntimeError( + "REAL DIAG CHECK FAILED for spn: {0}".format(check)) + A = A[:, :, + BANDSORT[ik]][:, BANDSORT[ik], :][:, indnQP, indmQP].reshape( + (3 * NBND * (NBND + 1) / 2), order='F') if SPNformatted: - f_spn_out.write("".join("{0:26.16e} {1:26.16e}\n".format(x.real,x.imag) for x in A)) + f_spn_out.write("".join( + "{0:26.16e} {1:26.16e}\n".format(x.real, x.imag) + for x in A)) else: f_spn_out.write_record(A) @@ -339,13 +381,13 @@ def reorder_uXu(ext,formatted=False): f_spn_out.close() print("----------\n SPN OK \n---------\n") except IOError as err: - print("WARNING: {0}.spn not written : ".format(seednameGW) ,err) + print("WARNING: {0}.spn not written : ".format(seednameGW), err) if calcUNK: - unkgwdir="UNK_GW" - unkdftdir="UNK_DFT" - files_list=[] - for f_unk_name in glob.glob("UNK*"): + unkgwdir = "UNK_GW" + unkdftdir = "UNK_DFT" + files_list = [] + for f_unk_name in glob.glob("UNK*"): files_list.append(f_unk_name) try: @@ -356,31 +398,43 @@ def reorder_uXu(ext,formatted=False): for f_unk_name in files_list: try: - shutil.move('./'+f_unk_name,'./'+unkdftdir+'/') + shutil.move('./' + f_unk_name, './' + unkdftdir + '/') if UNKformatted: - f_unk_out=open(os.path.join(unkgwdir,f_unk_name),"w") - f_unk_in=open(os.path.join(unkdftdir,f_unk_name),"r") - nr1,nr2,nr3,ik,nbnd=np.array(f_unk_in.readline().split(),dtype=int) - NR=nr1*nr2*nr3 - f_unk_out.write(" ".join(str(x) for x in (nr1,nr2,nr3,ik,NBND))+"\n") + f_unk_out = open(os.path.join(unkgwdir, f_unk_name), "w") + f_unk_in = open(os.path.join(unkdftdir, f_unk_name), "r") + nr1, nr2, nr3, ik, nbnd = np.array(f_unk_in.readline().split(), + dtype=int) + NR = nr1 * nr2 * nr3 + f_unk_out.write(" ".join( + str(x) for x in (nr1, nr2, nr3, ik, NBND)) + "\n") f_unk_out.write("\n".join( - np.array([l.strip() for l in f_unk_in],dtype=str).reshape((nbnd,NR),order='C')[BANDSORT[ik-1],:].reshape(-1,order='C') ) ) + np.array([l.strip() for l in f_unk_in], dtype=str).reshape( + (nbnd, NR), + order='C')[BANDSORT[ik - 1], :].reshape(-1, + order='C'))) else: - f_unk_out=FortranFile(os.path.join(unkgwdir,f_unk_name),"w") - f_unk_in=FortranFile(f_unk_name,"r") - nr1,nr2,nr3,ik,nbnd=f_unk_in.read_record(dtype=np.int32) - f_unk_out.write_record(np.array([nr1,nr2,nr3,ik,NBND],dtype=np.int32)) - unk=np.array([f_unk_in.read_record(dtype=np.complex) for ib in range(nbnd)] )[BANDSORT[ik-1],:] + f_unk_out = FortranFile(os.path.join(unkgwdir, f_unk_name), + "w") + f_unk_in = FortranFile(f_unk_name, "r") + nr1, nr2, nr3, ik, nbnd = f_unk_in.read_record(dtype=np.int32) + f_unk_out.write_record( + np.array([nr1, nr2, nr3, ik, NBND], dtype=np.int32)) + unk = np.array([ + f_unk_in.read_record(dtype=np.complex) + for ib in range(nbnd) + ])[BANDSORT[ik - 1], :] for i in range(NBND): f_unk_out.write_record(unk[ib]) f_unk_in.close() f_unk_out.close() - shutil.move('./'+unkgwdir+'/'+f_unk_name,'./') + shutil.move('./' + unkgwdir + '/' + f_unk_name, './') except IOError as err: - if err.errno==21: + if err.errno == 21: pass else: raise err os.rmdir(unkgwdir) - print('UNK files are being reordered, old files coming from DFT are available in UNK_DFT') + print( + 'UNK files are being reordered, old files coming from DFT are available in UNK_DFT' + ) f_raw.close() diff --git a/utility/k_mapper.py b/utility/k_mapper.py index e3116dd0c..adb3a5e65 100755 --- a/utility/k_mapper.py +++ b/utility/k_mapper.py @@ -20,54 +20,58 @@ import sys import numpy as np -def prepare_mesh(coarse_grid,nscf_output_file): + +def prepare_mesh(coarse_grid, nscf_output_file): import subprocess with open(nscf_output_file, 'r') as f: read_data = f.readlines() f.close() - read_kpts=False - k_fine_list=[] + read_kpts = False + k_fine_list = [] for line in read_data: if "number of k points=" in line: - numk_line=line.strip('\n').split() - num_kpoints=int(numk_line[4]) - print('Number of kpoints provided to Yambo through a NSCF calculation', num_kpoints) - if read_kpts==True: - kline=line.strip('\n').split() + numk_line = line.strip('\n').split() + num_kpoints = int(numk_line[4]) + print( + 'Number of kpoints provided to Yambo through a NSCF calculation', + num_kpoints) + if read_kpts == True: + kline = line.strip('\n').split() if 'wk' in kline: - a=kline[4:6] - b=kline[6].split(')')[0] - k_vec=[float(a[0]),float(a[1]),float(b)] + a = kline[4:6] + b = kline[6].split(')')[0] + k_vec = [float(a[0]), float(a[1]), float(b)] k_fine_list.append(k_vec) else: - read_kpts=False + read_kpts = False if "cryst. coord." in line and 'site' not in line: - read_kpts=True - coarse_text=[str(i)+' ' for i in coarse_grid] - k_coarse_mesh=subprocess.check_output(['./kmesh.pl', - coarse_text[0],coarse_text[1],coarse_text[2], - 'wan']) - k_coarse_mesh=k_coarse_mesh.split('\n') - k_coarse_list=[] - for i in range(coarse_grid[0]*coarse_grid[1]*coarse_grid[2]): - line=k_coarse_mesh[i].split() + read_kpts = True + coarse_text = [str(i) + ' ' for i in coarse_grid] + k_coarse_mesh = subprocess.check_output( + ['./kmesh.pl', coarse_text[0], coarse_text[1], coarse_text[2], 'wan']) + k_coarse_mesh = k_coarse_mesh.split('\n') + k_coarse_list = [] + for i in range(coarse_grid[0] * coarse_grid[1] * coarse_grid[2]): + line = k_coarse_mesh[i].split() k_coarse_list.append([float(j) for j in line]) - return (k_fine_list,k_coarse_list) + return (k_fine_list, k_coarse_list) + -def indeces_list(fine_mesh,coarse_mesh): +def indeces_list(fine_mesh, coarse_mesh): import numpy as np - opt=np.array([0,1,-1]) - k_list=[] + opt = np.array([0, 1, -1]) + k_list = [] for i in coarse_mesh: - count=1 + count = 1 for j in fine_mesh: - q=i-j - q=np.around(q,decimals=5) + q = i - j + q = np.around(q, decimals=5) if (q[0] in opt and q[1] in opt and q[2] in opt): k_list.append(count) - count=count+1 + count = count + 1 return k_list + print('####################') print('####Mesh mapper#####') print('####################') @@ -77,9 +81,9 @@ def indeces_list(fine_mesh,coarse_mesh): nscf_output_file = sys.argv[4] print('Path of the QE NSCF output file', nscf_output_file) -(k_fine_list,k_coarse_list)=prepare_mesh(coarse_grid,nscf_output_file) +(k_fine_list, k_coarse_list) = prepare_mesh(coarse_grid, nscf_output_file) -ind_list=indeces_list(np.array(k_fine_list),np.array(k_coarse_list)) +ind_list = indeces_list(np.array(k_fine_list), np.array(k_coarse_list)) print('List of k-indeces to pass to Yambo', ind_list) for i in ind_list: - print(str(i)+'|'+str(i)+'|'+'first band'+'|'+'last band'+'|') + print(str(i) + '|' + str(i) + '|' + 'first band' + '|' + 'last band' + '|') From d0f0e6b5bbadd5a135451d20b82c7aa369855b68 Mon Sep 17 00:00:00 2001 From: Junfeng Qiao Date: Mon, 7 Oct 2019 13:43:51 +0800 Subject: [PATCH 05/10] fix spelling error --- utility/gw2wannier90.py | 4 ++-- utility/k_mapper.py | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/utility/gw2wannier90.py b/utility/gw2wannier90.py index 0a0822f75..8df8d17ea 100755 --- a/utility/gw2wannier90.py +++ b/utility/gw2wannier90.py @@ -34,7 +34,7 @@ print("You need to provide the seedname") print("Usage: gw2wannier90.py seedname options") print("Options can be mmn, amn, spn, unk, uhu") - print("Be careful with unformatted files, they are compiler-dependdent") + print("Be careful with unformatted files, they are compiler-dependent") print( "A safer choice is to use (bigger) formatted files, in this case type:" ) @@ -134,7 +134,7 @@ ] #print providedGW f_raw.write('------------------------------\n') -f_raw.write('List of provided GW corrections (bands indeces)\n') +f_raw.write('List of provided GW corrections (bands indexes)\n') f_raw.write(str(providedGW)) f_raw.write('------------------------------\n') NBND = len(providedGW) diff --git a/utility/k_mapper.py b/utility/k_mapper.py index adb3a5e65..738663a52 100755 --- a/utility/k_mapper.py +++ b/utility/k_mapper.py @@ -11,7 +11,7 @@ # # https://github.com/wannier-developers/wannier90 # -# Python3 script to find the indeces of a coarse mesh into a finer mesh +# Python3 script to find the indexes of a coarse mesh into a finer mesh # provided they are commensurate. # # Written by Antimo Marrazzo (EPFL) @@ -57,7 +57,7 @@ def prepare_mesh(coarse_grid, nscf_output_file): return (k_fine_list, k_coarse_list) -def indeces_list(fine_mesh, coarse_mesh): +def indexes_list(fine_mesh, coarse_mesh): import numpy as np opt = np.array([0, 1, -1]) k_list = [] @@ -83,7 +83,7 @@ def indeces_list(fine_mesh, coarse_mesh): (k_fine_list, k_coarse_list) = prepare_mesh(coarse_grid, nscf_output_file) -ind_list = indeces_list(np.array(k_fine_list), np.array(k_coarse_list)) -print('List of k-indeces to pass to Yambo', ind_list) +ind_list = indexes_list(np.array(k_fine_list), np.array(k_coarse_list)) +print('List of k-indexes to pass to Yambo', ind_list) for i in ind_list: print(str(i) + '|' + str(i) + '|' + 'first band' + '|' + 'last band' + '|') From 932a17daafa84f431ec0e487fc6ccb56c724fe5e Mon Sep 17 00:00:00 2001 From: Junfeng Qiao Date: Mon, 7 Oct 2019 16:43:38 +0800 Subject: [PATCH 06/10] use __file__ to locate kmesh.pl, so k_mapper.py can be executed in any directory --- utility/k_mapper.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/utility/k_mapper.py b/utility/k_mapper.py index 738663a52..409754938 100755 --- a/utility/k_mapper.py +++ b/utility/k_mapper.py @@ -19,6 +19,7 @@ # import sys import numpy as np +import os def prepare_mesh(coarse_grid, nscf_output_file): @@ -47,8 +48,10 @@ def prepare_mesh(coarse_grid, nscf_output_file): if "cryst. coord." in line and 'site' not in line: read_kpts = True coarse_text = [str(i) + ' ' for i in coarse_grid] - k_coarse_mesh = subprocess.check_output( - ['./kmesh.pl', coarse_text[0], coarse_text[1], coarse_text[2], 'wan']) + k_coarse_mesh = subprocess.check_output([ + os.path.join(os.path.dirname(os.path.realpath(__file__)), 'kmesh.pl'), + coarse_text[0], coarse_text[1], coarse_text[2], 'wan' + ]) k_coarse_mesh = k_coarse_mesh.split('\n') k_coarse_list = [] for i in range(coarse_grid[0] * coarse_grid[1] * coarse_grid[2]): From f6de97e015e34f4897afa2b9d345377456036a8a Mon Sep 17 00:00:00 2001 From: Junfeng Qiao Date: Mon, 7 Oct 2019 19:31:36 +0800 Subject: [PATCH 07/10] transformed to python 3 syntax --- utility/gw2wannier90.py | 14 ++++++++------ utility/k_mapper.py | 3 ++- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/utility/gw2wannier90.py b/utility/gw2wannier90.py index 8df8d17ea..fbdd7b2e9 100755 --- a/utility/gw2wannier90.py +++ b/utility/gw2wannier90.py @@ -59,7 +59,7 @@ UHUformatted = "uhu_formatted" in targets UNKformatted = "unk_formatted" in targets -if set(targets).intersection(set(["spn", "uhu", "mmn", "amn", "unk"])): +if set(targets).intersection(set(["spn", "uhu", "mmn", "amn", "unk", "uiu"])): calcAMN = "amn" in targets calcMMN = "mmn" in targets calcUHU = "uhu" in targets @@ -75,6 +75,7 @@ calcUNK = True if calcUHU: calcMMN = True +if calcUIU: calcMMN = True #Here we open a file to dump all the intermediate steps (mainly for debugging) f_raw = open(seedname + '.gw2wannier90.raw', 'w') @@ -337,6 +338,7 @@ def reorder_uXu(ext, formatted=False): f_spn_out = FortranFile(seednameGW + ".spn", 'w') header = f_spn_in.read_record(dtype='c') f_spn_out.write_record(header) + header = header.astype(str) nbnd, NK = f_spn_in.read_record(dtype=np.int32) f_spn_out.write_record(np.array([NBND, NKPT], dtype=np.int32)) @@ -349,18 +351,18 @@ def reorder_uXu(ext, formatted=False): if SPNformatted: SPN = np.loadtxt(f_spn_in).view(complex).reshape(-1) start = 0 - length = (3 * nbnd * (nbnd + 1)) / 2 + length = (3 * nbnd * (nbnd + 1)) // 2 for ik in range(NK): A = np.zeros((3, nbnd, nbnd), dtype=np.complex) if SPNformatted: - A[:, indn, indm] = SPN[start:start + length].reshape( - 3, nbnd * (nbnd + 1) / 2, order='F') + A[:, indn, indm] = SPN[start:(start + length)].reshape( + 3, nbnd * (nbnd + 1) // 2, order='F') start += length else: A[:, indn, indm] = f_spn_in.read_record( dtype=np.complex).reshape(3, - nbnd * (nbnd + 1) / 2, + nbnd * (nbnd + 1) // 2, order='F') A[:, indm, indn] = A[:, indn, indm].conj() check = np.einsum('ijj->', np.abs(A.imag)) @@ -369,7 +371,7 @@ def reorder_uXu(ext, formatted=False): "REAL DIAG CHECK FAILED for spn: {0}".format(check)) A = A[:, :, BANDSORT[ik]][:, BANDSORT[ik], :][:, indnQP, indmQP].reshape( - (3 * NBND * (NBND + 1) / 2), order='F') + (3 * NBND * (NBND + 1) // 2), order='F') if SPNformatted: f_spn_out.write("".join( "{0:26.16e} {1:26.16e}\n".format(x.real, x.imag) diff --git a/utility/k_mapper.py b/utility/k_mapper.py index 409754938..205791d34 100755 --- a/utility/k_mapper.py +++ b/utility/k_mapper.py @@ -51,7 +51,8 @@ def prepare_mesh(coarse_grid, nscf_output_file): k_coarse_mesh = subprocess.check_output([ os.path.join(os.path.dirname(os.path.realpath(__file__)), 'kmesh.pl'), coarse_text[0], coarse_text[1], coarse_text[2], 'wan' - ]) + ], + universal_newlines=True) k_coarse_mesh = k_coarse_mesh.split('\n') k_coarse_list = [] for i in range(coarse_grid[0] * coarse_grid[1] * coarse_grid[2]): From e535e0b9c19dbf3dc176977b2aac7ce6ef53328b Mon Sep 17 00:00:00 2001 From: Junfeng Qiao Date: Mon, 7 Oct 2019 21:50:31 +0800 Subject: [PATCH 08/10] add option write_formatted --- utility/gw2wannier90.py | 76 ++++++++++++++++++++++++++++------------- 1 file changed, 52 insertions(+), 24 deletions(-) diff --git a/utility/gw2wannier90.py b/utility/gw2wannier90.py index fbdd7b2e9..24bc688cb 100755 --- a/utility/gw2wannier90.py +++ b/utility/gw2wannier90.py @@ -18,7 +18,8 @@ # Originally written by Stepan Tsirkin # Extended, developed and documented by Antimo Marrazzo # -# Last update February 19th, 2017 by Antimo Marrazzo (antimo.marrazzo@epfl.ch) +# Updated on February 19th, 2017 by Antimo Marrazzo (antimo.marrazzo@epfl.ch) +# Updated on October 7th, 2019 by Junfeng Qiao (qiaojunfeng@outlook.com) # import numpy as np import os, shutil @@ -38,10 +39,13 @@ print( "A safer choice is to use (bigger) formatted files, in this case type:" ) - print("spn_formatted, uiu_formatted, uhu_formatted") + print("spn_formatted, uiu_formatted, uhu_formatted, unk_formatted") print( "If no options is specified, all the matrices and files are considered" ) + print("In default, the output format is determined by the input format") + print("To generate formatted files with unformatted input, use option: " + + "write_formatted") exit() print('------------------------------\n') print('##############################\n') @@ -52,12 +56,13 @@ seednameGW = seedname + ".gw" #for instance "silicon.gw" targets = [s.lower() for s in argv[2:]] #options read from command line -#In case of formatted spn,uIu and uHu (mmn,amn,eig are formatted by default) +#In case of formatted spn, uIu, uHu and UNK (mmn, amn, eig are formatted by default) #NB: Formatted output is strongly reccommended! Fortran binaries are compilers dependent. SPNformatted = "spn_formatted" in targets UIUformatted = "uiu_formatted" in targets UHUformatted = "uhu_formatted" in targets UNKformatted = "unk_formatted" in targets +write_formatted = "write_formatted" in targets if set(targets).intersection(set(["spn", "uhu", "mmn", "amn", "unk", "uiu"])): calcAMN = "amn" in targets @@ -109,20 +114,20 @@ exbands = np.array(f.readline().split(), dtype=int) if len(exbands) > 1 or exbands[0] != 0: #raise RuntimeError("exclude bands is not supported yet") # actually it is OK, see below - print('Exclude bands option is used: be careful to be consistent') - 'with the choice of bands for the GW QP corrections.' + print('Exclude bands option is used: be careful to be consistent ' + + 'with the choice of bands for the GW QP corrections.') corrections = np.loadtxt(seedname + ".gw.unsorted.eig") corrections = {(int(l[1]) - 1, int(l[0]) - 1): l[2] for l in corrections} print("G0W0 QP corrections read from ", seedname + ".gw.unsorted.eig") -#print corrections +#print(corrections) eigenDFT = np.loadtxt(seedname + ".eig") nk = int(eigenDFT[:, 1].max()) assert nk == NKPT nbndDFT = int(eigenDFT[:, 0].max()) eigenDFT = eigenDFT[:, 2].reshape(NKPT, nbndDFT, order='C') -#print eigenDFT +#print(eigenDFT) f_raw.write('------------------------------\n') f_raw.write('Writing DFT eigenvalues \n') for line in eigenDFT: @@ -133,7 +138,7 @@ ib for ib in range(nbndDFT) if all((ik, ib) in list(corrections.keys()) for ik in range(NKPT)) ] -#print providedGW +#print(providedGW) f_raw.write('------------------------------\n') f_raw.write('List of provided GW corrections (bands indexes)\n') f_raw.write(str(providedGW)) @@ -277,11 +282,18 @@ def reorder_uXu(ext, formatted=False): f_uXu_out.write(" ".join(str(x) for x in [NBND, NK, nnb]) + "\n") else: f_uXu_in = FortranFile(seedname + "." + ext, 'r') - f_uXu_out = FortranFile(seednameGW + "." + ext, 'w') header = f_uXu_in.read_record(dtype='c') - f_uXu_out.write_record(header) nbnd, NK, nnb = np.array(f_uXu_in.read_record(dtype=np.int32)) - f_uXu_out.write_record(np.array([NBND, NK, nnb], dtype=np.int32)) + if write_formatted: + f_uXu_out = open(seednameGW + "." + ext, 'w') + f_uXu_out.write("".join(header.astype(str))) + f_uXu_out.write('\n') + f_uXu_out.write(np.array([NBND, NK, nnb], dtype=np.int32)) + f_uXu_out.write('\n') + else: + f_uXu_out = FortranFile(seednameGW + "." + ext, 'w') + f_uXu_out.write_record(header) + f_uXu_out.write_record(np.array([NBND, NK, nnb], dtype=np.int32)) assert nbnd == nbndDFT print(nbnd, NK, nnb) @@ -305,7 +317,7 @@ def reorder_uXu(ext, formatted=False): np.einsum('ln,lm,l->nm', MMN[ik][ib2].conj(), MMN[ik][ib1], eigenDE[ik])).reshape( -1, order='F') - if formatted: + if (formatted or write_formatted): f_uXu_out.write("".join( "{0:26.16e} {1:26.16f}\n".format(x.real, x.imag) for x in A)) @@ -335,14 +347,21 @@ def reorder_uXu(ext, formatted=False): f_spn_out.write("\n") else: f_spn_in = FortranFile(seedname + ".spn", 'r') - f_spn_out = FortranFile(seednameGW + ".spn", 'w') header = f_spn_in.read_record(dtype='c') - f_spn_out.write_record(header) - header = header.astype(str) nbnd, NK = f_spn_in.read_record(dtype=np.int32) - f_spn_out.write_record(np.array([NBND, NKPT], dtype=np.int32)) + if write_formatted: + f_spn_out = open(seednameGW + ".spn", 'w') + f_spn_out.write("".join(header.astype(str))) + f_spn_out.write('\n') + f_spn_out.write(" ".join(str(x) for x in (NBND, NKPT))) + f_spn_out.write("\n") + else: + f_spn_out = FortranFile(seednameGW + ".spn", 'w') + f_spn_out.write_record(header) + f_spn_out.write_record(np.array([NBND, NKPT], dtype=np.int32)) + header = "".join(header.astype(str)) - print("".join(header)) + print(header) assert nbnd == nbndDFT indm, indn = np.tril_indices(nbnd) @@ -372,7 +391,7 @@ def reorder_uXu(ext, formatted=False): A = A[:, :, BANDSORT[ik]][:, BANDSORT[ik], :][:, indnQP, indmQP].reshape( (3 * NBND * (NBND + 1) // 2), order='F') - if SPNformatted: + if (SPNformatted or write_formatted): f_spn_out.write("".join( "{0:26.16e} {1:26.16e}\n".format(x.real, x.imag) for x in A)) @@ -415,18 +434,27 @@ def reorder_uXu(ext, formatted=False): order='C')[BANDSORT[ik - 1], :].reshape(-1, order='C'))) else: - f_unk_out = FortranFile(os.path.join(unkgwdir, f_unk_name), - "w") f_unk_in = FortranFile(f_unk_name, "r") nr1, nr2, nr3, ik, nbnd = f_unk_in.read_record(dtype=np.int32) - f_unk_out.write_record( - np.array([nr1, nr2, nr3, ik, NBND], dtype=np.int32)) unk = np.array([ f_unk_in.read_record(dtype=np.complex) for ib in range(nbnd) ])[BANDSORT[ik - 1], :] - for i in range(NBND): - f_unk_out.write_record(unk[ib]) + if write_formatted: + f_unk_out = open(os.path.join(unkgwdir, f_unk_name), "w") + f_unk_out.write(" ".join( + str(x) for x in (nr1, nr2, nr3, ik, NBND)) + "\n") + NR = nr1 * nr2 * nr3 + for i in range(NBND): + for j in range(NR): + f_unk_out.write("{0:21.10e}{1:21.10e}".format(unk[ib, j].real, unk[ib, j].imag) + '\n') + else: + f_unk_out = FortranFile(os.path.join(unkgwdir, f_unk_name), + "w") + f_unk_out.write_record( + np.array([nr1, nr2, nr3, ik, NBND], dtype=np.int32)) + for i in range(NBND): + f_unk_out.write_record(unk[ib]) f_unk_in.close() f_unk_out.close() shutil.move('./' + unkgwdir + '/' + f_unk_name, './') From 67ec21ba3a23c98050f17db7f0bc0b8f762c6f6f Mon Sep 17 00:00:00 2001 From: Junfeng Qiao Date: Tue, 8 Oct 2019 15:59:55 +0800 Subject: [PATCH 09/10] add support for non-collinear UNK files & prettify stdout --- utility/gw2wannier90.py | 124 ++++++++++++++++++++++++---------------- 1 file changed, 75 insertions(+), 49 deletions(-) diff --git a/utility/gw2wannier90.py b/utility/gw2wannier90.py index 24bc688cb..f4aea243b 100755 --- a/utility/gw2wannier90.py +++ b/utility/gw2wannier90.py @@ -32,25 +32,29 @@ if len(argv) < 2: print("### gw2wannier90 interface ###") - print("You need to provide the seedname") + print("") print("Usage: gw2wannier90.py seedname options") - print("Options can be mmn, amn, spn, unk, uhu") - print("Be careful with unformatted files, they are compiler-dependent") - print( - "A safer choice is to use (bigger) formatted files, in this case type:" - ) - print("spn_formatted, uiu_formatted, uhu_formatted, unk_formatted") - print( - "If no options is specified, all the matrices and files are considered" - ) - print("In default, the output format is determined by the input format") - print("To generate formatted files with unformatted input, use option: " - + "write_formatted") + print("") + print("Options can be:") + print(" mmn, amn, spn, unk, uhu, uiu,") + print(" spn_formatted, unk_formatted, uhu_formatted, uiu_formatted,") + print(" write_formatted") + print("") + print("If no options are specified, all the files are considered.") + print("") + print("Be careful with unformatted files, they are compiler-dependent.") + print("A safer choice is to use (bigger) formatted files, with options:") + print(" spn_formatted, uiu_formatted, uhu_formatted, unk_formatted") + print("") + print("In default, the output format is determined by the input format.") + print("To generate formatted files with unformatted input, use option:") + print(" write_formatted") + print("") exit() -print('------------------------------\n') -print('##############################\n') -print('### gw2wannier90 interface ###\n') -print('##############################\n') +print('------------------------------') +print('##############################') +print('### gw2wannier90 interface ###') +print('##############################') seedname = argv[1] # for instance "silicon" seednameGW = seedname + ".gw" #for instance "silicon.gw" @@ -115,7 +119,7 @@ if len(exbands) > 1 or exbands[0] != 0: #raise RuntimeError("exclude bands is not supported yet") # actually it is OK, see below print('Exclude bands option is used: be careful to be consistent ' + - 'with the choice of bands for the GW QP corrections.') + 'with the choice of bands for the GW QP corrections.') corrections = np.loadtxt(seedname + ".gw.unsorted.eig") corrections = {(int(l[1]) - 1, int(l[0]) - 1): l[2] for l in corrections} @@ -129,10 +133,10 @@ eigenDFT = eigenDFT[:, 2].reshape(NKPT, nbndDFT, order='C') #print(eigenDFT) f_raw.write('------------------------------\n') -f_raw.write('Writing DFT eigenvalues \n') +f_raw.write('Writing DFT eigenvalues\n') for line in eigenDFT: f_raw.write(str(line) + '\n') -f_raw.write('------------------------------ \n') +f_raw.write('------------------------------\n') providedGW = [ ib for ib in range(nbndDFT) @@ -141,7 +145,7 @@ #print(providedGW) f_raw.write('------------------------------\n') f_raw.write('List of provided GW corrections (bands indexes)\n') -f_raw.write(str(providedGW)) +f_raw.write(str(providedGW) + '\n') f_raw.write('------------------------------\n') NBND = len(providedGW) print("Adding GW QP corrections to KS eigenvalues") @@ -152,7 +156,7 @@ for ik in range(NKPT)]) f_raw.write('------------------------------\n') -f_raw.write('Writing GW eigenvalues unsorted (KS + QP correction) \n') +f_raw.write('Writing GW eigenvalues unsorted (KS + QP correction)\n') for line in eigenDFTGW: f_raw.write(str(line) + '\n') f_raw.write('------------------------------\n') @@ -178,7 +182,7 @@ print("GW eigenvalues sorted") #print eigenDFT -print('------------------------------\n') +print('------------------------------') print("writing " + seednameGW + ".eig") feig_out = open(seednameGW + ".eig", "w") for ik in range(NKPT): @@ -187,14 +191,15 @@ ib + 1, ik + 1, eigenDFTGW[ik, ib])) feig_out.close() print(seednameGW + ".eig", ' written.') -('------------------------------\n') +print('------------------------------\n') if calcAMN: try: - print("----------\n AMN module \n---------\n") + print("----------\n AMN module \n----------") f_amn_out = open(seednameGW + ".amn", "w") f_amn_in = open(seedname + ".amn", "r") s = f_amn_in.readline().strip() + print(s) f_amn_out.write( "{0}, sorted by GW quasi-particle energies on {1} \n".format( s, @@ -217,18 +222,19 @@ amn[ib, ipr].imag)) f_amn_in.close() f_amn_out.close() - print("----------\n AMN - OK \n---------\n") + print("----------\n AMN - OK \n----------\n") except IOError as err: print("WARNING: {0}.amn not written : ".format(seednameGW), err) if calcMMN: try: - print("----------\n MMN module \n---------\n") + print("----------\n MMN module \n----------") f_mmn_out = open(os.path.join(seednameGW + ".mmn"), "w") f_mmn_in = open(os.path.join(seedname + ".mmn"), "r") s = f_mmn_in.readline().strip() + print(s) f_mmn_out.write( "{0}, sorted by GW quasi-particle energies on {1} \n".format( s, @@ -260,7 +266,7 @@ for m in range(NBND): f_mmn_out.write(" {0:16.12f} {1:16.12f}\n".format( MMN[ik][ib][m, n].real, MMN[ik][ib][m, n].imag)) - print("----------\n MMN OK \n---------\n") + print("----------\n MMN OK \n----------\n") except IOError as err: print("WARNING: {0}.mmn not written : ".format(seednameGW), err) if calcUHU: @@ -271,7 +277,7 @@ def reorder_uXu(ext, formatted=False): try: - print("----------\n {0} \n----------".format(ext)) + print("----------\n {0} module \n----------".format(ext)) if formatted: f_uXu_in = open(seedname + "." + ext, 'r') @@ -288,16 +294,20 @@ def reorder_uXu(ext, formatted=False): f_uXu_out = open(seednameGW + "." + ext, 'w') f_uXu_out.write("".join(header.astype(str))) f_uXu_out.write('\n') - f_uXu_out.write(np.array([NBND, NK, nnb], dtype=np.int32)) + f_uXu_out.write(" ".join(str(x) for x in [NBND, NK, nnb])) f_uXu_out.write('\n') else: f_uXu_out = FortranFile(seednameGW + "." + ext, 'w') f_uXu_out.write_record(header) - f_uXu_out.write_record(np.array([NBND, NK, nnb], dtype=np.int32)) + f_uXu_out.write_record( + np.array([NBND, NK, nnb], dtype=np.int32)) + header = "".join(header.astype(str)) - assert nbnd == nbndDFT + print(header.strip()) print(nbnd, NK, nnb) + assert nbnd == nbndDFT + if formatted: uXu = np.loadtxt(f_uXu_in).reshape(-1) start = 0 @@ -319,7 +329,7 @@ def reorder_uXu(ext, formatted=False): -1, order='F') if (formatted or write_formatted): f_uXu_out.write("".join( - "{0:26.16e} {1:26.16f}\n".format(x.real, x.imag) + "{0:26.16e} {1:26.16e}\n".format(x.real, x.imag) for x in A)) else: f_uXu_out.write_record(A) @@ -335,7 +345,7 @@ def reorder_uXu(ext, formatted=False): if calcSPN: try: - print("----------\n SPN \n---------\n") + print("----------\n SPN module \n----------") if SPNformatted: f_spn_in = open(seedname + ".spn", 'r') @@ -361,7 +371,7 @@ def reorder_uXu(ext, formatted=False): f_spn_out.write_record(np.array([NBND, NKPT], dtype=np.int32)) header = "".join(header.astype(str)) - print(header) + print(header.strip()) assert nbnd == nbndDFT indm, indn = np.tril_indices(nbnd) @@ -400,15 +410,17 @@ def reorder_uXu(ext, formatted=False): f_spn_in.close() f_spn_out.close() - print("----------\n SPN OK \n---------\n") + print("----------\n SPN OK \n----------\n") except IOError as err: print("WARNING: {0}.spn not written : ".format(seednameGW), err) if calcUNK: + print("----------\n UNK module \n----------") + unkgwdir = "UNK_GW" unkdftdir = "UNK_DFT" files_list = [] - for f_unk_name in glob.glob("UNK*"): + for f_unk_name in glob.glob("UNK*.*"): files_list.append(f_unk_name) try: @@ -419,6 +431,7 @@ def reorder_uXu(ext, formatted=False): for f_unk_name in files_list: try: + NC = os.path.splitext(f_unk_name)[1] == '.NC' shutil.move('./' + f_unk_name, './' + unkdftdir + '/') if UNKformatted: f_unk_out = open(os.path.join(unkgwdir, f_unk_name), "w") @@ -426,28 +439,39 @@ def reorder_uXu(ext, formatted=False): nr1, nr2, nr3, ik, nbnd = np.array(f_unk_in.readline().split(), dtype=int) NR = nr1 * nr2 * nr3 + if NC: NR *= 2 f_unk_out.write(" ".join( str(x) for x in (nr1, nr2, nr3, ik, NBND)) + "\n") f_unk_out.write("\n".join( - np.array([l.strip() for l in f_unk_in], dtype=str).reshape( + np.array([l.rstrip() for l in f_unk_in], dtype=str).reshape( (nbnd, NR), order='C')[BANDSORT[ik - 1], :].reshape(-1, order='C'))) else: - f_unk_in = FortranFile(f_unk_name, "r") + f_unk_in = FortranFile(os.path.join(unkdftdir, f_unk_name), + "r") nr1, nr2, nr3, ik, nbnd = f_unk_in.read_record(dtype=np.int32) - unk = np.array([ - f_unk_in.read_record(dtype=np.complex) - for ib in range(nbnd) - ])[BANDSORT[ik - 1], :] + NR = nr1 * nr2 * nr3 + unk = np.zeros((nbnd, NR), dtype=np.complex) + if NC: unk2 = np.zeros((nbnd, NR), dtype=np.complex) + for ib in range(nbnd): + unk[ib, :] = f_unk_in.read_record(dtype=np.complex) + if NC: unk2[ib, :] = f_unk_in.read_record(dtype=np.complex) + unk = unk[BANDSORT[ik - 1], :] + if NC: unk2 = unk2[BANDSORT[ik - 1], :] if write_formatted: f_unk_out = open(os.path.join(unkgwdir, f_unk_name), "w") f_unk_out.write(" ".join( - str(x) for x in (nr1, nr2, nr3, ik, NBND)) + "\n") - NR = nr1 * nr2 * nr3 + str(x) for x in (nr1, nr2, nr3, ik, NBND))) for i in range(NBND): for j in range(NR): - f_unk_out.write("{0:21.10e}{1:21.10e}".format(unk[ib, j].real, unk[ib, j].imag) + '\n') + f_unk_out.write("\n{0:21.10e} {1:21.10e}".format( + unk[ib, j].real, unk[ib, j].imag)) + if NC: + for j in range(NR): + f_unk_out.write( + "\n{0:21.10e} {1:21.10e}".format( + unk2[ib, j].real, unk2[ib, j].imag)) else: f_unk_out = FortranFile(os.path.join(unkgwdir, f_unk_name), "w") @@ -455,6 +479,7 @@ def reorder_uXu(ext, formatted=False): np.array([nr1, nr2, nr3, ik, NBND], dtype=np.int32)) for i in range(NBND): f_unk_out.write_record(unk[ib]) + if NC: f_unk_out.write_record(unk2[ib]) f_unk_in.close() f_unk_out.close() shutil.move('./' + unkgwdir + '/' + f_unk_name, './') @@ -464,7 +489,8 @@ def reorder_uXu(ext, formatted=False): else: raise err os.rmdir(unkgwdir) - print( - 'UNK files are being reordered, old files coming from DFT are available in UNK_DFT' - ) + print('UNK files have been reordered, ' + + 'old files coming from DFT are available in UNK_DFT folder.') + print("----------\n UNK OK \n----------\n") + f_raw.close() From 33f248ade19ad0036f9bb7ba4bbeb4bb8a7c42b8 Mon Sep 17 00:00:00 2001 From: Junfeng Qiao Date: Tue, 8 Oct 2019 17:00:20 +0800 Subject: [PATCH 10/10] update documentaion of gw2wannier90.py & k_mapper.py --- doc/user_guide/utilities.tex | 36 ++++++++++++++++++++++++++++++------ utility/gw2wannier90.py | 11 ++++++----- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/doc/user_guide/utilities.tex b/doc/user_guide/utilities.tex index 335c6bb5e..a886d642c 100644 --- a/doc/user_guide/utilities.tex +++ b/doc/user_guide/utilities.tex @@ -198,15 +198,39 @@ \section{$\tt{k\_mapper.py}$} \label{sec:k_mapper} The \wannier\ code requires the definition of a full Monkhorst--Pack grid of $\mathbf{k}$-vectors, which can be obtained by means of the \verb|kmesh.pl| utility. -In order to perform a GW calculation with the Yambo code, you need to perform a nscf calculation on a grid in the irreducible BZ. Moreover, you may need a finer grid to converge the GW calculation than what you need to interpolate the band structure. The \verb|k_mapper.py| tools helps in finding the $\mathbf{k}$-vectors indeces of a full grid needed for interpolation into the reduced grid needed for the GW calculation with Yambo. \newline \newline -Within the \verb|/utility| folder type:\newline -\verb|./k_mapper.py nx ny nz "path_of_the_QE_nscf_output_file_given_to_Yambo"| +In order to perform a GW calculation with the Yambo code, you need to perform a nscf calculation on a grid in the irreducible BZ. Moreover, you may need a finer grid to converge the GW calculation than what you need to interpolate the band structure. The \verb|k_mapper.py| tools helps in finding the $\mathbf{k}$-vectors indexes of a full grid needed for interpolation into the reduced grid needed for the GW calculation with Yambo. \newline \newline +Usage:\newline + +\verb|path/k_mapper.py nx ny nz QE_nscf_output|\newline + +where \verb|path| is the path of \verb|utility| folder, +\verb|QE_nscf_output| is the path of the QE nscf output file given to Yambo. \section{$\tt{gw2wannier90.py}$} This utility allows to sort in energy the input data of \verb|wannier90| (e.g. overlap matrices and energy eigenvalues). \verb|gw2wannier90.py| allows to use \verb|wannier90| at the $G_0W_0$ level, where quasi-particle corrections can change the energy ordering of eigenvalues (Some \verb|wannier90| modules require states to be ordered in energy).\newline \newline -Within the work directory type: -\verb|./gw2wannier90.py seedname options| +Usage: + +\verb|path/gw2wannier90.py seedname options| \newline \newline -NB: Binary files are supported, though not reccommended. +where \verb|path| is the path of \verb|utility| folder.\newline\newline + +Available options are: +\begin{verbatim} +mmn, amn, spn, unk, uhu, uiu, +spn_formatted, unk_formatted, uhu_formatted, uiu_formatted, +write_formatted +\end{verbatim} + +If no options are specified, all the files (\verb|mmn, amn, spn, UNK, uHu, uIu|) are considered. + +Binary (unformatted Fortran) files are supported, though not reccommended, +since they are compiler-dependent. +A safer choice is to use (bigger) formatted files, with options: + +\verb|spn_formatted, uiu_formatted, uhu_formatted, unk_formatted| + +In default, the output format is the same as the input format. +To generate formatted files with unformatted input, use option: +\verb|write_formatted| \label{sec:w90aaa} \section{$\tt{w90spn2spn.x}$\label{sec:w90spn2spn}} diff --git a/utility/gw2wannier90.py b/utility/gw2wannier90.py index f4aea243b..80eb3661e 100755 --- a/utility/gw2wannier90.py +++ b/utility/gw2wannier90.py @@ -46,7 +46,7 @@ print("A safer choice is to use (bigger) formatted files, with options:") print(" spn_formatted, uiu_formatted, uhu_formatted, unk_formatted") print("") - print("In default, the output format is determined by the input format.") + print("In default, the output format is the same as the input format.") print("To generate formatted files with unformatted input, use option:") print(" write_formatted") print("") @@ -443,10 +443,11 @@ def reorder_uXu(ext, formatted=False): f_unk_out.write(" ".join( str(x) for x in (nr1, nr2, nr3, ik, NBND)) + "\n") f_unk_out.write("\n".join( - np.array([l.rstrip() for l in f_unk_in], dtype=str).reshape( - (nbnd, NR), - order='C')[BANDSORT[ik - 1], :].reshape(-1, - order='C'))) + np.array([l.rstrip() for l in f_unk_in], + dtype=str).reshape( + (nbnd, NR), + order='C')[BANDSORT[ik - 1], :].reshape( + -1, order='C'))) else: f_unk_in = FortranFile(os.path.join(unkdftdir, f_unk_name), "r")