@@ -923,14 +923,15 @@ def distances_squared(particles, other_particles):
923923 return (dxdydz ** 2 ).sum (- 1 )
924924
925925
926- def nearest_neighbour (particles , neighbours = None , max_array_length = 10000000 ):
926+ def nearest_neighbour (particles , neighbours = None , self_search = False , max_array_length = 10000000 ):
927927 """
928928 Returns the nearest neighbour of each particle in this set. If the 'neighbours'
929929 particle set is supplied, the search is performed on the neighbours set, for
930930 each particle in the orignal set. Otherwise the nearest neighbour in the same
931931 set is searched.
932932
933933 :argument neighbours: the particle set in which to search for the nearest neighbour (optional)
934+ :argument self_search: if True, the nearest neighbour can be the particle itself (default False)
934935
935936 >>> from amuse.datamodel import Particles
936937 >>> particles = Particles(3)
@@ -964,7 +965,7 @@ def nearest_neighbour(particles, neighbours=None, max_array_length=10000000):
964965 )
965966 for indices in indices_in_each_batch :
966967 distances_squared = particles [indices ].distances_squared (other_particles )
967- if neighbours is None :
968+ if not self_search and neighbours is None :
968969 diagonal_indices = (numpy .arange (len (indices )), indices )
969970 distances_squared .number [
970971 diagonal_indices
@@ -973,11 +974,11 @@ def nearest_neighbour(particles, neighbours=None, max_array_length=10000000):
973974 return other_particles [numpy .concatenate (neighbour_indices )]
974975
975976 distances_squared = particles .distances_squared (other_particles )
976- if neighbours is None :
977+ if not self_search and neighbours is None : # can't be your own neighbour
977978 diagonal_indices = numpy .diag_indices (len (particles ))
978979 distances_squared .number [
979980 diagonal_indices
980- ] = numpy .inf # can't be your own neighbour
981+ ] = numpy .inf
981982 return other_particles [distances_squared .argmin (axis = 1 )]
982983
983984
0 commit comments