Skip to content

Commit

Permalink
update hartree
Browse files Browse the repository at this point in the history
  • Loading branch information
Woochang Kim committed Jul 20, 2023
1 parent c833ee9 commit 73cb1e9
Show file tree
Hide file tree
Showing 7 changed files with 520 additions and 31 deletions.
49 changes: 49 additions & 0 deletions WS2/LDA_SG15/1-scf/read_pw2bgw_grid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import h5py
import numpy as np

f = h5py.File('./WS2.save/charge-density.hdf5','r')
print(list(f.keys()))
print()
['MillerIndices', 'rhotot_g']
igvec = f['MillerIndices'][:]
rhog = f['rhotot_g'][:]
ngm_g = f.attrs['ngm_g']
print(ngm_g)
print(igvec.dtype)
print(igvec.shape)
print(rhog.dtype)
print(rhog.shape)
print(rhog[ngm_g])

print(rhog[0]*1449.7341)
print(rhog[1]*1449.7341)
rhog = rhog[::2] + 1j*rhog[1::2]
print(rhog.shape)
np.save('rhog', rhog)
np.save('igvec', igvec)
##data = np.loadtxt('./VXC.txt', skiprows=13, dtype=np.complex128)
##print(data)
#f = open('./pw2bgw.save/VXC.txt', 'r')
#f.readline()
##nsf, ng_g, ntran, cell_symmetry, nat, ecutrho = np.int32(f.readline().split())
#f.readline()
##n1, n2, n3 = no.int32(f.readline().split())
#f.readline()
#f.readline()
#f.readline()
#f.readline()
#f.readline()
#f.readline()
#f.readline()
#ng = np.int32(f.readline().split()[0])
#gvec = np.int32(f.readline().split()).reshape((ng,3))
#print(gvec[:5])
#f.readline()
#ng = np.int32(f.readline().split()[0])
#data = f.readline().split()
#f.close()
#data = np.array([np.float64(eval(str)) for str in data]).view(np.complex128).reshape(ng)
#print(data[0:5])
#print(data.shape)
#np.save('vxc_gvec',gvec)
#np.save('vxc_pw2bgw',data)
29 changes: 18 additions & 11 deletions WS2/LDA_SG15/3.0-libxc/libxc_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,29 @@
from fft_mod import put_FFTbox2, ifft_g2r
from pymatgen.io.cube import Cube
from write_cube_mod import write_cube
bohr2ang = 0.529177210903

# Load data
def vxc_libxc():
n1, n2, n3 = [18, 18, 135]
#gvec_rho = np.loadtxt('../2.1-wfn/rho_gvec.txt',dtype=np.int32)
#rho_pw2bgw = np.load('../2.1-wfn/rho_pw2bgw.npy')
#rho_FFTbox = put_FFTbox2(rho_pw2bgw, gvec_rho, [n1,n2,n3], noncolin=False)
#rho = ifft_g2r(rho_FFTbox)
gvec_rho = np.load('../1-scf/igvec.npy')
rho_pw2bgw = np.load('../1-scf/rhog.npy')*18*18*135
rho_FFTbox = put_FFTbox2(rho_pw2bgw, gvec_rho, [n1,n2,n3], noncolin=False)
rho = ifft_g2r(rho_FFTbox)
#rho_pp = Cube("../1.1-pp/Rho.cube")
#rho = rho_pp.data
rho_pw2bgw = np.load("../2.1-wfn/rhor_pw2bgw.npy")
rho = rho_pw2bgw[0,:,:,:]
#rho_pw2bgw = np.load("../2.1-wfn/rhor_pw2bgw.npy")
#rho = rho_pw2bgw[0,:,:,:]

# Create input
const = 1
#const = 214.82791025888773*18*18*135
#const = 18*18*135
inp = {}
rho_flatten = rho.flatten()
#print(rho - np.reshape(rho_flatten, (18,18, 135)))
inp["rho"] = const*rho_flatten
inp["rho"] = rho_flatten

# Build functional
func_c = pylibxc.LibXCFunctional("LDA_C_PZ", "unpolarized")
Expand All @@ -47,14 +49,21 @@ def vxc_libxc():

