Skip to content

Commit 4dcfc55

Browse files
committed
API update to allow pyscf.dh as pyscf extension
1 parent 07d23f6 commit 4dcfc55

16 files changed

+142
-91
lines changed

.idea/dh.iml

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/00-dh-energy.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
# for development, import dh
2+
# for general user, import pyscf.dh
3+
14
from pyscf import gto, dft, mp
2-
from dh import RDFDH
5+
from dh import DFDH
36

47

58
def get_energy_xDH(mol: gto.Mole, xc_scf: str, c_pt2: float, xc_dh: str or None=None):
@@ -22,19 +25,19 @@ def get_energy_xDH(mol: gto.Mole, xc_scf: str, c_pt2: float, xc_dh: str or None=
2225
# PySCF -76.2910535257
2326
# RDFDH -76.2910470614
2427
print(get_energy_xDH(mol, "B3LYPg", 0.3211, "0.8033*HF - 0.0140*LDA + 0.2107*B88, 0.6789*LYP"))
25-
print(RDFDH(mol, xc="XYG3").run().e_tot)
28+
print(DFDH(mol, xc="XYG3").run().e_tot)
2629
# B2PLYP QChem -76.29075997
2730
# PySCF -76.29075969
2831
# RDFDH -76.29076324
2932
print(get_energy_xDH(mol, "0.53*HF + 0.47*B88, 0.73*LYP", 0.27))
30-
print(RDFDH(mol, xc="B2PLYP").run().e_tot)
33+
print(DFDH(mol, xc="B2PLYP").run().e_tot)
3134
# XYGJ-OS QChem -76.1460262831
3235
# RDFDH -76.1460317123
33-
print(RDFDH(mol, xc="XYGJ-OS").run().e_tot)
36+
print(DFDH(mol, xc="XYGJ-OS").run().e_tot)
3437
# MP2 PySCF -76.1108060780191
3538
# RDFDH -76.1107838767765
3639
print(mp.MP2(mol).run().e_tot)
37-
print(RDFDH(mol, xc="MP2").run().e_tot)
40+
print(DFDH(mol, xc="MP2").run().e_tot)
3841
# Details: QChem uses 99, 590 grid; i.e. XC_GRID 000099000590
3942
# no density fitting used in QChem for XYG3, B2PLYP, MP2
4043
# LT-SOS-RI-MP2 for XYGJ-OS

examples/01-dh-grad.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
# for development, import dh
2+
# for general user, import pyscf.dh
3+
14
from pyscf import gto
2-
from dh import RDFDH
5+
from dh import DFDH
36
import numpy as np
47

58
np.set_printoptions(5, suppress=True, linewidth=180)
@@ -11,25 +14,25 @@
1114
# [-0.16752 0.02649 0.02154]
1215
# [ 0.02384 -0.03202 0.01631]
1316
# [ 0.01762 0.01483 0.04038]]
14-
print(RDFDH(mol, xc="XYG3").run().e_tot)
15-
mf = RDFDH(mol, xc="XYG3").nuc_grad_method().run()
17+
print(DFDH(mol, xc="XYG3").run().e_tot)
18+
mf = DFDH(mol, xc="XYG3").nuc_grad_method().run()
1619
print(mf.e_tot)
1720
print(mf.de)
1821
# B2PLYP
1922
# [[ 0.13023 -0.00424 -0.07277]
2023
# [-0.17088 0.02598 0.0211 ]
2124
# [ 0.02338 -0.03631 0.01602]
2225
# [ 0.01726 0.01457 0.03565]]
23-
print(RDFDH(mol, xc="B2PLYP").run().e_tot)
24-
mf = RDFDH(mol, xc="B2PLYP").nuc_grad_method().run()
26+
print(DFDH(mol, xc="B2PLYP").run().e_tot)
27+
mf = DFDH(mol, xc="B2PLYP").nuc_grad_method().run()
2528
print(mf.e_tot)
2629
print(mf.de)
2730
# MP2
2831
# [[ 0.1345 -0.00321 -0.07428]
2932
# [-0.17439 0.02557 0.02063]
3033
# [ 0.02301 -0.03636 0.01541]
3134
# [ 0.01688 0.01401 0.03824]]
32-
print(RDFDH(mol, xc="MP2").run().e_tot)
33-
mf = RDFDH(mol, xc="MP2").nuc_grad_method().run()
35+
print(DFDH(mol, xc="MP2").run().e_tot)
36+
mf = DFDH(mol, xc="MP2").nuc_grad_method().run()
3437
print(mf.e_tot)
3538
print(mf.de)

examples/02-dh-polar.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
# for development, import dh
2+
# for general user, import pyscf.dh
3+
14
from pyscf import gto
2-
from dh import RDFDH
5+
from dh import DFDH
36
import numpy as np
47

58
np.set_printoptions(5, suppress=True, linewidth=180)
@@ -12,22 +15,22 @@
1215
# [[ 7.26795 -0.15045 -0.21885]
1316
# [-0.15045 8.75195 -0.367 ]
1417
# [-0.21885 -0.367 10.47337]]
15-
mf = RDFDH(mol, xc="XYG3").polar_method().run()
18+
mf = DFDH(mol, xc="XYG3").polar_method().run()
1619
print(mf.dipole())
1720
print(mf.pol_tot)
1821
# B2PLYP
1922
# [0.50747 0.48579 0.45491]
2023
# [[ 7.38219 -0.15075 -0.21973]
2124
# [-0.15075 8.86653 -0.36795]
2225
# [-0.21973 -0.36795 10.5606 ]]
23-
mf = RDFDH(mol, xc="B2PLYP").polar_method().run()
26+
mf = DFDH(mol, xc="B2PLYP").polar_method().run()
2427
print(mf.dipole())
2528
print(mf.pol_tot)
2629
# MP2
2730
# [0.51397 0.49493 0.46905]
2831
# [[ 7.28733 -0.14367 -0.2035 ]
2932
# [-0.14367 8.72405 -0.33421]
3033
# [-0.2035 -0.33421 10.34546]]
31-
mf = RDFDH(mol, xc="MP2").polar_method().run()
34+
mf = DFDH(mol, xc="MP2").polar_method().run()
3235
print(mf.dipole())
3336
print(mf.pol_tot)

examples/04-opt.py

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# for development, import dh
2+
# for general user, import pyscf.dh
3+
14
from pyscf import gto, lib
25
from dh import DFDH
36
from pyscf.geomopt.geometric_solver import optimize

pyscf/dh/__init__.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
from dh.rdfdh import RDFDH
2-
from dh.udfdh import UDFDH
1+
from . import rdfdh, udfdh, dhutil
32
from pyscf import gto
43

54

65
def DFDH(mol: gto.Mole, *args, **kwargs):
76
if mol.spin != 0:
8-
return UDFDH(mol, *args, **kwargs)
7+
return udfdh.UDFDH(mol, *args, **kwargs)
98
else:
10-
return RDFDH(mol, *args, **kwargs)
9+
return rdfdh.RDFDH(mol, *args, **kwargs)
1110

1211

pyscf/dh/dhutil.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ def restricted_biorthogonalize(t_ijab, cc, c_os, c_ss):
226226
def hermi_sum_last2dim(tsr, inplace=True, hermi=HERMITIAN):
227227
# shameless call lib.hermi_sum, just for a tensor wrapper
228228
tsr_shape = tsr.shape
229-
tsr.shape = (-1, tsr.shape[-1], tsr.shape[-2])
229+
tsr = tsr.reshape(-1, tsr.shape[-2], tsr.shape[-1])
230230
res = lib.hermi_sum(tsr, axes=(0, 2, 1), hermi=hermi, inplace=inplace)
231231
tsr.shape = tsr_shape
232232
res.shape = tsr_shape

pyscf/dh/grad/rdfdh.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
from __future__ import annotations
2-
3-
from pyscf.dft.numint import _dot_ao_dm, _contract_rho
4-
5-
from dh import RDFDH
6-
from dh.dhutil import calc_batch_size, gen_batch, gen_shl_batch, timing, as_scanner_grad
2+
# dh import
3+
try:
4+
from dh.rdfdh import RDFDH
5+
from dh.dhutil import calc_batch_size, gen_batch, gen_shl_batch, timing, as_scanner_grad
6+
except ImportError:
7+
from pyscf.dh.rdfdh import RDFDH
8+
from pyscf.dh.dhutil import calc_batch_size, gen_batch, gen_shl_batch, timing, as_scanner_grad
9+
# pyscf import
710
from pyscf import gto, lib, df
11+
from pyscf.dft.numint import _dot_ao_dm, _contract_rho
812
from pyscf.df.grad.rhf import _int3c_wrapper as int3c_wrapper
13+
# other import
914
import numpy as np
1015

1116
einsum = lib.einsum

pyscf/dh/grad/udfdh.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
from __future__ import annotations
2-
3-
from dh import UDFDH
4-
from dh.dhutil import calc_batch_size, gen_batch, gen_shl_batch, tot_size, timing
5-
from dh.grad.rdfdh import contract_multiple_rho, get_H_1_ao, get_S_1_ao, generator_L_1
6-
import dh.grad.rdfdh
2+
# dh import
3+
try:
4+
from dh.udfdh import UDFDH
5+
from dh.dhutil import calc_batch_size, gen_batch, gen_shl_batch, tot_size, timing
6+
from dh.grad.rdfdh import get_H_1_ao, get_S_1_ao, generator_L_1
7+
from dh.grad.rdfdh import Gradients as RGradients
8+
except ImportError:
9+
from pyscf.dh.udfdh import UDFDH
10+
from pyscf.dh.dhutil import calc_batch_size, gen_batch, gen_shl_batch, tot_size, timing
11+
from pyscf.dh.grad.rdfdh import get_H_1_ao, get_S_1_ao, generator_L_1
12+
from pyscf.dh.grad.rdfdh import Gradients as RGradients
13+
# pyscf import
714
from pyscf import gto, lib, df
815
from pyscf.df.grad.rhf import _int3c_wrapper as int3c_wrapper
16+
# other import
917
import numpy as np
1018
import itertools
1119

@@ -93,7 +101,7 @@ def get_gradient_jk(dfobj: df.DF, C, D, D_r, Y_mo, cx, cx_n, max_memory=2000):
93101
return grad_contrib
94102

95103

96-
class Gradients(UDFDH, dh.grad.rdfdh.Gradients):
104+
class Gradients(UDFDH, RGradients):
97105

98106
def __init__(self, mol: gto.Mole, *args, skip_construct=False, **kwargs):
99107
if not skip_construct:

pyscf/dh/polar/rdfdh.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
from __future__ import annotations
2-
3-
from dh import RDFDH
4-
from dh.dhutil import gen_batch, get_rho_from_dm_gga, restricted_biorthogonalize, hermi_sum_last2dim
2+
# dh import
3+
try:
4+
from dh.rdfdh import RDFDH
5+
from dh.dhutil import gen_batch, get_rho_from_dm_gga, restricted_biorthogonalize, hermi_sum_last2dim
6+
except ImportError:
7+
from pyscf.dh.rdfdh import RDFDH
8+
from pyscf.dh.dhutil import gen_batch, get_rho_from_dm_gga, restricted_biorthogonalize, hermi_sum_last2dim
59
from pyscf import gto, lib, dft
610
import numpy as np
711

pyscf/dh/polar/udfdh.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
from __future__ import annotations
2-
2+
# dh import
3+
try:
4+
from dh.udfdh import UDFDH
5+
from dh.polar.rdfdh import Polar as RPolar
6+
from dh.dhutil import gen_batch, get_rho_from_dm_gga, tot_size, hermi_sum_last2dim
7+
except ImportError:
8+
from pyscf.dh.udfdh import UDFDH
9+
from pyscf.dh.polar.rdfdh import Polar as RPolar
10+
from pyscf.dh.dhutil import gen_batch, get_rho_from_dm_gga, tot_size, hermi_sum_last2dim
11+
# pyscf import
12+
from pyscf import gto, lib, dft
313
from pyscf.dft.numint import _scale_ao
414
from pyscf.lib.numpy_helper import ANTIHERMI
5-
6-
from dh import UDFDH
7-
import dh.polar.rdfdh
8-
from dh.dhutil import gen_batch, get_rho_from_dm_gga, tot_size, hermi_sum_last2dim
9-
from pyscf import gto, lib, dft
15+
# other import
1016
import numpy as np
1117
import itertools
1218

@@ -107,7 +113,7 @@ def _uks_gga_wv2_inner(rho0, rho1, rho2):
107113
return _uks_gga_wv2_inner
108114

109115

110-
class Polar(UDFDH, dh.polar.rdfdh.Polar):
116+
class Polar(UDFDH, RPolar):
111117

112118
def __init__(self, mol: gto.Mole, *args, skip_construct=False, **kwargs):
113119
if not skip_construct:

pyscf/dh/rdfdh.py

+18-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
from __future__ import annotations
2-
3-
import os
4-
import pickle
2+
# dh import
3+
try:
4+
from dh.dhutil import parse_xc_dh, gen_batch, calc_batch_size, HybridDict, timing, restricted_biorthogonalize
5+
except ImportError:
6+
from pyscf.dh.dhutil import parse_xc_dh, gen_batch, calc_batch_size, HybridDict, timing, restricted_biorthogonalize
7+
# typing import
58
from typing import Tuple, TYPE_CHECKING
69
if TYPE_CHECKING:
710
from dh.grad.rdfdh import Gradients
811
from dh.polar.rdfdh import Polar
9-
12+
# pyscf import
1013
from pyscf.scf import cphf
11-
12-
from dh.dhutil import parse_xc_dh, gen_batch, calc_batch_size, HybridDict, timing, restricted_biorthogonalize
1314
from pyscf import lib, gto, df, dft, scf
1415
from pyscf.ao2mo import _ao2mo
1516
from pyscf.scf._response_functions import _gen_rhf_response
17+
# other import
18+
import os
19+
import pickle
1620
import numpy as np
1721

1822
einsum = lib.einsum
@@ -630,18 +634,23 @@ def load_intermediates(self, dir_path="scratch", rerun_scf=False):
630634
# to avoid cyclic imports in typing https://stackoverflow.com/questions/39740632/
631635

632636
def nuc_grad_method(self) -> Gradients:
633-
from dh.grad.rdfdh import Gradients
637+
try:
638+
from dh.grad.rdfdh import Gradients
639+
except ImportError:
640+
from pyscf.dh.grad.rdfdh import Gradients
634641
self.__class__ = Gradients
635642
Gradients.__init__(self, self.mol, skip_construct=True)
636643
return self
637644

638645
def polar_method(self) -> Polar:
639-
from dh.polar.rdfdh import Polar
646+
try:
647+
from dh.polar.rdfdh import Polar
648+
except ImportError:
649+
from pyscf.dh.polar.rdfdh import Polar
640650
self.__class__ = Polar
641651
Polar.__init__(self, self.mol, skip_construct=True)
642652
return self
643653

644-
645654
# endregion first derivative related in class
646655

647656
energy_elec_nc = energy_elec_nc

pyscf/dh/udfdh.py

+20-13
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
from __future__ import annotations
2-
2+
# dh import
3+
try:
4+
from dh.dhutil import gen_batch, calc_batch_size, timing, tot_size, hermi_sum_last2dim
5+
from dh.rdfdh import get_cderi_mo, kernel, RDFDH
6+
except ImportError:
7+
from pyscf.dh.dhutil import gen_batch, calc_batch_size, timing, tot_size, hermi_sum_last2dim
8+
from pyscf.dh.rdfdh import get_cderi_mo, kernel, RDFDH
9+
# typing import
310
from typing import Tuple, TYPE_CHECKING
4-
5-
from pyscf.lib.numpy_helper import ANTIHERMI
6-
711
if TYPE_CHECKING:
812
from dh.grad.udfdh import Gradients
913
from dh.polar.udfdh import Polar
10-
14+
# pyscf import
15+
from pyscf import lib, gto, df, dft
1116
from pyscf.scf import ucphf
17+
from pyscf.lib.numpy_helper import ANTIHERMI
18+
# other import
1219
import h5py
13-
from dh import RDFDH
14-
from dh.dhutil import gen_batch, calc_batch_size, timing, tot_size, hermi_sum_last2dim
15-
from pyscf import lib, gto, df, dft
1620
import numpy as np
1721

18-
from dh.rdfdh import get_cderi_mo, kernel
19-
2022
einsum = lib.einsum
2123
α, β = 0, 1
2224
αα, αβ, ββ = 0, 1, 2
23-
2425
ndarray = np.ndarray or h5py.Dataset
2526

2627

@@ -485,13 +486,19 @@ def dipole(self):
485486
# to avoid cyclic imports in typing https://stackoverflow.com/questions/39740632/
486487

487488
def nuc_grad_method(self) -> Gradients:
488-
from dh.grad.udfdh import Gradients
489+
try:
490+
from dh.grad.udfdh import Gradients
491+
except ImportError:
492+
from pyscf.dh.grad.udfdh import Gradients
489493
self.__class__ = Gradients
490494
Gradients.__init__(self, self.mol, skip_construct=True)
491495
return self
492496

493497
def polar_method(self) -> Polar:
494-
from dh.polar.udfdh import Polar
498+
try:
499+
from dh.polar.udfdh import Polar
500+
except ImportError:
501+
from pyscf.dh.polar.udfdh import Polar
495502
self.__class__ = Polar
496503
Polar.__init__(self, self.mol, skip_construct=True)
497504
return self

0 commit comments

Comments
 (0)