Skip to content

Commit

Permalink
allowed more co* variants to center call
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Papior <[email protected]>
  • Loading branch information
zerothi committed Dec 18, 2023
1 parent 3b8117a commit 1bf4a26
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/sisl/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -2811,12 +2811,12 @@ def center(
By specifying `what` one can control whether it should be:
* ``xyz|position``: Center of coordinates (default)
* ``cop|xyz|position``: Center of coordinates (default)
* ``mm:xyz`` or ``mm(xyz)``: Center of minimum/maximum of coordinates
* ``mass``: Center of mass
* ``mass:pbc``: Center of mass using periodicity, if the point 0, 0, 0 is returned it
* ``com|mass``: Center of mass
* ``com:pbc|mass:pbc``: Center of mass using periodicity, if the point 0, 0, 0 is returned it
may likely be because of a completely periodic system with no true center of mass
* ``cell``: Center of cell
* ``cou|cell``: Center of cell
Parameters
----------
Expand All @@ -2825,15 +2825,16 @@ def center(
what : {'xyz', 'mm:xyz', 'mass', 'mass:pbc', 'cell'}
determine which center to calculate
"""
if "cell" == what:
what = what.lower()
if what in ("cou", "cell", "lattice"):
return self.lattice.center()

if atoms is None:
g = self
else:
g = self.sub(atoms)

if "mass:pbc" == what:
if what in ("com:pbc", "mass:pbc"):
mass = g.mass
sum_mass = mass.sum()
# the periodic center of mass is determined by transfering all
Expand All @@ -2847,14 +2848,14 @@ def center(
avg_theta = np.arctan2(-avg_sin, -avg_cos) / (2 * np.pi) + 0.5
return avg_theta @ g.lattice.cell

if "mass" == what:
if what in ("com", "mass"):
mass = g.mass
return dot(mass, g.xyz) / np.sum(mass)
return mass @ g.xyz / mass.sum()

if what in ("mm:xyz", "mm(xyz)"):
return (g.xyz.min(0) + g.xyz.max(0)) / 2

if what in ("xyz", "position"):
if what in ("cop", "xyz", "position"):
return np.mean(g.xyz, axis=0)

raise ValueError(
Expand Down

0 comments on commit 1bf4a26

Please sign in to comment.