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

Fix apply_operation(fractional=True) #4057

Merged
merged 3 commits into from
Sep 9, 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
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