#vxc = zk_x + zk_c + inp["rho"]*vrho_x + inp["rho"]*vrho_c
vxc = vrho_x + vrho_c
zk = zk_x + zk_c

vxc_FFTbox = np.reshape(vxc,(18,18,135))
zk_FFTbox = np.reshape(zk,(18,18,135))
Exc = np.sum(zk_FFTbox*rho)
np.save('vxc_FFTbox', vxc_FFTbox)
return vxc_FFTbox
return vxc_FFTbox, Exc

vxc_gen = vxc_libxc()
vxc_gen, Exc = vxc_libxc()
print(vxc_gen.shape)
n1, n2, n3 = [18, 18, 135]
nfft = n1*n2*n3
vxc_pp = Cube("../1.1-pp/Vxc.cube")
vol = vxc_pp.volume/(bohr2ang**3) # in Bohr^3
vol = 1449.7341
vxc_pp = vxc_pp.data
#vxc_pp = np.load('../2.1-wfn/vxcr_pw2bgw.npy')
#vxc_pp = vxc_pp[0,:,:,:]
Expand All @@ -67,11 +76,9 @@ def vxc_libxc():
#write_cube('ratio.cube',ratio,struct)
#print(ratio)
#print('\n')
print(np.max(ratio))
print(np.min(ratio))
print(np.average((ratio)))
diff = np.abs(2*vxc_gen - vxc_pp)
print(np.max(diff))
print(np.min(diff))
print(np.average(diff))
#print(np.sort(ratio.flat)[-1000:])
print('Exc*2, ',2*Exc*(vol/nfft))
36 changes: 25 additions & 11 deletions WS2/LDA_SG15/4.0-hartree/compute_hartree.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,53 @@
import numpy as np
from fft_mod import put_FFTbox2, ifft_g2r
from fft_mod import put_FFTbox2, ifft_g2r, fft_r2g, flat_FFTbox
from pymatgen.io.cube import Cube
bohr2ang = 0.529177
bohr2ang = 0.529177249


def main():
rho_g = np.load("../2.1-wfn/rho_pw2bgw.npy")
igvec = np.load("../2.1-wfn/rho_gvec.npy")
#rho_g = np.load("../2.1-wfn/rho_pw2bgw.npy")
#igvec = np.load("../2.1-wfn/rho_gvec.npy")
igvec = np.load('../1-scf/igvec.npy')
rho_g = np.load('../1-scf/rhog.npy')
n1, n2, n3 = [18, 18, 135]
nfft = n1*n2*n3

vbh = Cube("../1.1-pp/Vh.cube") #just for struct
struct = vbh.structure
vol = vbh.volume/(bohr2ang**3) # in Bohr^3
vol = 1449.7341
cell = struct.lattice._matrix*(1/bohr2ang) # in Bohr
bcell = 2*np.pi*np.linalg.inv(cell).T # in Bohr^-1

# debug
rho_g /= (vol/nfft)
#rho_g /= (vol/nfft)
rho_g *= (nfft)
rho_g_FFTbox = put_FFTbox2(rho_g, igvec, [n1, n2, n3], noncolin=False)
rho_r = ifft_g2r(rho_g_FFTbox)
print('np.sum(rho_r)*(vol/nfft)',np.sum(rho_r)*(vol/nfft))
rho_r_FFTbox = ifft_g2r(rho_g_FFTbox)
#rho_r_FFTbox = np.zeros((1,n1,n2,n3), dtype=np.cdouble)
#rho = Cube("../1.1-pp/Rho.cube") #just for struct
#rho_r_FFTbox[0,:,:,:] = rho.data
print('sum(rho_r)*(vol/nfft)',np.sum(rho_r_FFTbox)*(vol/nfft))
# debug

rho_g_FFTbox = fft_r2g(rho_r_FFTbox)
rho_g, igvec = flat_FFTbox(rho_g_FFTbox)
vh_r = get_vh(rho_g, igvec, bcell)
rho_r = rho_r_FFTbox
Eh = np.real(0.5*np.sum(vh_r*rho_r*(vol/nfft)))

