Skip to content

strain

Cell deformation, and which deformations a structure's symmetry allows.

A cell degree of freedom is an amplitude along a symmetry-adapted strain direction, not a lattice parameter and not a raw Voigt component. That choice is what keeps a cell optimisation inside the structure's space group.

The strains a crystal may undergo without losing its space group are those invariant under every rotation R in its point group, eps = R eps R^T. Those form a linear subspace of the symmetric 3x3 tensors, and this module builds an orthonormal basis for it by projection:

P = (1 / |G|) sum_R  T(R),        T(R): eps -> R eps R^T

P is the projector onto the invariant subspace, and its eigenvectors of eigenvalue 1 are the basis. Nothing here consults a lattice type or assumes the cell is in a standard setting: a rhombohedral cell with its three-fold along a + b + c and one in the hexagonal setting both come out right, because both are described by their own rotation matrices.

Mapping one degree of freedom to one Voigt component -- the obvious scheme -- cannot express these subspaces at all. Tetragonal requires eps_xx = eps_yy, cubic requires all three equal, and rhombohedral requires an isotropic part plus a uniaxial one along an axis that need not be a Cartesian direction. Left uncoupled, a cell optimisation walks the metric out of its lattice type within a few steps.

Strain is the symmetric tensor eps in r -> (I + eps) r, and because every inner product here is the Frobenius one, no engineering factor of two on the shear components ever appears.

cartesian_rotations(crystal)

The Cartesian point operations of a crystal's space group.

A symmetry operation acts on fractional column coordinates as R x + t. With the lattice vectors stored as the rows of A, a Cartesian column vector is r = A^T x, so the same operation in Cartesian space is A^T R A^-T.

Parameters:

Name Type Description Default
crystal

a chmpy.crystal.Crystal

required

Returns:

Type Description
ndarray

(M, 3, 3) Cartesian rotation matrices, duplicates removed

Source code in chmpy/opt/strain.py
def cartesian_rotations(crystal) -> np.ndarray:
    """The Cartesian point operations of a crystal's space group.

    A symmetry operation acts on fractional column coordinates as `R x + t`.
    With the lattice vectors stored as the rows of `A`, a Cartesian column
    vector is `r = A^T x`, so the same operation in Cartesian space is
    `A^T R A^-T`.

    Args:
        crystal: a `chmpy.crystal.Crystal`

    Returns:
        (M, 3, 3) Cartesian rotation matrices, duplicates removed
    """
    direct = np.asarray(crystal.unit_cell.direct).T
    inverse = np.linalg.inv(direct)
    seen = {}
    for operation in crystal.space_group.symmetry_operations:
        cartesian = direct @ np.asarray(operation.rotation, dtype=float) @ inverse
        seen[np.round(cartesian, 9).tobytes()] = cartesian
    rotations = np.array(list(seen.values()))
    _check_orthogonal(rotations, crystal)
    return rotations

deformation(amplitudes, basis)

The deformation matrix I + eps for amplitudes along a strain basis.

Parameters:

Name Type Description Default
amplitudes

(k,) amplitudes

required
basis

(k, 3, 3) strain basis

required

Returns:

Type Description
ndarray

(3, 3) deformation to apply as cell @ F and positions @ F

Source code in chmpy/opt/strain.py
def deformation(amplitudes, basis) -> np.ndarray:
    """The deformation matrix `I + eps` for amplitudes along a strain basis.

    Args:
        amplitudes: (k,) amplitudes
        basis: (k, 3, 3) strain basis

    Returns:
        (3, 3) deformation to apply as `cell @ F` and `positions @ F`
    """
    return np.eye(3) + np.einsum("k,kab->ab", np.asarray(amplitudes), basis)

elastic_from_voigt(voigt)

A Voigt 6x6 elastic matrix in this module's orthonormal basis.

Source code in chmpy/opt/strain.py
def elastic_from_voigt(voigt) -> np.ndarray:
    """A Voigt 6x6 elastic matrix in this module's orthonormal basis."""
    return np.asarray(voigt, dtype=float) * np.outer(VOIGT_SCALE, VOIGT_SCALE)

elastic_to_voigt(tensor)

An orthonormal-basis elastic matrix as an engineering Voigt 6x6.

Source code in chmpy/opt/strain.py
def elastic_to_voigt(tensor) -> np.ndarray:
    """An orthonormal-basis elastic matrix as an engineering Voigt 6x6."""
    return np.asarray(tensor, dtype=float) / np.outer(VOIGT_SCALE, VOIGT_SCALE)

