Esempio n. 1
0
    def __init__(self, name):
        self.raw = name

        # element symbol and atomic weight
        if name == "p":
            self.el = "H"
            self.A = 1
        elif name == "d":
            self.el = "H"
            self.A = 2
        elif name == "t":
            self.el = "H"
            self.A = 3
        elif name == "n":
            self.el = "n"
            self.A = 1
        else:
            e = re.match("([a-zA-Z]*)(\d*)", name)
            self.el = e.group(1).title()  # chemical symbol
            self.A = int(e.group(2))

        # atomic number comes from periodtable
        i = elements.isotope("{}-{}".format(self.A, self.el))
        self.Z = i.number
        self.N = self.A - self.Z

        # latex formatted style
        self.pretty = r"{{}}^{{{}}}\mathrm{{{}}}".format(self.A, self.el)
Esempio n. 2
0
def molecule_mass(M: str, as_amu=False) -> float:
    atoms = M.split(" ")
    m = 0
    for ai in atoms:
        match = re.match(r'([0-9]*)([A-Z][a-z]?)([0-9]*)', ai)
        if match is None:
            raise Exception(f"Invalid atom '{ai}'")
        element = elements.isotope(match.group(2))
        if match.group(1) == "":
            mi = element.mass
        else:
            mi = element[int(match.group(1))].mass
        m += mi*(int(match.group(3)) if match.group(3) != "" else 1)
    return m if as_amu else m*amu2kg
Esempio n. 3
0
def atomic_mass(atom_symbol, isotope=''):
   """atomic mass for atom_symbol, return most abundant atomic mass
      if isotope not given.
   """ 
   elem = elements.isotope(atom_symbol)
   if isotope != '': 
        mass = elem[int(isotope)].mass   # have isotopic mass
   else:
       # most abundant isotopic mass
       elem_abund = []
       for iso in elem.isotopes:
           elem_abund.append(elem[iso].abundance)
       elem_abund = np.asarray(elem_abund)
       mass = elem[elem.isotopes[elem_abund.argmax()]].mass
   return mass
Esempio n. 4
0
def test():
    # Check that we can access element properties
    assert H.name == "hydrogen"
    assert H.symbol == "H"
    assert H.number == 1
    assert helium.symbol == 'He'

    # Check that isotopes work and produce the correct strings and symbols
    O.add_isotope(18)
    assert H[2].symbol == 'D'
    assert H[3].symbol == 'T'
    assert O[18].symbol == 'O'
    assert str(H[2]) == 'D'
    assert str(H[3]) == 'T'
    assert str(O[18]) == '18-O'
    try:
        Fe[12]
    except KeyError as msg:
        assert msg.args[0] == '12 is not an isotope of Fe'

    # Check that "for el in elements" works and for iso in el works
    els = tuple(el for el in elements)
    assert els[0].number == 0
    assert els[1].number == 1
    isotopes = tuple(iso for iso in O)
    assert isotopes[0].isotope == 12  # 12 is the first oxygen isotope listed

    # Check that table lookup works and fails appropriately
    Fe.add_isotope(56)
    assert elements.symbol('Fe') == Fe
    assert elements.name('iron') == Fe
    assert elements.isotope('Fe') == Fe
    assert elements.isotope('56-Fe') == Fe[56]
    assert elements.isotope('D') == H[2]
    try:
        elements.symbol('Qu')
    except ValueError as msg:
        assert str(msg) == "unknown element Qu"
    try:
        elements.name('Qu')
    except ValueError as msg:
        assert str(msg) == "unknown element Qu"
    try:
        elements.isotope('Qu')
    except ValueError as msg:
        assert str(msg) == "unknown element Qu"
    try:
        elements.isotope('4-D')
    except ValueError as msg:
        assert str(msg) == "unknown element 4-D"

    # Check that ions work
    assert Fe.ion[2].charge == 2
    assert Fe.ions == (-4, -2, -1, 1, 2, 3, 4, 5, 6, 7)
    assert str(Fe.ion[2]) == "Fe{2+}"
    assert str(O.ion[-2]) == "O{2-}"
    try:
        Fe.ion[-3]
        raise Exception("accepts invalid ions")
    except ValueError as msg:
        assert str(msg) == "-3 is not a valid charge for Fe"

    assert data_files()[0][0] == "periodictable-data/xsf"
        Fe[12]
    except KeyError,msg:
        assert msg.args[0] == '12 is not an isotope of Fe'

    # Check that "for el in elements" works and for iso in el works
    els = tuple(el for el in elements)
    assert els[0].number == 0
    assert els[1].number == 1
    isotopes = tuple(iso for iso in O)
    assert isotopes[0].isotope == 12  # 12 is the first oxygen isotope listed

    # Check that table lookup works and fails appropriately
    Fe.add_isotope(56)
    assert elements.symbol('Fe') == Fe
    assert elements.name('iron') == Fe
    assert elements.isotope('Fe') == Fe
    assert elements.isotope('56-Fe') == Fe[56]
    assert elements.isotope('D') == H[2]
    try:
        elements.symbol('Qu')
    except ValueError,msg:
        assert str(msg) == "unknown element Qu"
    try:
        elements.name('Qu')
    except ValueError,msg:
        assert str(msg) == "unknown element Qu"
    try:
        elements.isotope('Qu')
    except ValueError,msg:
        assert str(msg) == "unknown element Qu"
    try:
Esempio n. 6
0
def test():
    # Check that we can access element properties
    assert H.name == "hydrogen"
    assert H.symbol == "H"
    assert H.number == 1
    assert helium.symbol == 'He'

    # Check that isotopes work and produce the correct strings and symbols
    O.add_isotope(18)
    assert H[2].symbol == 'D'
    assert H[3].symbol == 'T'
    assert O[18].symbol == 'O'
    assert str(H[2]) == 'D'
    assert str(H[3]) == 'T'
    assert str(O[18]) == '18-O'
    try:
        Fe[12]
    except KeyError as msg:
        assert msg.args[0] == '12 is not an isotope of Fe'

    # Check that "for el in elements" works and for iso in el works
    els = tuple(el for el in elements)
    assert els[0].number == 0
    assert els[1].number == 1
    isotopes = tuple(iso for iso in O)
    assert isotopes[0].isotope == 12  # 12 is the first oxygen isotope listed

    # Check that table lookup works and fails appropriately
    Fe.add_isotope(56)
    assert elements.symbol('Fe') == Fe
    assert elements.name('iron') == Fe
    assert elements.isotope('Fe') == Fe
    assert elements.isotope('56-Fe') == Fe[56]
    assert elements.isotope('D') == H[2]
    try:
        elements.symbol('Qu')
    except ValueError as msg:
        assert str(msg) == "unknown element Qu"
    try:
        elements.name('Qu')
    except ValueError as msg:
        assert str(msg) == "unknown element Qu"
    try:
        elements.isotope('Qu')
    except ValueError as msg:
        assert str(msg) == "unknown element Qu"
    try:
        elements.isotope('4-D')
    except ValueError as msg:
        assert str(msg) == "unknown element 4-D"

    # Check that ions work
    assert Fe.ion[2].charge == 2
    assert Fe.ions == (2, 3)
    assert str(Fe.ion[2]) == "Fe{2+}"
    assert str(O.ion[-2]) == "O{2-}"
    try:
        Fe.ion[1]
        raise Exception("accepts invalid ions")
    except ValueError as msg:
        assert str(msg) == "1 is not a valid charge for Fe"

    assert data_files()[0][0] == "periodictable-data/xsf"