From a0e9a82fb3a586311e262338ceeedb9ca0ae1a6b Mon Sep 17 00:00:00 2001 From: Stef Smeets Date: Tue, 8 Apr 2025 08:18:35 +0200 Subject: [PATCH 1/4] Fix typo --- src/gemdat/trajectory.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gemdat/trajectory.py b/src/gemdat/trajectory.py index 16172ca..09fe039 100644 --- a/src/gemdat/trajectory.py +++ b/src/gemdat/trajectory.py @@ -33,7 +33,7 @@ def _lengths(vectors: np.ndarray, lattice: Lattice) -> np.ndarray: - """Calculate vector lengths using the metric tensor (Dunitz 1078, p227). + """Calculate vector lengths using the metric tensor (Dunitz 1978, p227). Parameters ---------- From 8773e249a8eeac32d6d437ec1e61becee569fedc Mon Sep 17 00:00:00 2001 From: Stef Smeets Date: Tue, 8 Apr 2025 08:20:45 +0200 Subject: [PATCH 2/4] Add function to remove disorder to utils --- src/gemdat/transitions.py | 25 +++++++++++-------------- src/gemdat/utils.py | 24 ++++++++++++++++++++++++ tests/utils_test.py | 25 ++++++++++++++++++++++++- 3 files changed, 59 insertions(+), 15 deletions(-) diff --git a/src/gemdat/transitions.py b/src/gemdat/transitions.py index 86ea695..c7d8f0d 100644 --- a/src/gemdat/transitions.py +++ b/src/gemdat/transitions.py @@ -40,6 +40,13 @@ class Transitions: Assingn NOSITE if the atom is in transition """ + DISORDER_ERROR_MSG = ( + 'Input `sites` are disordered! ' + 'Remove disorder and partial occupancies or try ' + '`gemdat.utils.remove_disorder_from_structure(). ' + 'See https://github.com/GEMDAT-repos/GEMDAT/issues/339 for more information.' + ) + def __init__( self, *, @@ -72,20 +79,7 @@ def __init__( Change partial occupancies in the disordered sites and set them to 1. """ if not (sites.is_ordered): - warn( - 'Input `sites` are disordered! ' - 'Although the code may work, it was written under the assumption ' - 'that an ordered structure would be passed. ' - 'Use `set_partial_occupancies_to_1=True` to set all occupancies to 1.' - 'See https://github.com/GEMDAT-repos/GEMDAT/issues/339 for more information.', - stacklevel=2, - ) - - if set_partial_occupancies_to_1: - for idx, site in enumerate(sites): - if site.is_ordered: - continue - sites.replace(idx=idx, species=site.species.elements[0], label=site.label) + raise ValueError(self.DISORDER_ERROR_MSG) self.sites = sites self.trajectory = trajectory @@ -125,6 +119,9 @@ def from_trajectory( ------- transitions: Transitions """ + if not (sites.is_ordered): + raise ValueError(cls.DISORDER_ERROR_MSG) + diff_trajectory = trajectory.filter(floating_specie) if site_radius is None: diff --git a/src/gemdat/utils.py b/src/gemdat/utils.py index 1a36444..494cac6 100644 --- a/src/gemdat/utils.py +++ b/src/gemdat/utils.py @@ -305,3 +305,27 @@ def fft_autocorrelation(coords: np.ndarray) -> np.ndarray: autocorrelation = autocorrelation / autocorrelation[:, 0, np.newaxis] return autocorrelation + + +def remove_disorder_from_structure(structure: Structure) -> Structure: + """Attempts to remove disorder and partial occupancies from input + structure. + + Parameters + ---------- + structure : Structure + Input structure + + Returns + ------- + new_structure : Structure + Output structure with disorder removed + """ + new_structure = structure.copy() + + for idx, site in enumerate(new_structure): + if site.is_ordered: + continue + new_structure.replace(idx=idx, species=site.species.elements[0], label=site.label) + + return new_structure diff --git a/tests/utils_test.py b/tests/utils_test.py index 9494d86..349f0a2 100644 --- a/tests/utils_test.py +++ b/tests/utils_test.py @@ -3,8 +3,16 @@ import numpy as np import pytest from numpy.testing import assert_allclose, assert_equal +from pymatgen.core import Structure -from gemdat.utils import bfill, cartesian_to_spherical, ffill, integer_remap, meanfreq +from gemdat.utils import ( + bfill, + cartesian_to_spherical, + ffill, + integer_remap, + meanfreq, + remove_disorder_from_structure, +) @pytest.fixture @@ -132,3 +140,18 @@ def test_cartesian_to_spherical(): ] ) assert_allclose(ret, expected, rtol=1e-5) + + +def test_remove_disorder_from_structure(): + structure = Structure( + lattice=np.eye(3) * 10, + coords=[(0, 0, 0), (0.5, 0.5, 0.5)], + species=[{'Si': 0.5, 'Ge': 0.5}, {'Ge': 0.5}], + labels=['A', 'B'], + ) + assert structure.is_ordered + + new_structure = remove_disorder_from_structure(structure) + assert new_structure.is_ordered + assert len(new_structure) == 2 + assert new_structure.labels == structure.labels From acaa2e8a5c503f0328028b6fd91e54200361ad26 Mon Sep 17 00:00:00 2001 From: Stef Smeets Date: Tue, 8 Apr 2025 08:23:22 +0200 Subject: [PATCH 3/4] Improve error message --- src/gemdat/transitions.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/gemdat/transitions.py b/src/gemdat/transitions.py index c7d8f0d..b01c93b 100644 --- a/src/gemdat/transitions.py +++ b/src/gemdat/transitions.py @@ -42,6 +42,7 @@ class Transitions: DISORDER_ERROR_MSG = ( 'Input `sites` are disordered! ' + 'The analysis does not work with disordered structures. ' 'Remove disorder and partial occupancies or try ' '`gemdat.utils.remove_disorder_from_structure(). ' 'See https://github.com/GEMDAT-repos/GEMDAT/issues/339 for more information.' From ad879eb518ecec27e27fab53a12644829fccc613 Mon Sep 17 00:00:00 2001 From: Stef Smeets Date: Tue, 8 Apr 2025 08:25:23 +0200 Subject: [PATCH 4/4] Fix logic and remove docstring --- src/gemdat/transitions.py | 3 --- tests/utils_test.py | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/src/gemdat/transitions.py b/src/gemdat/transitions.py index b01c93b..5c344f8 100644 --- a/src/gemdat/transitions.py +++ b/src/gemdat/transitions.py @@ -57,7 +57,6 @@ def __init__( events: pd.DataFrame, states: np.ndarray, inner_states: np.ndarray, - set_partial_occupancies_to_1: bool = False, ): """Store event data for jumps and transitions between sites. @@ -76,8 +75,6 @@ def __init__( Input states inner_states : np.ndarray Input states for inner sites - set_partial_occupancies_to_1 : bool - Change partial occupancies in the disordered sites and set them to 1. """ if not (sites.is_ordered): raise ValueError(self.DISORDER_ERROR_MSG) diff --git a/tests/utils_test.py b/tests/utils_test.py index 349f0a2..29ebfe1 100644 --- a/tests/utils_test.py +++ b/tests/utils_test.py @@ -149,7 +149,7 @@ def test_remove_disorder_from_structure(): species=[{'Si': 0.5, 'Ge': 0.5}, {'Ge': 0.5}], labels=['A', 'B'], ) - assert structure.is_ordered + assert not structure.is_ordered new_structure = remove_disorder_from_structure(structure) assert new_structure.is_ordered