Skip to content

Commit 65dc92b

Browse files
committed
fix dftb problems
1 parent 03c2111 commit 65dc92b

9 files changed

Lines changed: 66 additions & 18 deletions

File tree

‎environment-cpu.yml‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ dependencies:
3232
# Conda PySCF pulls XCFun with pybind11 ABI 4, conflicting with PyTorch >=2.9's ABI 11.
3333
# PyPI wheels bundle XCFun and avoid that solver conflict.
3434
- pyscf >=2.11,<2.15
35-
# Use its bundled DFT-D3 library instead of installing a second, incompatible copy.
36-
- pyscf-dispersion
35+
# Linux GPU4PySCF requires this backend; use standalone DFT-D3 elsewhere because
36+
# current pyscf-dispersion releases do not provide macOS wheels.
37+
- pyscf-dispersion >=1.5; sys_platform == 'linux'
38+
- dftd3 >=1.2.1,<1.3; sys_platform == 'darwin'
39+
- dftd3 >=1.3; sys_platform != 'linux' and sys_platform != 'darwin'
3740
- scipy-stubs
3841
- types-PyYAML

‎environment-gpu.yml‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ dependencies:
3535
# Conda PySCF pulls XCFun with pybind11 ABI 4, conflicting with PyTorch >=2.9's ABI 11.
3636
# PyPI wheels bundle XCFun and avoid that solver conflict.
3737
- pyscf >=2.11,<2.15
38-
# Use its bundled DFT-D3 library instead of installing a second, incompatible copy.
39-
- pyscf-dispersion
38+
# GPU4PySCF requires this backend; do not install standalone DFT-D3 on Linux,
39+
# because loading both native libraries in one process causes symbol collisions.
40+
- pyscf-dispersion >=1.5; sys_platform == 'linux'
4041
- scipy-stubs
4142
- types-PyYAML
4243
- gpu4pyscf-cuda12x >=1.8,<1.9 # GPU-accelerated PySCF backend used by Skala

‎pyproject.toml‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ dependencies = [
2525
"numpy",
2626
"opt_einsum_fx",
2727
"pyscf>=2.11,<2.15",
28-
"pyscf-dispersion",
28+
"pyscf-dispersion>=1.5; sys_platform == 'linux'",
29+
"dftd3>=1.2.1,<1.3; sys_platform == 'darwin'",
30+
"dftd3>=1.3; sys_platform != 'linux' and sys_platform != 'darwin'",
2931
"torch>=2.9"
3032
]
3133

