class NearestNeighbor(md_trajectory, cutoff_radius=None, start_time=None, end_time=None, pair_selection=None, time_resolution=None, info_panel=None)

Class for calculating the time-dependent nearest neighbors from an MD simulation.

  • md_trajectory (MDTrajectory | AtomicConfiguration) – The MDTrajectory or configuration to calculate the nearest neighbors for.
  • cutoff_radius (PhysicalQuantity of type length) – Cut off distance for nearest neighbors (must be positive).
    Default: 2.5 * Angstrom
  • start_time (PhysicalQuantity of type time) – The start time.
    Default: 0.0 * fs
  • end_time (PhysicalQuantity of type time) – The end time.
    Default: The last time frame
  • pair_selection (sequence) – Only include contributions between this selection of atoms. A sequence has to contain two of the following types: Element, tag name, list of indices, or None.
    Default: all atoms pairs.
  • time_resolution (PhysicalQuantity of type time) – The time interval between snapshots in the MD trajectory that are included in the analysis.
  • info_panel (InfoPanel (Plot2D)) – Info panel to show the calculation progress.
    Default: No info panel
Returns:An array with the average number of nearest neighbors for each selected snapshot in the first column and its variance in the second column.
Return type:array

Return the time values associated with the selected snapshots.

Usage Examples

Load an MDTrajectory, calculate the average nearest neighbor number and the corresponding variance as a function of time and plot the results:

md_trajectory = nlread('')[-1]

nearest_neighbor = NearestNeighbor(md_trajectory,

# Get the times in ps and the nearest neighbor values.
t       = nearest_neighbor.times().inUnitsOf(ps)
nn_data =

# Plot the nearest neighbor number and its variance using pylab.
import pylab

pylab.plot(t, nn_data[:, 0], label='Number of nearest neighbors')
pylab.plot(t, nn_data[:, 1], label='Variance nearest neighbors')
pylab.xlabel('t (ps)')


This analysis calculates for each snapshot in an MDTrajectory the average number of nearest neighbors of a selected group of atoms (the first entry in pair_selection) with a second selected group of atoms (the second entry in pair_selection). By default the nearest neighbors are calculated among all atoms in the configuration.

In contrast to CoordinationNumber, which calculates the evolution of the number of atoms with a given coordination number, this analysis evaluates the average number of nearest neighbors along the simulation time.