Chemical formula type¶
Warning
This module is preliminary! API may change or disappear in the future.
-
class
ase.formula.Formula(formula: str = '', *, strict: bool = False, _tree: Union[str, Tuple[Tree, int], List[Tree]] = None, _count: Dict[str, int] = None)[source]¶ Chemical formula object.
Parameters: - formula (str) – Text string representation of formula. Examples:
'6CO2','30Cu+2CO','Pt(CO)6'. - strict (bool) – Only allow real chemical symbols.
Examples
>>> from ase.formula import Formula >>> w = Formula('H2O') >>> w.count() {'H': 2, 'O': 1} >>> 'H' in w True >>> w == 'HOH' True >>> f'{w:latex}' 'H$_{2}$O' >>> w.format('latex') 'H$_{2}$O' >>> divmod(6 * w + 'Cu', w) (6, Formula('Cu'))
Raises: ValueError– on malformed formula-
count() → Dict[str, int][source]¶ Return dictionary mapping chemical symbol to number of atoms.
Example
>>> Formula('H2O').count() {'H': 2, 'O': 1}
-
reduce() → Tuple[ase.formula.Formula, int][source]¶ Reduce formula.
Returns: - formula (Formula) – Reduced formula.
- n (int) – Number of reduced formula units.
Example
>>> Formula('2H2O').reduce() (Formula('H2O'), 2)
-
stoichiometry() → Tuple[ase.formula.Formula, ase.formula.Formula, int][source]¶ Reduce to unique stoichiomerty using “chemical symbols” A, B, C, …
Examples
>>> Formula('CO2').stoichiometry() (Formula('AB2'), Formula('CO2'), 1) >>> Formula('(H2O)4').stoichiometry() (Formula('AB2'), Formula('OH2'), 4)
-
format(fmt: str = '') → str[source]¶ Format formula as string.
Formats:
'hill': alphabetically ordered with C and H first'metal': alphabetically ordered with metals first'abc': count ordered first then alphabetically ordered'latex': LaTeX representation'html': HTML representation'rest': reStructuredText representation
Example
>>> Formula('H2O').format('html') 'H<sub>2</sub>O'
-
__format__(fmt: str) → str[source]¶ Format Formula as str.
Possible formats:
'hill','metal','abc','latex','html','rest'.Example
>>> f = Formula('OH2') >>> '{f}, {f:hill}, {f:latex}'.format(f=f) 'OH2, H2O, OH$_{2}$'
-
__contains__(f: Union[str, Formula]) → bool[source]¶ Check if formula contains chemical symbols in f.
Type of f must be str or Formula.
Example
>>> 'OH' in Formula('H2O') True
- formula (str) – Text string representation of formula. Examples: