Skip to content

Commit a12069e

Browse files
committed
refactor dh
1 parent ccb2449 commit a12069e

5 files changed

Lines changed: 23 additions & 66 deletions

File tree

‎pyscf/dh/dh.py‎

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,23 @@ def __init__(self,
8282
self.e_tot = NotImplemented
8383
self.eng_tot = self.eng_nc = self.eng_pt2 = self.eng_nuc = self.eng_os = self.eng_ss = NotImplemented
8484

85+
def _init_common(self):
86+
"""Shared initialization after subclass creates mf_s / mf_n."""
87+
mf_s = self.mf_s
88+
mol = self.mol
89+
if not hasattr(self, 'grids') or self.grids is None:
90+
self.grids = getattr(mf_s, 'grids', dft.Grids(mol))
91+
if not hasattr(self, 'grids_cpks') or self.grids_cpks is None:
92+
self.grids_cpks = self.grids
93+
self.mf_s.grids = self.grids
94+
self.ni = getattr(mf_s, '_numint', dft.numint.NumInt())
95+
self.cx = self.ni.hybrid_coeff(self.xc)
96+
self.cx_n = self.ni.hybrid_coeff(self.xc_n)
97+
self.df_jk = mf_s.with_df
98+
self.df_ri = df.DF(mol, self.auxbasis_ri) if not self.same_aux else self.df_jk
99+
if self._scf is not None and self._scf.e_tot != 0:
100+
self.run_scf()
101+
85102
@property
86103
def base(self):
87104
return self
@@ -163,47 +180,8 @@ def build(self):
163180
self.mf_s.grids = self.mf_n.grids = self.grids
164181
if self.df_jk.auxmol is None:
165182
self.df_jk.build()
166-
self.aux_jk = self.df_jk.auxmol
167183
if self.df_ri.auxmol is None:
168184
self.df_ri.build()
169-
self.aux_ri = self.df_ri.auxmol
170-
171-
def set_frozen(self, method='auto'):
172-
if method != 'auto':
173-
raise NotImplementedError("Only method='auto' is supported.")
174-
return _ccsd_set_frozen(self)
175-
176-
def dump_intermediates(self, dir_path="scratch"):
177-
os.makedirs(dir_path, exist_ok=True)
178-
tensors = self.tensors
179-
h5_path = dir_path + "/tensors.h5"
180-
dat_path = dir_path + "/tensors.dat"
181-
tensors.dump(h5_path, dat_path)
182-
att_path = dir_path + "/attributes.dat"
183-
dct = {
184-
"mo_coeff": self.mo_coeff,
185-
"mo_energy": self.mo_energy,
186-
"D": self.D,
187-
"mo_occ": self.mo_occ,
188-
"mf_s_e_tot": self.mf_s.e_tot,
189-
}
190-
with open(att_path, "wb") as f:
191-
pickle.dump(dct, f)
192-
193-
def load_intermediates(self, dir_path="scratch", rerun_scf=False):
194-
h5_path = dir_path + "/tensors.h5"
195-
dat_path = dir_path + "/tensors.dat"
196-
self.tensors = HybridDict.pick(h5_path, dat_path)
197-
att_path = dir_path + "/attributes.dat"
198-
with open(att_path, "rb") as f:
199-
dct = pickle.load(f)
200-
self.mf_s.mo_coeff = dct["mo_coeff"]
201-
self.mf_s.mo_energy = dct["mo_energy"]
202-
self.mf_s.mo_occ = dct["mo_occ"]
203-
self.mf_s.e_tot = dct["mf_s_e_tot"]
204-
if rerun_scf:
205-
self.mf_s.kernel(dm=self.mf_s.make_rdm1())
206-
self.run_scf()
207185
return self
208186

209187

‎pyscf/dh/grad/dfdh.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ def prepare_gradient_gga(self):
407407
def prepare_gradient_pt2(self):
408408
tensors = self.tensors
409409
C, e = self.mo_coeff, self.mo_energy
410-
mol, aux_ri = self.mol, self.aux_ri
410+
mol, aux_ri = self.mol, self.df_ri.auxmol
411411
natm, nao, nmo, nocc, nvir, naux = mol.natm, self.nao, self.nmo, self.nocc, self.nvir, self.df_ri.get_naoaux()
412412
# this algorithm asserts naux = aux.nao, i.e. no linear dependency in auxiliary basis
413413
assert naux == aux_ri.nao

‎pyscf/dh/grad/udfdh.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def prepare_gradient_gga(self):
164164
def prepare_gradient_pt2(self):
165165
tensors = self.tensors
166166
C, e = self.mo_coeff, self.mo_energy
167-
mol, aux_ri = self.mol, self.aux_ri
167+
mol, aux_ri = self.mol, self.df_ri.auxmol
168168
natm, nao, nocc, nvir, naux = mol.natm, self.nao, self.nocc, self.nvir, self.df_ri.get_naoaux()
169169
mocc = max(nocc)
170170
# this algorithm asserts naux = aux.nao, i.e. no linear dependency in auxiliary basis

‎pyscf/dh/rdfdh.py‎

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,25 +68,15 @@ def __init__(self,
6868
self.grids = grids if grids else (getattr(mf_s, 'grids', dft.Grids(mol)))
6969
self.grids_cpks = grids_cpks if grids_cpks else self.grids
7070
self.mf_s = mf_s
71-
self.mf_s.grids = self.grids
7271
self.xc_n = None if xc_equal(self.xc_n, self.xc) else self.xc_n
7372
self.mf_n = self.mf_s
7473
if self.xc_n:
7574
self.mf_n = dft.KS(mol, xc=self.xc_n).density_fit(auxbasis=self.auxbasis_jk)
76-
self.mf_n.grids = self.mf_s.grids
77-
self.mf_n.grids = self.grids
78-
self.ni = getattr(self.mf_s, '_numint', dft.numint.NumInt())
79-
self.cx = self.ni.hybrid_coeff(self.xc)
80-
self.cx_n = self.ni.hybrid_coeff(self.xc_n)
81-
self.df_jk = mf_s.with_df
82-
self.aux_jk = self.df_jk.auxmol
83-
self.df_ri = df.DF(mol, self.auxbasis_ri) if not self.same_aux else self.df_jk
84-
self.aux_ri = self.df_ri.auxmol
75+
self.mf_n.grids = self.mf_s.grids = self.grids
76+
self._init_common()
8577
self.nocc = mol.nelec[0]
8678
self.nmo = self.nao
8779
self.nvir = self.nmo - self.nocc
88-
if self._scf is not None and self._scf.e_tot != 0:
89-
self.run_scf()
9080
if mp2_backend == "dfmp2_native":
9181
self.energy_elec_mp2 = partial(energy_elec_mp2_dfmp2_native, self)
9282
elif mp2_backend == "dfmp2":

‎pyscf/dh/udfdh.py‎

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,27 +71,16 @@ def __init__(self,
7171
self.grids = grids if grids else (getattr(mf_s, 'grids', dft.Grids(mol)))
7272
self.grids_cpks = grids_cpks if grids_cpks else self.grids
7373
self.mf_s = mf_s
74-
self.mf_s.grids = self.grids
7574
self.xc_n = None if xc_equal(self.xc_n, self.xc) else self.xc_n
7675
self.mf_n = self.mf_s
7776
if self.xc_n:
7877
self.mf_n = dft.UKS(mol, xc=self.xc_n).density_fit(auxbasis=self.auxbasis_jk)
79-
self.mf_n.grids = self.mf_s.grids
80-
self.mf_n.grids = self.grids
81-
self.ni = getattr(self.mf_s, '_numint', dft.numint.NumInt())
82-
self.cx = self.ni.hybrid_coeff(self.xc)
83-
self.cx_n = self.ni.hybrid_coeff(self.xc_n)
84-
self.df_jk = mf_s.with_df
85-
self.aux_jk = self.df_jk.auxmol
86-
self.df_ri = df.DF(mol, self.auxbasis_ri) if not self.same_aux else self.df_jk
87-
self.aux_ri = self.df_ri.auxmol
78+
self.mf_n.grids = self.mf_s.grids = self.grids
79+
self._init_common()
8880
self.nocc = mol.nelec
89-
self.mvir = NotImplemented
9081
self.mocc = max(max(self.nocc), 1)
9182
self.nmo = self.nao
9283
self.nvir = (self.nmo - self.nocc[α], self.nmo - self.nocc[β])
93-
if self._scf is not None and self._scf.e_tot != 0:
94-
self.run_scf()
9584
if mp2_backend == "dfmp2_native":
9685
self.energy_elec_mp2 = partial(energy_elec_mp2_dfump2_native, self)
9786
elif mp2_backend == "dfmp2":

0 commit comments

Comments
 (0)