Skip to content

disorder

Describe and resolve the substitutional disorder in a crystal structure.

A disordered crystal is really several structures superimposed: a site that is only 0.63 occupied is there in 63% of unit cells and something else is there in the rest. chmpy models one set of sites, so until the disorder is resolved a methyl group modelled in two orientations has six hydrogens on one carbon, and every molecule, surface and energy derived from it is wrong.

The CIF dictionary describes disorder with two labels per site:

_atom_site_disorder_assembly which independent set of alternatives a site takes part in _atom_site_disorder_group which alternative, within that assembly, the site belongs to

A component of the structure is every ordered site plus one group from each assembly, so taking group 1 everywhere gives one ordered structure, group 2 everywhere gives the next, and so on.

Plenty of files -- most of the ones deposited without those labels -- only give partial occupancies, so :func:analyse_disorder falls back to reading the occupancies -- values that pair up to 1.0 are two alternatives of one assembly -- and then to the site labels, where O1A and O1B name one oxygen in two places. What none of those can work out is reported rather than guessed at, because a wrong guess here quietly produces a plausible and incorrect structure.

Disorder dataclass

How the sites of an asymmetric unit are shared between alternatives.

Attributes:

Name Type Description
assemblies tuple

the independent sets of alternatives

unresolved tuple

indices of partially occupied sites that could not be placed in any assembly

reason str

what is going on with those sites, in words

source str

where the grouping came from, one of "disorder_group", "occupancy", "label", "occupancy+label" or "none"

n_sites int

how many sites the asymmetric unit has

Source code in chmpy/crystal/disorder.py
@dataclass(frozen=True)
class Disorder:
    """How the sites of an asymmetric unit are shared between alternatives.

    Attributes:
        assemblies: the independent sets of alternatives
        unresolved: indices of partially occupied sites that could not be
            placed in any assembly
        reason: what is going on with those sites, in words
        source: where the grouping came from, one of ``"disorder_group"``,
            ``"occupancy"``, ``"label"``, ``"occupancy+label"`` or ``"none"``
        n_sites: how many sites the asymmetric unit has
    """

    n_sites: int
    assemblies: tuple = ()
    unresolved: tuple = ()
    source: str = "none"
    occupancies: np.ndarray = field(default=None, repr=False)
    #: why the unresolved sites could not be grouped, if there are any
    reason: str = field(default="", repr=False)

    @property
    def is_disordered(self):
        "whether any site is shared between alternatives"
        return bool(self.assemblies) or bool(self.unresolved)

    @property
    def is_resolvable(self):
        "whether every partially occupied site was placed in an assembly"
        return not self.unresolved

    @property
    def n_components(self):
        """How many ordered structures this disorder describes.

        One, if there is nothing to resolve; otherwise the largest number of
        alternatives any one assembly has.
        """
        if not self.assemblies:
            return 1
        return max(len(a.groups) for a in self.assemblies)

    @property
    def site_groups(self):
        """An ``(n_sites,)`` array naming the alternative each site belongs to.

        Ordered sites are ``"."``; the rest read ``"assembly:group"``.  Handy
        for looking at what was inferred before trusting it.
        """
        labels = np.full(self.n_sites, ORDERED, dtype=object)
        for assembly in self.assemblies:
            for group, sites in assembly.groups.items():
                labels[sites] = f"{assembly.name}:{group}"
        return labels

    def component_mask(self, index):
        """A boolean mask over the asymmetric unit selecting component ``index``.

        Component 0 takes the most occupied alternative of every assembly,
        component 1 the next, and so on.  An assembly with fewer alternatives
        than ``index`` contributes its least occupied one.
        """
        if index < 0 or index >= self.n_components:
            raise IndexError(
                f"component {index} out of range for {self.n_components} components"
            )
        mask = np.ones(self.n_sites, dtype=bool)
        for assembly in self.assemblies:
            labels = assembly.group_labels
            chosen = labels[min(index, len(labels) - 1)]
            for label, sites in assembly.groups.items():
                if label != chosen:
                    mask[sites] = False
        return mask

    def __repr__(self):
        if not self.is_disordered:
            return "<Disorder: ordered>"
        state = "" if self.is_resolvable else f", {len(self.unresolved)} unresolved"
        return (
            f"<Disorder from {self.source}: {len(self.assemblies)} "
            f"assemblies, {self.n_components} components{state}>"
        )

