Source code for tcmu.data.atom

import pathlib as pl


# read data
data_dir = pl.Path(__file__).parents[0] / "_atom_data_info"

with open(data_dir / "name.txt") as data:
    lines = data.readlines()
_element_order = [line.split(",")[1].strip() for line in lines]

with open(data_dir / "symbol.txt") as data:
    lines = data.readlines()
_symbol_order = [line.split(",")[1].strip() for line in lines]

with open(data_dir / "radius.txt") as data:
    lines = data.readlines()
_radii = {int(line.split(",")[0]): float(line.split(",")[1].strip()) for line in lines}

with open(data_dir / "ionic_radius.txt") as data:
    lines = data.readlines()
_ionic_radii = {(int(line.split(",")[0]), line.split(",")[1], int(line.split(",")[2])): float(line.split(",")[3].strip()) for line in lines}

with open(data_dir / "color.txt") as data:
    lines = data.readlines()
_colors = {int(line.split(",")[0]): [int(x.strip()) for x in line.split(",")[1:]] for line in lines}


[docs] def parse_element(val): """ Parse a str or int to an atom number. Args: val: Element name, symbol or atom number. Returns: Atom number corresponding to val. Examples: .. code-block:: python parse_element('Hydrogen') == 1 parse_element('C') == 6 parse_element(23) == 23 """ # we will assume an integer value is an atom number already if isinstance(val, int): return val # if it is not an int it should be a string if val.lower() in _element_order: # first try to get it in the element name list return _element_order.index(val.lower()) if val in _symbol_order: # alternatively try to get it in the symbol list return _symbol_order.index(val) raise KeyError(f'Element "{val}" not parsable.')
[docs] def radius(element): """ Args: element: the symbol, name or atom number of the element. See :func:`parse_element`. Return: The empirical covalent radius of an element in angstroms, up to element 96. Source: https://en.wikipedia.org/wiki/Atomic_radii_of_the_elements_(data_page) """ num = parse_element(element) return _radii.get(num)
[docs] def ionic_radius(element, charge=0, spin_state=None): """ Args: element: the symbol, name or atom number of the element. See :func:`parse_element`. charge: the charge of the ion. spin_state: some elements are classified as `high` or `low` spin. Other elements have spin-state `none`. Return: The empirical covalent radius of an element in angstroms, up to element 96. Source: https://en.wikipedia.org/wiki/Ionic_radius """ num = parse_element(element) spin_state = str(spin_state) return _ionic_radii.get((num, spin_state, charge))
[docs] def color(element, mode: str = "rgb"): """ Args: element: the symbol, name or atom number of the element. See :func:`parse_element`. mode: the type of color to return. Can be `rgb` or `hex`. Return: The standard CPK colors of the elements, up to element 109. If the mode is `rgb` returns a tuple of RGB values between `0` and `255`. Source: https://en.wikipedia.org/wiki/CPK_coloring """ num = parse_element(element) c = _colors[num] if mode == "rgb": return c if mode == "hex": return "#%02x%02x%02x" % tuple(c)
[docs] def atom_number(element): return parse_element(element)
[docs] def symbol(element): num = parse_element(element) return _symbol_order[num]
[docs] def element(element): num = parse_element(element) return _element_order[num]
if __name__ == "__main__": print(symbol(0)) print(ionic_radius(1, -1))