1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
def test_atoms_formula():
from ase.build import fcc111, add_adsorbate
import warnings
# some random system
slab = fcc111('Al', size=(2, 2, 3))
add_adsorbate(slab, 'C', 2.5, 'bridge')
add_adsorbate(slab, 'C', 3.5, 'bridge')
add_adsorbate(slab, 'H', 1.5, 'ontop')
add_adsorbate(slab, 'H', 1.5, 'fcc')
add_adsorbate(slab, 'C', 0.5, 'bridge')
add_adsorbate(slab, 'C', 1.5, 'bridge')
assert slab.get_chemical_formula(mode='hill') == 'C4H2Al12'
assert slab.get_chemical_formula(mode='metal') == 'Al12C4H2'
all_str = 'Al' * 12 + 'C' * 2 + 'H' * 2 + 'C' * 2
assert slab.get_chemical_formula(mode='all') == all_str
reduce_str = 'Al12C2H2C2'
assert slab.get_chemical_formula(mode='reduce') == reduce_str
assert slab.get_chemical_formula(mode='hill', empirical=True) == 'C2HAl6'
assert slab.get_chemical_formula(mode='metal', empirical=True) == 'Al6C2H'
# check for warning if empirical formula is not available
for mode in ('all', 'reduce'):
with warnings.catch_warnings(record=True) as w:
# Cause all warnings to always be triggered.
warnings.simplefilter('always')
# Trigger a warning.
slab.get_chemical_formula(mode=mode, empirical=True)
# Verify some things
assert len(w) == 1
assert issubclass(w[-1].category, Warning)
|