Skip to content
Open
9 changes: 9 additions & 0 deletions deepmd/utils/pair_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ def reinit(self, filename: str, rcut: float | None = None) -> None:
self.rmin = self.vdata[0][0]
self.rmax = self.vdata[-1][0]
self.hh = self.vdata[1][0] - self.vdata[0][0]
dx = np.diff(self.vdata[:, 0])
if not np.allclose(dx, self.hh, rtol=1e-5, atol=1e-8):
raise ValueError(
f"The distance grid in the pairwise table {filename} is not "
"evenly spaced. The tabulated potential must be provided on a "
"uniform grid, but the stride inferred from the first two rows "
f"({self.hh}) does not match all distance intervals. Please "
"regrid the table to use a constant distance step."
)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
ncol = self.vdata.shape[1] - 1
n0 = (-1 + np.sqrt(1 + 8 * ncol)) * 0.5
self.ntypes = int(n0 + 0.1)
Expand Down
30 changes: 30 additions & 0 deletions source/tests/common/dpmodel/test_pairtab_preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,5 +275,35 @@ def test_preprocess(self) -> None:
)


class TestPairTabGridSpacing(unittest.TestCase):
@patch("numpy.loadtxt")
def test_non_uniform_grid(self, mock_loadtxt) -> None:
mock_loadtxt.return_value = np.array(
[
[0.00, 1.0],
[0.01, 0.8],
[0.02, 0.6],
[0.09, 0.3],
[0.16, 0.0],
]
)
with self.assertRaisesRegex(ValueError, "evenly spaced"):
PairTab(filename="dummy_path", rcut=0.16)

@patch("numpy.loadtxt")
def test_uniform_grid(self, mock_loadtxt) -> None:
mock_loadtxt.return_value = np.array(
[
[0.00, 1.0],
[0.01, 0.8],
[0.02, 0.6],
[0.03, 0.3],
[0.04, 0.0],
]
)
tab = PairTab(filename="dummy_path", rcut=0.04)
np.testing.assert_allclose(tab.hh, 0.01)


if __name__ == "__main__":
unittest.main(warnings="ignore")