mdanalysis
mdanalysis copied to clipboard
write a test for cylindrical selections tokens using the periodic kdtree
test_atomselections.py is lacking a test using the CylindricalSelection, CylindricalLayerSelection, and CylindricalZoneSelection distance-based atom selections employing distance searches with PeriodicKDTree.
Currently version of MDAnalysis:
(run python -c "import MDAnalysis as mda; print(mda.__version__)")
Cylindrical Layer Selection Test
Hello! I'm Lexi, a prospective GSOC contributor. I was wondering if a test like this is what you are looking for and if you have any suggestions. Thanks!
def test_cylindrical_layer_with_kdtree(self, universe, kdtree):
# Test cylindrical layer selection with PeriodicKDTree
center = universe.atoms[0].position
inner_radius = 5.0
outer_radius = 10.0
zmin, zmax = center[2] - 15.0, center[2] + 15.0
# Use selection string for MDAnalysis selection along z axis
center_str = "{} {} {}".format(center[0], center[1], center[2])
sel_str = universe.select_atoms(
f"cylayer {inner_radius} {outer_radius} {zmin} {zmax} bynum 0-10000")
assert kdtree.cutoff >= outer_radius, "KDTree cutoff must be >= outer radius"
indices = kdtree.search(center, outer_radius)
# Filter the points
pos = universe.atoms.positions
selected_pos = pos[indices]
dxy = np.sqrt((selected_pos[:, 0] - center[0])**2 +
(selected_pos[:, 1] - center[1])**2)
# Apply radius and z-range filters
mask_r = (inner_radius <= dxy) & (dxy <= outer_radius)
mask_z = (zmin <= selected_pos[:, 2]) & (selected_pos[:, 2] <= zmax)
mask_combined = mask_r & mask_z
filtered_indices = indices[mask_combined]
assert_equal(
sorted(filtered_indices),
sorted(sel_str.indices),
"PeriodicKDTree selection doesn't match MDAnalysis selection for cylindrical layer"
)