Skip to content

Commit

Permalink
Fix apply_operation(fractional=True) (#4057)
Browse files Browse the repository at this point in the history
* Fix `apply_operation(fractional=True)`

* Add tests for `apply_operation`

* pre-commit auto-fixes

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
kavanase and pre-commit-ci[bot] authored Sep 9, 2024
1 parent dc6a292 commit ed4de1d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/pymatgen/core/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -4297,8 +4297,9 @@ def apply_operation(self, symm_op: SymmOp, fractional: bool = False) -> Self:
def operate_site(site):
return PeriodicSite(
site.species,
symm_op.operate(site.frac_coords),
site.lattice.get_cartesian_coords(symm_op.operate(site.frac_coords)),
self._lattice,
coords_are_cartesian=True,
properties=site.properties,
skip_checks=True,
label=site.label,
Expand Down
23 changes: 22 additions & 1 deletion tests/core/test_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -1237,19 +1237,40 @@ def test_add_remove_spin_states(self):
def test_apply_operation(self):
op = SymmOp.from_axis_angle_and_translation([0, 0, 1], 90)
struct = self.struct.copy()
spg_info = struct.get_space_group_info()
returned = struct.apply_operation(op)
assert returned is struct
assert_allclose(
struct.lattice.matrix,
[[0, 3.840198, 0], [-3.325710, 1.920099, 0], [2.217138, -0, 3.135509]],
atol=1e-6,
)
assert returned.get_space_group_info() == spg_info

op = SymmOp([[1, 1, 0, 0.5], [1, 0, 0, 0.5], [0, 0, 1, 0.5], [0, 0, 0, 1]])
op = SymmOp([[1, 1, 0, 0.5], [1, 0, 0, 0.5], [0, 0, 1, 0.5], [0, 0, 0, 1]]) # not a SymmOp of this struct
struct = self.struct.copy()
struct.apply_operation(op, fractional=True)
assert_allclose(struct.lattice.matrix, [[5.760297, 3.325710, 0], [3.840198, 0, 0], [0, -2.217138, 3.135509]], 5)

struct = self.struct.copy()
# actual SymmOp of this struct: (SpacegroupAnalyzer(struct).get_symmetry_operations()[-2])
op = SymmOp([[0.0, 0.0, -1.0, 0.75], [-1.0, -1.0, 1.0, 0.5], [0.0, -1.0, 1.0, 0.75], [0.0, 0.0, 0.0, 1.0]])
struct.apply_operation(op, fractional=True)
assert struct.get_space_group_info() == spg_info

# same SymmOp in Cartesian coordinates:
op = SymmOp(
[
[-0.5, -0.288675028, -0.816496280, 3.84019793],
[-0.866025723, 0.166666176, 0.471404694, 0],
[0, -0.942808868, 0.333333824, 2.35163180],
[0, 0, 0, 1.0],
]
)
struct = self.struct.copy()
struct.apply_operation(op, fractional=False)
assert struct.get_space_group_info() == spg_info

def test_apply_strain(self):
struct = self.struct
initial_coord = struct[1].coords
Expand Down

0 comments on commit ed4de1d

Please sign in to comment.