From 5b5ca7d4dd7c5b59ec7949a36ffb0565886199ae Mon Sep 17 00:00:00 2001 From: "Tyler S. Mathis" <35553152+tsmathis@users.noreply.github.com> Date: Thu, 2 May 2024 16:22:39 -0700 Subject: [PATCH] move undeform_structure function to core utils (#999) --- .../emmet/builders/vasp/materials.py | 33 +------------------ emmet-core/emmet/core/utils.py | 30 +++++++++++++++++ 2 files changed, 31 insertions(+), 32 deletions(-) diff --git a/emmet-builders/emmet/builders/vasp/materials.py b/emmet-builders/emmet/builders/vasp/materials.py index 4b9c6da932..a9db1aeb6a 100644 --- a/emmet-builders/emmet/builders/vasp/materials.py +++ b/emmet-builders/emmet/builders/vasp/materials.py @@ -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 @@ -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 diff --git a/emmet-core/emmet/core/utils.py b/emmet-core/emmet/core/utils.py index f48ab5b5c0..707653eaf2 100644 --- a/emmet-core/emmet/core/utils.py +++ b/emmet-core/emmet/core/utils.py @@ -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 @@ -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 @@ -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