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

cRPA Screening and external RPA corrections #119

Merged
merged 33 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
9a3bfd0
Started adding cRPA screened coulomb interaction.
cjcscott Sep 13, 2022
8abee14
Working cRPA calculations with arbitrary excitation space.
cjcscott Sep 22, 2022
0a63691
Update to cRPA functionality.
cjcscott Sep 26, 2022
3091808
Functioning cRPA screening protocol with and without resticted intera…
cjcscott Sep 27, 2022
04e3a2e
Added functionality to use dynamical screened coulomb interaction res…
cjcscott Oct 3, 2022
80eac9d
Slight refactor of static cRPA calculation.
cjcscott Oct 4, 2022
0db7205
Fixed bug in ssRPA, and added option to use N^6 RPA in mRPA screening.
cjcscott Oct 4, 2022
29387e1
Added standardised reporting of spin symmetry breaking and dependence…
cjcscott Oct 6, 2022
694ed4a
Ensured crpa_full screening returns a fermionic wavefunction for ewf.
cjcscott Oct 7, 2022
2dbdfd6
Added ability to use spin-integrated mRPA screened interactions.
cjcscott Oct 8, 2022
1340cb9
Fix bug in unscreened interactions.
cjcscott Oct 8, 2022
921954b
Ensured proper treatment of cRPA when no excitations are in the cRPA …
cjcscott Oct 9, 2022
c33fe3d
Added warnings for storage of cRPA object within static cRPA calculat…
cjcscott Oct 11, 2022
ad6cd71
Update RPA moment calculation to support cRPA.
cjcscott Oct 11, 2022
2350016
Added polarisation coupling calculation into cRPA calculation.
cjcscott Oct 12, 2022
b3d8e27
Added option to only use cRPA screening in the ov space, and changed …
cjcscott Oct 17, 2022
fa4fb1f
Added RPA external correction for ewf.
cjcscott Nov 1, 2022
d0f6242
Added RPA external correction via cumulant 2rdm contribution.
cjcscott Nov 3, 2022
c84d8d9
Added fixed expression for nonlocal cumulant correction.
cjcscott Nov 7, 2022
0c15bfb
Added option to match cluster fock matrix, rather than one-body hamil…
cjcscott Nov 28, 2022
b26fdc1
Update crpa and add tests for cRPA calculation.
cjcscott May 25, 2023
315cfae
Added Exception when requesting RPA external correction with unrestri…
cjcscott May 31, 2023
c99489c
Remove duplicate log message.
cjcscott Jun 2, 2023
fd05d9c
Added documentation on what screened interaction options do in the Ha…
cjcscott Jun 2, 2023
b6c9290
Restored missing import in ssURPA.
cjcscott Jun 9, 2023
0cd658b
Moved calculation of external RPA corrections to separate instance me…
cjcscott Jul 28, 2023
aead966
Avoided clash in e_nonlocal namespace.
cjcscott Jul 28, 2023
13366d3
Remove defunct code.
cjcscott Jul 28, 2023
fd94a19
Added warnings for calculations using cRPA screened interactions.
cjcscott Jul 28, 2023
d621499
Added tests for RPA external corrections.
cjcscott Jul 28, 2023
c825f43
Rename function to generate local RPA correlation energy.
cjcscott Jul 28, 2023
ed6d838
Added example for screening and RPA external corrections.
cjcscott Jul 28, 2023
0270c47
Response to Ollie's review.
cjcscott Aug 3, 2023
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
49 changes: 49 additions & 0 deletions examples/ewf/molecules/08-screening-rpa-corrections.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import pyscf.gto
import pyscf.scf
import pyscf.cc
import vayesta.ewf


mol = pyscf.gto.Mole()
mol.atom = """
O 0.0000 0.0000 0.1173
H 0.0000 0.7572 -0.4692
H 0.0000 -0.7572 -0.4692
"""
mol.basis = 'cc-pVTZ'
mol.output = 'pyscf.out'
mol.build()

# Hartree-Fock
mf = pyscf.scf.RHF(mol).density_fit()
mf.kernel()

# Reference full system CCSD:
cc = pyscf.cc.CCSD(mf)
cc.kernel()

eta = 1e-6

# Embedded CCSD calculation with bare interactions and no energy correction.
emb_bare = vayesta.ewf.EWF(mf, bath_options=dict(threshold=eta))
emb_bare.kernel()

