Skip to content

Commit

Permalink
Align setups with pyscf (#146)
Browse files Browse the repository at this point in the history
* fixed df class names

* align some changes with pyscf

* update unit test for gradient
  • Loading branch information
wxj6000 authored May 8, 2024
1 parent bafd2d8 commit 0509a00
Show file tree
Hide file tree
Showing 12 changed files with 26 additions and 22 deletions.
4 changes: 2 additions & 2 deletions gpu4pyscf/df/df.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import numpy as np
from cupyx.scipy.linalg import solve_triangular
from pyscf import lib
from pyscf.df import df, addons
from pyscf.df import df, addons, incore
from gpu4pyscf.lib.cupy_helper import (
cholesky, tag_array, get_avail_mem, cart2sph, take_last2d, transpose_sum)
from gpu4pyscf.df import int3c2e, df_jk
Expand All @@ -30,7 +30,7 @@

MIN_BLK_SIZE = getattr(__config__, 'min_ao_blksize', 128)
ALIGNED = getattr(__config__, 'ao_aligned', 32)
LINEAR_DEP_TOL = 1e-7
LINEAR_DEP_TOL = incore.LINEAR_DEP_THR

class DF(lib.StreamObject):
from gpu4pyscf.lib.utils import to_gpu, device
Expand Down
2 changes: 1 addition & 1 deletion gpu4pyscf/df/grad/rhf.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,9 @@ class Gradients(rhf_grad.Gradients):
def __init__(self, mf):
# Whether to include the response of DF auxiliary basis when computing
# nuclear gradients of J/K matrices
self.auxbasis_response = True
rhf_grad.Gradients.__init__(self, mf)

auxbasis_response = True
get_jk = get_jk
grad_elec = rhf_grad.grad_elec
check_sanity = NotImplemented
Expand Down
10 changes: 8 additions & 2 deletions gpu4pyscf/df/grad/rks.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,14 @@ def get_veff(ks_grad, mol=None, dm=None):
class Gradients(rks_grad.Gradients):
from gpu4pyscf.lib.utils import to_gpu, device

_keys = df_rks_grad.Gradients._keys
__init__ = df_rks_grad.Gradients.__init__
_keys = {'with_df', 'auxbasis_response'}

def __init__(self, mf):
rks_grad.Gradients.__init__(self, mf)

# Whether to include the response of DF auxiliary basis when computing
# nuclear gradients of J/K matrices
auxbasis_response = True

get_jk = df_rhf_grad.Gradients.get_jk
grad_elec = df_rhf_grad.Gradients.grad_elec
Expand Down
6 changes: 3 additions & 3 deletions gpu4pyscf/df/grad/uhf.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,12 @@ class Gradients(uhf_grad.Gradients):
_keys = {'with_df', 'auxbasis_response'}

def __init__(self, mf):
# Whether to include the response of DF auxiliary basis when computing
# nuclear gradients of J/K matrices
self.auxbasis_response = True
self._keys = self._keys.union(['auxbasis_response'])
uhf_grad.Gradients.__init__(self, mf)

# Whether to include the response of DF auxiliary basis when computing
# nuclear gradients of J/K matrices
auxbasis_response = True
get_jk = get_jk

# TODO: finish these two functions
Expand Down
6 changes: 3 additions & 3 deletions gpu4pyscf/df/grad/uks.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ class Gradients(uks_grad.Gradients):
_keys = {'with_df', 'auxbasis_response'}

def __init__(self, mf):
# Whether to include the response of DF auxiliary basis when computing
# nuclear gradients of J/K matrices
self.auxbasis_response = True
uks_grad.Gradients.__init__(self, mf)

# Whether to include the response of DF auxiliary basis when computing
# nuclear gradients of J/K matrices
auxbasis_response = True
get_jk = get_jk

def get_j(self, mol=None, dm=None, hermi=0, mo_coeff=None, mo_occ=None, dm2 = None, omega=None):
Expand Down
2 changes: 1 addition & 1 deletion gpu4pyscf/df/hessian/rhf.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,9 +580,9 @@ class Hessian(rhf_hess.Hessian):

from gpu4pyscf.lib.utils import to_gpu, device
def __init__(self, mf):
self.auxbasis_response = 1
rhf_hess.Hessian.__init__(self, mf)

auxbasis_response = 1
partial_hess_elec = partial_hess_elec
make_h1 = make_h1
kernel = rhf_hess.kernel
Expand Down
2 changes: 1 addition & 1 deletion gpu4pyscf/df/hessian/rks.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ class Hessian(rks_hess.Hessian):
from gpu4pyscf.lib.utils import to_gpu, device

def __init__(self, mf):
self.auxbasis_response = 1
rks_hess.Hessian.__init__(self, mf)

auxbasis_response = 1
partial_hess_elec = partial_hess_elec
make_h1 = make_h1
kernel = rhf_hess.kernel
Expand Down
2 changes: 1 addition & 1 deletion gpu4pyscf/df/hessian/uhf.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,9 +687,9 @@ class Hessian(uhf_hess.Hessian):
from gpu4pyscf.lib.utils import to_gpu, device

def __init__(self, mf):
self.auxbasis_response = 1
uhf_hess.Hessian.__init__(self, mf)

auxbasis_response = 1
partial_hess_elec = partial_hess_elec
make_h1 = make_h1
kernel = rhf_hess.kernel
Expand Down
2 changes: 1 addition & 1 deletion gpu4pyscf/df/hessian/uks.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,14 @@ class Hessian(uks_hess.Hessian):
from gpu4pyscf.lib.utils import to_gpu, device

def __init__(self, mf):
self.auxbasis_response = 1
uks_hess.Hessian.__init__(self, mf)

def solve_mo1(self, mo_energy, mo_coeff, mo_occ, h1ao_or_chkfile,
fx=None, atmlst=None, max_memory=4000, verbose=None):
return uhf_hess.solve_mo1(self.base, mo_energy, mo_coeff, mo_occ, h1ao_or_chkfile,
fx, atmlst, max_memory, verbose)

auxbasis_response = 1
partial_hess_elec = partial_hess_elec
make_h1 = make_h1
hess_elec = uhf_hess.hess_elec
Expand Down
4 changes: 2 additions & 2 deletions gpu4pyscf/grad/rhf.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,8 +547,8 @@ def calculate_h1e(h1_gpu, s1_gpu):
if log.verbose >= logger.DEBUG:
log.timer_debug1('gradients of electronic part', *t0)

# net force should be zero
de -= cupy.sum(de, axis=0)/len(atmlst)
## net force should be zero
#de -= cupy.sum(de, axis=0)/len(atmlst)
return de.get()

def get_grad_hcore(mf_grad, mo_coeff=None, mo_occ=None):
Expand Down
1 change: 0 additions & 1 deletion gpu4pyscf/grad/rks.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,6 @@ def __init__ (self, mf):
rhf_grad.Gradients.__init__(self, mf)
self.grids = None
self.nlcgrids = None
self.grid_response = False

get_veff = _get_veff
# TODO: add grid response into this function
Expand Down
7 changes: 3 additions & 4 deletions gpu4pyscf/lib/tests/test_to_gpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def test_rks(self):
mf = rks.RKS(mol).run()
gobj = mf.nuc_grad_method().to_gpu()
g = gobj.kernel()
assert numpy.abs(lib.fp(g) - -0.04339987221752033) < 1e-7
assert numpy.abs(lib.fp(g) - -0.04340162663176693) < 1e-7

# RKS Hessian it not supported yet
# mf = rks.RKS(mol).run()
Expand Down Expand Up @@ -102,7 +102,7 @@ def test_df_b3lyp(self):
mf = rks.RKS(mol, xc='b3lyp').density_fit().run()
gobj = mf.nuc_grad_method().to_gpu()
g = gobj.kernel()
assert numpy.abs(lib.fp(g) - -0.04079190644707999) < 1e-7
assert numpy.abs(lib.fp(g) - -0.04079319540057938) < 1e-5

mf = rks.RKS(mol, xc='b3lyp').density_fit().run()
mf.conv_tol_cpscf = 1e-7
Expand All @@ -119,8 +119,7 @@ def test_df_RKS(self):
mf = rks.RKS(mol, xc='wb97x').density_fit().run()
gobj = mf.nuc_grad_method().to_gpu()
g = gobj.kernel()
g -= g.sum(axis=0)/len(g)
assert numpy.abs(lib.fp(g) - -0.034343799164131) < 1e-5
assert numpy.abs(lib.fp(g) - -0.03432881437746617) < 1e-5

mf = rks.RKS(mol, xc='wb97x').density_fit().run()
mf.conv_tol_cpscf = 1e-7
Expand Down

0 comments on commit 0509a00

Please sign in to comment.