amep.cluster.center_of_mass#
- amep.cluster.center_of_mass(coords: ndarray, box_boundary: ndarray, mass: ndarray, pbc: bool = True, clusters: list | None = None) ndarray #
Calculates the center of mass 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 (weighted by the masses) as described in Ref. [1] generalized to three spatial dimensions. Note that this method can give slightly inaccurate results when applied to only a few particles with distances to each other which are comparable to the box size (see also example below).
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]]).
mass (np.ndarray of shape (N,)) – Mass of all particles inside the cluster.
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:
Center of mass of the cluster.
- Return type:
np.ndarray of shape (3,)
Examples
>>> import amep >>> import numpy as np >>> coords = [ ... np.array([[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]]) ... ] >>> mass = [ ... np.array([2,1,1,2]), ... np.array([1,2]), ... np.array([1,2]), ... np.array([1,2]) ... ] >>> com = [ ... amep.cluster.center_of_mass( ... coords[0], box_boundary[0], mass[0], pbc=True ... ), ... amep.cluster.center_of_mass( ... coords[1], box_boundary[1], mass[1], pbc=True ... ), ... amep.cluster.center_of_mass( ... coords[2], box_boundary[2], mass[2], pbc=False ... ), ... amep.cluster.center_of_mass( ... coords[3], box_boundary[3], mass[3], pbc=True ... ) ... ] >>> titles = ['pbc=True', 'pbc=True', 'pbc=False', 'pbc=True'] >>> print(com) [array([ 0.20483276, -0.20483276, 0. ]), array([0. , 1.31861167, 0. ]), array([1.66666667, 0. , 0. ]), array([4.81540634, 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*mass[i]**2, c='k') ... axs[i].scatter(com[i][0], com[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-center_of_mass_1.png')
>>> traj = amep.load.traj("../examples/data/lammps.h5amep") >>> frame = traj[-1] >>> clusters, idx = amep.cluster.identify( ... frame.coords(), frame.box, pbc=True ... ) >>> com = amep.cluster.center_of_mass( ... frame.coords(), frame.box, frame.mass(), ... 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( ... com[:imax+1,0], com[:imax+1,1], color="k", s=50, ... marker="x", label="center of mass" ... ) >>> 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-center_of_mass_2.png')