# Embedded CCSD with mRPA screened interactions and RPA cumulant approximation for nonlocal correlations.
emb = vayesta.ewf.EWF(mf, bath_options=dict(threshold=eta), screening="mrpa", ext_rpa_correction="cumulant")
emb.kernel()
e_nonlocal_cumulant = emb.e_nonlocal
# Embedded CCSD with mRPA screened interactions and delta RPA approximation for nonlocal correlations.
emb = vayesta.ewf.EWF(mf, bath_options=dict(threshold=eta), screening="mrpa", ext_rpa_correction="erpa")
emb.kernel()
e_nonlocal_erpa = emb.e_nonlocal

# Note that mRPA screening and external corrections often cancel with each other in the case of the energy.
print("E(CCSD)= %+16.8f Ha" % cc.e_tot)
print("E(RPA)= %+16.8f Ha (error= %+.8f Ha)" % (emb.e_mf + emb.e_rpa,
emb.e_mf + emb.e_rpa - cc.e_tot))
print("E(Emb. CCSD)= %+16.8f Ha (error= %+.8f Ha)" % (emb_bare.e_tot, emb_bare.e_tot-cc.e_tot))
print("E(Emb. Screened CCSD)= %+16.8f Ha (error= %+.8f Ha)" % (emb.e_tot, emb.e_tot-cc.e_tot))
print("E(Emb. Screened CCSD + \Delta E_k)= %+16.8f Ha (error= %+.8f Ha)" % (emb.e_tot+e_nonlocal_cumulant,
emb.e_tot+e_nonlocal_cumulant-cc.e_tot))
print("E(Emb. Screened CCSD + \Delta RPA)= %+16.8f Ha (error= %+.8f Ha)" % (emb.e_tot+e_nonlocal_erpa,
emb.e_tot+e_nonlocal_erpa-cc.e_tot))
22 changes: 20 additions & 2 deletions vayesta/core/qemb/fragment.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import pyscf.lo
# --- Internal
from vayesta.core.util import (OptionsBase, cache, deprecated, dot, einsum, energy_string, fix_orbital_sign, hstack,
log_time, time_string, timer)
log_time, time_string, timer, AbstractMethodError)
from vayesta.core import spinalg
from vayesta.core.types import Cluster
from vayesta.core.symmetry import SymmetryIdentity
Expand All @@ -28,6 +28,7 @@
from vayesta.misc.cubefile import CubeFile
from vayesta.mpi import mpi
from vayesta.solver import get_solver_class, check_solver_config, ClusterHamiltonian
from vayesta.core.screening.screening_crpa import get_frag_W

Check warning on line 31 in vayesta/core/qemb/fragment.py

View check run for this annotation

Codecov / codecov/patch

vayesta/core/qemb/fragment.py#L31

Added line #L31 was not covered by tests

# Get MPI rank of fragment
get_fragment_mpi_rank = lambda *args : args[0].mpi_rank
Expand All @@ -45,6 +46,7 @@
store_eris: bool = None # If True, ERIs will be cached in Fragment.hamil
dm_with_frozen: bool = None # TODO: is still used?
screening: typing.Optional[str] = None
match_cluster_fock: bool = None

Check warning on line 49 in vayesta/core/qemb/fragment.py

View check run for this annotation

Codecov / codecov/patch

vayesta/core/qemb/fragment.py#L49

Added line #L49 was not covered by tests
# Fragment specific
# -----------------
# Auxiliary fragments are treated before non-auxiliary fragments, but do not contribute to expectation values
Expand Down Expand Up @@ -73,6 +75,7 @@
converged: bool = None # True, if solver reached convergence criterion or no convergence required (eg. MP2 solver)
# --- Energies
e_corr: float = None # Fragment correlation energy contribution
e_corr_rpa: float = None # Fragment correlation energy contribution at the level of RPA.

Check warning on line 78 in vayesta/core/qemb/fragment.py

View check run for this annotation

Codecov / codecov/patch

vayesta/core/qemb/fragment.py#L78

Added line #L78 was not covered by tests
# --- Wave-function
wf: WaveFunction = None # WaveFunction object (MP2, CCSD,...)
pwf: WaveFunction = None # Fragment-projected wave function
Expand Down Expand Up @@ -1036,10 +1039,25 @@
cluster_solver.hamil.add_screening(self._seris_ov)
return cluster_solver

def get_local_rpa_correction(self, hamil=None):
e_loc_rpa = None
if self.base.opts.ext_rpa_correction:
hamil = hamil or self.hamil
cumulant = self.base.opts.ext_rpa_correction == "cumulant"
if self.base.opts.ext_rpa_correction not in ["erpa", "cumulant"]:

Check warning on line 1047 in vayesta/core/qemb/fragment.py

View check run for this annotation

