Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add UKS to pyscfad #3

Open
wants to merge 31 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
8710531
add UKS
yangjunjie0320 Oct 7, 2021
94fe819
add UKS
yangjunjie0320 Oct 7, 2021
4a5110f
add uks
yangjunjie0320 Oct 7, 2021
39f9522
add UKS
yangjunjie0320 Oct 7, 2021
1f1551a
add UKS
yangjunjie0320 Oct 7, 2021
707e35d
add UKS to pyscfad
yangjunjie0320 Oct 8, 2021
afe8b30
add UKS
yangjunjie0320 Oct 9, 2021
9c45fc5
Merge branch 'main' of https://github.com/fishjojo/pyscfad into dev
yangjunjie0320 Nov 5, 2021
45f0e11
update to origin main
yangjunjie0320 Nov 5, 2021
2092eb9
update
yangjunjie0320 Nov 7, 2021
67dd73e
update UKS
yangjunjie0320 Nov 7, 2021
43950c3
Merge branch 'main' of https://github.com/fishjojo/pyscfad into dev
yangjunjie0320 Nov 8, 2021
23d0acc
update
yangjunjie0320 Nov 8, 2021
f62a735
update
yangjunjie0320 Nov 8, 2021
9f15b41
add uks
yangjunjie0320 Nov 9, 2021
f8da1c6
update uks
yangjunjie0320 Nov 9, 2021
c5a3fb9
update uks
yangjunjie0320 Nov 9, 2021
abb12ce
update uks
yangjunjie0320 Nov 9, 2021
1bfbfef
update uks
yangjunjie0320 Nov 9, 2021
cf4c865
update uks
yangjunjie0320 Nov 9, 2021
1740cb1
update uks
yangjunjie0320 Nov 9, 2021
e82e111
update uks
yangjunjie0320 Nov 10, 2021
cd4d671
update
yangjunjie0320 Nov 26, 2021
1ce1a93
update
yangjunjie0320 Nov 26, 2021
f9ac6ea
update
yangjunjie0320 Nov 26, 2021
f1e3ad8
add uks
yangjunjie0320 Jan 4, 2022
aa70a6d
add UKS
yangjunjie0320 Jan 4, 2022
bb86204
add MGGA tests
yangjunjie0320 Jan 7, 2022
5e96d37
test UKS-MGGA
yangjunjie0320 Jan 7, 2022
c281432
add UKS
yangjunjie0320 Jan 7, 2022
9e9e01e
fixme: UKS-MGGA
yangjunjie0320 Jan 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 50 additions & 7 deletions examples/dft/00-simple.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,61 @@
import numpy

import pyscf
from pyscfad import gto, dft

"""
Analytic nuclear gradient for RKS computed by auto-differentiation
"""

mol = gto.Mole()
mol.atom = 'H 0 0 0; H 0 0 0.74' # in Angstrom
mol.basis = '631g'
mol.verbose=5
mol = gto.Mole()
mol.atom = '''
H 0.0000000 0.0000000 0.3540000
H 0.0000000 0.0000000 -0.3540000
'''
mol.basis = 'cc-pvdz'
mol.verbose = 3
mol.build()

mf = dft.RKS(mol)
mf.xc = 'b3lyp'
# LDA
mf = dft.RKS(mol)
mf.xc = "LDA"
mf.kernel()

jac = mf.energy_grad()
g1 = jac.coords

grad = mf.nuc_grad_method()
grad.verbose = 0
g2 = grad.kernel()

assert abs(g1-g2).max() < 1e-6

# GGA
mf = dft.RKS(mol)
mf.xc = "PBE"
mf.kernel()

jac = mf.energy_grad()
g1 = jac.coords

grad = mf.nuc_grad_method()
grad.verbose = 0
g2 = grad.kernel()

assert abs(g1-g2).max() < 1e-6

# meta-GGA
mf = dft.RKS(mol)
mf.xc = "TPSS"
mf.kernel()

