def _part_ind_KDTree(self, ptype):
    'Find the particles in cells using a KDTree approach.'
    parent = getattr(self, 'parent', self.base_object)
    units = 'code_length'
    pos = np.stack([self[('index', 'x')].to(units), self[('index', 'y')].to(units), self[('index', 'z')].to(units)], axis=1).value
    dx = np.stack([self[('index', 'dx')].to(units), self[('index', 'dy')].to(units), self[('index', 'dz')].to(units)], axis=1).value
    ppos = np.stack([parent[(ptype, 'particle_position_x')], parent[(ptype, 'particle_position_y')], parent[(ptype, 'particle_position_z')]], axis=1).value
    mask = np.zeros(ppos.shape[0], dtype=bool)
    levels = self[('index', 'grid_level')].astype('int32').value
    if (levels.size == 0):
        return mask
    levelmin = levels.min()
    levelmax = levels.max()
    for lvl in range(levelmax, (levelmin - 1), (- 1)):
        lvl_mask = (levels == lvl)
        dx_loc = dx[lvl_mask]
        pos_loc = pos[lvl_mask]
        grid_tree = _scipy.spatial.cKDTree(pos_loc, boxsize=1)
        (dist, icell) = grid_tree.query(ppos[(~ mask)], distance_upper_bound=dx_loc.max(), p=np.inf)
        mask_loc = np.isfinite(dist[:])
        i = icell[mask_loc]
        dist = np.abs((ppos[(~ mask)][mask_loc, :] - pos_loc[i]))
        tmp_mask = np.all((dist <= (dx_loc[i] / 2)), axis=1)
        mask_loc[mask_loc] = tmp_mask
        mask[(~ mask)] |= mask_loc
    return mask
Find the particles in cells using a KDTree approach.
def _part_ind_KDTree(self, ptype):
    
    parent = getattr(self, 'parent', self.base_object)
    units = 'code_length'
    pos = np.stack([self[('index', 'x')].to(units), self[('index', 'y')].to(units), self[('index', 'z')].to(units)], axis=1).value
    dx = np.stack([self[('index', 'dx')].to(units), self[('index', 'dy')].to(units), self[('index', 'dz')].to(units)], axis=1).value
    ppos = np.stack([parent[(ptype, 'particle_position_x')], parent[(ptype, 'particle_position_y')], parent[(ptype, 'particle_position_z')]], axis=1).value
    mask = np.zeros(ppos.shape[0], dtype=bool)
    levels = self[('index', 'grid_level')].astype('int32').value
    if (levels.size == 0):
        return mask
    levelmin = levels.min()
    levelmax = levels.max()
    for lvl in range(levelmax, (levelmin - 1), (- 1)):
        lvl_mask = (levels == lvl)
        dx_loc = dx[lvl_mask]
        pos_loc = pos[lvl_mask]
        grid_tree = _scipy.spatial.cKDTree(pos_loc, boxsize=1)
        (dist, icell) = grid_tree.query(ppos[(~ mask)], distance_upper_bound=dx_loc.max(), p=np.inf)
        mask_loc = np.isfinite(dist[:])
        i = icell[mask_loc]
        dist = np.abs((ppos[(~ mask)][mask_loc, :] - pos_loc[i]))
        tmp_mask = np.all((dist <= (dx_loc[i] / 2)), axis=1)
        mask_loc[mask_loc] = tmp_mask
        mask[(~ mask)] |= mask_loc
    return mask