Codecov / codecov/patch

vayesta/core/qemb/fragment.py#L1042-L1047

Added lines #L1042 - L1047 were not covered by tests
raise ValueError("Unknown external rpa correction %s specified.")
e_loc_rpa = hamil.calc_loc_erpa(*self._seris_ov[1:], cumulant)
return e_loc_rpa

Check warning on line 1050 in vayesta/core/qemb/fragment.py

View check run for this annotation

Codecov / codecov/patch

vayesta/core/qemb/fragment.py#L1049-L1050

Added lines #L1049 - L1050 were not covered by tests

def get_frag_hamil(self):
if self.opts.screening is not None:
if "crpa_full" in self.opts.screening:
self.bos_freqs, self.couplings = get_frag_W(self.mf, self, pcoupling=("pcoupled" in self.opts.screening),

Check warning on line 1055 in vayesta/core/qemb/fragment.py

View check run for this annotation

Codecov / codecov/patch

vayesta/core/qemb/fragment.py#L1053-L1055

Added lines #L1053 - L1055 were not covered by tests
only_ov_screened=("ov" in self.opts.screening),
log=self.log)
# This detects based on fragment what kind of Hamiltonian is appropriate (restricted and/or EB).
return ClusterHamiltonian(self, self.mf, self.log, screening=self.opts.screening,
cache_eris=self.opts.store_eris)
cache_eris=self.opts.store_eris, match_fock=self.opts.match_cluster_fock)

def get_solver_options(self, *args, **kwargs):
raise AbstractMethodError
49 changes: 39 additions & 10 deletions vayesta/core/qemb/qemb.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from vayesta.core.ao2mo import postscf_ao2mo
from vayesta.core.ao2mo import postscf_kao2gmo
from vayesta.core.scmf import PDMET, Brueckner
from vayesta.core.qemb.scrcoulomb import build_screened_eris
from vayesta.core.screening.screening_moment import build_screened_eris

Check warning on line 28 in vayesta/core/qemb/qemb.py

View check run for this annotation

Codecov / codecov/patch

vayesta/core/qemb/qemb.py#L28

Added line #L28 was not covered by tests
from vayesta.mpi import mpi
from vayesta.core.qemb.register import FragmentRegister
from vayesta.rpa import ssRIRPA
Expand Down Expand Up @@ -105,15 +105,17 @@
# EBFCI/EBCCSD
max_boson_occ=2,
# EBCC
ansatz=None,
ansatz=None, fermion_wf=False,
# Dump
dumpfile='clusters.h5',
# MP2
compress_cderi=False)
# --- Other
symmetry_tol: float = 1e-6 # Tolerance (in Bohr) for atomic positions
symmetry_mf_tol: float = 1e-5 # Tolerance for mean-field solution
screening: Optional[str] = None
screening: Optional[str] = None # What form of screening to use in clusters.
ext_rpa_correction: Optional[str] = None
match_cluster_fock: bool = False

Check warning on line 118 in vayesta/core/qemb/qemb.py

View check run for this annotation

Codecov / codecov/patch

vayesta/core/qemb/qemb.py#L116-L118

Added lines #L116 - L118 were not covered by tests

