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

Add a from_ase_atoms() method to Structure #3812

Merged
merged 4 commits into from
May 7, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions pymatgen/core/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,19 @@ def to_ase_atoms(self, **kwargs) -> Atoms:

return AseAtomsAdaptor.get_atoms(self, **kwargs)

def from_ase_atoms(self, **kwargs) -> Structure:
"""Convert ase.Atoms to pymatgen Structure.

Args:
kwargs: Passed to AseAtomsAdaptor.get_structure.

Returns:
Structure
"""
from pymatgen.io.ase import AseAtomsAdaptor

return AseAtomsAdaptor.get_structure(self, **kwargs)


class IStructure(SiteCollection, MSONable):
"""Basic immutable Structure object with periodicity. Essentially a sequence
Expand Down
9 changes: 8 additions & 1 deletion tests/core/test_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from ase.calculators.calculator import Calculator
from ase.calculators.emt import EMT
except ImportError:
ase = None
ase = Atoms = Calculator = EMT = None


enum_cmd = which("enum.x") or which("multienum.x")
Expand Down Expand Up @@ -1848,6 +1848,13 @@ def test_to_ase_atoms(self):
assert isinstance(atoms, Atoms)
assert len(atoms) == len(self.struct)
assert AseAtomsAdaptor.get_structure(atoms) == self.struct
assert Structure.from_ase_atoms(atoms) == self.struct
Andrew-S-Rosen marked this conversation as resolved.
Show resolved Hide resolved

labeled_atoms = self.labeled_structure.to_ase_atoms()
assert Structure.from_ase_atoms(labeled_atoms) == self.labeled_structure

with pytest.raises(ValueError, match="ASE Atoms only supports ordered structures"):
self.disordered.to_ase_atoms()

def test_struct_with_isotope(self):
struct = Structure.from_file(f"{VASP_IN_DIR}/POSCAR_LiFePO4")
Expand Down