Skip to content

Commit

Permalink
Staggered mesh method for PBC exact exchange (#98)
Browse files Browse the repository at this point in the history
* begin khf_stagger implementation

* Save progress

* More progress

* Create tests, delete __init__.py s

* Progress on tests. Something might be up with split-scf

* Fix split scf

* Renaming symbols

* refactoring

* Begin example file, add KRKS compatibility

* Progress on KRKS, fixing bug with regular version

* Fixed regular

* Cleanup, finalize example

* Add documentation

* Formatting

* pep-625 compliance

* Compatibility issue when building aarch64 wheel

* Update pypi upload configurations

* Add MSDFT method (#77)

* Add MSDFT method

* Using generalized Slater-Condon rules for HF integrals in NOCI

* Add comments

* typo

* Lint

* Fix bug in nuclear energy treatments

* Error in sm_t

* Update tests

* Update tests

* Adjust threshold in tests

* Adjust tests

* Numerical noises in mcpdft tests

* Remove 2d code

* Formatting

* Fix split-scf bug. Switch to FFTDF in tests (verified with QChem).

* Small cleanup

* Fix typos

* Fix flake8, typos, unneeded print statements

* turn build_probe_cell, modified_madelung, converged_modified into static methods of KHF_stagger

* Refactoring staggered mesh exchange calculation

* Refactor kernel function. Non-scf and split-scf now just asserts mf.converged instead of calling mf.kernel

* fix flake8

* Rename functions and fix documentation for clairity

* Remove xcfun

---------

Co-authored-by: Qiming Sun <[email protected]>
  • Loading branch information
stephen-quiton and sunqm authored Jan 20, 2025
1 parent 0c25868 commit e5bf149
Show file tree
Hide file tree
Showing 3 changed files with 682 additions and 0 deletions.
130 changes: 130 additions & 0 deletions examples/pbc/22-kpoints_khf_stagger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#!/usr/bin/env python

"""
Example script for exact exchange with staggered mesh method.
Author: Stephen Quiton ([email protected])
Reference: The Staggered Mesh Method: Accurate Exact Exchange Toward the
Thermodynamic Limit for Solids, J. Chem. Theory Comput. 2024, 20,
18, 7958-7968
"""

from pyscf.pbc import df, gto, scf
from pyscf.pbc.scf.khf_stagger import KHF_stagger
from pyscf.pbc import dft as pbcdft
from pyscf import dft

"""
Hydrogen dimer
"""
cell = gto.Cell()
cell.pseudo = "gth-pbe"
cell.basis = "gth-szv"
cell.ke_cutoff = 100
cell.atom = """
H 3.00 3.00 2.10
H 3.00 3.00 3.90
"""
cell.a = """
6.0 0.0 0.0
0.0 6.0 0.0
0.0 0.0 6.0
"""
cell.unit = "B"
cell.verbose = 4
cell.build()

"""
For Non-SCF and Split-SCF, need to run a normal kpts SCF calculation first.
"""
nks = [2, 2, 2]
kpts = cell.make_kpts(nks, with_gamma_point=True)
kmf = scf.KRHF(cell, kpts, exxdiv="ewald")
kmf.with_df = df.GDF(cell, kpts).build()
ehf = kmf.kernel()

"""
KHF Stagger, Non-SCF version
Compute densities at shifted kpt mesh non-self-consistently using the Fock
matrix at the unshifted mesh. Additional cost is ~ 1 extra K-build.
"""
kmf_stagger = KHF_stagger(kmf, "non-scf")
kmf_stagger.kernel()
etot = kmf_stagger.e_tot
ek_stagger = kmf_stagger.ek

print("Non-SCF Stagger")
print("Total energy: ", etot)
print("Exchange energy: ", ek_stagger)

assert abs(etot - -1.0915433999061728) < 1e-6
assert abs(ek_stagger - -0.5688182610550594) < 1e-6

"""
KHF Stagger, Split-SCF version
Converge densities at shifted kpt mesh self-conistently. Additional cost
is ~ 1 extra SCF kernel.
"""
kmf_stagger = KHF_stagger(kmf, "split-scf")
kmf_stagger.kernel()
etot = kmf_stagger.e_tot
ek_stagger = kmf_stagger.ek

print("Split-SCF Stagger")
print("Total energy: ", etot)
print("Exchange energy: ", ek_stagger)

assert abs(etot - -1.0907254038200516) < 1e-6
assert abs(ek_stagger - -0.5680002649689386) < 1e-6

"""
KHF Stagger, regular version
Converge all densities with combined unshifted + shifted mesh. Total estimated
cost is 4x normal SCF. No need for prior SCF calculation.
"""
nks = [2, 2, 2]
kpts = cell.make_kpts(nks, with_gamma_point=True)
kmf = scf.KRHF(cell, kpts, exxdiv="ewald")
kmf.with_df = df.GDF(cell, kpts).build()
# No kernel needed.
kmf_stagger = KHF_stagger(kmf, "regular")
kmf_stagger.kernel()
etot = kmf_stagger.e_tot
ek_stagger = kmf_stagger.ek

print("Regular Stagger")
print("Total energy: ", etot)
print("Exchange energy: ", ek_stagger)


assert abs(etot - -1.0973224854862946 < 1e-6)
assert abs(ek_stagger - -0.5684614923801601) < 1e-6


"""
KHF Stagger Non-SCF with DFT
"""

nks = [2, 2, 2]
kpts = cell.make_kpts(nks, with_gamma_point=True)

dft.numint.NumInt.libxc = dft.xcfun
xc = "PBE0"
krks = pbcdft.KRKS(cell, kpts)
krks.xc = xc
krks.exxdiv = "ewald"
krks.with_df = df.GDF(cell, kpts).build()
edft = krks.kernel()

krks_stagger = KHF_stagger(krks, "non-scf")
krks_stagger.kernel()
etot = krks_stagger.e_tot
ek_stagger = krks_stagger.ek

print("Non-SCF Stagger with DFT")
print("Total energy: ", etot)
print("Exchange energy: ", ek_stagger)

assert abs(etot - -1.133718165281441) < 1e-6
assert abs(ek_stagger - -0.5678939393308997) < 1e-6
Loading

0 comments on commit e5bf149

Please sign in to comment.