class Embedding:
"""Base class for quantum embedding methods.
Expand Down Expand Up @@ -518,6 +520,12 @@
"""Nuclear-repulsion energy per unit cell (not folded supercell)."""
return self.mol.energy_nuc()/self.ncells

@property
def e_nonlocal(self):
if self.opts.ext_rpa_correction is None:
return 0.0
return self.e_rpa - sum([x.results.e_corr_rpa * x.symmetry_factor for x in self.get_fragments(sym_parent=None)])

Check warning on line 527 in vayesta/core/qemb/qemb.py

View check run for this annotation

Codecov / codecov/patch

vayesta/core/qemb/qemb.py#L523-L527

Added lines #L523 - L527 were not covered by tests

# Embedding properties

@property
Expand Down Expand Up @@ -755,13 +763,33 @@
@log_method()
@with_doc(build_screened_eris)
def build_screened_eris(self, *args, **kwargs):
scrfrags = [x.opts.screening for x in self.fragments if x.opts.screening is not None]
if len(scrfrags) > 0:
# Calculate total dRPA energy in N^4 time; this is cheaper than screening calculations.
rpa = ssRIRPA(self.mf, log=self.log)
self.e_nonlocal, energy_error = rpa.kernel_energy(correction='linear')
if scrfrags.count("mrpa") > 0:
build_screened_eris(self, *args, **kwargs)
nmomscr = len([x.opts.screening for x in self.fragments if x.opts.screening == "mrpa"])
if self.opts.ext_rpa_correction:
cumulant = self.opts.ext_rpa_correction == "cumulant"
if nmomscr < self.nfrag:

Check warning on line 769 in vayesta/core/qemb/qemb.py

View check run for this annotation

Codecov / codecov/patch

vayesta/core/qemb/qemb.py#L766-L769

Added lines #L766 - L769 were not covered by tests
raise NotImplementedError("External dRPA correction currently requires all fragments use mrpa screening.")

if self.opts.ext_rpa_correction not in ["erpa", "cumulant"]:

Check warning on line 772 in vayesta/core/qemb/qemb.py

View check run for this annotation

Codecov / codecov/patch

vayesta/core/qemb/qemb.py#L772

Added line #L772 was not covered by tests
raise ValueError("Unknown external rpa correction %s specified.")
l_ = self.get_cderi(self.mf.mo_coeff)[0]
rpa = ssRIRPA(self.mf, log=self.log, Lpq=l_)
if cumulant:
l_ = l_[:, :self.nocc, self.nocc:].reshape((l_.shape[0], -1))
l_ = np.concatenate([l_, l_], axis=1)

Check warning on line 778 in vayesta/core/qemb/qemb.py

View check run for this annotation

Codecov / codecov/patch

vayesta/core/qemb/qemb.py#L774-L778

Added lines #L774 - L778 were not covered by tests

m0 = rpa.kernel_moms(0, target_rot=l_)[0][0]

Check warning on line 780 in vayesta/core/qemb/qemb.py

View check run for this annotation

Codecov / codecov/patch

vayesta/core/qemb/qemb.py#L780

Added line #L780 was not covered by tests
# Deduct effective mean-field contribution and project the RHS and we're done.
self.e_rpa = 0.5 * einsum("pq,pq->", m0 - l_, l_)

Check warning on line 782 in vayesta/core/qemb/qemb.py

View check run for this annotation

Codecov / codecov/patch

vayesta/core/qemb/qemb.py#L782

Added line #L782 was not covered by tests
else:
# Calculate total dRPA energy in N^4 time; this is cheaper than screening calculations.
self.e_rpa, energy_error = rpa.kernel_energy(correction='linear')
self.log.info("Set total RPA correlation energy contribution as %s", energy_string(self.e_rpa))
if nmomscr > 0:
self.log.info("")
self.log.info("SCREENED INTERACTION SETUP")
self.log.info("==========================")
with log_time(self.log.timing, "Time for screened interation setup: %s"):
build_screened_eris(self, *args, store_m0=self.opts.ext_rpa_correction, **kwargs)

Check warning on line 792 in vayesta/core/qemb/qemb.py

View check run for this annotation

Codecov / codecov/patch

vayesta/core/qemb/qemb.py#L785-L792

Added lines #L785 - L792 were not covered by tests

# Symmetry between fragments
# --------------------------
Expand Down Expand Up @@ -1561,6 +1589,7 @@
def _reset(self):
self.e_corr = None
self.converged = False
self.e_rpa = None

Check warning on line 1592 in vayesta/core/qemb/qemb.py

View check run for this annotation

Codecov / codecov/patch

vayesta/core/qemb/qemb.py#L1592

Added line #L1592 was not covered by tests
cjcscott marked this conversation as resolved.
Show resolved Hide resolved

def reset(self, *args, **kwargs):
self._reset()
Expand Down
7 changes: 7 additions & 0 deletions vayesta/core/qemb/uqemb.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,13 @@
eris = postscf_ao2mo(postscf, fock=fock, mo_energy=mo_energy, e_hf=e_hf)
return eris

@log_method()
@with_doc(Embedding.build_screened_eris)
def build_screened_eris(self, *args, **kwargs):
if self.opts.ext_rpa_correction is not None:

Check warning on line 202 in vayesta/core/qemb/uqemb.py

View check run for this annotation

Codecov / codecov/patch

vayesta/core/qemb/uqemb.py#L199-L202

Added lines #L199 - L202 were not covered by tests
raise NotImplementedError("External RPA correlation energy only implemented for restricted references!")
super().build_screened_eris(*args, **kwargs)

Check warning on line 204 in vayesta/core/qemb/uqemb.py

View check run for this annotation

Codecov / codecov/patch

vayesta/core/qemb/uqemb.py#L204

Added line #L204 was not covered by tests

def update_mf(self, mo_coeff, mo_energy=None, veff=None):
"""Update underlying mean-field object."""
# Chech orthonormal MOs
Expand Down
Empty file.
Loading
Loading