### Author Topic: Order of neighbors from NearestNeighborFinder  (Read 389 times)

#### eason1021

• Newbie
• Posts: 24
##### Order of neighbors from NearestNeighborFinder
« on: July 09, 2018, 07:02:26 PM »
And I still want to ask a question.
Now I stored 6 nearest neighbors around the central atom.
The neigh index in NearestNeighborFinder is sorted list of nearest neighbors.
But I want to sort these 6 atoms is array by +x +y +z -x -y -z direction to central atom.
Has any function to re-array the neigh index list?

#### Constanze Kalcher

• Administrator
• Full Member
• Posts: 197
##### Re: Order of neighbors from NearestNeighborFinder
« Reply #1 on: July 10, 2018, 04:06:39 PM »
Indeed, the NearestNeighborFinder sorts the neighbors by the length of the distance vector to the central particle.
In order to print the indices of the neighbors of each particle sorted like you asked could try something along these lines:

Code: [Select]
`from ovito.io import import_filefrom ovito.data import NearestNeighborFinderimport numpy as nppipeline = import_file("simulation.dump")data = pipeline.compute()N = 3finder = NearestNeighborFinder(N, data)for index in range(data.particles.count):    neighbors = [ (neigh.index, neigh.delta) for neigh in finder.find(index) ]    #print neighbor indices                                                                                                                                                                                                                           print( [n[0] for n in neighbors] )    resorted_neighbors = sorted( neighbors , key=lambda k: [k[1][0], k[1][1], k[1][2]], reverse=True )    #print rearranged neighbor indices                                                                                                                                                                                                                print( [n[0] for n in resorted_neighbors] )`