wcenter_pos

mdtools.structure.wcenter_pos(pos, weights=None, wrap_pos=False, wrap_result=False, box=None, dtype=None)[source]

Calculate the weighted center of an position array.

Parameters:
  • pos (array_like) – The position array for which to calculate the weighted center. Must be either of shape (3,), (n, 3) or (k, n, 3), where n is the number of particles and k is the number of frames. If pos has shape (k, n, 3), the center for each frame is computed. If pos has shape (3,), i.e. pos contains the position of a single particle, the weighted center is identical to pos.

  • weights (None or array_like, optional) – Array of shape (n,) containing the weight of each particle contained in pos. If weights is None, all particles are assumed to have a weight equal to one.

  • wrap_pos (bool, optional) – If True, wrap all positions in pos back to the primary unit cell using mdtools.box.wrap_pos() before calculating the weighted center. Note that this likely splits molecules across periodic boundaries, which is undesired when calculating their centers. If True, box must be provided.

  • wrap_result (bool, optional) – If True, wrap the calculated center(s) into the primary unit cell using mdtools.box.wrap_pos(). If True, box must be provided.

  • box (None or array_like, optional) – See mdtools.box.wrap_pos(). Is ignored if neither wrap_pos nor wrap_result is True.

  • dtype (type, optional) – The data type of the output array. If None, the data type is inferred from the input array(s).

Returns:

center (numpy.ndarray) – The weighted center of all particle positions in pos per frame.

Shapes when box is None.

pos

center

(3,)

(3,)

(n, 3)

(3,)

(k, n, 3)

(k, 1, 3)

Shapes when box is not None.

pos

box

center

(3,)

(6,)

(3,)

(3,)

(k, 6)

(k, 1, 3)

(n, 3)

(6,)

(3,)

(n, 3)

(k, 6)

(k, 1, 3)

(k, n, 3)

(6,)

(k, 1, 3)

(k, n, 3)

(k, 6)

(k, 1, 3)

n particles are lumped into one center for each individual frame k.

See also

numpy.average()

Compute the weighted average

MDAnalysis.core.groups.AtomGroup.center()

Weighted center of (compounds of) the group

MDAnalysis.core.groups.AtomGroup.center_of_geometry()

Center of geometry of (compounds of) the group

MDAnalysis.core.groups.AtomGroup.center_of_mass()

Center of mass of (compounds of) the group

mdtools.structure.center()

Different types of centers of (compounds of) an MDAnalysis AtomGroup

mdtools.structure.wcenter()

Weighted center of (compounds of) an MDAnalysis AtomGroup

mdtools.structure.coc()

Center of charge of (compounds of) an MDAnalysis AtomGroup

mdtools.structure.cog()

Center of geometry of (compounds of) an MDAnalysis AtomGroup

mdtools.structure.com()

Center of mass of (compounds of) an MDAnalysis AtomGroup

Notes

The weighted center is calculated according to

\[\mathbf{r}_{center} = \frac{1}{\sum_{i=1}^N w_i} \ \sum_{i=1}^N w_i \mathbf{r}_i\]

where \(r_i\) is the position vector of the \(i\)-th particle, \(w_i\) is its weight and \(N\) is the number of particles.

Examples

Shape of pos is (3,):

>>> pos = np.array([0, 2, 5])
>>> mdt.strc.wcenter_pos(pos)
array([0, 2, 5])
>>> mdt.strc.wcenter_pos(pos, weights=[3])
array([0, 2, 5])
>>> box = np.array([1, 2, 3, 90, 90, 90])
>>> mdt.box.wrap_pos(pos, box=box)
array([0., 0., 2.])
>>> mdt.strc.wcenter_pos(pos, wrap_pos=True, box=box)
array([0., 0., 2.])
>>> mdt.strc.wcenter_pos(pos, wrap_result=True, box=box)
array([0., 0., 2.])
>>> box = np.array([[1, 2, 3, 90, 90, 90],
...                 [2, 3, 4, 90, 90, 90]])
>>> mdt.box.wrap_pos(pos, box=box)
array([[[0., 0., 2.]],

       [[0., 2., 1.]]])
>>> mdt.strc.wcenter_pos(pos, wrap_pos=True, box=box)
array([[[0., 0., 2.]],

       [[0., 2., 1.]]])
