Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for carbon in elements for Hill #586

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 6 additions & 5 deletions optimade/models/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -774,11 +774,12 @@ def check_ordered_formula(cls, v, field):
expected_elements = sorted(elements)

if field.name == "chemical_formula_hill":
# Make sure C is first and H is second.
for elem in ("H", "C"):
if elem in expected_elements:
expected_elements.pop(expected_elements.index(elem))
expected_elements.insert(0, elem)
# Make sure C is first (and H is second, if present along with C).
if "C" in expected_elements:
expected_elements = sorted(
expected_elements,
key=lambda elem: {"C": "0", "H": "1"}.get(elem, elem),
)

if any(elem not in CHEMICAL_SYMBOLS for elem in elements):
raise ValueError(
Expand Down
4 changes: 2 additions & 2 deletions tests/models/test_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ def test_bad_structures(bad_structures, mapper):
"Elements in 'chemical_formula_hill' must appear in Hill order: ['Ge', 'Si'] not ['Si', 'Ge']",
),
(
{"chemical_formula_hill": "GeHSi"},
"Elements in 'chemical_formula_hill' must appear in Hill order: ['H', 'Ge', 'Si'] not ['Ge', 'H', 'Si']",
{"chemical_formula_hill": "HGeSi"},
"Elements in 'chemical_formula_hill' must appear in Hill order: ['Ge', 'H', 'Si'] not ['H', 'Ge', 'Si']",
),
(
{"chemical_formula_hill": "CGeHSi"},
Expand Down