jac = mf.energy_grad()
print(jac.coords)
g1 = jac.coords

# MGGA Gradient Not Implemented
g2 = numpy.asarray(
[[0.0, 0.0, -4.14229185e-02],
[0.0, 0.0, 4.14229185e-02]]
) # From finite-difference

assert abs(g1-g2).max() < 1e-6
97 changes: 97 additions & 0 deletions examples/dft/02-uks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import numpy

import pyscf
from pyscf import scf
from pyscfad import gto, dft

def init_guess_mixed(mf, mixing_parameter=numpy.pi/4):
mol = mf.mol
h1e = scf.hf.get_hcore(mol)
s1e = scf.hf.get_ovlp(mol)

dm0 = mf.get_init_guess(key='minao')
vhf = mf.get_veff(dm=dm0)
fock = mf.get_fock(h1e, s1e, vhf, dm0, 0, None)

mo_energy, mo_coeff = mf.eig(fock, s1e)
nao, nmo = mo_coeff[0].shape

mo_occ = mf.get_occ(mo_energy=mo_energy, mo_coeff=mo_coeff)

homo_idx_a = mol.nelec[0] - 1
lumo_idx_a = homo_idx_a + 1

homo_idx_b = mol.nelec[1] - 1
lumo_idx_b = homo_idx_b + 1

assert mo_occ[0][homo_idx_a] == 1.0
assert mo_occ[0][lumo_idx_a] == 0.0
assert mo_occ[1][homo_idx_b] == 1.0
assert mo_occ[1][lumo_idx_b] == 0.0

theta = mixing_parameter
r_theta = numpy.array(
[[numpy.cos(theta), -numpy.sin(theta)],
[numpy.sin(theta), numpy.cos(theta)]]
)
t_theta = [numpy.eye(nmo, nmo), numpy.eye(nmo, nmo)]

t_theta[0][homo_idx_a:lumo_idx_a+1, homo_idx_a:lumo_idx_a+1] = r_theta
t_theta[1][homo_idx_b:lumo_idx_b+1, homo_idx_b:lumo_idx_b+1] = r_theta.T

mo_coeff = numpy.einsum("spq,sqn->spn", t_theta, mo_coeff)
return scf.uhf.make_rdm1(mo_coeff, mo_occ)


mol = gto.Mole()
mol.atom = '''
H 0.0000000 0.0000000 1.00000
H 0.0000000 0.0000000 -1.00000
'''
mol.basis = 'cc-pvdz'
mol.verbose = 3
mol.build()

# LDA
mf = dft.UKS(mol)
mf.xc = "LDA"
dm0 = init_guess_mixed(mf, mixing_parameter=numpy.pi/4)
mf.kernel(dm0=dm0)

jac = mf.energy_grad()
g1 = jac.coords

grad = mf.nuc_grad_method()
grad.verbose = 0
g2 = grad.kernel()

assert abs(g1-g2).max() < 1e-6

# GGA
mf = dft.UKS(mol)
mf.xc = "PBE"
mf.kernel(dm0=dm0)

jac = mf.energy_grad()
g1 = jac.coords

grad = mf.nuc_grad_method()
grad.verbose = 0
g2 = grad.kernel()

assert abs(g1-g2).max() < 1e-6

# meta-GGA
# mf = dft.UKS(mol)
# mf.xc = "TPSS"
# mf.kernel(dm0=dm0)

# jac = mf.energy_grad()
# g1 = jac.coords

# g2 = numpy.asarray(
# [[0.0, 0.0, 2.64877520e-02],
# [0.0, 0.0, -2.64877520e-02]]
# ) # From finite-difference

# assert abs(g1-g2).max() < 1e-6
1 change: 1 addition & 0 deletions pyscfad/dft/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from pyscfad.dft.rks import RKS
from pyscfad.dft.uks import UKS
Loading