2020from gpu4pyscf .dft .numint import NumInt as numint_gpu
2121from pyscf .dft .numint import NumInt as numint_cpu
2222import cupy
23+ import os
2324
2425def setUpModule ():
2526 global mol , dm1 , dm0
@@ -51,7 +52,7 @@ def _diff(dat, ref):
5152 return np .min ((abs (d / (ref + 1e-300 )), abs (d )), axis = 0 )
5253
5354class KnownValues (unittest .TestCase ):
54- def _check_xc (self , xc , spin = 0 , fxc_tol = 1e-10 , kxc_tol = 1e-10 ):
55+ def _check_xc (self , xc , spin = 0 , deriv = 2 , fxc_tol = 1e-10 , kxc_tol = 1e-10 ):
5556 ni_cpu = numint_cpu ()
5657 ni_gpu = numint_gpu ()
5758 xctype = ni_cpu ._xc_type (xc )
@@ -66,8 +67,15 @@ def _check_xc(self, xc, spin=0, fxc_tol=1e-10, kxc_tol=1e-10):
6667 if spin != 0 :
6768 rho = (rho , rho )
6869
69- exc_cpu , vxc_cpu , fxc_cpu , kxc_cpu = ni_cpu .eval_xc_eff (xc , rho , deriv = 2 , xctype = xctype )
70- exc_gpu , vxc_gpu , fxc_gpu , kxc_gpu = ni_gpu .eval_xc_eff (xc , cupy .array (rho ), deriv = 2 , xctype = xctype )
70+ exc_cpu , vxc_cpu , fxc_cpu , kxc_cpu = ni_cpu .eval_xc_eff (xc , rho , deriv = deriv , xctype = xctype )
71+ exc_gpu , vxc_gpu , fxc_gpu , kxc_gpu = ni_gpu .eval_xc_eff (xc , cupy .array (rho ), deriv = deriv , xctype = xctype )
72+
73+ print (f"{ xc } { spin } exc" , _diff (exc_gpu [:,0 ].get (), exc_cpu ).max ())
74+ print (f"{ xc } { spin } vxc" , _diff (vxc_gpu .get (), vxc_cpu ).max ())
75+ if fxc_gpu is not None :
76+ print (f"{ xc } { spin } fxc" , _diff (fxc_gpu .get (), fxc_cpu ).max ())
77+ if kxc_gpu is not None :
78+ print (f"{ xc } { spin } kxc" , _diff (kxc_gpu .get (), kxc_cpu ).max ())
7179
7280 assert _diff (exc_gpu [:,0 ].get (), exc_cpu ).max () < 1e-10
7381 assert _diff (vxc_gpu .get (), vxc_cpu ).max () < 1e-10
@@ -77,15 +85,32 @@ def _check_xc(self, xc, spin=0, fxc_tol=1e-10, kxc_tol=1e-10):
7785 assert _diff (kxc_gpu .get (), kxc_cpu ).max () < kxc_tol
7886
7987 def test_LDA (self ):
80- self ._check_xc ('LDA_C_VWN' )
88+ whether_use_gpu = os .environ .get ('LIBXC_ON_GPU' , '0' ) == '1'
89+ if whether_use_gpu :
90+ deriv = 3
91+ print ("test LDA with deriv 3" )
92+ else :
93+ deriv = 2
94+ print ("test LDA with deriv 2" )
95+ self ._check_xc ('LDA_C_VWN' , deriv = deriv )
8196
8297 def test_GGA (self ):
83- self ._check_xc ('HYB_GGA_XC_B3LYP' )
84- self ._check_xc ('GGA_X_B88' , fxc_tol = 1e-10 )
85- self ._check_xc ('GGA_C_PBE' , fxc_tol = 1e-4 )
98+ whether_use_gpu = os .environ .get ('LIBXC_ON_GPU' , '0' ) == '1'
99+ if whether_use_gpu :
100+ deriv = 3
101+ else :
102+ deriv = 2
103+ self ._check_xc ('HYB_GGA_XC_B3LYP' , deriv = deriv , kxc_tol = 1e-9 )
104+ self ._check_xc ('GGA_X_B88' , fxc_tol = 1e-10 , deriv = deriv , kxc_tol = 1e-8 )
105+ self ._check_xc ('GGA_C_PBE' , fxc_tol = 1e-4 , deriv = deriv , kxc_tol = 3e2 )
86106
87107 def test_mGGA (self ):
88- self ._check_xc ('MGGA_C_M06' , fxc_tol = 1e-4 )
108+ whether_use_gpu = os .environ .get ('LIBXC_ON_GPU' , '0' ) == '1'
109+ if whether_use_gpu :
110+ deriv = 3
111+ else :
112+ deriv = 2
113+ self ._check_xc ('MGGA_C_M06' , fxc_tol = 1e-4 , deriv = deriv , kxc_tol = 1e-2 )
89114
90115 def test_u_LDA (self ):
91116 self ._check_xc ('LDA_C_VWN' , spin = 1 )
@@ -101,4 +126,4 @@ def test_u_mGGA(self):
101126
102127if __name__ == "__main__" :
103128 print ("Full Tests for xc fun" )
104- unittest .main ()
129+ unittest .main ()
0 commit comments