Skip to content

Commit

Permalink
move undeform_structure function to core utils (#999)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsmathis authored May 2, 2024
1 parent 2932ae8 commit 5b5ca7d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 32 deletions.
33 changes: 1 addition & 32 deletions emmet-builders/emmet/builders/vasp/materials.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,9 @@
from maggma.builders import Builder
from maggma.stores import Store
from maggma.utils import grouper
from pymatgen.analysis.elasticity.strain import Deformation
from pymatgen.core.structure import Structure
from pymatgen.transformations.standard_transformations import (
DeformStructureTransformation,
)

from emmet.builders.settings import EmmetBuildSettings
from emmet.core.utils import group_structures, jsanitize
from emmet.core.utils import group_structures, jsanitize, undeform_structure
from emmet.core.vasp.calc_types import TaskType
from emmet.core.vasp.material import MaterialsDoc
from emmet.core.vasp.task_valid import TaskDocument
Expand Down Expand Up @@ -343,29 +338,3 @@ def filter_and_group_tasks(
for group in grouped_structures:
grouped_tasks = [filtered_tasks[struct.index] for struct in group] # type: ignore
yield grouped_tasks


def undeform_structure(structure: Structure, transformations: Dict) -> Structure:
"""
Get the undeformed structure by applying the transformations in a reverse order.
Args:
structure: deformed structure
transformation: transformation that deforms the structure
Returns:
undeformed structure
"""

for transformation in reversed(transformations.get("history", [])):
if transformation["@class"] == "DeformStructureTransformation":
deform = Deformation(transformation["deformation"])
dst = DeformStructureTransformation(deform.inv)
structure = dst.apply_transformation(structure)
else:
raise RuntimeError(
"Expect transformation to be `DeformStructureTransformation`; "
f"got {transformation['@class']}"
)

return structure
30 changes: 30 additions & 0 deletions emmet-core/emmet/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import numpy as np
from monty.json import MSONable
from pydantic import BaseModel
from pymatgen.analysis.elasticity.strain import Deformation
from pymatgen.analysis.graphs import MoleculeGraph
from pymatgen.analysis.local_env import OpenBabelNN, metal_edge_extender
from pymatgen.analysis.molecule_matcher import MoleculeMatcher
Expand All @@ -16,6 +17,9 @@
StructureMatcher,
)
from pymatgen.core.structure import Molecule, Structure
from pymatgen.transformations.standard_transformations import (
DeformStructureTransformation,
)
from pymatgen.util.graph_hashing import weisfeiler_lehman_graph_hash

from emmet.core.mpid import MPculeID
Expand Down Expand Up @@ -76,6 +80,32 @@ def _get_sg(struc):
yield group


def undeform_structure(structure: Structure, transformations: Dict) -> Structure:
"""
Get an undeformed structure by applying transformations in a reverse order.
Args:
structure: deformed structure
transformation: transformation that deforms the structure
Returns:
undeformed structure
"""

for transformation in reversed(transformations.get("history", [])):
if transformation["@class"] == "DeformStructureTransformation":
deform = Deformation(transformation["deformation"])
dst = DeformStructureTransformation(deform.inv)
structure = dst.apply_transformation(structure)
else:
raise RuntimeError(
"Expect transformation to be `DeformStructureTransformation`; "
f"got {transformation['@class']}"
)

return structure


def group_molecules(molecules: List[Molecule]):
"""
Groups molecules according to composition, charge, and equality
Expand Down

0 comments on commit 5b5ca7d

Please sign in to comment.