Forum Navigation
You need to log in to create posts and topics.

Locate nearest neighbor of selected particle id / Neighbor List


I would like to locate the first neighbors or second neighbors of a selected particle id. I'm not interested in the number of neighbors, with a cutoff but with a specific particle id.

and if i can export the .xyz file containing the chosen particle id with their neighbor id I had found answers on this topic using Ovito Basic, but with a cut. Could you help me with this?


this is a often requested feature. The following python script modifier can either take the number of neighbors per shell as input and calculate the corresponding neighbor lists. Due to technical limitations the maximum neighbor count cannot be greater than 30. If particle identifiers exist, they will be used for the neighbor lists, otherwise the particle index is used. The lists are stored as additional particle properties which can be exporting using standard ovito functionality. This script will soon be available and documented on my github page: .

from import NearestNeighborFinder, DataCollection
from numpy import zeros, cumsum

def modify(frame: int, data: DataCollection, num_neighors=[12, 8]):
    if isinstance(num_neighors, int):
        num_neighors = [num_neighors]
    elif not isinstance(num_neighors, list):
        raise ValueError(
            "num_neighors variable needs to be a list of atoms per neighbor shell"
    if sum(num_neighors) >= 30:
        raise ValueError("The total number of neighbors has to be less than 30")

    use_id = "Particle Identifier" in data.particles.keys()
    finder = NearestNeighborFinder(sum(num_neighors), data)
    neighbor_list = zeros((data.particles.count, sum(num_neighors)), dtype=int)
    for index in range(data.particles.count):
        for i, neigh in enumerate(finder.find(index)):
            if use_id:
                neigh = data.particles["Particle Identifier"][neigh.index]
                neigh = neigh.index
            neighbor_list[index, i] = neigh
        yield index / data.particles.count

    csum = [0] + list(cumsum(num_neighors))
    for i, shell in enumerate(csum):
        if i == 0:
            f"{i} neighbor shell", data=(neighbor_list[:, csum[i - 1] : csum[i]])




Thank you so much Daniel Utt for your replay,

but whats I'm looking for is define the particle identifier on python code to search their neighbor  by id

and i will wait for the script on your github page

Thank you and good luck


the only thing I would like to add to Daniel's nice all-in-one solution here is that you can in principle look up and export the particle identifiers (or indices) of a specific particle and its neighbors from the OVITO Basic Desktop Application by using a combination of 1) Expression Selection, 2) Expand Selection, 3) Invert Selection and 4) Delete Selected Modifiers, and then using the regular xyz file export function.

New for our users in China: OVITO on WeChat 

Official OVITO WeChat channel operated by Foshan Diesi Technology Co., Ltd.