Skip to content

Commit

Permalink
Support Pymatgen Species object in aims (#4118)
Browse files Browse the repository at this point in the history
* Support pymatgen Species in FHI-aims control.in

* Simplify building geometry.in file

* A bug fixed
  • Loading branch information
ansobolev authored Oct 22, 2024
1 parent c611280 commit f7d964c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 30 deletions.
40 changes: 16 additions & 24 deletions src/pymatgen/io/aims/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,28 +148,23 @@ def from_structure(cls, structure: Structure | Molecule) -> Self:
for lv in structure.lattice.matrix:
content_lines.append(f"lattice_vector {lv[0]: .12e} {lv[1]: .12e} {lv[2]: .12e}")

charges = structure.site_properties.get("charge", np.zeros(structure.num_sites))
magmoms = structure.site_properties.get("magmom", [None] * structure.num_sites)
velocities = structure.site_properties.get("velocity", [None for _ in structure.species])

for species, coord, charge, magmom, v in zip(
structure.species, structure.cart_coords, charges, magmoms, velocities, strict=True
):
if isinstance(species, Element):
spin = magmom
element = species
else:
spin = species.spin
element = species.element
if magmom is not None and magmom != spin:
for site in structure:
element = site.species_string
charge = site.properties.get("charge", 0)
spin = site.properties.get("magmom", None)
coord = site.coords
v = site.properties.get("velocity", [0.0, 0.0, 0.0])
if isinstance(site.specie, Species) and site.specie.spin is not None:
if spin is not None and spin != site.specie.spin:
raise ValueError("species.spin and magnetic moments don't agree. Please only define one")
spin = site.specie.spin

content_lines.append(f"atom {coord[0]: .12e} {coord[1]: .12e} {coord[2]: .12e} {element}")
if charge != 0:
content_lines.append(f" initial_charge {charge:.12e}")
if (spin is not None) and (spin != 0):
content_lines.append(f" initial_moment {spin:.12e}")
if v is not None and any(v_i != 0.0 for v_i in v):
if (v is not None) and any(v_i != 0.0 for v_i in v):
content_lines.append(f" velocity {' '.join([f'{v_i:.12e}' for v_i in v])}")

return cls(_content="\n".join(content_lines), _structure=structure)
Expand Down Expand Up @@ -865,15 +860,12 @@ def from_structure(
"""Initialize species defaults from a structure."""
labels = []
elements = {}
for label, el in sorted(zip(struct.labels, struct.species, strict=True)):
if isinstance(el, Species):
el = el.element
if (label is None) or (el is None):
raise ValueError("Something is terribly wrong with the structure")
if label not in labels:
labels.append(label)
elements[label] = el.name
return SpeciesDefaults(labels, basis_set, species_dir=species_dir, elements=elements)
for site in struct:
el = site.specie
if site.species_string not in labels:
labels.append(site.species_string)
elements[site.species_string] = el.name
return SpeciesDefaults(sorted(labels), basis_set, species_dir=species_dir, elements=elements)

def to_dict(self):
"""Dictionary representation of the species' defaults"""
Expand Down
12 changes: 6 additions & 6 deletions tests/io/aims/test_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from monty.json import MontyDecoder, MontyEncoder
from numpy.testing import assert_allclose

from pymatgen.core import SETTINGS, Lattice, Species, Structure
from pymatgen.core import SETTINGS, Composition, Lattice, Species, Structure
from pymatgen.io.aims.inputs import (
ALLOWED_AIMS_CUBE_TYPES,
ALLOWED_AIMS_CUBE_TYPES_STATE,
Expand Down Expand Up @@ -297,9 +297,9 @@ def test_species_defaults(monkeypatch: pytest.MonkeyPatch):
]
assert species_defaults.elements == {"Si": "Si"}

si.relabel_sites()
si[0].species = Composition({"Si0+": 1}, strict=True)
species_defaults = SpeciesDefaults.from_structure(si, "light")
assert species_defaults.labels == ["Si_1", "Si_2"]
assert species_defaults.elements == {"Si_1": "Si", "Si_2": "Si"}
assert "Si_1" in str(species_defaults)
assert "Si_2" in str(species_defaults)
assert species_defaults.labels == ["Si", "Si0+"]
assert species_defaults.elements == {"Si": "Si", "Si0+": "Si"}
assert "Si0+" in str(species_defaults)
assert "Si" in str(species_defaults)

0 comments on commit f7d964c

Please sign in to comment.