Skip to content

Commit

Permalink
add ut for the open boundary condition. fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Han Wang committed Jan 31, 2024
1 parent 515a724 commit 9ca6de3
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 11 deletions.
28 changes: 18 additions & 10 deletions deepmd/model_format/make_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,26 @@ def call(
box: Optional[np.ndarray] = None,
do_atomic_virial: bool = False,
) -> Dict[str, np.ndarray]:
"""Return total energy of the system.
Args:
- coord: Atom coordinates with shape [nframes, natoms[1]*3].
- atype: Atom types with shape [nframes, natoms[1]].
- natoms: Atom statisics with shape [self.ntypes+2].
- box: Simulation box with shape [nframes, 9].
- atomic_virial: Whether or not compoute the atomic virial.
"""Return predictions of a model.
Parameters
----------
coord
The coordinates of the atoms.
shape: nf x (nloc x 3)
atype
The type of atoms. shape: nf x nloc
box
The simulation box. shape: nf x 9
do_atomic_virial
If calculate the atomic virial.
Returns
-------
- energy: Energy per atom.
- force: XYZ force per atom.
ret_dict
The result dict of type Dict[str,np.ndarray].
The keys are defined by the `ModelOutputDef`.
"""
nframes, nloc = atype.shape[:2]
if box is not None:
Expand All @@ -64,7 +72,7 @@ def call(
box.reshape(nframes, 3, 3),
)
else:
coord_normalized = coord.clone()
coord_normalized = coord.copy()
extended_coord, extended_atype, mapping = extend_coord_with_ghosts(
coord_normalized, atype, box, self.get_rcut()
)
Expand Down
2 changes: 1 addition & 1 deletion deepmd/model_format/nlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def build_neighbor_list(
rr = rr[:, :, 1:]
nlist = nlist[:, :, 1:]
nnei = rr.shape[2]
if nsel != nnei:
if nsel <= nnei:
rr = rr[:, :, :nsel]
nlist = nlist[:, :, :nsel]
else:
Expand Down
2 changes: 2 additions & 0 deletions deepmd/model_format/region.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ def normalize_coord(
----------
coord : np.ndarray
orignal coordinates of shape [*, na, 3].
cell : np.ndarray
simulation cell shape [*, 3, 3].
Returns
-------
Expand Down
30 changes: 30 additions & 0 deletions source/tests/pt/test_dp_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,36 @@ def test_dp_consistency(self):
to_numpy_array(ret1["energy_redu"]),
)

def test_dp_consistency_nopbc(self):
nf, nloc = self.atype.shape

Check notice

Code scanning / CodeQL

Unused local variable Note test

Variable nf is not used.

Check notice

Code scanning / CodeQL

Unused local variable Note test

Variable nloc is not used.
ds = DPDescrptSeA(
self.rcut,
self.rcut_smth,
self.sel,
)
ft = DPInvarFitting(
"energy",
self.nt,
ds.get_dim_out(),
1,
distinguish_types=ds.distinguish_types(),
)
type_map = ["foo", "bar"]
md0 = DPDPModel(ds, ft, type_map=type_map)
md1 = DPModel.deserialize(md0.serialize()).to(env.DEVICE)
args0 = [self.coord, self.atype]
args1 = [to_torch_tensor(ii) for ii in [self.coord, self.atype]]
ret0 = md0.call(*args0)
ret1 = md1.forward_common(*args1)
np.testing.assert_allclose(
ret0["energy"],
to_numpy_array(ret1["energy"]),
)
np.testing.assert_allclose(
ret0["energy_redu"],
to_numpy_array(ret1["energy_redu"]),
)


class TestDPModelLower(unittest.TestCase, TestCaseSingleFrameWithNlist):
def setUp(self):
Expand Down

0 comments on commit 9ca6de3

Please sign in to comment.