diff --git a/pymatgen/core/structure.py b/pymatgen/core/structure.py index d6a9e1607db..adae716b97f 100644 --- a/pymatgen/core/structure.py +++ b/pymatgen/core/structure.py @@ -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 diff --git a/tests/core/test_structure.py b/tests/core/test_structure.py index 8a1f10076a5..78435463f6f 100644 --- a/tests/core/test_structure.py +++ b/tests/core/test_structure.py @@ -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") @@ -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 + + 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")