Skip to content

Commit

Permalink
Merge #469, bugfixing.
Browse files Browse the repository at this point in the history
Ensure atom positions are expressed in the new basis when initializing Phase with structure
  • Loading branch information
pc494 authored Mar 27, 2024
2 parents 8f98c15 + dc16c1a commit cdc314a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Fixed
-----
- Transparency of polar stereographic grid lines can now be controlled by Matplotlib's
``grid.alpha``, just like the azimuth grid lines.
- Previously ``Phase`` was failing to adjust atom position to accomodate for the change of basis. This is now fixed.

2023-03-14 - version 0.11.1
===========================
Expand Down
3 changes: 3 additions & 0 deletions orix/crystal_map/phase_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,10 @@ def structure(self, value: Structure):
old_matrix = value.lattice.base
new_matrix = _new_structure_matrix_from_alignment(old_matrix, x="a", z="c*")
value = copy.deepcopy(value)
# Ensure atom positions are expressed in the new basis
old_xyz_cartn = value.xyz_cartn
value.lattice.setLatBase(new_matrix)
value.xyz_cartn = old_xyz_cartn
if value.title == "" and hasattr(self, "_structure"):
value.title = self.name
self._structure = value
Expand Down
53 changes: 52 additions & 1 deletion orix/tests/test_phase_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# You should have received a copy of the GNU General Public License
# along with orix. If not, see <http://www.gnu.org/licenses/>.

from diffpy.structure import Lattice, Structure
from diffpy.structure import Atom, Lattice, Structure
from diffpy.structure.spacegroups import GetSpaceGroup
import numpy as np
import pytest
Expand Down Expand Up @@ -333,6 +333,57 @@ def test_lattice_vectors(self):
assert np.allclose(br.data, [0, 0.679, 0], atol=1e-3)
assert np.allclose(cr.data, [0, 0, 0.714], atol=1e-3)

@pytest.mark.parametrize(
["lat", "atoms"],
[
[
Lattice(1, 1, 1, 90, 90, 90),
[
Atom("C", [0, 0, 0]),
Atom("C", [0.5, 0.5, 0.5]),
Atom("C", [0.5, 0, 0]),
],
],
[
Lattice(1, 1, 1, 90, 90, 120),
[
Atom("C", [0, 0, 0]),
Atom("C", [0.5, 0, 0]),
Atom("C", [0.5, 0.5, 0.5]),
],
],
[
Lattice(1, 2, 3, 90, 90, 60),
[
Atom("C", [0, 0, 0]),
Atom("C", [0.1, 0.1, 0.6]),
Atom("C", [0.5, 0, 0]),
],
],
],
)
def test_atom_positions(self, lat, atoms):
structure = Structure(atoms, lat)
phase = Phase(structure=structure)
# xyz_cartn is independent of basis
assert np.allclose(phase.structure.xyz_cartn, structure.xyz_cartn)

# however, Phase should (in many cases) change the basis.
if np.allclose(structure.lattice.base, phase.structure.lattice.base):
# In this branch we are in the same basis & all atoms should be the same
for atom_from_structure, atom_from_phase in zip(structure, phase.structure):
assert np.allclose(atom_from_structure.xyz, atom_from_phase.xyz)
else:
# Here we have differing basis, so xyz must disagree for at least some atoms
disagreement_found = False

for atom_from_structure, atom_from_phase in zip(structure, phase.structure):
if not np.allclose(atom_from_structure.xyz, atom_from_phase.xyz):
disagreement_found = True
break

assert disagreement_found

def test_from_cif(self, cif_file):
"""CIF files parsed correctly with space group and all."""
phase = Phase.from_cif(cif_file)
Expand Down

0 comments on commit cdc314a

Please sign in to comment.