is_disordered property

whether any site is shared between alternatives

is_resolvable property

whether every partially occupied site was placed in an assembly

n_components property

How many ordered structures this disorder describes.

One, if there is nothing to resolve; otherwise the largest number of alternatives any one assembly has.

site_groups property

An (n_sites,) array naming the alternative each site belongs to.

Ordered sites are "."; the rest read "assembly:group". Handy for looking at what was inferred before trusting it.

component_mask(index)

A boolean mask over the asymmetric unit selecting component index.

Component 0 takes the most occupied alternative of every assembly, component 1 the next, and so on. An assembly with fewer alternatives than index contributes its least occupied one.

Source code in chmpy/crystal/disorder.py
def component_mask(self, index):
    """A boolean mask over the asymmetric unit selecting component ``index``.

    Component 0 takes the most occupied alternative of every assembly,
    component 1 the next, and so on.  An assembly with fewer alternatives
    than ``index`` contributes its least occupied one.
    """
    if index < 0 or index >= self.n_components:
        raise IndexError(
            f"component {index} out of range for {self.n_components} components"
        )
    mask = np.ones(self.n_sites, dtype=bool)
    for assembly in self.assemblies:
        labels = assembly.group_labels
        chosen = labels[min(index, len(labels) - 1)]
        for label, sites in assembly.groups.items():
            if label != chosen:
                mask[sites] = False
    return mask

DisorderAssembly dataclass

One independent set of alternatives, and the sites in each.

Source code in chmpy/crystal/disorder.py
@dataclass(frozen=True)
class DisorderAssembly:
    """One independent set of alternatives, and the sites in each."""

    name: str
    #: group label -> indices of the asymmetric unit sites in that group
    groups: dict
    #: group label -> the occupancy its sites carry
    occupancies: dict

    @property
    def group_labels(self):
        "group labels, most occupied first, ties broken by label"
        return sorted(self.groups, key=lambda g: (-self.occupancies.get(g, 0.0), g))

    def __repr__(self):
        parts = ", ".join(
            f"{g}:{len(self.groups[g])} sites @ {self.occupancies.get(g, 1.0):.3g}"
            for g in self.group_labels
        )
        return f"<DisorderAssembly {self.name}: {parts}>"

group_labels property

group labels, most occupied first, ties broken by label

analyse_disorder(crystal, tolerance=0.001)

Work out how the sites of crystal are shared between alternatives.

The CIF's own _atom_site_disorder_group labels are used when the file carries them. Otherwise the occupancies are read, and then the site labels for whatever is left -- which is what resolves the common case of two alternatives at 0.5 each, where the occupancies say nothing. See the module docstring for what each can and cannot say.

Parameters:

Name Type Description Default
crystal

the crystal structure to examine

required
tolerance

how far an occupancy may stray from a whole number, or from summing to 1.0 with its partner, and still count

0.001

Returns:

Name Type Description
Disorder Disorder

the grouping, and what could not be grouped