vbh_r = vbh.data/2 # in Hartree
vbh_r = vbh.data/2 # from Ry to Hartree
Eh_ref = np.real(0.5*np.sum(vbh_r*rho_r*(vol/nfft)))

diff = np.abs(vbh_r-vh_r)
print('Error = abs(vsub - vsub_aprox) in Hartree')
print(f'Eh_ref = {2*Eh_ref} in Ry')
print(f'Eh_ref-Eh = {2*(Eh_ref-Eh)} in Ry')
print(f'Eh_ref-Eh/Eh_ref = {(Eh_ref-Eh)/Eh_ref} ')
print('Error = abs(vh_qe - vh) in Hartree')
print('max(Error)',np.max(diff))
print('min(Error)',np.min(diff))
print('avg(Error)',np.average(diff))
diff = np.abs(vbh_r/(vh_r))
print('Ratio = abs(vbh_r/vh_r) in Hartree')
print('Ratio = abs(vbh_r/vh_r)')
print('max(Error)',np.max(diff))
print('min(Error)',np.min(diff))
print('avg(Error)',np.average(diff))
Expand Down
172 changes: 172 additions & 0 deletions WS2/LDA_SG15/4.0-hartree/density2Vh.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import numpy as np
from fft_mod import put_FFTbox2, ifft_g2r, fft_r2g, flat_FFTbox
from pymatgen.io.cube import Cube
bohr2ang = 0.529177249


def main():
n1, n2, n3 = [18, 18, 135]
nfft = n1*n2*n3

# vh
vbh = Cube("../1.1-pp/Vh.cube") #just for struct
struct = vbh.structure
vol = vbh.volume/(bohr2ang**3) # in Bohr^3
vol = 1449.7341
cell = struct.lattice._matrix*(1/bohr2ang) # in Bohr
bcell = 2*np.pi*np.linalg.inv(cell).T # in Bohr^-1

# rho_r
rho_r_FFTbox = np.zeros((1,n1,n2,n3), dtype=np.cdouble)
rho = Cube("../1.1-pp/Rho.cube") #just for struct
rho_r_FFTbox[0,:,:,:] = rho.data
print('sum(rho_r)*(vol/nfft)',np.sum(rho_r_FFTbox)*(vol/nfft))
# debug
rho_g_FFTbox = fft_r2g(rho_r_FFTbox)
rho_g, igvec = flat_FFTbox(rho_g_FFTbox)
vh_r = get_vh(rho_g, igvec, bcell)
rho_r = rho_r_FFTbox
Eh = np.real(0.5*np.sum(vh_r*rho_r*(vol/nfft)))

vbh_r = vbh.data/2 # from Ry to Hartree
Eh_ref = np.real(0.5*np.sum(vbh_r*rho_r*(vol/nfft)))

diff = np.abs(vbh_r-vh_r)
print(f'Eh_ref = {2*Eh_ref} in Ry')
print(f'Eh_ref-Eh = {2*(Eh_ref-Eh)} in Ry')
print(f'Eh_ref-Eh/Eh_ref = {(Eh_ref-Eh)/Eh_ref} ')
print('Error = abs(vsub - vsub_aprox) in Hartree')
print('max(Error)',np.max(diff))
print('min(Error)',np.min(diff))
print('avg(Error)',np.average(diff))
diff = np.abs(vbh_r/(vh_r))
print('Ratio = abs(vbh_r/vh_r)')
print('max(Error)',np.max(diff))
print('min(Error)',np.min(diff))
print('avg(Error)',np.average(diff))
#print()
#print('diff = abs(vsub - vsub_aprox) in Hartree')
#print('max(diff)',np.max(diff))
#print('min(diff)',np.min(diff))
#print('avg(diff)',np.average(diff))
#print(np.sort(ratio.flat)[-1000:])
return


