amep.cluster.geometric_center#

amep.cluster.geometric_center(coords: ndarray, box_boundary: ndarray, pbc: bool = True, clusters: list | None = None) ndarray#

Calculate the geometric center of the cluster containing the given points.

Uses the minimum image convention if pbc is True.

Notes

To consider periodic boundary conditions, we here use the projection method as described in Ref. [1] generalized to three spatial dimensions.

References

Parameters:
  • coords (np.ndarray of shape (N,3)) – Coordinates of all particles inside the cluster.

  • 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]]).

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

  • clusters (list or None, optional) – List of lists, where each list contains the indices of the particles that belong to the same cluster. If None, the geometric center of the given coords is calculated. If not None, the geometric center of each cluster in clusters is calculated, where coords must be the coordinates of the particles in clusters. The default is None.

Returns:

Geometric center of coords or of each cluster in clusters.

Return type:

np.ndarray of shape (3,) or (N,3)

Examples

>>> import amep
>>> import numpy as np
>>> coords = [
...     np.array([[0,0,0],[1,0,0],[-1,0,0],[0,1,0],[0,-1,0]]),
...     np.array([[0,-1.5,0],[0,1,0]]),
...     np.array([[-3,0,0],[4,0,0]]),
...     np.array([[-3,0,0],[4,0,0]])
... ]
>>> box_boundary = [
...     np.array([[-2,2],[-2,2],[-0.5,0.5]]),
...     np.array([[-2,2],[-2,2],[-0.5,0.5]]),
...     np.array([[-5,5],[-5,5],[-0.5,0.5]]),
...     np.array([[-5,5],[-5,5],[-0.5,0.5]])
... ]
>>> gc = [
...     amep.cluster.geometric_center(
...         coords[0], box_boundary[0], pbc=True
...     ), # expected: [0, 0, 0]
...     amep.cluster.geometric_center(
...         coords[1], box_boundary[1], pbc=True
...     ), # expected: [0, 1.75, 0]
...     amep.cluster.geometric_center(
...         coords[2], box_boundary[2], pbc=False
...     ), # expected: [0.5, 0, 0]
...     amep.cluster.geometric_center(
...         coords[3], box_boundary[3], pbc=True
...     ) # expected: [-4.5, 0, 0]
... ]
>>> titles = ['pbc=True', 'pbc=True', 'pbc=False', 'pbc=True']
>>> print(gc)
[array([0., 0., 0.]), array([0.  , 1.75, 0.  ]), array([0.5, 0. , 0. ]), array([-4.5,  0. ,  0. ])]
>>> fig, axs = amep.plot.new(figsize=(4,4), nrows=2, ncols=2)
>>> axs = axs.flatten()
>>> for i in range(4):
...     axs[i].scatter(coords[i][:,0], coords[i][:,1], s=50, c='k')
...     axs[i].scatter(gc[i][0], gc[i][1], s=100, marker='x', color='red')
...     amep.plot.box(axs[i], box_boundary[i])
...     axs[i].set_title(titles[i])
...     axs[i].set_xlabel(r"$x$")
...     axs[i].set_ylabel(r"$y$")
>>> fig.savefig('./figures/cluster/cluster-geometric_center_1.png')
../_images/cluster-geometric_center_1.png
>>> traj = amep.load.traj("../examples/data/lammps.h5amep")
>>> frame = traj[-1]
>>> clusters, idx = amep.cluster.identify(
...     frame.coords(), frame.box, pbc=True
... )
>>> gmc = amep.cluster.geometric_center(
...     frame.coords(), frame.box, pbc=True, clusters=clusters
... )
>>> imax = 5
>>> fig, axs = amep.plot.new(figsize=(4,3.5))
>>> colors = ["red", "orange", "yellow", "green", "blue", "purple"]
>>> mp = amep.plot.particles(
...     axs, frame.coords()[idx<=imax], frame.box, radius=0.5,
...     values=idx[idx<=imax], cmap=colors, vmin=-0.5, vmax=5.5
... )
>>> amep.plot.particles(
...     axs, frame.coords()[idx>imax], frame.box, radius=0.5, color="gray"
... )
>>> axs.scatter(
...     gmc[:imax+1,0], gmc[:imax+1,1], color="k", s=50, marker="x",
...     label="geometric center"
... )
>>> cax = amep.plot.add_colorbar(
...     fig, axs, mp, label="cluster id"
... )
>>> amep.plot.format_axis(cax, ticks=False)
>>> axs.legend(frameon=True)
>>> axs.set_xlabel(r"$x$")
>>> axs.set_ylabel(r"$y$")
>>> fig.savefig("./figures/cluster/cluster-geometric_center_2.png")
../_images/cluster-geometric_center_2.png