‎src/skala/dispersion.py‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""Cross-platform DFT-D3 backend compatibility."""
2+
3+
import sys
4+
from importlib import import_module
5+
from typing import Any
6+
7+
import numpy as np
8+
from pyscf import gto
9+
10+
_USE_PYSCF_DISPERSION = sys.platform == "linux"
11+
_backend_module = import_module(
12+
"pyscf.dispersion.dftd3" if _USE_PYSCF_DISPERSION else "dftd3.pyscf"
13+
)
14+
15+
16+
class DFTD3Dispersion:
17+
"""Normalize the DFT-D3 APIs available on supported platforms."""
18+
19+
def __init__(self, mol: gto.Mole, xc: str):
20+
self._xc = xc
21+
self._backend: Any = _backend_module.DFTD3Dispersion(mol, xc)
22+
23+
def get_energy(self) -> float:
24+
"""Return the dispersion energy as a Python scalar."""
25+
if _USE_PYSCF_DISPERSION:
26+
energy = self._backend.get_dispersion()["energy"]
27+
else:
28+
energy = self._backend.kernel()[0]
29+
return float(np.asarray(energy).item())
30+
31+
def get_gradient(self) -> np.ndarray:
32+
"""Return the dispersion nuclear gradient."""
33+
if _USE_PYSCF_DISPERSION:
34+
gradient = self._backend.get_dispersion(grad=True)["gradient"]
35+
else:
36+
gradient = self._backend.kernel()[1]
37+
return np.asarray(gradient)
38+
39+
def reset(self, mol: gto.Mole) -> "DFTD3Dispersion":
40+
"""Reset the backend for a new molecular geometry."""
41+
self._backend = _backend_module.DFTD3Dispersion(mol, self._xc)
42+
return self

‎src/skala/gpu4pyscf/dft.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@
6060
from gpu4pyscf import dft
6161
from gpu4pyscf.df import df_jk
6262
from pyscf import gto
63-
from pyscf.dispersion.dftd3 import DFTD3Dispersion
6463

64+
from skala.dispersion import DFTD3Dispersion
6565
from skala.functional.base import ExcFunctionalBase
6666
from skala.gpu4pyscf.gradients import SkalaRKSGradient, SkalaUKSGradient
6767
from skala.gpu4pyscf.grids import SkalaGrids
@@ -103,7 +103,7 @@ def __init__(
103103
def energy_nuc(self) -> float:
104104
enuc = float(super().energy_nuc())
105105
if self.with_dftd3:
106-
edisp = self.with_dftd3.get_dispersion()["energy"].item()
106+
edisp = self.with_dftd3.get_energy()
107107
self.scf_summary["dispersion"] = edisp
108108
enuc += edisp
109109
return enuc
@@ -185,7 +185,7 @@ def __init__(
185185
def energy_nuc(self) -> float:
186186
enuc = float(super().energy_nuc())
187187
if self.with_dftd3:
188-
edisp = self.with_dftd3.get_dispersion()["energy"].item()
188+
edisp = self.with_dftd3.get_energy()
189189
self.scf_summary["dispersion"] = edisp
190190
enuc += edisp
191191
return enuc

‎src/skala/gpu4pyscf/gradients.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
from gpu4pyscf.grad.uhf import Gradients as UHFGradient
1515
from gpu4pyscf.scf.hf import SCF
1616
from pyscf import gto
17-
from pyscf.dispersion.dftd3 import DFTD3Dispersion
1817
from torch.utils.dlpack import from_dlpack
1918

2019
import skala.pyscf.features as feature
20+
from skala.dispersion import DFTD3Dispersion
2121
from skala.features import Feature, FeatureMap
2222
from skala.functional.base import ExcFunctionalBase
2323

@@ -352,7 +352,7 @@ def grad_nuc(
352352
nuc_g = super().grad_nuc(mol, atmlst)
353353
if self.with_dftd3 is None:
354354
return nuc_g
355-
disp_g = self.with_dftd3.get_dispersion(grad=True)["gradient"]
355+
disp_g = self.with_dftd3.get_gradient()
356356
if atmlst is not None:
357357
disp_g = disp_g[atmlst]
358358
nuc_g += disp_g
@@ -452,7 +452,7 @@ def grad_nuc(
452452
nuc_g = super().grad_nuc(mol, atmlst)
453453
if self.with_dftd3 is None:
454454
return nuc_g
455-
disp_g = self.with_dftd3.get_dispersion(grad=True)["gradient"]
455+
disp_g = self.with_dftd3.get_gradient()
456456
if atmlst is not None:
457457
disp_g = disp_g[atmlst]
458458
nuc_g += disp_g

‎src/skala/pyscf/dft.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@
5858
import torch
5959
from pyscf import dft, gto
6060
from pyscf.df import df_jk
61-
from pyscf.dispersion.dftd3 import DFTD3Dispersion
6261

62+
from skala.dispersion import DFTD3Dispersion
6363
from skala.functional.base import ExcFunctionalBase
6464
from skala.pyscf.gradients import SkalaRKSGradient, SkalaUKSGradient
6565
from skala.pyscf.grids import SkalaGrids
@@ -112,7 +112,7 @@ def initialize_grids(
112112
def energy_nuc(self) -> float:
113113
enuc = float(super().energy_nuc())
114114
if self.with_dftd3:
115-
edisp = self.with_dftd3.get_dispersion()["energy"].item()
115+
edisp = self.with_dftd3.get_energy()
116116
self.scf_summary["dispersion"] = edisp
117117
enuc += edisp
118118
return enuc
@@ -202,7 +202,7 @@ def initialize_grids(
202202
def energy_nuc(self) -> float:
203203
enuc = float(super().energy_nuc())
204204
if self.with_dftd3:
205-
edisp = self.with_dftd3.get_dispersion()["energy"].item()
205+
edisp = self.with_dftd3.get_energy()
206206
self.scf_summary["dispersion"] = edisp
207207
enuc += edisp
208208
return enuc

‎src/skala/pyscf/gradients.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
import numpy as np
99
import torch
1010
from pyscf import dft, gto
11-
from pyscf.dispersion.dftd3 import DFTD3Dispersion
1211
from pyscf.grad.rhf import Gradients as RHFGradient
1312
from pyscf.grad.rks import grids_noresponse_cc, grids_response_cc
1413
from pyscf.grad.uks import Gradients as UHFGradient
1514
from pyscf.scf.hf import SCF
1615

1716
import skala.pyscf.features as feature
17+
from skala.dispersion import DFTD3Dispersion
1818
from skala.features import Feature, FeatureMap
1919
from skala.functional.base import ExcFunctionalBase
2020

@@ -300,7 +300,7 @@ def grad_nuc(
300300
nuc_g = super().grad_nuc(mol, atmlst)
301301
if self.with_dftd3 is None:
302302
return nuc_g
303-
disp_g = self.with_dftd3.get_dispersion(grad=True)["gradient"]
303+
disp_g = self.with_dftd3.get_gradient()
304304
if atmlst is not None:
305305
disp_g = disp_g[atmlst]
306306
nuc_g += disp_g
@@ -378,7 +378,7 @@ def grad_nuc(
378378
nuc_g = super().grad_nuc(mol, atmlst)
379379
if self.with_dftd3 is None:
380380
return nuc_g
381-
disp_g = self.with_dftd3.get_dispersion(grad=True)["gradient"]
381+
disp_g = self.with_dftd3.get_gradient()
382382
if atmlst is not None:
383383
disp_g = disp_g[atmlst]
384384
nuc_g += disp_g

‎tests/test_ao_screening_benchmark.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ def test_screened_and_dense_values_agree(
380380
vxc_relative_l2_difference = np.linalg.norm(vxc_difference) / np.linalg.norm(
381381
dense_vxc
382382
)
383-
vxc_max_atol = 5e-8 if case.backend == "cpu" else 2e-7
383+
vxc_max_atol = 1e-7 if case.backend == "cpu" else 2e-7
384384
vxc_relative_rtol = 1e-8 if case.backend == "cpu" else 1e-7
385385
assert (
386386
density_close

0 commit comments

Comments
 (0)