from_vector(vector)

The symmetric (3, 3) tensor with these coordinates in SYMMETRIC_BASIS.

Source code in chmpy/opt/strain.py
def from_vector(vector) -> np.ndarray:
    """The symmetric (3, 3) tensor with these coordinates in `SYMMETRIC_BASIS`."""
    return np.einsum("...k,kab->...ab", np.asarray(vector), SYMMETRIC_BASIS)

invariant_elastic_basis(rotations, tolerance=1e-08)

An orthonormal basis for the elastic tensors a point group allows.

Parameters:

Name Type Description Default
rotations

(M, 3, 3) Cartesian point operations. An empty list gives the full 21-dimensional triclinic space.

required
tolerance float

how far an eigenvalue of the projector may sit from 1

1e-08

Returns:

Type Description
ndarray

(k, 6, 6) orthonormal symmetric matrices, in this module's basis, where

ndarray

k is the number of independent elastic constants

Source code in chmpy/opt/strain.py
def invariant_elastic_basis(rotations, tolerance: float = 1e-8) -> np.ndarray:
    """An orthonormal basis for the elastic tensors a point group allows.

    Args:
        rotations: (M, 3, 3) Cartesian point operations. An empty list gives
            the full 21-dimensional triclinic space.
        tolerance: how far an eigenvalue of the projector may sit from 1

    Returns:
        (k, 6, 6) orthonormal symmetric matrices, in this module's basis, where
        `k` is the number of independent elastic constants
    """
    space = _symmetric_matrix_basis(6)
    rotations = np.asarray(rotations, dtype=float).reshape(-1, 3, 3)
    if len(rotations) == 0:
        return space

    actions = np.array([rotation_action(rotation) for rotation in rotations])
    # <B_a, T B_b T^T> averaged over the group
    transformed = np.einsum("rij,bjk,rlk->rbil", actions, space, actions)
    projector = np.einsum("aij,rbij->ab", space, transformed) / len(rotations)
    projector = 0.5 * (projector + projector.T)

    values, vectors = np.linalg.eigh(projector)
    kept = _tidy(vectors[:, values > 1.0 - tolerance])
    return np.einsum("ak,aij->kij", kept, space)

invariant_strain_basis(rotations, tolerance=1e-08)

An orthonormal basis for the strains a set of rotations leaves invariant.

Parameters:

Name Type Description Default
rotations

(M, 3, 3) Cartesian point operations of the structure. Pass an empty list for no symmetry, which gives all six directions.

required
tolerance float

how far an eigenvalue of the projector may sit from 1

1e-08

Returns:

Type Description
ndarray

(k, 3, 3) orthonormal symmetric tensors spanning the allowed strains,

ndarray

with k between 1 (cubic) and 6 (triclinic)

Source code in chmpy/opt/strain.py
def invariant_strain_basis(rotations, tolerance: float = 1e-8) -> np.ndarray:
    """An orthonormal basis for the strains a set of rotations leaves invariant.

    Args:
        rotations: (M, 3, 3) Cartesian point operations of the structure. Pass
            an empty list for no symmetry, which gives all six directions.
        tolerance: how far an eigenvalue of the projector may sit from 1

    Returns:
        (k, 3, 3) orthonormal symmetric tensors spanning the allowed strains,
        with `k` between 1 (cubic) and 6 (triclinic)
    """
    rotations = np.asarray(rotations, dtype=float).reshape(-1, 3, 3)
    if len(rotations) == 0:
        return SYMMETRIC_BASIS.copy()

    projector = np.mean([rotation_action(r) for r in rotations], axis=0)
    projector = 0.5 * (projector + projector.T)
    values, vectors = np.linalg.eigh(projector)
    kept = vectors[:, values > 1.0 - tolerance]
    if kept.shape[1] == 0:
        raise ValueError(
            "no strain is invariant under these operations, which cannot happen "
            "for a genuine point group -- check that the rotations are Cartesian"
        )
    return from_vector(_tidy(kept).T)

project_elastic(tensor, basis)

The part of an elastic tensor its symmetry allows.

Finite differences and a finite atomic relaxation leave a little of the tensor outside the invariant subspace. Projecting it back is not cosmetic: the components that symmetry forbids are pure noise, and leaving them in puts a spurious anisotropy into every modulus derived from the tensor.

Parameters:

Name Type Description Default
tensor

(6, 6) in this module's orthonormal basis

required
basis

(k, 6, 6) from invariant_elastic_basis

required

Returns:

Type Description
ndarray

(6, 6) projected tensor

