amep.pbc.distance_matrix_parallel#

amep.pbc.distance_matrix_parallel(coords: ndarray, box_boundary: ndarray, other: ndarray | None = None, pbc: bool = True, maxdist: float = 1.122, njobs: int = 1) ndarray#

Calculates the distance matrix. Distances larger than maxdist are ignored and set to zero in the distance matrix. This is the parallel version using less RAM but taking more time to be calculated.

Parameters:
  • coords (numpy.ndarray) – Coordinates of the particles as array of shape (N,3).

  • box_boundary (np.ndarray of shape (3,2)) – Boundary of the simulation box in the form of np.array([[xmin, xmax], [ymin, ymax], [zmin, zmax]]).

  • other (numpy.ndarray, optional) – Other coordinates to which the pairwise distances are calculated as array of shape (N_other,3). If None, coords is used. The default is None.

  • pbc (bool, optional) – If True, periodic boundary conditions are considered. The default is True.

  • maxdist (float, optional) – Maxmimum distance to consider. Always use the smallest suitable value saves a lot computation time. The default is 1.122.

  • njobs (int, optional) – Number of workers used for the parallelization. If this number exceeds the number of CPU cores, it is set to the number of available CPU cores. The default is 2.

Returns:

d – Distance matrix of shape (N,N_other).

Return type:

np.ndarray

Examples

Create a simple test setup:

>>> import amep
>>> import numpy as np
>>> coords = np.array([[1,0,0], [4,0,0], [-2,0,0], [4.5,0,0]])
>>> box_boundary = np.array([[-5,5],[-5,5],[-0.5,0.5]])
>>> box = box_boundary[:,1] - box_boundary[:,0]
>>> fig, axs = amep.plot.new(figsize=(3,3))
>>> axs.grid(visible=True)
>>> amep.plot.particles(axs, coords, box_boundary, 0.25)
>>> amep.plot.box(axs, box_boundary)
>>> axs.set_xlabel(r'$x$')
>>> axs.set_ylabel(r'$y$')
>>> fig.savefig('./figures/pbc/pbc-distance_matrix_parallel.png')
>>>
../_images/pbc-distance_matrix_parallel.png

Calculate the distance matrix without periodic boundary conditions:

>>> D = amep.pbc.distance_matrix_parallel(
...     coords, box_boundary, pbc=False, maxdist=5
... )
>>> print(D)
[[0.  3.  3.  3.5]
 [3.  0.  0.  0.5]
 [3.  0.  0.  0. ]
 [3.5 0.5 0.  0. ]]
>>>

Calculate the distance matrix with periodic boundary conditions:

>>> D = amep.pbc.distance_matrix_parallel(
...     coords, box_boundary, pbc=True, maxdist=5
... )
>>> print(D)
[[0.  3.  3.  3.5]
 [3.  0.  4.  0.5]
 [3.  4.  0.  3.5]
 [3.5 0.5 3.5 0. ]]
>>>

Calculate the distance matrix for other_coords:

>>> D = amep.pbc.distance_matrix_parallel(
...     coords, box_boundary, other=coords[:2],
...     pbc=True, maxdist=5
... )
>>> print(D)
[[0.  3. ]
 [3.  0. ]
 [3.  4. ]
 [3.5 0.5]]
>>>