Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Jan 29, 2024
1 parent 8cbb98c commit a08092c
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 85 deletions.
2 changes: 0 additions & 2 deletions deepmd/pt/model/model/pair_tab.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
import logging
from typing import (
Dict,
List,
Optional,
Union,
)

import numpy as np
import torch
from torch import (
nn,
Expand Down
20 changes: 11 additions & 9 deletions deepmd/utils/pair_tab.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
#!/usr/bin/env python3

# SPDX-License-Identifier: LGPL-3.0-or-later
import logging
from typing import (
Optional,
Tuple,
Optional
)

import numpy as np
from scipy.interpolate import (
CubicSpline,
)

import logging


class PairTab:
"""Pairwise tabulated potential.
Expand Down Expand Up @@ -105,7 +104,6 @@ def _check_table_upper_boundary(self) -> None:
[0.03 0. 0. 0. ]
[0.035 0. 0. 0. ]]
"""

upper_val = self.vdata[-1][1:]
upper_idx = self.vdata.shape[0] - 1

Expand Down Expand Up @@ -147,13 +145,11 @@ def _check_table_upper_boundary(self) -> None:
).T
self.vdata = np.concatenate((self.vdata[:-1, :], pad_linear), axis=0)


def get(self) -> Tuple[np.array, np.array]:
"""Get the serialized table."""
return self.tab_info, self.tab_data

def _make_data(self):

# here we need to do postprocess, to overwrite coefficients when padding zeros resulting in negative energies.
data = np.zeros([self.ntypes * self.ntypes * 4 * self.nspline])
stride = 4 * self.nspline
Expand All @@ -168,10 +164,16 @@ def _make_data(self):
dtmp = np.zeros(stride)
for ii in range(self.nspline):
# check if vv is zero, if so, that's case 1, set all coefficients to 0,
dtmp[ii * 4 + 0] = 2 * vv[ii] - 2 * vv[ii + 1] + dd[ii] + dd[ii + 1] if vv[ii] != 0 else 0
dtmp[ii * 4 + 0] = (
2 * vv[ii] - 2 * vv[ii + 1] + dd[ii] + dd[ii + 1]
if vv[ii] != 0
else 0
)
dtmp[ii * 4 + 1] = (
-3 * vv[ii] + 3 * vv[ii + 1] - 2 * dd[ii] - dd[ii + 1]
) if vv[ii] != 0 else 0
(-3 * vv[ii] + 3 * vv[ii + 1] - 2 * dd[ii] - dd[ii + 1])
if vv[ii] != 0
else 0
)
dtmp[ii * 4 + 2] = dd[ii] if vv[ii] != 0 else 0
dtmp[ii * 4 + 3] = vv[ii]
data[
Expand Down
166 changes: 92 additions & 74 deletions source/tests/test_pairtab_preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
)

import numpy as np

from deepmd.utils.pair_tab import (
PairTab,
)
Expand All @@ -27,50 +28,61 @@ def setUp(self, mock_loadtxt) -> None:
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)


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],
]
))
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],
]
))
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],
]
),
)
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],
]
),
)

# for this test case, the table does not decay to zero at rcut = 0.22,
# in the cubic spline code, we use a fixed size grid, if will be a problem if we introduce variable gird size.
# we will do post process to overwrite spline coefficient `a3`,`a2`,`a1`,`a0`, to ensure energy decays to `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., 0.],
]
))
np.testing.assert_allclose(self.tab4.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.125, 0.2, 0.375],
[0.03, 0., 0., 0.],
[0.035, 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, 0.0, 0.0],
]
),
)
np.testing.assert_allclose(
self.tab4.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.125, 0.2, 0.375],
[0.03, 0.0, 0.0, 0.0],
[0.035, 0.0, 0.0, 0.0],
]
),
)


class TestPairTabPreprocessZero(unittest.TestCase):
@patch("numpy.loadtxt")
Expand All @@ -82,50 +94,56 @@ def setUp(self, mock_loadtxt) -> None:
[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.025, 0.0, 0.0, 0.0],
]
)

self.tab1 = PairTab(filename=file_path, rcut=0.023)
self.tab2 = PairTab(filename=file_path, rcut=0.025)
self.tab3 = PairTab(filename=file_path, rcut=0.028)


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., 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., 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., 0.],
[0.03, 0., 0., 0.],
]
))
np.testing.assert_equal(self.tab3.nspline,5)
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, 0.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, 0.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, 0.0, 0.0],
[0.03, 0.0, 0.0, 0.0],
]
),
)
np.testing.assert_equal(self.tab3.nspline, 5)

# 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)))

0 comments on commit a08092c

Please sign in to comment.