Source code in chmpy/opt/strain.py
def project_elastic(tensor, basis) -> np.ndarray:
    """The part of an elastic tensor its symmetry allows.

    Finite differences and a finite atomic relaxation leave a little of the
    tensor outside the invariant subspace. Projecting it back is not cosmetic:
    the components that symmetry forbids are pure noise, and leaving them in
    puts a spurious anisotropy into every modulus derived from the tensor.

    Args:
        tensor: (6, 6) in this module's orthonormal basis
        basis: (k, 6, 6) from `invariant_elastic_basis`

    Returns:
        (6, 6) projected tensor
    """
    tensor = np.asarray(tensor, dtype=float)
    tensor = 0.5 * (tensor + tensor.T)
    return np.einsum("k,kij->ij", np.einsum("kij,ij->k", basis, tensor), basis)

rotation_action(rotation)

The (6, 6) matrix of eps -> R eps R^T in SYMMETRIC_BASIS.

Source code in chmpy/opt/strain.py
def rotation_action(rotation) -> np.ndarray:
    """The (6, 6) matrix of `eps -> R eps R^T` in `SYMMETRIC_BASIS`."""
    rotation = np.asarray(rotation, dtype=float)
    rotated = np.einsum("ac,kcd,bd->kab", rotation, SYMMETRIC_BASIS, rotation)
    return to_vector(rotated).T

step_fraction(amplitudes, step, basis)

The largest t in [0, 1] keeping eps(q + t * dq) inside the bounds.

The whole step is scaled by one factor rather than each component being clamped separately. Clamping per component breaks a trust region twice over: the geometry evaluated is not the step the predicted reduction was computed for, and the unclamped step is still handed to the curvature update as the secant. It would also push the strain straight out of the symmetry subspace the basis exists to define.

Source code in chmpy/opt/strain.py
def step_fraction(amplitudes, step, basis) -> float:
    """The largest `t` in [0, 1] keeping `eps(q + t * dq)` inside the bounds.

    The whole step is scaled by one factor rather than each component being
    clamped separately. Clamping per component breaks a trust region twice
    over: the geometry evaluated is not the step the predicted reduction was
    computed for, and the unclamped step is still handed to the curvature
    update as the secant. It would also push the strain straight out of the
    symmetry subspace the basis exists to define.
    """
    current = strain_of(amplitudes, basis)
    change = strain_of(step, basis)
    fraction = 1.0
    for a in range(3):
        for b in range(a, 3):
            limit = MAX_NORMAL_STRAIN if a == b else MAX_SHEAR_STRAIN
            here, delta = current[a, b], change[a, b]
            if delta == 0.0:
                continue
            target = limit if delta > 0 else -limit
            if abs(here + delta) > limit:
                fraction = min(fraction, (target - here) / delta)
    return max(fraction, 0.0)

strain_of(amplitudes, basis)

The strain tensor eps for these amplitudes.

Source code in chmpy/opt/strain.py
def strain_of(amplitudes, basis) -> np.ndarray:
    """The strain tensor `eps` for these amplitudes."""
    return np.einsum("k,kab->ab", np.asarray(amplitudes), basis)

symmetric_basis()

An orthonormal basis of the symmetric 3x3 tensors, under Frobenius.

Returns:

Type Description
ndarray

(6, 3, 3) with the three normal directions first, then the shears

Source code in chmpy/opt/strain.py
def symmetric_basis() -> np.ndarray:
    """An orthonormal basis of the symmetric 3x3 tensors, under Frobenius.

    Returns:
        (6, 3, 3) with the three normal directions first, then the shears
    """
    basis = np.zeros((6, 3, 3))
    for k in range(3):
        basis[k, k, k] = 1.0
    root_half = 1.0 / np.sqrt(2.0)
    for k, (a, b) in enumerate(((1, 2), (0, 2), (0, 1)), start=3):
        basis[k, a, b] = basis[k, b, a] = root_half
    return basis

to_vector(tensor)

A symmetric (3, 3) tensor as its (6,) coordinates in SYMMETRIC_BASIS.

Unlike engineering Voigt notation this is an isometry: lengths and inner products are the same in both representations.

Source code in chmpy/opt/strain.py
def to_vector(tensor) -> np.ndarray:
    """A symmetric (3, 3) tensor as its (6,) coordinates in `SYMMETRIC_BASIS`.

    Unlike engineering Voigt notation this is an isometry: lengths and inner
    products are the same in both representations.
    """
    return np.einsum("kab,...ab->...k", SYMMETRIC_BASIS, np.asarray(tensor))