Finding the nearest atoms around a given atoms

244 views
Skip to first unread message

nguyen man

unread,
Oct 1, 2018, 12:33:56 PM10/1/18
to MDnalysis discussion
Hi everybody,

I am new in MDAnalysis, I am trying to find the 10 atoms around Litium atom, here is a part in my code:
for ts in u.trajectory:
    box = u.dimensions[0:3]
    Li.pack_into_box(box=box)
    O.pack_into_box(box=box)
    Cl.pack_into_box(box=box)
    pos_Li = Li.positions
    pos_O = O.positions
    pos_Cl = Cl.positions
    tree = cKDTree(pos_O,pos_Li, pos_Cl,boxsize=box)
    dist, ids = tree.query(pos_Li,10)
but it did not work, the error "
TypeError: only size-1 arrays can be converted to Python scalars
"
could anyone help me with this problem?

Thanks very much.
Hong Man

nguyen man

unread,
Oct 1, 2018, 12:33:56 PM10/1/18
to mdnalysis-...@googlegroups.com

Johannes Zeman

unread,
Oct 1, 2018, 5:55:58 PM10/1/18
to MDnalysis discussion
Hi Hong Man,

Can you provide some additional information about the implementation of cKDTree that you use?
If it's scipy.spatial.cKDTree, that explains the error (The second argument must be a scalar, see here: https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.cKDTree.html)

Apart from that, scipy.spatial.cKDTree is *not* suited for systems with periodic boundary conditions!

If you use MDAnalysis 18.0, I'd recommend something along these lines:

Finding the N closest oxygens around each lithium:
    N = 10
1. compute a distance array:
    distances = MDAnalysis.lib.distances.distance_array(Li.positions, O.positions, box=u.dimensions)
2. get the indices of the N nearest oxygen atoms:
    indices = numpy.argpartition(distances, N, axis=1)[:, :N]
3. create empty AtomGroup:
    O_around_Li = MDAnalysis.AtomGroup([], u)
4. Add oxygens to group using the indices we found:
    for idx in indices:
        O_around_Li += O[idx]

Beware: If you have multiple Li atoms, the obtained group might contain duplicate atoms (oxygen atoms that solvate two or more Li atoms)

Hope this helps!

Cheers,

Johannes

nguyen man

unread,
Oct 2, 2018, 4:20:31 AM10/2/18
to MDnalysis discussion
Hi Johannes,

Thank you very much for your help,
Yes, it's scipy.spatial.cKDTree
I will try what you suggested

Cheers,
Hong Man



Vào 23:55:58 UTC+2 Thứ Hai, ngày 01 tháng 10 năm 2018, Johannes Zeman đã viết:
Reply all
Reply to author
Forward
0 new messages