Skip to content

Commit

Permalink
Add test for AiiDA with masses
Browse files Browse the repository at this point in the history
Fix masses creation for AiiDA StructureData to take concentration into
account.
  • Loading branch information
CasperWA committed Dec 21, 2020
1 parent 705533f commit 8148ed8
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 5 deletions.
11 changes: 6 additions & 5 deletions optimade/adapters/structures/aiida.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ def get_aiida_structure_data(optimade_structure: OptimadeStructure) -> Structure
for kind in attributes.species:
symbols = []
concentration = []
masses = []
mass = 1.0
mass = 0.0
for index, chemical_symbol in enumerate(kind.chemical_symbols):
# NOTE: The non-chemical element identifier "X" is identical to how AiiDA handles this,
# so it will be treated the same as any other true chemical identifier.
Expand All @@ -64,11 +63,13 @@ def get_aiida_structure_data(optimade_structure: OptimadeStructure) -> Structure

# AiiDA needs a definition for the mass, and for it to be > 0
# mass is OPTIONAL for OPTIMADE structures
masses.append(kind.mass[index] if kind.mass else mass)
mass = sum(masses) if masses else mass
if kind.mass:
mass += kind.concentration[index] * kind.mass[index]

structure.append_kind(
Kind(symbols=symbols, weights=concentration, mass=mass, name=kind.name)
Kind(
symbols=symbols, weights=concentration, mass=mass or 1.0, name=kind.name
)
)

# Add Sites
Expand Down
71 changes: 71 additions & 0 deletions tests/adapters/structures/special_species.json
Original file line number Diff line number Diff line change
Expand Up @@ -208,5 +208,76 @@
]
}
}
},
{
"id": "mpf_1",
"type": "structures",
"attributes": {
"last_modified": "2019-06-08T05:13:37.331000",
"elements": [
"Ac", "O"
],
"nelements": 1,
"elements_ratios": [
1
],
"chemical_formula_descriptive": "AcO",
"chemical_formula_reduced": "AcO",
"chemical_formula_anonymous": "AB",
"dimension_types": [
1,
1,
1
],
"nperiodic_dimensions": 3,
"lattice_vectors": [
[
1.2503264826932692,
0,
0
],
[
0,
9.888509716321765,
0
],
[
0,
0,
0.2972637673241818
]
],
"cartesian_site_positions": [
[
0.17570227444196573,
0.17570227444196573,
0.17570227444196573
]
],
"nsites": 1,
"species_at_sites": [
"AcO"
],
"species": [
{
"name": "AcO",
"chemical_symbols": ["Ac", "O"],
"concentration": [0.9, 0.1],
"mass": [227.0, 15.999]
}
],
"structure_features": ["disorder"],
"_exmpl_chemsys": "AcO"
},
"relationships": {
"references": {
"data": [
{
"id": "dijkstra1968",
"type": "references"
}
]
}
}
}
]
21 changes: 21 additions & 0 deletions tests/adapters/structures/test_aiida.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def test_special_species(SPECIAL_SPECIES_STRUCTURES):

assert isinstance(aiida_structure, StructureData)

# Test species.chemical_symbols
if "vacancy" in structure.attributes.species[0].chemical_symbols:
assert aiida_structure.has_vacancies
assert not aiida_structure.is_alloy
Expand All @@ -53,3 +54,23 @@ def test_special_species(SPECIAL_SPECIES_STRUCTURES):
else:
assert not aiida_structure.has_vacancies
assert not aiida_structure.is_alloy

# Test species.mass
if structure.attributes.species[0].mass:
if len(structure.attributes.species[0].mass) > 1:
assert aiida_structure.kinds[0].mass == sum(
[
conc * mass
for conc, mass in zip(
structure.attributes.species[0].concentration,
structure.attributes.species[0].mass,
)
]
)
else:
assert (
aiida_structure.kinds[0].mass
== structure.attributes.species[0].mass[0]
)
else:
assert aiida_structure.kinds[0].mass == 1.0

0 comments on commit 8148ed8

Please sign in to comment.