|
| 1 | +# SPDX-License-Identifier: LGPL-3.0-or-later |
| 2 | +import sys |
| 3 | +import unittest |
| 4 | +from copy import ( |
| 5 | + deepcopy, |
| 6 | +) |
| 7 | + |
| 8 | +import numpy as np |
| 9 | + |
| 10 | +from deepmd.dpmodel.common import ( |
| 11 | + to_numpy_array, |
| 12 | +) |
| 13 | + |
| 14 | +if sys.version_info >= (3, 10): |
| 15 | + from deepmd.jax.common import ( |
| 16 | + to_jax_array, |
| 17 | + ) |
| 18 | + from deepmd.jax.descriptor.se_e2_a import ( |
| 19 | + DescrptSeA, |
| 20 | + ) |
| 21 | + from deepmd.jax.env import ( |
| 22 | + jnp, |
| 23 | + ) |
| 24 | + from deepmd.jax.fitting.fitting import ( |
| 25 | + PropertyFittingNet, |
| 26 | + ) |
| 27 | + from deepmd.jax.model.property_model import ( |
| 28 | + PropertyModel, |
| 29 | + ) |
| 30 | + |
| 31 | + dtype = jnp.float64 |
| 32 | + |
| 33 | + |
| 34 | +@unittest.skipIf( |
| 35 | + sys.version_info < (3, 10), |
| 36 | + "JAX requires Python 3.10 or later", |
| 37 | +) |
| 38 | +class TestCaseSingleFrameWithoutNlist: |
| 39 | + def setUp(self) -> None: |
| 40 | + # nf=2, nloc == 3 |
| 41 | + self.nloc = 3 |
| 42 | + self.nt = 2 |
| 43 | + self.coord = np.array( |
| 44 | + [ |
| 45 | + [ |
| 46 | + [0, 0, 0], |
| 47 | + [0, 1, 0], |
| 48 | + [0, 0, 1], |
| 49 | + ], |
| 50 | + [ |
| 51 | + [1, 0, 1], |
| 52 | + [0, 1, 1], |
| 53 | + [1, 1, 0], |
| 54 | + ], |
| 55 | + ], |
| 56 | + dtype=np.float64, |
| 57 | + ) |
| 58 | + self.atype = np.array([[0, 0, 1], [1, 1, 0]], dtype=int).reshape([2, self.nloc]) |
| 59 | + self.cell = 2.0 * np.eye(3).reshape([1, 9]) |
| 60 | + self.cell = np.array([self.cell, self.cell]).reshape(2, 9) |
| 61 | + self.sel = [16, 8] |
| 62 | + self.rcut = 2.2 |
| 63 | + self.rcut_smth = 0.4 |
| 64 | + self.atol = 1e-12 |
| 65 | + |
| 66 | + |
| 67 | +@unittest.skipIf( |
| 68 | + sys.version_info < (3, 10), |
| 69 | + "JAX requires Python 3.10 or later", |
| 70 | +) |
| 71 | +class TestPaddingAtoms(unittest.TestCase, TestCaseSingleFrameWithoutNlist): |
| 72 | + def setUp(self): |
| 73 | + TestCaseSingleFrameWithoutNlist.setUp(self) |
| 74 | + |
| 75 | + def test_padding_atoms_consistency(self): |
| 76 | + ds = DescrptSeA( |
| 77 | + self.rcut, |
| 78 | + self.rcut_smth, |
| 79 | + self.sel, |
| 80 | + ) |
| 81 | + ft = PropertyFittingNet( |
| 82 | + self.nt, |
| 83 | + ds.get_dim_out(), |
| 84 | + mixed_types=ds.mixed_types(), |
| 85 | + intensive=True, |
| 86 | + ) |
| 87 | + type_map = ["foo", "bar"] |
| 88 | + model = PropertyModel(ds, ft, type_map=type_map) |
| 89 | + var_name = model.get_var_name() |
| 90 | + args = [to_jax_array(ii) for ii in [self.coord, self.atype, self.cell]] |
| 91 | + result = model.call(*args) |
| 92 | + # test intensive |
| 93 | + np.testing.assert_allclose( |
| 94 | + to_numpy_array(result[f"{var_name}_redu"]), |
| 95 | + np.mean(to_numpy_array(result[f"{var_name}"]), axis=1), |
| 96 | + atol=self.atol, |
| 97 | + ) |
| 98 | + # test padding atoms |
| 99 | + padding_atoms_list = [1, 5, 10] |
| 100 | + for padding_atoms in padding_atoms_list: |
| 101 | + coord = deepcopy(self.coord) |
| 102 | + atype = deepcopy(self.atype) |
| 103 | + atype_padding = np.pad( |
| 104 | + atype, |
| 105 | + pad_width=((0, 0), (0, padding_atoms)), |
| 106 | + mode="constant", |
| 107 | + constant_values=-1, |
| 108 | + ) |
| 109 | + coord_padding = np.pad( |
| 110 | + coord, |
| 111 | + pad_width=((0, 0), (0, padding_atoms), (0, 0)), |
| 112 | + mode="constant", |
| 113 | + constant_values=0, |
| 114 | + ) |
| 115 | + args = [ |
| 116 | + to_jax_array(ii) for ii in [coord_padding, atype_padding, self.cell] |
| 117 | + ] |
| 118 | + result_padding = model.call(*args) |
| 119 | + np.testing.assert_allclose( |
| 120 | + to_numpy_array(result[f"{var_name}_redu"]), |
| 121 | + to_numpy_array(result_padding[f"{var_name}_redu"]), |
| 122 | + atol=self.atol, |
| 123 | + ) |
| 124 | + |
| 125 | + |
| 126 | +if __name__ == "__main__": |
| 127 | + unittest.main() |
0 commit comments