Skip to content
29 changes: 22 additions & 7 deletions src/structuretoolkit/analyse/symmetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import ast
import dataclasses
import string
import warnings

Comment thread
jan-janssen marked this conversation as resolved.
import numpy as np
import spglib
Expand Down Expand Up @@ -400,26 +401,40 @@ def get_primitive_cell(
>>> symmetry = Symmetry(structure)
>>> len(symmetry.get_primitive_cell()) == len(basis)
True

.. warning::
Custom arrays defined in the base structures
:attr:`ase.atoms.Atoms.arrays` and other state (.info, .calc, etc.) are not copied to the new structure!
"""
if not all(self._structure.pbc):
raise ValueError("Can only symmetrize periodic structures.")
ret = spglib.standardize_cell(
self._get_spglib_cell(use_elements=use_elements, use_magmoms=use_magmoms),
to_primitive=not standardize,
)
if ret is None:
raise SymmetryError(spglib.error.get_error_message())
cell, positions, indices = ret
positions = (cell.T @ positions.T).T
new_structure = self._structure.copy()
new_structure.cell = cell
new_structure = new_structure[: len(indices)]
cell, scaled_positions, indices = ret
indices_dict = {
v: k
for k, v in structuretoolkit.common.helper.get_species_indices_dict(
structure=self._structure
).items()
}
new_structure.symbols = [indices_dict[i] for i in indices]
new_structure.positions = positions
symbols = [indices_dict[i] for i in indices]
new_structure = type(self._structure)(
symbols=symbols,
scaled_positions=scaled_positions,
cell=cell,
pbc=[True, True, True],
)
Comment on lines +405 to +430
Copy link

Copilot AI Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docstring warns only about losing custom Atoms.arrays, but the new construction type(self._structure)(...) also drops other ase.Atoms state that copy() would preserve (e.g. info, constraints, calculator, and standard per-atom arrays like initial_magmoms/tags if present). Either preserve the relevant metadata when possible or expand the warning/documentation to reflect all state that will be lost.

Copilot uses AI. Check for mistakes.
keys = set(self._structure.arrays) - {"numbers", "positions"}
if len(keys) > 0:
warnings.warn(
f"Custom arrays {keys} do not carry over to new structure!",
stacklevel=2,
)

return new_structure

def get_ir_reciprocal_mesh(
Expand Down
Loading
Loading