From d3090b97b21e3be42b0900a9f9adb38229661ff4 Mon Sep 17 00:00:00 2001 From: Anyang Peng Date: Mon, 29 Jan 2024 14:12:35 +0800 Subject: [PATCH] fix: update unit tests --- deepmd/utils/pair_tab.py | 2 +- source/tests/test_pairtab_preprocess.py | 71 +++++++++++++++++++++++-- 2 files changed, 69 insertions(+), 4 deletions(-) diff --git a/deepmd/utils/pair_tab.py b/deepmd/utils/pair_tab.py index 9f7f07671d..719a9a9ed7 100644 --- a/deepmd/utils/pair_tab.py +++ b/deepmd/utils/pair_tab.py @@ -127,7 +127,7 @@ def _check_table_upper_boundary(self) -> None: self.vdata = np.concatenate((self.vdata, pad_zero), axis=0) else: # if table values do not decay to `0` at rcut - if self.rcut <= self.rmax: + if self.rcut < self.rmax: logging.warning( "The energy provided in the table does not decay to 0 at rcut." ) diff --git a/source/tests/test_pairtab_preprocess.py b/source/tests/test_pairtab_preprocess.py index e5ebd6d028..a500ae1c24 100644 --- a/source/tests/test_pairtab_preprocess.py +++ b/source/tests/test_pairtab_preprocess.py @@ -24,7 +24,7 @@ def setUp(self, mock_loadtxt) -> None: ] ) - self.tab1 = PairTab(filename=file_path, rcut=0.01) + self.tab1 = PairTab(filename=file_path, rcut=0.028) self.tab2 = PairTab(filename=file_path, rcut=0.02) self.tab3 = PairTab(filename=file_path, rcut=0.022) self.tab4 = PairTab(filename=file_path, rcut=0.03) @@ -38,6 +38,8 @@ def test_preprocess(self): [0.01, 0.8, 1.6, 2.4], [0.015, 0.5, 1.0, 1.5], [0.02, 0.25, 0.4, 0.75], + [0.025, 0.0, 0.0, 0.0], + [0.03, 0., 0., 0.], ] ), ) @@ -49,6 +51,7 @@ def test_preprocess(self): [0.01, 0.8, 1.6, 2.4], [0.015, 0.5, 1.0, 1.5], [0.02, 0.25, 0.4, 0.75], + [0.025, 0., 0., 0.], ] ), ) @@ -145,5 +148,67 @@ def test_preprocess(self): # for this test case, padding zeros between 0.025 and 0.03 will cause the cubic spline go below zero and result in negative energy values, # we will do post process to overwrite spline coefficient `a3`,`a2`,`a1`,`a0`, to ensure energy decays to `0`. - temp_data = self.tab3.tab_data.reshape(2, 2, -1, 4) - np.testing.assert_allclose(temp_data[:, :, -1, :], np.zeros((2, 2, 4))) + temp_data = self.tab3.tab_data.reshape(2,2,-1,4) + np.testing.assert_allclose(temp_data[:,:,-1,:], np.zeros((2,2,4))) + + +class TestPairTabPreprocessUneven(unittest.TestCase): + @patch("numpy.loadtxt") + def setUp(self, mock_loadtxt) -> None: + file_path = "dummy_path" + mock_loadtxt.return_value = np.array( + [ + [0.005, 1.0, 2.0, 3.0], + [0.01, 0.8, 1.6, 2.4], + [0.015, 0.5, 1.0, 1.5], + [0.02, 0.25, 0.4, 0.75], + [0.025, 0., 0.1, 0.], + ] + ) + + self.tab1 = PairTab(filename=file_path, rcut=0.025) + self.tab2 = PairTab(filename=file_path, rcut=0.028) + self.tab3 = PairTab(filename=file_path, rcut=0.03) + + + def test_preprocess(self): + + np.testing.assert_allclose(self.tab1.vdata, np.array( + [ + [0.005, 1.0, 2.0, 3.0], + [0.01, 0.8, 1.6, 2.4], + [0.015, 0.5, 1.0, 1.5], + [0.02, 0.25, 0.4, 0.75], + [0.025, 0., 0.1, 0.], + [0.03, 0., 0., 0.], + ] + )) + np.testing.assert_allclose(self.tab2.vdata, np.array( + [ + [0.005, 1.0, 2.0, 3.0], + [0.01, 0.8, 1.6, 2.4], + [0.015, 0.5, 1.0, 1.5], + [0.02, 0.25, 0.4, 0.75], + [0.025, 0., 0.1, 0.], + [0.03, 0., 0., 0.], + ] + )) + np.testing.assert_allclose(self.tab3.vdata, np.array( + [ + [0.005, 1.0, 2.0, 3.0], + [0.01, 0.8, 1.6, 2.4], + [0.015, 0.5, 1.0, 1.5], + [0.02, 0.25, 0.4, 0.75], + [0.025, 0., 0.1, 0.], + [0.03, 0., 0., 0.], + [0.035, 0., 0., 0.], + ] + )) + + temp_data = self.tab3.tab_data.reshape(2,2,-1,4) + + np.testing.assert_allclose(temp_data[0,0,-2:], np.zeros((2,4))) + np.testing.assert_allclose(temp_data[1,1,-2:], np.zeros((2,4))) + np.testing.assert_allclose(temp_data[0,1,-1:], np.zeros((1,4))) + np.testing.assert_allclose(temp_data[1,0,-1:], np.zeros((1,4))) + \ No newline at end of file