mdanalysis icon indicating copy to clipboard operation
mdanalysis copied to clipboard

write a test for cylindrical selections tokens using the periodic kdtree

Open jmborr opened this issue 8 years ago • 1 comments

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__)")

jmborr avatar Dec 12 '17 13:12 jmborr

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

lexi-x avatar Mar 21 '25 01:03 lexi-x