From: Josh Vermaas (
Date: Fri Jun 05 2020 - 10:34:36 CDT

Hi Bart,

I think you'd need to cycle over all the residues, and attack each residue
one at a time. Since the loops are slow, and pbwithin needs to recalculate
a data structure each time its called, what I've done recently is to
use/abuse cKDTree implementations in scipy to ask the question of what is
close to what in a periodic sense. A snippet would look something like this:

from scipy.spatial import cKDTree
#More setup to get R, which is a matrix of all atomic coordinates, and
select specific indexes onidxs.
tree = cKDTree(R[onidxs],boxsize=boxdims[i]+np.finfo(np.float32).eps)

>From the built tree (which you'd only need to do once per frame) you can
ask questions like how many pairs of interactions fall within a cutoff
(tree.query_pairs(cutoff)), or how many atoms are within a radius of a
given point. See the scipy.spatial documentation for more:


On Fri, Jun 5, 2020 at 7:40 AM Bart Bruininks <>

> Dear VMDers,
> I was trying to calculate contacts between molecules of the same species.
> Usually the VMD selection procedure is very fast and accurate compared to
> other options (GROMACS, MDAnalysis, etc). However, I can't seem to prevent
> every molecule from returning True to the selection below:
> resname X and pbwithin 8 of resname X
> This makes sense, for X is a multiparticle molecule, therefore it is
> always in range of itself. Is there something neat I can do to prevent this
> from happening (preferably without affecting calculation time too much).
> I did find something related about the 'gofr' module from 2008, but I am
> not sure how relevant it is. Back then such a selection was not possible, I
> wonder if this is still the case.
> Cheers,
> Bart