|
3 | 3 |
|
4 | 4 | from deepmd.dpmodel.utils.neighbor_graph import ( |
5 | 5 | GraphLayout, |
| 6 | + attach_angles, |
6 | 7 | build_angle_index, |
| 8 | + build_neighbor_graph, |
7 | 9 | pad_and_guard_angles, |
8 | 10 | ) |
9 | 11 |
|
@@ -203,3 +205,85 @@ def test_build_angle_index_ordered_no_self(): |
203 | 205 | rev_pair = (b, a) |
204 | 206 | if a != b and a not in [2] and b not in [2]: # edge 2 is outside a_rcut |
205 | 207 | assert rev_pair in got # symmetric pairs should both exist |
| 208 | + |
| 209 | + |
| 210 | +# --------------------------------------------------------------------------- |
| 211 | +# Task 3: attach_angles tests |
| 212 | +# --------------------------------------------------------------------------- |
| 213 | + |
| 214 | + |
| 215 | +def test_attach_angles_sets_fields_and_preserves_edges(): |
| 216 | + """attach_angles populates angle_index/mask; edge fields are unchanged.""" |
| 217 | + coord = np.array([[[0.0, 0, 0], [0.8, 0, 0], [0, 0.8, 0]]]) |
| 218 | + atype = np.array([[0, 0, 0]]) # (nf, nloc) |
| 219 | + ng = build_neighbor_graph(coord, atype, None, 2.0) |
| 220 | + # default carry-all builder leaves angles None |
| 221 | + assert ng.angle_index is None |
| 222 | + assert ng.angle_mask is None |
| 223 | + ng2 = attach_angles(ng, a_rcut=1.5) |
| 224 | + assert ng2.angle_index is not None and ng2.angle_mask is not None |
| 225 | + # edge fields must be identical (by value and shape) |
| 226 | + np.testing.assert_array_equal(np.asarray(ng2.edge_index), np.asarray(ng.edge_index)) |
| 227 | + np.testing.assert_array_equal(np.asarray(ng2.edge_mask), np.asarray(ng.edge_mask)) |
| 228 | + np.testing.assert_array_equal(np.asarray(ng2.edge_vec), np.asarray(ng.edge_vec)) |
| 229 | + |
| 230 | + |
| 231 | +def test_attach_angles_angle_shape_consistent(): |
| 232 | + """angle_index has shape (2, A) and angle_mask has shape (A,).""" |
| 233 | + coord = np.array([[[0.0, 0, 0], [0.5, 0, 0], [0, 0.5, 0]]]) |
| 234 | + atype = np.array([[0, 0, 0]]) # (nf, nloc) |
| 235 | + ng = build_neighbor_graph(coord, atype, None, 2.0) |
| 236 | + ng2 = attach_angles(ng, a_rcut=1.5) |
| 237 | + assert ng2.angle_index.shape[0] == 2 |
| 238 | + assert ng2.angle_mask.shape[0] == ng2.angle_index.shape[1] |
| 239 | + |
| 240 | + |
| 241 | +def test_attach_angles_valid_angles_reference_valid_edges(): |
| 242 | + """All valid angle pairs (q_e, k_e) must index edges that are within a_rcut.""" |
| 243 | + coord = np.array([[[0.0, 0, 0], [0.6, 0, 0], [0, 0.6, 0]]]) |
| 244 | + atype = np.array([[0, 0, 0]]) # (nf, nloc) |
| 245 | + ng = build_neighbor_graph(coord, atype, None, 2.0) |
| 246 | + ng2 = attach_angles(ng, a_rcut=1.0) |
| 247 | + ei = np.asarray(ng2.edge_index) |
| 248 | + ev = np.asarray(ng2.edge_vec) |
| 249 | + em = np.asarray(ng2.edge_mask) |
| 250 | + ai = np.asarray(ng2.angle_index) |
| 251 | + am = np.asarray(ng2.angle_mask) |
| 252 | + for p in range(am.shape[0]): |
| 253 | + if not am[p]: |
| 254 | + continue |
| 255 | + q, k = int(ai[0, p]), int(ai[1, p]) |
| 256 | + # both referenced edges must be valid and within a_rcut |
| 257 | + assert em[q] and em[k] |
| 258 | + assert np.linalg.norm(ev[q]) < 1.0 |
| 259 | + assert np.linalg.norm(ev[k]) < 1.0 |
| 260 | + # both referenced edges must share the same center (dst) |
| 261 | + assert ei[1, q] == ei[1, k] |
| 262 | + |
| 263 | + |
| 264 | +def test_attach_angles_with_layout(): |
| 265 | + """Static layout.angle_capacity is respected.""" |
| 266 | + coord = np.array([[[0.0, 0, 0], [0.6, 0, 0], [0, 0.6, 0]]]) |
| 267 | + atype = np.array([[0, 0, 0]]) # (nf, nloc) |
| 268 | + ng = build_neighbor_graph(coord, atype, None, 2.0) |
| 269 | + layout = GraphLayout(edge_capacity=100, angle_capacity=20) |
| 270 | + ng2 = attach_angles(ng, a_rcut=1.5, layout=layout) |
| 271 | + assert ng2.angle_index.shape == (2, 20) |
| 272 | + assert ng2.angle_mask.shape == (20,) |
| 273 | + |
| 274 | + |
| 275 | +def test_attach_angles_ordered_include_self(): |
| 276 | + """ordered=True, include_self=True produces a superset of default pairs.""" |
| 277 | + coord = np.array([[[0.0, 0, 0], [0.5, 0, 0], [0, 0.5, 0]]]) |
| 278 | + atype = np.array([[0, 0, 0]]) # (nf, nloc) |
| 279 | + ng = build_neighbor_graph(coord, atype, None, 2.0) |
| 280 | + ng_default = attach_angles(ng, a_rcut=1.5) |
| 281 | + ng_full = attach_angles(ng, a_rcut=1.5, ordered=True, include_self=True) |
| 282 | + ai_def = np.asarray(ng_default.angle_index) |
| 283 | + am_def = np.asarray(ng_default.angle_mask) |
| 284 | + ai_full = np.asarray(ng_full.angle_index) |
| 285 | + am_full = np.asarray(ng_full.angle_mask) |
| 286 | + n_default = int(am_def.sum()) |
| 287 | + n_full = int(am_full.sum()) |
| 288 | + # ordered+include_self must produce at least as many angles as default |
| 289 | + assert n_full >= n_default |
0 commit comments