Skip to content

Commit 5986e82

Browse files
committed
use pyscf convention for nuc_grad_method
1 parent 8d1b621 commit 5986e82

7 files changed

Lines changed: 21 additions & 33 deletions

File tree

‎examples/dh/03-relaxed-dm.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@
4040
# 3. Relaxed DM (AO basis)
4141
rdm1_ao = mf_g.make_rdm1_relaxed(ao_repr=True)
4242

43-
# 4. .dipole() method uses relaxed DM internally
44-
dip = mf.dipole()
43+
# 4. .dipole() method on Gradients uses relaxed DM internally
44+
dip = mf_g.dipole()
4545
print(f"Dipole (via .dipole): {np.linalg.norm(dip):.6f} a.u.")

‎pyscf/dh/grad/dfdh.py‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -343,10 +343,9 @@ def _add_dispersion_gradient(self, grad_contrib):
343343

344344
class Gradients(RDFDH, RDHRespMixin, GradientMixin):
345345

346-
def __init__(self, mol: gto.Mole, *args, skip_construct=False, **kwargs):
347-
if not skip_construct:
348-
super(Gradients, self).__init__(mol, *args, **kwargs)
349-
# results
346+
def __init__(self, method):
347+
self.__dict__.update(method.__dict__)
348+
self._base = method
350349
self.grad_jk = NotImplemented
351350
self.grad_gga = NotImplemented
352351
self.grad_pt2 = NotImplemented

‎pyscf/dh/grad/udfdh.py‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from pyscf.dh.dhutil import calc_batch_size, gen_batch, gen_shl_batch, tot_size, timing, as_scanner_grad
2626
from pyscf.dh.grad.dfdh import get_H_1_ao, get_S_1_ao, generator_L_1, kernel, GradientMixin
2727
# pyscf import
28-
from pyscf import gto, lib, df
28+
from pyscf import lib, df
2929
from pyscf.df.grad.rhf import _int3c_wrapper as int3c_wrapper
3030
# other import
3131
import numpy as np
@@ -118,10 +118,9 @@ def get_gradient_jk(dfobj: df.DF, C, D, D_r, Y_mo, cx, cx_n, max_memory=2000):
118118

119119
class Gradients(UDFDH, UDHRespMixin, GradientMixin):
120120

121-
def __init__(self, mol: gto.Mole, *args, skip_construct=False, **kwargs):
122-
if not skip_construct:
123-
super(Gradients, self).__init__(mol, *args, **kwargs)
124-
# results
121+
def __init__(self, method):
122+
self.__dict__.update(method.__dict__)
123+
self._base = method
125124
self.grad_jk = NotImplemented
126125
self.grad_gga = NotImplemented
127126
self.grad_pt2 = NotImplemented

‎pyscf/dh/polar/rdfdh.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from pyscf.dh.resp import RDHRespMixin
2525
from pyscf.dh.dhutil import gen_batch, get_rho_from_dm_gga, restricted_biorthogonalize, hermi_sum_last2dim
2626
from pyscf.dh.xccode import xc_equal
27-
from pyscf import gto, lib
27+
from pyscf import lib
2828
import numpy as np
2929

3030
einsum = lib.einsum
@@ -92,9 +92,9 @@ def _rks_gga_wv2(rho0, rho1, rho2, fxc, kxc, weight):
9292

9393
class Polar(RDFDH, RDHRespMixin):
9494

95-
def __init__(self, mol: gto.Mole, *args, skip_construct=False, **kwargs):
96-
if not skip_construct:
97-
super(Polar, self).__init__(mol, *args, **kwargs)
95+
def __init__(self, method):
96+
self.__dict__.update(method.__dict__)
97+
self._base = method
9898
self.pol_scf = NotImplemented
9999
self.pol_corr = NotImplemented
100100
self.pol_tot = NotImplemented

‎pyscf/dh/polar/udfdh.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from pyscf.dh.dhutil import gen_batch, get_rho_from_dm_gga, tot_size, hermi_sum_last2dim
2727
from pyscf.dh.xccode import xc_equal
2828
# pyscf import
29-
from pyscf import gto, lib, dft
29+
from pyscf import lib, dft
3030
from pyscf.dft.numint import _scale_ao
3131
from pyscf.lib.numpy_helper import ANTIHERMI
3232
# other import
@@ -138,9 +138,9 @@ def nprop(self):
138138
self.prepare_H_1()
139139
return self.tensors["H_1_ao"].shape[0]
140140

141-
def __init__(self, mol: gto.Mole, *args, skip_construct=False, **kwargs):
142-
if not skip_construct:
143-
super(Polar, self).__init__(mol, *args, **kwargs)
141+
def __init__(self, method):
142+
self.__dict__.update(method.__dict__)
143+
self._base = method
144144
self.pol_scf = NotImplemented
145145
self.pol_corr = NotImplemented
146146
self.pol_tot = NotImplemented

‎pyscf/dh/rdfdh.py‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,15 +172,11 @@ def build(sI, t_ijab, g_ijab):
172172

173173
def nuc_grad_method(self):
174174
from pyscf.dh.grad.dfdh import Gradients
175-
self.__class__ = Gradients
176-
Gradients.__init__(self, self.mol, skip_construct=True)
177-
return self
175+
return Gradients(self)
178176

179177
def polar_method(self):
180178
from pyscf.dh.polar.rdfdh import Polar
181-
self.__class__ = Polar
182-
Polar.__init__(self, self.mol, skip_construct=True)
183-
return self
179+
return Polar(self)
184180

185181
energy_elec_pt2 = energy_elec_pt2
186182
energy_elec_mp2 = energy_elec_mp2_ajz

‎pyscf/dh/udfdh.py‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -219,19 +219,13 @@ def prepare_pt2(self, dump_t_ijab=True, fast_trans=True):
219219

220220
return self
221221

222-
@timing
223-
# A REALLY DIRTY WAY transform to son class https://stackoverflow.com/questions/7078134/
224222
def nuc_grad_method(self):
225223
from pyscf.dh.grad.udfdh import Gradients
226-
self.__class__ = Gradients
227-
Gradients.__init__(self, self.mol, skip_construct=True)
228-
return self
224+
return Gradients(self)
229225

230226
def polar_method(self):
231227
from pyscf.dh.polar.udfdh import Polar
232-
self.__class__ = Polar
233-
Polar.__init__(self, self.mol, skip_construct=True)
234-
return self
228+
return Polar(self)
235229

236230
energy_elec_pt2 = energy_elec_pt2
237231
energy_elec_mp2 = energy_elec_ump2_ajz

0 commit comments

Comments
 (0)