Based on a cutoff distance I want to create a list of points which are neighbors for a given point. I'm stuck how to add the ids of the points to a numpy array.
I read the 'coordinate' in a numpy array like below:
ID x y z
1. 1 1 0.000000 3.078000 0.554000
2. 2 0.000000 3.078000 2.170000
3. 3 2.666000 1.539000 0.554000
4. 4 2.666000 1.539000 2.170000
5. 5 1.774000 0.000000 3.324000
6. 6 2.712000 0.000000 7.207000
and so on...
I want the output list like this
ID neighbor1 neighbor2 ....neighbor5
1. 1 4 5 6 8
2. 2 1 4 6 n/a
3. 3 5 1 n/a 2
4. etc.
so each position (1,2,3,4...so on) can have a maximum of 5 neighbors, if less than 5 I want to put n/a there. I have the following code so far.
#neighborlist
flist = glob.glob(filename)
for f in flist:
load = np.loadtxt(f, usecols=(2,3,4))
coordinate=np.array(load)
s = (len(coordinate),6)
Nlist = np.zeros(s)
rcut = 2.5 #cutoff distance for neighbors
for f in flist:
load = np.loadtxt(f, usecols=(2,3,4))
coordinate=np.array(load)
for i in range(len(coordinate)):
idx = [i,]
for j in range(len(coordinate)):
if np.linalg.norm(coordinate[i]-coordinate[j]) < rcut and np.linalg.norm(coordinate[i]-coordinate[j]) > 0.1:
idx.append(j)
else:
idx = idx
while len(idx)<6:
idx.append('n/a')
print idx
Nlist[i,:]=idx
print Nlist
for this i'm getting:
ValueError: could not convert string to float: n/a
as I have fixed the Nlist array size to (len(data),6) it cannot copy Nlist[i,:]=idx when the neighbors are less than 5 (the first element is the point itself). In this case is there a way to declare Nlist dynamically or make its size variable? I know its a tough question but there has to be a way around this problem. Thanks in advance.