How to get the distances of the neighbor atoms?

Hi,
I am trying to get the distances of a particular atom with its neighbors. I have written the following but it asks me for “self” attribute, which I don’t know what it is.

from pymatgen.core.structure import Structure
from pymatgen.analysis.local_env import CrystalNN

struct=Structure.from_file(“CONTCAR”)
CrystalNN.get_nn_info(“self”,struct,struct[0])

How can I get the distances to the neighbors of an specific site? What is the problem in this code?
Thank you very much

If you want the distance, you can call the get_distance() method. As an aside, in your code snippet you do not use the CrystalNN class correctly. You might not be familiar with object oriented programming; first you must initialize an instance of the CrystalNN class, which is for your class, then that object can be used to call methods that analyze that structure. “self” is not an argument that you provide, but represents the object instance, and is automatically included when a class instantiation calls one of its own functions. Correct code snippet below, where I have saved the distance inside of the nn info, rather than storing it elsewhere.

from pymatgen.core.structure import Structure
from pymatgen.analysis.local_env import CrystalNN

struct=Structure.from_file(“CONTCAR”)
cnn = CrystalNN() # using default params

N = 0 # Site of interest, can be anything in [0, num_sites - 1]
info = cnn.get_nn_info(s, N)
for i, site in enumerate(info):
    info[i]['distance'] = s.get_distance(N, site['site_index']))
1 Like

Thank you very much. With your answer I was able to build my code, which is working perfectly.
Best
Jon