center
- mdtools.structure.center(ag, center='cog', pbc=False, cmp='group', make_whole=False, debug=False)[source]
Calculate different types of centers of (compounds of) an MDAnalysis
AtomGroup
.- Parameters:
ag (
MDAnalysis.core.groups.AtomGroup
) – The MDAnalysisAtomGroup
for which to calculate the center.center (
{'cog', 'com', 'coc'}
, optional) –Which type of center to calculate.
'cog'
: Center of geometry'com'
: Center of mass'coc'
: Center of charge
pbc (
bool
, optional) – IfTrue
and cmp is'group'
, move allAtoms
in ag to the primary unit cell before calculating the center. IfTrue
and cmp is not'group'
, the center of each compound will be calculated without moving anyAtoms
to keep the compounds intact. Instead, the resulting position vectors will be moved to the primary unit cell after calculating the center. Note that this option does not make compounds whole that are split across periodic boundaries. To fix broken compounds before calculating their center use the option make_whole.cmp (
{'group', 'segments', 'residues', 'molecules', 'fragments', 'atoms'}
, optional) – The compounds of ag for which to calculate the center. If'group'
, the center of allAtoms
in ag will be returned as a single position vector. If'atoms'
, simply the positions of all atoms in ag will be returned (2d array). Else, the center of eachSegment
,Residue
, molecule, orfragment
contained in ag will be returned as an array of position vectors, i.e. a 2d array. Refer to the MDAnalysis’ user guide for an explanation of these terms. Note that in any case, also if cmp is e.g.'residues'
, only theAtoms
belonging to ag are taken into account, even if the compound might comprise additionalAtoms
that are not contained in ag.make_whole (
bool
, optional) – IfTrue
, compounds whose bonds are split across periodic boundaries are made whole before calculating their center. Note that allAtoms
in ag are wrapped back into the primary unit cell before making compounds whole to ensure that the algorithm is working properly. This means that making compounds whole in an unwrapped trajectory will lead to a wrapped trajectory with whole compounds (someAtoms
may lie outside the primary unit cell, but each compound’s center will lie inside the primary unit cell). Note that make_whole has no effect if cmp is set to'atoms'
.debug (
bool
, optional) – IfTrue
, run in debug mode.
- Returns:
centers (
numpy.ndarray
) – Position of the center of each compound in ag. If cmp was set to'group'
, the output will be a single position vector of shape(3,)
. Else, the output will be a 2d array of shape(n, 3)
wheren
is the number of compounds in ag.- Raises:
RuntimeWarning : – If debug is
True
and the center of any compound isnan
.
See also
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.wcenter()
Weighted center of (compounds of) an MDAnalysis
AtomGroup
mdtools.structure.wcenter_pos()
Calculate the weighted center of a position array
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
This function calls either
mdtools.structure.coc()
,mdtools.structure.cog()
ormdtools.structure.com()
depending on the value of center. These functions in turn call the corresponding method of the inputAtomGroup
.Important
If the weights of a compound (i.e. the charges in the case of
center='coc'
or the masses in the case ofcenter='com'
) sum up to zero, the coordinates of that compound’s center will benan
! If debug is set toTrue
, a warning will be raised if any compound’s center isnan
.If make_whole is
True
, allAtoms
in ag are wrapped back into the primary unit cell usingmdtools.box.wrap()
before making compounds whole. This is done to ensure that the unwrap algorithm (better called “make whole” algorithm) of MDAnalysis is working properly. This means that making compounds whole in an unwrapped trajectory will lead to a wrapped trajectory with whole compounds (someAtoms
may lie outside the primary unit cell, but each compound’s center will lie inside the primary unit cell). make_whole has no effect if cmp is set to'atoms'
.Todo
Check if it is really necessary to wrap all
Atoms
back into the primary unit cell before making compounds whole. The currently done back-wrapping is a serious problem, because it implies an inplace-change of theAtom
coordinates.Note
For developers
This function is meant to be used in MDTools scripts that offer the user to choose a specific compound and center for which to perform the analysis (see the --cmp and --center options of the
scripts.script_template
). To get the requested positions, you can simply dopos = mdt.strc.center(ag, center=args.CENTER, cmp=args.CMP)
.