From b0171ce3e98d5c06521e5c63f54d0893dd54a5d4 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Tue, 5 Mar 2024 14:51:21 -0500 Subject: [PATCH] throw errros if rmin is no less than rmax (#3393) when rmin==rmax, the previous implementation of compute_smooth_weight will give all nan. In theory, it should not happen. --------- Signed-off-by: Jinzhe Zeng --- deepmd/dpmodel/utils/env_mat.py | 2 ++ deepmd/pt/utils/preprocess.py | 2 ++ source/tests/common/dpmodel/test_linear_atomic_model.py | 8 ++++---- source/tests/pt/model/test_linear_atomic_model.py | 4 ++-- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/deepmd/dpmodel/utils/env_mat.py b/deepmd/dpmodel/utils/env_mat.py index 0e861d9f38..5fb4ac4107 100644 --- a/deepmd/dpmodel/utils/env_mat.py +++ b/deepmd/dpmodel/utils/env_mat.py @@ -17,6 +17,8 @@ def compute_smooth_weight( rmax: float, ): """Compute smooth weight for descriptor elements.""" + if rmin >= rmax: + raise ValueError("rmin should be less than rmax.") min_mask = distance <= rmin max_mask = distance >= rmax mid_mask = np.logical_not(np.logical_or(min_mask, max_mask)) diff --git a/deepmd/pt/utils/preprocess.py b/deepmd/pt/utils/preprocess.py index 806bacdcd2..ed46292f84 100644 --- a/deepmd/pt/utils/preprocess.py +++ b/deepmd/pt/utils/preprocess.py @@ -228,6 +228,8 @@ def build_neighbor_list( def compute_smooth_weight(distance, rmin: float, rmax: float): """Compute smooth weight for descriptor elements.""" + if rmin >= rmax: + raise ValueError("rmin should be less than rmax.") min_mask = distance <= rmin max_mask = distance >= rmax mid_mask = torch.logical_not(torch.logical_or(min_mask, max_mask)) diff --git a/source/tests/common/dpmodel/test_linear_atomic_model.py b/source/tests/common/dpmodel/test_linear_atomic_model.py index aa56feb3e5..cc08a3b3dd 100644 --- a/source/tests/common/dpmodel/test_linear_atomic_model.py +++ b/source/tests/common/dpmodel/test_linear_atomic_model.py @@ -40,8 +40,8 @@ def test_pairwise(self, mock_loadtxt): nlist = np.array([[[1], [-1]]]) ds = DescrptSeA( - rcut=0.3, - rcut_smth=0.4, + rcut_smth=0.3, + rcut=0.4, sel=[3], ) ft = InvarFitting( @@ -122,8 +122,8 @@ def setUp(self, mock_loadtxt): ], dtype=int, ).reshape([1, self.nloc, sum(self.sel)]) - self.rcut = 0.4 - self.rcut_smth = 2.2 + self.rcut_smth = 0.4 + self.rcut = 2.2 file_path = "dummy_path" mock_loadtxt.return_value = np.array( diff --git a/source/tests/pt/model/test_linear_atomic_model.py b/source/tests/pt/model/test_linear_atomic_model.py index fab6481a6f..e0904097e3 100644 --- a/source/tests/pt/model/test_linear_atomic_model.py +++ b/source/tests/pt/model/test_linear_atomic_model.py @@ -56,8 +56,8 @@ def test_pairwise(self, mock_loadtxt): nlist = torch.tensor([[[1], [-1]]], device=env.DEVICE) ds = DescrptSeA( - rcut=0.3, - rcut_smth=0.4, + rcut_smth=0.3, + rcut=0.4, sel=[3], ).to(env.DEVICE) ft = InvarFitting(