>>> mdt.strc.wcenter_pos(pos, wrap_result=True, box=box)
array([[[0., 0., 2.]],

       [[0., 2., 1.]]])
>>> mdt.strc.wcenter_pos(pos, wrap_result=True)
Traceback (most recent call last):
...
ValueError: ...

Shape of pos is (n, 3):

>>> pos = np.array([[0, 2, 5],
...                 [1, 0, 1]])
>>> mdt.strc.wcenter_pos(pos)
array([0.5, 1. , 3. ])
>>> mdt.strc.wcenter_pos(pos, weights=[3, 1])
array([0.25, 1.5 , 4.  ])
>>> box = np.array([1, 2, 3, 90, 90, 90])
>>> mdt.box.wrap_pos(pos, box=box)
array([[0., 0., 2.],
       [0., 0., 1.]])
>>> mdt.strc.wcenter_pos(pos, wrap_pos=True, box=box)
array([0. , 0. , 1.5])
>>> mdt.strc.wcenter_pos(pos, wrap_result=True, box=box)
array([0.5, 1. , 0. ])
>>> box = np.array([[1, 2, 3, 90, 90, 90],
...                 [2, 3, 4, 90, 90, 90]])
>>> mdt.box.wrap_pos(pos, box=box)
array([[[0., 0., 2.],
        [0., 0., 1.]],

       [[0., 2., 1.],
        [1., 0., 1.]]])
>>> mdt.strc.wcenter_pos(pos, wrap_pos=True, box=box)
array([[[0. , 0. , 1.5]],

       [[0.5, 1. , 1. ]]])
>>> mdt.strc.wcenter_pos(pos, wrap_result=True, box=box)
array([[[0.5, 1. , 0. ]],

       [[0.5, 1. , 3. ]]])

Shape of pos is (k, n, 3):

>>> pos = np.array([[[0, 2, 5],
...                  [1, 0, 1]],
...
...                 [[1, 0, 1],
...                  [0, 2, 5]]])
>>> mdt.strc.wcenter_pos(pos)
array([[[0.5, 1. , 3. ]],

       [[0.5, 1. , 3. ]]])
>>> mdt.strc.wcenter_pos(pos, weights=[3, 1])
array([[[0.25, 1.5 , 4.  ]],

       [[0.75, 0.5 , 2.  ]]])
>>> box = np.array([1, 2, 3, 90, 90, 90])
>>> mdt.box.wrap_pos(pos, box=box)
array([[[0., 0., 2.],
        [0., 0., 1.]],

       [[0., 0., 1.],
        [0., 0., 2.]]])
>>> mdt.strc.wcenter_pos(pos, wrap_pos=True, box=box)
array([[[0. , 0. , 1.5]],

       [[0. , 0. , 1.5]]])
>>> mdt.strc.wcenter_pos(pos, wrap_result=True, box=box)
array([[[0.5, 1. , 0. ]],

       [[0.5, 1. , 0. ]]])
>>> box = np.array([[1, 2, 3, 90, 90, 90],
...                 [2, 3, 4, 90, 90, 90]])
>>> mdt.box.wrap_pos(pos, box=box)
array([[[0., 0., 2.],
        [0., 0., 1.]],

       [[1., 0., 1.],
        [0., 2., 1.]]])
>>> mdt.strc.wcenter_pos(pos, wrap_pos=True, box=box)
array([[[0. , 0. , 1.5]],

       [[0.5, 1. , 1. ]]])
>>> mdt.strc.wcenter_pos(pos, wrap_result=True, box=box)
array([[[0.5, 1. , 0. ]],

       [[0.5, 1. , 3. ]]])

Triclinic boxes:

>>> pos = np.array([[0, 2, 5],
...                 [1, 0, 1]])
>>> mdt.strc.wcenter_pos(pos)
array([0.5, 1. , 3. ])
>>> box = np.array([2, 3, 4, 80, 90, 100])
>>> mdt.box.wrap_pos(pos, box=box)
array([[0.        , 1.29469208, 1.0626734 ],
       [0.47905547, 2.95442326, 1.        ]])
>>> mdt.strc.wcenter_pos(pos, wrap_pos=True, box=box)
array([0.23952773, 2.12455767, 1.0313367 ])
>>> mdt.strc.wcenter_pos(pos, wrap_result=True, box=box)
array([0.5, 1. , 3. ])