def get_vh(rho_g, igvec, bcell):
"""
--Input--
rho_g : complex128(ng)
momentum space charge density in Hartree Atomic Unit
gvec : int(ng,3)
momentum space charge density in Hartree Atomic Unit
bcell_in: float64(3,3)
reciprocal lattice vectors (bohr^{-1})
--Output--
vh : float(:,:,:)
"""
vh_g = np.zeros_like(rho_g)
gvec = igvec.dot(bcell)
gvec_sq = np.einsum('ix,ix->i', gvec, gvec) # bohr^{-1} or Ry
for ig, g_sq in enumerate(gvec_sq):
if g_sq < 1e-12:
vh_g[ig] = 0
else:
vh_g[ig] = 4*np.pi*rho_g[ig]/g_sq

n1, n2, n3 = [18, 18, 135]
vh_g_FFTbox = put_FFTbox2(vh_g, igvec, [n1, n2, n3], noncolin=False)
vh_r = ifft_g2r(vh_g_FFTbox)
return np.real(vh_r[0,:,:,:])



def get_vxc(rho):
"""
--Input--
rho : float(:,:,:)
real space charge density in Hartree Atomic Unit
--Output--
vxc : float(:,:,:)
"""
inp = {}
n1, n2, n3 = rho.shape
rho_flatten = rho.flatten()
inp["rho"] = rho_flatten

# Build functional
func_c = pylibxc.LibXCFunctional("LDA_C_PZ", "unpolarized")
func_x = pylibxc.LibXCFunctional("LDA_X", "unpolarized")

# Compute exchange part
ret_x = func_x.compute(inp, do_fxc=True)
v2rho2_x = ret_x['v2rho2'][:,0]
vrho_x = ret_x['vrho'][:,0]
zk_x = ret_x['zk'][:,0]

# Compute correlation part
ret_c = func_c.compute(inp, do_fxc=True)
v2rho2_c = ret_x['v2rho2'][:,0]
vrho_c = ret_c['vrho'][:,0]
zk_c = ret_c['zk'][:,0]

vxc = vrho_x + vrho_c
fxc = v2rho2_x + v2rho2_c

vxc_FFTbox = np.reshape(vxc,(n1,n2,n3))
fxc_FFTbox = np.reshape(fxc,(n1,n2,n3))

return vxc_FFTbox, fxc_FFTbox

def vxc_libxc():
n1, n2, n3 = [18, 18, 135]
#gvec_rho = np.loadtxt('../2.1-wfn/rho_gvec.txt',dtype=np.int32)
#rho_pw2bgw = np.load('../2.1-wfn/rho_pw2bgw.npy')
#rho_FFTbox = put_FFTbox2(rho_pw2bgw, gvec_rho, [n1,n2,n3], noncolin=False)
#rho = ifft_g2r(rho_FFTbox)
#rho_pp = Cube("../1.1-pp/Rho.cube")
#rho = rho_pp.data
rho_pw2bgw = np.load("../2.1-wfn/rhor_pw2bgw.npy")
rho = rho_pw2bgw[0,:,:,:]
frac = 0.0001
rho_f = rho * frac
rho_r = rho - rho_f

# Create input
const = 1
inp = {}
rho_flatten = rho.flatten()
rho_r_flatten = rho_r.flatten()
rho_f_flatten = rho_f.flatten()
inp["rho"] = const*rho_flatten

# Build functional
func_c = pylibxc.LibXCFunctional("LDA_C_PZ", "unpolarized")
func_x = pylibxc.LibXCFunctional("LDA_X", "unpolarized")

# Compute exchange part
ret_x = func_x.compute(inp, do_fxc=True)
v2rho2_x = ret_x['v2rho2'][:,0]
vrho_x = ret_x['vrho'][:,0]
zk_x = ret_x['zk'][:,0]

# Compute correlation part
ret_c = func_c.compute(inp, do_fxc=True)
v2rho2_c = ret_x['v2rho2'][:,0]
vrho_c = ret_c['vrho'][:,0]
zk_c = ret_c['zk'][:,0]

vxc = vrho_x + vrho_c
fxc = v2rho2_x + v2rho2_c

vxc_FFTbox = np.reshape(vxc,(18,18,135))
fxc_FFTbox = np.reshape(fxc,(18,18,135))
return vxc_FFTbox, fxc_FFTbox

main()
Loading

0 comments on commit 73cb1e9

Please sign in to comment.