Utility functions

periodictable.util

Helper functions

periodictable.util.cell_volume(a=None, b=None, c=None, alpha=None, beta=None, gamma=None)

Compute cell volume from lattice parameters.

Parameters:
a, b, c
: float | Å

Lattice spacings. a is required. b and c default to a.

alpha, beta, gamma
: float | °

Lattice angles. alpha defaults to 90°. beta and gamma default to alpha.

Returns:
V
: float | Å3

Cell volume

Raises:

TypeError : missing or invalid parameters

The following formula works for all lattice types:

\[V = a b c \sqrt{1 - \cos^2 \alpha - \cos^2 \beta - \cos^2 \gamma + 2 \cos \alpha \cos \beta \cos \gamma}\]
periodictable.util.require_keywords(function)

Decorator which forces all keyword arguments to the function to be explicitly named.

For example:

>>> @require_keywords
... def fn(a, b, c=3): pass
>>> fn(1, 2, 3)
Traceback (most recent call last):
...
TypeError: name=value required for c
>>> fn(1, 2, c=6)
>>> fn(b=1, a=2, c=6)

Variable arguments are not currently supported:

>>> @require_keywords
... def fn(a, b, c=6, *args, **kw): pass
Traceback (most recent call last):
...
NotImplementedError: only named arguments for now

Note

The call signature is not preserved.

We can’t preserve the function signature for the call since the only way we can count the number of non-keyword arguments is to use the *args, **kw call style. Python 3+ provides the ‘*’ call signature element which will force all keywords after ‘*’ to be named.