Skip to content

Commit

Permalink
improve examples (#142)
Browse files Browse the repository at this point in the history
* improve examples

* fixed a bug on gaming cards
  • Loading branch information
wxj6000 authored Apr 30, 2024
1 parent f979712 commit 7c751a9
Show file tree
Hide file tree
Showing 23 changed files with 258 additions and 254 deletions.
51 changes: 29 additions & 22 deletions examples/00-h2o.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,62 +13,69 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.


##############################################################################
# This example shows the basic usage of PySCF/GPU4PySCF #
# For the complete guide, please refer to #
# - PySCF user guide (https://pyscf.org/user.html) #
# - PySCF examples (https://github.com/pyscf/pyscf/tree/master/examples) #
##############################################################################

import numpy as np
import cupy
import pyscf
from pyscf import lib
from pyscf.hessian import thermo
from gpu4pyscf.dft import rks
lib.num_threads(8)

atom = '''
O 0.0000000000 -0.0000000000 0.1174000000
H -0.7570000000 -0.0000000000 -0.4696000000
H 0.7570000000 0.0000000000 -0.4696000000
'''

xc = 'B3LYP'
bas = 'def2-tzvpp'
auxbasis = 'def2-tzvpp-jkfit'
scf_tol = 1e-10
max_scf_cycles = 50
screen_tol = 1e-14
mol = pyscf.M(
atom=atom, # water molecule
basis='def2-tzvpp', # basis set
output='./pyscf.log', # save log file
verbose=1 # control
)

mol = pyscf.M(atom=atom, basis=bas, max_memory=32000)
mf_GPU = rks.RKS( # restricted Kohn-Sham DFT
mol, # pyscf.gto.object
xc='b3lyp' # xc funtionals, such as pbe0, wb97m-v, tpss,
).density_fit() # density fitting

mol.verbose = 4
mf_GPU = rks.RKS(mol, xc=xc).density_fit(auxbasis=auxbasis)
mf_GPU.grids.atom_grid = (99,590)
mf_GPU.conv_tol = scf_tol
mf_GPU.max_cycle = max_scf_cycles
mf_GPU.screen_tol = screen_tol
mf_GPU.conv_tol_cpscf = 1e-3
mf_GPU.grids.atom_grid = (99,590) # (99,590) lebedev grids, (75,302) is often enough
mf_GPU.conv_tol = 1e-10 # controls SCF convergence tolerance
mf_GPU.max_cycle = 50 # controls max iterations of SCF
mf_GPU.conv_tol_cpscf = 1e-3 # controls max iterations of CPSCF (for hessian)

# Compute Energy
e_dft = mf_GPU.kernel()
print(f"total energy = {e_dft}") # -76.26736519501688

# Compute Gradient
g = mf_GPU.nuc_grad_method()
g.max_memory = 20000
g.auxbasis_response = True
g_dft = g.kernel()

# Compute Hessian
h = mf_GPU.Hessian()
h.auxbasis_response = 2
h.auxbasis_response = 2 # 0: no aux contribution, 1: some contributions, 2: all
h_dft = h.kernel()

# harmonic analysis
results = thermo.harmonic_analysis(mol, h_dft)
thermo.dump_normal_mode(mol, results)

results = thermo.thermo(mf_GPU, results['freq_au'], 298.15, 101325)
results = thermo.thermo(
mf_GPU, # GPU4PySCF object
results['freq_au'],
298.15, # room temperature
101325) # standard atmosphere

thermo.dump_thermo(mol, results)

# force translational symmetry
natm = mol.natm
h_dft = h_dft.transpose([0,2,1,3]).reshape(3*natm,3*natm)
h_diag = h_dft.sum(axis=0)
h_dft -= np.diag(h_diag)
print(h_dft[:3,:3])
48 changes: 11 additions & 37 deletions examples/01-h2o_dftd3.py → examples/01-h2o_with_dispersion.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,47 +13,32 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

###########################################################
# Example of DFT with Dispersion correction (dftd3/dftd4)
###########################################################

import numpy as np
import pyscf
from pyscf import lib
from gpu4pyscf.dft import rks

lib.num_threads(8)

atom ='''
O 0.0000000000 -0.0000000000 0.1174000000
H -0.7570000000 -0.0000000000 -0.4696000000
H 0.7570000000 0.0000000000 -0.4696000000
'''

xc = 'B3LYP'
bas = 'def2-tzvpp'
auxbasis = 'def2-tzvpp-jkfit'
scf_tol = 1e-10
max_scf_cycles = 50
screen_tol = 1e-14
grids_level = 8

mol = pyscf.M(atom=atom, basis=bas, max_memory=32000)
# set verbose >= 6 for debugging timer
mol.verbose = 1
mf_GPU = rks.RKS(mol, xc=xc, disp='d3bj').density_fit(auxbasis=auxbasis)
mf_GPU.grids.level = grids_level
mf_GPU.conv_tol = scf_tol
mf_GPU.max_cycle = max_scf_cycles
mol = pyscf.M(atom=atom, basis='def2-tzvpp')
mf_GPU = rks.RKS(mol, xc='b3lyp').density_fit()
mf_GPU.disp = 'd3bj' # d3zero, d3bjm, d4
mf_GPU.grids.level = 5
mf_GPU.conv_tol = 1e-10
mf_GPU.max_cycle = 50
mf_GPU.conv_tol_cpscf = 1e-6
mf_GPU.screen_tol = screen_tol

# Compute Energy
print('------------------- Energy -----------------------------')
e_dft = mf_GPU.kernel()
print('DFT energy by GPU4PySCF')
print(e_dft)
print('DFT energy by Q-Chem')
e_qchem = -76.4672557846
print(e_qchem) # reference from q-chem: -76.4672557846
print('Energy diff between Q-Chem and PySCF', e_dft - e_qchem)

# Compute Gradient
print('------------------ Gradient ----------------------------')
Expand All @@ -62,28 +47,17 @@
g_dft = g.kernel()
print('Gradient by GPU4PySCF')
print(g_dft)
print('Gradient by Q-Chem')
# reference from q-chem
g_qchem = np.array([[0.0000000, 0.0030278, -0.0030278],
[-0.0000000, -0.0000000, 0.0000000],
[-0.0023449, 0.0011724, 0.0011724]]).T
print(g_qchem)
print('Gradient diff between Q-Chem and PySCF', np.linalg.norm(g_dft - g_qchem))

# Compute Hessian
print('------------------- Hessian -----------------------------')
h = mf_GPU.Hessian()
h.auxbasis_response = 2
h_dft = h.kernel()
print('Diagonal entries of Mass-weighted Hessian by GPU4PySCF')
mass = [15.99491, 1.00783, 1.00783]
for i in range(3):
for j in range(3):
h_dft[i,j] = h_dft[i,j]/np.sqrt(mass[i]*mass[j])
n = h_dft.shape[0]
h_dft = h_dft.transpose([0,2,1,3]).reshape(3*n,3*n)
print(np.diag(h_dft))
print('Diagonals entries of Mass-weighted Hessian by Q-Chem')
hess_qchem = np.loadtxt('hess_qchem.txt')
print(np.diag(hess_qchem))
print('Hessian diff between Q-Chem and PySCF', np.linalg.norm(hess_qchem - h_dft))
print('Hessian by GPU4PySCF')
print(h_dft)
30 changes: 11 additions & 19 deletions examples/02-h2o_geomopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,27 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

####################################################
# Example of geometry optimization with DFT
####################################################

import time
import pyscf
from pyscf import lib
from pyscf.geomopt.geometric_solver import optimize
from gpu4pyscf.dft import rks

lib.num_threads(8)

atom = '''
O 0.0000000000 -0.0000000000 0.1174000000
H -0.7570000000 -0.0000000000 -0.4696000000
H 0.7570000000 0.0000000000 -0.4696000000
'''

xc = 'B3LYP'
bas = 'def2-tzvpp'
auxbasis = 'def2-tzvpp-jkfit'
scf_tol = 1e-10
max_scf_cycles = 50
screen_tol = 1e-14
grids_level = 3
mol = pyscf.M(atom=atom, basis=bas, max_memory=32000)

mol.verbose = 1
mf_GPU = rks.RKS(mol, xc=xc, disp='d3bj').density_fit(auxbasis=auxbasis)
mf_GPU.grids.level = grids_level
mf_GPU.conv_tol = scf_tol
mf_GPU.max_cycle = max_scf_cycles
mf_GPU.screen_tol = screen_tol
mol = pyscf.M(atom=atom, basis='def2-tzvpp')
mf_GPU = rks.RKS(mol, xc='b3lyp', disp='d3bj').density_fit()
mf_GPU.disp = 'd3bj'
mf_GPU.grids.level = 3
mf_GPU.conv_tol = 1e-10
mf_GPU.max_cycle = 50

gradients = []
def callback(envs):
Expand All @@ -51,4 +43,4 @@ def callback(envs):
mol_eq = optimize(mf_GPU, maxsteps=20, callback=callback)
print("Optimized coordinate:")
print(mol_eq.atom_coords())
print(time.time() - start_time)
print('geometry optimization took', time.time() - start_time, 's')
29 changes: 11 additions & 18 deletions examples/03-h2o_dzvp.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,22 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

####################################################
# Example of DFT with different customized auxbasis
####################################################

import pyscf
import numpy as np
from pyscf import lib, gto
from pyscf import gto
from gpu4pyscf.dft import rks
lib.num_threads(8)

atom ='''
O 0.0000000000 -0.0000000000 0.1174000000
H -0.7570000000 -0.0000000000 -0.4696000000
H 0.7570000000 0.0000000000 -0.4696000000
'''

xc = 'B3LYP'
bas = 'dzvp'
scf_tol = 1e-10
max_scf_cycles = 50
screen_tol = 1e-1
grids_level = 3

mol = pyscf.M(atom=atom, basis=bas, max_memory=32000)

mol = pyscf.M(atom=atom, basis='dzvp')
auxbasis = {}
from pyscf import df
etb_basis = df.aug_etb(mol, beta=2.0)
Expand All @@ -47,13 +42,11 @@
else:
auxbasis[sym] = 'def2-tzvpp-jkfit'

# set verbose >= 6 for debugging timer
mol.verbose = 1
mf_GPU = rks.RKS(mol, xc=xc, disp='d3bj').density_fit(auxbasis=auxbasis)
mf_GPU.grids.level = grids_level
mf_GPU.conv_tol = scf_tol
mf_GPU.max_cycle = max_scf_cycles
mf_GPU.screen_tol = screen_tol
mf_GPU = rks.RKS(mol, xc='b3lyp').density_fit()
mf_GPU.disp = 'd3bj'
mf_GPU.grids.level = 3
mf_GPU.conv_tol = 1e-10
mf_GPU.max_cycle = 50

# Compute Energy
print('------------------- Energy -----------------------------')
Expand Down
15 changes: 9 additions & 6 deletions examples/04-h2o_mep.py → examples/04-h2o_esp.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,25 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

#########################################################
# Example of calculating Electrostatic potential (ESP)
#########################################################

import pyscf
import numpy as np
from pyscf import lib, gto
from pyscf import gto
from gpu4pyscf.dft import rks

atom ='''
atom ='''
O 0.0000000000 -0.0000000000 0.1174000000
H -0.7570000000 -0.0000000000 -0.4696000000
H 0.7570000000 0.0000000000 -0.4696000000
'''

mol = pyscf.M(atom=atom, basis='def2-tzvpp', max_memory=32000)
mol.verbose = 1
mf = rks.RKS(mol, xc='B3LYP').density_fit(auxbasis='def2-tzvpp-jkfit')
mol = pyscf.M(atom=atom, basis='def2-tzvpp')
mf = rks.RKS(mol, xc='B3LYP').density_fit()
mf.kernel()
dm = mf.make_rdm1()
dm = mf.make_rdm1() # compute one-electron density matrix

# Use default mesh grids
coords = mf.grids.coords.get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

##########################################################
# Example of compute dipole moment and quadrupole moment
##########################################################

import pyscf
from gpu4pyscf.dft import rks

Expand All @@ -22,9 +26,9 @@
H 0.7570000000 0.0000000000 -0.4696000000
'''

mol = pyscf.M(atom=atom, basis='def2-tzvpp', max_memory=32000)
mol = pyscf.M(atom=atom, basis='def2-tzvpp')
mol.verbose = 1
mf = rks.RKS(mol, xc='B3LYP').density_fit(auxbasis='def2-tzvpp-jkfit')
mf = rks.RKS(mol, xc='B3LYP').density_fit()
mf.kernel()
dm = mf.make_rdm1()

Expand All @@ -34,4 +38,4 @@

quad = mf.quad_moment(unit='DEBYE-ANG', dm=dm.get())
print('quadrupole moment:')
print(quad)
print(quad)
5 changes: 4 additions & 1 deletion examples/06-h2o_hf.py → examples/06-h2o_direct_scf.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

#######################################################
# Example of DFT with direct SCF scheme
#######################################################

from pyscf import gto
from gpu4pyscf import scf
import numpy as np
import cupy

mol = gto.M(atom=
'''
Expand Down
Loading

0 comments on commit 7c751a9

Please sign in to comment.