Source code in chmpy/crystal/disorder.py
def analyse_disorder(crystal, tolerance=1e-3) -> Disorder:
    """Work out how the sites of ``crystal`` are shared between alternatives.

    The CIF's own ``_atom_site_disorder_group`` labels are used when the file
    carries them.  Otherwise the occupancies are read, and then the site
    labels for whatever is left -- which is what resolves the common case of
    two alternatives at 0.5 each, where the occupancies say nothing.  See the
    module docstring for what each can and cannot say.

    Args:
        crystal: the crystal structure to examine
        tolerance: how far an occupancy may stray from a whole number, or
            from summing to 1.0 with its partner, and still count

    Returns:
        Disorder: the grouping, and what could not be grouped
    """
    asym = crystal.asymmetric_unit
    n_sites = len(asym)
    occupancies = np.asarray(
        asym.properties.get("occupation", np.ones(n_sites)), dtype=float
    )

    cif_data = crystal.properties.get("cif_data") or {}
    group_labels = cif_data.get("atom_site_disorder_group")
    if group_labels is not None and len(group_labels) == n_sites:
        labels = [str(g) for g in group_labels]
        if any(g != ORDERED for g in labels):
            assembly_labels = cif_data.get("atom_site_disorder_assembly")
            if assembly_labels is None or len(assembly_labels) != n_sites:
                assembly_labels = [ORDERED] * n_sites
            return Disorder(
                n_sites=n_sites,
                assemblies=tuple(
                    _labelled_assemblies(
                        [str(a) for a in assembly_labels], labels, occupancies
                    )
                ),
                source="disorder_group",
                occupancies=occupancies,
            )

    assemblies, unresolved = _occupancy_assemblies(occupancies, tolerance)
    source = "occupancy" if assemblies else "none"
    if unresolved:
        source = source if assemblies else "none"
        by_label, placed = _label_assemblies(
            asym.labels, occupancies, unresolved, tolerance
        )
        if by_label:
            assemblies = assemblies + by_label
            unresolved = tuple(i for i in unresolved if i not in placed)
            source = "occupancy+label" if source == "occupancy" else "label"
    if unresolved and source == "none":
        source = "occupancy"
    return Disorder(
        n_sites=n_sites,
        assemblies=tuple(assemblies),
        unresolved=unresolved,
        source=source,
        occupancies=occupancies,
        reason=_diagnose(crystal, unresolved, occupancies) if unresolved else "",
    )

descend_for_disorder(crystal, disorder=None, max_index=4, clash_distance=CLASH_DISTANCE)

Lower the symmetry until the disorder is a choice rather than a fact.

Where alternatives are related by a symmetry operation -- a group straddling the mirror it sits on, say -- no selection of sites is ordered in the parent group, because picking one alternative picks its image too. Dropping that operation splits the orbit, and the alternatives become separate sites that can be chosen between.

The descent is to a translationengleiche subgroup, so the cell is unchanged and only the point operations are reduced. Giving up a factor of n in symmetry multiplies the size of the asymmetric unit by n: Ima2 with 8 operations and 6 sites becomes, at index 2, one of Pna2_1, Pnc2, Pma2, Pmc2_1, Pm, Cc or C2 with 4 operations and 11 sites.

Parameters:

Name Type Description Default
crystal

the structure to lower

required
disorder

a :class:Disorder to use instead of analysing the crystal

None
max_index

