Skip to content

Commit

Permalink
fix: update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Anyang Peng authored and Anyang Peng committed Jan 29, 2024
1 parent a08092c commit d3090b9
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 4 deletions.
2 changes: 1 addition & 1 deletion deepmd/utils/pair_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(

Check warning on line 131 in deepmd/utils/pair_tab.py

View check run for this annotation

Codecov / codecov/patch

deepmd/utils/pair_tab.py#L131

Added line #L131 was not covered by tests
"The energy provided in the table does not decay to 0 at rcut."
)
Expand Down
71 changes: 68 additions & 3 deletions source/tests/test_pairtab_preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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.],
]
),
)
Expand All @@ -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.],
]
),
)
Expand Down Expand Up @@ -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)))

0 comments on commit d3090b9

Please sign in to comment.