# Efficient calculation of bond overlap

For a small library dealing with molecules, I have to calculate a so called connectivity table for the chemical bonds. I assume that there is a bond between atom i and atom j, if the distance between them is smaller than their summed radius. This function has to be fast (So it's not premature optimisation ;) ). At the moment I have two possible solutions, where give_bond_array1 is the faster one. I have two questions:

1. Why is give_bond_array1 faster? Because from the code I would expect give_bond_array2 to be faster. The whole calculation is boiled down to vectorised calls to numpy.
2. Do you have an idea for a faster implementation, or is the next step Fortran, C...?
3. Why is the subtraction step in give_bond_array2 the bottleneck? It is only n**2 number of operations compared to 3 / 2 * n * (n-1) in other lines.

I have created a file minimal_example.py with the following content:

import numpy as np

"""Calculate a boolean array where A[i,j] is True indicates a
bond between the i-th and j-th atom.
"""
coords = ['x', 'y', 'z']
delta = {axis: None for axis in coords}
for i, axis in enumerate(coords):
coord = positions[:, i]
delta[axis] = coord - coord.reshape((len(coord), 1))
squared_distances = delta['x']**2 + delta['y']**2 + delta['z']**2
bond_array = overlap >= 0
if not self_bonding_allowed:
np.fill_diagonal(bond_array, False)
return bond_array

"""Calculate a boolean array where A[i,j] is True indicates a
bond between the i-th and j-th atom.
"""
delta = (positions[None, :, :] - positions[:, None, :])
squared_delta = delta ** 2
squared_distances = np.sum(squared_delta, axis=2)
bond_array = overlap >= 0
if not self_bonding_allowed:
np.fill_diagonal(bond_array, False)
return bond_array

n_atoms = 15000
positions, bond_radii = np.random.rand(n_atoms, 3), np.random.rand(n_atoms)


For the testing I got the following output:

In [1]: from minimal_example import *
...:

In [2]: %lprun -f give_bond_array1 give_bond_array1(positions, bond_radii)

Timer unit: 1e-06 s

Total time: 5.72649 s
File: /home/oweser/Dropbox/Tests_and_debugging/chemcoord_test/minimal_example.py
Function: give_bond_array1 at line 3

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
4                                               """Calculate a boolean array where A[i,j] is True indicates a
5                                               bond between the i-th and j-th atom.
6                                               """
7         1            3      3.0      0.0      coords = ['x', 'y', 'z']
10         1           18     18.0      0.0      delta = {axis: None for axis in coords}
11         4           32      8.0      0.0      for i, axis in enumerate(coords):
12         3           35     11.7      0.0          coord = positions[:, i]
13         3      1114585 371528.3     19.5          delta[axis] = coord - coord.reshape((len(coord), 1))
14         1      3030061 3030061.0     52.9      squared_distances = delta['x']**2 + delta['y']**2 + delta['z']**2
15         1       640631 640631.0     11.2      overlap = squared_radii - squared_distances
16         1       224410 224410.0      3.9      bond_array = overlap >= 0
17         1            4      4.0      0.0      if not self_bonding_allowed:
18         1          504    504.0      0.0          np.fill_diagonal(bond_array, False)
19         1            2      2.0      0.0      return bond_array

In [1]: from minimal_example import *
...:

In [2]: %lprun -f give_bond_array2 give_bond_array2(positions, bond_radii)

Timer unit: 1e-06 s

Total time: 75.13 s
File: /home/oweser/Dropbox/Tests_and_debugging/chemcoord_test/minimal_example.py
Function: give_bond_array2 at line 21

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
22                                               """Calculate a boolean array where A[i,j] is True indicates a
23                                               bond between the i-th and j-th atom.
24                                               """
27         1      1106056 1106056.0      1.5      delta = (positions[None, :, :] - positions[:, None, :])
28         1      1578934 1578934.0      2.1      squared_delta = delta ** 2
29         1     29164470 29164470.0     38.8      squared_distances = np.sum(squared_delta, axis=2)
30         1     39366942 39366942.0     52.4      overlap = squared_radii - squared_distances
31         1      1611265 1611265.0      2.1      bond_array = overlap >= 0
32         1            3      3.0      0.0      if not self_bonding_allowed:
33         1      1583333 1583333.0      2.1          np.fill_diagonal(bond_array, False)
34         1            2      2.0      0.0      return bond_array


PS: I know that I should also cut my molecule into smaller pieces in order to make "divide and conquer". This is already implemented and greatly speeds up calculation. Nevertheless I would like to understand why the above functions differ so much in performance.

PPS: The number of atoms (n_atoms in the example) may safely be assumed to be less than 10^5.

• You don't mention what the order of magnitude for the number of your molecules is, but if it's sufficiently large a grid based approach (somewhat explained here stackoverflow.com/questions/4172358/…) could help you reduce your search space – Harald Scheirich May 19 '17 at 18:34
• Sorry for the missing number, it is added now. As stated in the PS. I implemented the grid based approach. Using it, the calculation is fast enough. I would nevertheless like to know, why the second function works so much worse. – mcocdawc May 20 '17 at 8:16

Thanks to the timings of Gareth Rees I think I found the reason for the timing differences. First thing to note is, that I got similar timings on my better computer. But the timings on the worse computer were consistently different. I tried to answer the question on my own. If I use wrong terminology please correct me.

The main reason is that the maximum amount of used memory and the number of reshaping operations is higher in the first function and the operating system had to start swapping on the bad computer. So the finished version of the function looks like this:

def give_bond_array3(positions, bond_radii, self_bonding_allowed=False):
"""Calculate a boolean array where A[i,j] is True indicates a
bond between the i-th and j-th atom.
"""

coord = positions[:, 0]
squared_distances = (coord - coord[:, None])**2
for i in range(1,3):
coord = positions[:, i]
squared_distances += (coord - coord[:, None])**2
bond_array = overlap >= 0
if not self_bonding_allowed:
np.fill_diagonal(bond_array, False)
return bond_array


The main trick is that the large (n_atoms, n_atoms, 3) tensor of position differences between each pair of atom is not created at once. Instead (n_atoms, n_atoms) matrices are created for each one of the three coordinate axes and added in place.

One thing that made give_bond_array2 so especially bad was the assignment to variables like squared_radii instead of directly working with radii**2. The assigment meant there existed a reference to the object in memory, preventing the garbage collector from doing its work. Usually this would be no problem, because upon function exit the memory is freed anyway, but if you run into swapping problems during function execution, it becomes a thing.

If you just avoid the problem of variable assigment the function may be written as:

def give_bond_array4(positions, bond_radii, self_bonding_allowed=False):
"""Calculate a boolean array where A[i,j] is True indicates a
bond between the i-th and j-th atom.
"""
- np.sum((positions[None, :, :] - positions[:, None, :])**2, axis=2))
bond_array = overlap >= 0
if not self_bonding_allowed:
np.fill_diagonal(bond_array, False)
return bond_array


On the bad computer the timings are then like following:

1. give_bond_array3:4.40s
2. give_bond_array4: 5.63s

I explained the timing differences between give_bond_array3 and give_bond_array4 with the additional reshaping operations of which give_bond_array3 makes less use of. Additionally the coord vector is in all cases linearly aligned in memory. On the other hand the summing operation on tensor (positions[None, :, :] - positions[:, None, :])**2 along the axis=2 has to "jump" probably from address to address.

A final nice thing to note is the now visible timing difference between C and Fortran layout. The timings for give_bond_array3 are:

1. Fortran memory layout: 4.40 s
2. C memory layout: 4.85 s

Summary Don't do assignments for intermediate steps if memory is a problem.

I am unable to replicate your timing results. Your two functions take almost identical amounts of time when I run them (Python 3.5.3, NumPy 1.12.1):

>>> from timeit import timeit