how much symmetry may be given up. The index of a subgroup is |G|/|H|, how many times smaller it is than the parent; since the lattice is unchanged here that is just the ratio of the number of symmetry operations, and the asymmetric unit grows by the same factor (as does Z'). So max_index=4 allows dropping down to a quarter of the operations -- for a group of 8, a subgroup of 4 or 2. The smallest index that works is used, so as little symmetry as possible is given up, and only indices dividing the order of the group exist at all.

4
clash_distance

how close two sites must be to be alternatives

CLASH_DISTANCE

Returns:

Name Type Description
Crystal

the same structure in a subgroup, its asymmetric unit

expanded, or None if no subgroup within max_index helps

Note

The subgroup is one of several symmetry equivalent choices, so the structures that come out are one ordering of many rather than the ordering. Disorder that is genuinely smeared -- alternatives that do not correspond to any discrete arrangement -- is not made resolvable by this, and is still reported as unresolved afterwards.

Source code in chmpy/crystal/disorder.py
def descend_for_disorder(
    crystal, disorder=None, max_index=4, clash_distance=CLASH_DISTANCE
):
    """Lower the symmetry until the disorder is a choice rather than a fact.

    Where alternatives are related by a symmetry operation -- a group
    straddling the mirror it sits on, say -- no selection of sites is ordered
    in the parent group, because picking one alternative picks its image too.
    Dropping that operation splits the orbit, and the alternatives become
    separate sites that can be chosen between.

    The descent is to a translationengleiche subgroup, so the cell is
    unchanged and only the point operations are reduced.  Giving up a factor
    of ``n`` in symmetry multiplies the size of the asymmetric unit by ``n``:
    Ima2 with 8 operations and 6 sites becomes, at index 2, one of Pna2_1,
    Pnc2, Pma2, Pmc2_1, Pm, Cc or C2 with 4 operations and 11 sites.

    Args:
        crystal: the structure to lower
        disorder: a :class:`Disorder` to use instead of analysing the crystal
        max_index: how much symmetry may be given up.  The index of a
            subgroup is ``|G|/|H|``, how many times smaller it is than the
            parent; since the lattice is unchanged here that is just the
            ratio of the number of symmetry operations, and the asymmetric
            unit grows by the same factor (as does Z').  So ``max_index=4``
            allows dropping down to a quarter of the operations -- for a
            group of 8, a subgroup of 4 or 2.  The smallest index that works
            is used, so as little symmetry as possible is given up, and only
            indices dividing the order of the group exist at all.
        clash_distance: how close two sites must be to be alternatives

    Returns:
        Crystal: the same structure in a subgroup, its asymmetric unit
        expanded, or None if no subgroup within ``max_index`` helps

    Note:
        The subgroup is one of several symmetry equivalent choices, so the
        structures that come out are one ordering of many rather than the
        ordering.  Disorder that is genuinely smeared -- alternatives that do
        not correspond to any discrete arrangement -- is not made resolvable
        by this, and is still reported as unresolved afterwards.
    """
    from .asymmetric_unit import AsymmetricUnit
    from .crystal import Crystal
    from .space_group_table import SpaceGroupTable
    from .subgroup import SubgroupEnumerator, expand_asymmetric_unit

    if disorder is None:
        disorder = analyse_disorder(crystal)
    if disorder.is_resolvable:
        return None

    straddled = set(_straddled_operations(crystal, disorder.unresolved, clash_distance))
    if not straddled:
        LOG.debug("no symmetry operation relates the unresolved sites")
        return None

    space_group = crystal.space_group
    symops = space_group.symmetry_operations
    enumerator = SubgroupEnumerator.from_space_group(space_group)
    chosen = None
    for index in range(2, max_index + 1):
        candidates = [
            result
            for result in enumerator.find_by_index(index)
            if straddled.isdisjoint(result.symop_indices)
        ]
        if candidates:
            # prefer one whose operations are a standard setting, so the
            # result is a fully described space group rather than a bare list
            candidates.sort(key=lambda r: (r.space_group_number is None,))
            chosen = candidates[0]
            break
    if chosen is None:
        LOG.debug("no subgroup up to index %d drops the operations", max_index)
        return None

    asym = crystal.asymmetric_unit
    # carry the parent site index along, so the expansion says which sites
    # came out of which orbit and the alternatives need not be guessed again
    tagged = AsymmetricUnit(
        asym.elements,
        asym.positions,
        labels=asym.labels,
        **{**asym.properties, _PARENT_SITE: np.arange(len(asym))},
    )
    expanded = expand_asymmetric_unit(
        tagged,
        symops,
        chosen.symop_indices,
        SpaceGroupTable.from_space_group(space_group),
    )
    new_space_group = _subgroup_space_group(
        [symops[i] for i in chosen.symop_indices], chosen
    )
    properties = dict(crystal.properties)
    properties.pop("unit_cell_atoms", None)
    properties["descended_from"] = space_group.symbol
    LOG.debug(
        "descended %s -> %s (index %d), %d -> %d sites",
        space_group.symbol,
        new_space_group.symbol,
        chosen.index,
        len(asym),
        len(expanded),
    )
    return Crystal(crystal.unit_cell, new_space_group, expanded, **properties)

disorder_components(crystal, disorder=None, strict=True, descend=False, max_index=4)

Split a disordered crystal into ordered ones.

Each returned crystal keeps every fully occupied site and one alternative from each disorder assembly, with the surviving occupancies set to 1.0. An ordered crystal comes back as a single-item list, so callers need no special case for it.

Parameters:

Name Type Description Default
crystal

the structure to resolve

required
disorder

a :class:Disorder to use instead of analysing the crystal, for when you have worked the grouping out yourself

None
strict

raise if some partially occupied site could not be placed in an assembly. Pass False to build the components anyway, which keeps those sites in every one of them.

True
descend

where alternatives are related by symmetry, lower the symmetry to a subgroup in which they are not, and resolve there. The components then have the subgroup's symmetry, and are one of several symmetry equivalent orderings -- see :func:descend_for_disorder.

False
max_index

how much symmetry descend may give up, as the index |G|/|H| of the subgroup it may drop to -- see :func:descend_for_disorder

4

Returns:

Name Type Description
list list

one ordered :class:~chmpy.crystal.crystal.Crystal per component,

list

most occupied alternatives first

Source code in chmpy/crystal/disorder.py
def disorder_components(
    crystal, disorder=None, strict=True, descend=False, max_index=4
) -> list:
    """Split a disordered crystal into ordered ones.

    Each returned crystal keeps every fully occupied site and one alternative
    from each disorder assembly, with the surviving occupancies set to 1.0.
    An ordered crystal comes back as a single-item list, so callers need no
    special case for it.

    Args:
        crystal: the structure to resolve
        disorder: a :class:`Disorder` to use instead of analysing the crystal,
            for when you have worked the grouping out yourself
        strict: raise if some partially occupied site could not be placed in
            an assembly.  Pass ``False`` to build the components anyway, which
            keeps those sites in every one of them.
        descend: where alternatives are related by symmetry, lower the
            symmetry to a subgroup in which they are not, and resolve there.
            The components then have the subgroup's symmetry, and are one of
            several symmetry equivalent orderings -- see
            :func:`descend_for_disorder`.
        max_index: how much symmetry ``descend`` may give up, as the index
            ``|G|/|H|`` of the subgroup it may drop to -- see
            :func:`descend_for_disorder`

    Returns:
        list: one ordered :class:`~chmpy.crystal.crystal.Crystal` per component,
        most occupied alternatives first
    """
    if disorder is None:
        disorder = analyse_disorder(crystal)
    if descend and not disorder.is_resolvable:
        lowered = descend_for_disorder(crystal, disorder, max_index=max_index)
        if lowered is not None:
            components = disorder_components(
                lowered, _disorder_from_split_orbits(lowered, 1e-3), strict=strict
            )
            broken = max(_overbonded_hydrogens(c) for c in components)
            if broken and strict:
                raise ValueError(
                    f"lowering the symmetry of {crystal.titl} to "
                    f"{lowered.space_group.symbol} did not resolve its disorder: "
                    f"{broken} hydrogen(s) in the result have more than one bonded "
                    "neighbour, so the alternatives are a smeared model rather "
                    "than a discrete set of arrangements and no choice among "
                    "them is a real structure. Use strict=False to get them anyway."
                )
            if broken:
                LOG.warning(
                    "%d hydrogen(s) in the descended components are overbonded; "
                    "the disorder is not a discrete set of arrangements",
                    broken,
                )
            return components
    if strict and not disorder.is_resolvable:
        labels = [str(crystal.asymmetric_unit.labels[i]) for i in disorder.unresolved]
        raise ValueError(
            f"cannot resolve the disorder in {crystal.titl}: "
            f"{len(labels)} partially occupied site(s) ({', '.join(labels[:6])}"
            f"{', ...' if len(labels) > 6 else ''}) could not be grouped into "
            f"alternatives -- {disorder.reason}. Pass a Disorder built by hand, "
            "or strict=False to keep those sites in every component."
        )
    if not disorder.is_disordered:
        return [crystal]
    return [
        _component(crystal, disorder.component_mask(i), i)
        for i in range(disorder.n_components)
    ]