From 2a1100eaea033e4745e8f77b2d46386b5cf8c6f0 Mon Sep 17 00:00:00 2001 From: Davis Rens <166565828+davrens101@users.noreply.github.com> Date: Wed, 4 Nov 2026 01:16:49 -0800 Subject: [PATCH 1/3] Tolerate determinant roundoff for unimodular unit cells --- doc/source/whatsnew.rst | 10 ++++++++ src/tbmodels/_tb_model.py | 7 ++++-- tests/test_change_unit_cell.py | 45 ++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew.rst b/doc/source/whatsnew.rst index be94bca6..24c76e46 100644 --- a/doc/source/whatsnew.rst +++ b/doc/source/whatsnew.rst @@ -8,6 +8,16 @@ What's new The following is a short summary of the most important changes in new releases of TBmodels, starting *after* version 1.1. +Unreleased +---------- + +Bug fixes +''''''''' + +- ``Model.change_unit_cell`` now tolerates floating-point determinant + roundoff for integer, determinant-one lattice transformations instead + of rejecting valid changes of basis. + What's new in TBmodels 1.4 -------------------------- diff --git a/src/tbmodels/_tb_model.py b/src/tbmodels/_tb_model.py index 03c2d903..70fad2c3 100644 --- a/src/tbmodels/_tb_model.py +++ b/src/tbmodels/_tb_model.py @@ -1618,10 +1618,13 @@ def change_unit_cell( # pylint: disable=too-many-branches f"but in reduced coordinates it is:\n{uc_reduced}" ) uc_reduced = np.round(uc_reduced).astype(int) - if la.det(uc_reduced) != 1: + determinant = la.det(uc_reduced) + # LU factorization can round the determinant of an integer + # unimodular cell, so avoid exact floating-point equality. + if not np.isclose(determinant, 1, rtol=0, atol=1e-8): raise ValueError( "The determinant of the unit cell in reduced coordinates must " - f"be 1, but it is {la.det(uc_reduced)} instead." + f"be 1, but it is {determinant} instead." ) # apply offset to positions diff --git a/tests/test_change_unit_cell.py b/tests/test_change_unit_cell.py index 571fca31..d4663785 100644 --- a/tests/test_change_unit_cell.py +++ b/tests/test_change_unit_cell.py @@ -193,3 +193,48 @@ def test_equivalent_uc_shape(get_model, models_close): model1, model2.change_unit_cell(uc=[[1, 0, 0], [-1, 1, 0], [0, 0, 1]], cartesian=False), ) + + +@pytest.mark.parametrize("cartesian", [True, False]) +def test_unimodular_uc_roundoff_band_invariance( + get_model, sparse, cartesian, models_close +): + """ + Ensure that a valid unimodular unit-cell transformation whose floating-point + determinant suffers small round-off error (e.g. det = 1 + 4.2e-15) is accepted + and preserves the physical energy eigenvalues under k_new = k_old @ U.T. + Also verify roundtrip transformation recovers the original model. + """ + # SL(3, Z) matrix with exact integer determinant 1 + U = np.array([[1, -5, -5], [-2, 11, 4], [2, -10, -9]]) + model = get_model( + t1=0.1, + t2=0.7, + sparse=sparse, + uc=np.eye(3), + pos=[(0.01, 0.02, 0.03), (0.5, 0.5, 0.5)], + ) + model_transformed = model.change_unit_cell( + uc=U, cartesian=cartesian, offset=np.zeros(3) + ) + + # Physical eigenvalues must match at k_new = k_old @ U.T + k_old = np.array( + [ + [0.1, 0.2, 0.3], + [-0.25, 0.4, 0.1], + [0.0, 0.0, 0.0], + [0.5, -0.3, 0.2], + ] + ) + k_new = k_old @ U.T + eigenvals_old = np.array(model.eigenval(k_old)) + eigenvals_new = np.array(model_transformed.eigenval(k_new)) + np.testing.assert_allclose(eigenvals_old, eigenvals_new, atol=1e-12) + + # Roundtrip transformation using inverse operation + revert_uc = np.eye(3) if cartesian else np.linalg.inv(U) + model_reverted = model_transformed.change_unit_cell( + uc=revert_uc, cartesian=cartesian, offset=np.zeros(3) + ) + assert models_close(model, model_reverted) From 9bdac91e193acd2144d2646d5684e456fef90990 Mon Sep 17 00:00:00 2001 From: Davis Rens <166565828+davrens101@users.noreply.github.com> Date: Thu, 5 Nov 2026 00:28:30 -0800 Subject: [PATCH 2/3] Use supported cache and artifact actions in CI --- .github/workflows/ci.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2a5c3629..33be161b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,7 +10,7 @@ jobs: steps: - name: Checkout code uses: actions/checkout@v2 - - uses: actions/cache@v2 + - uses: actions/cache@v4 with: path: ~/.cache/pip key: pip-${{ matrix.python-version }}-${{ matrix.install-type }}-${{ hashFiles('**/poetry.lock') @@ -26,7 +26,7 @@ jobs: run: .ci/install_script.sh - name: Build documentation run: poetry run make SPHINXOPTS='-nW' -C doc html - - uses: actions/upload-artifact@v2 + - uses: actions/upload-artifact@v4 with: name: doc-build path: doc/build/html @@ -39,7 +39,7 @@ jobs: steps: - name: Checkout code uses: actions/checkout@v2 - - uses: actions/cache@v2 + - uses: actions/cache@v4 with: path: ~/.cache/pip key: pip-${{ matrix.python-version }}-${{ matrix.install-type }}-${{ hashFiles('**/poetry.lock') @@ -70,7 +70,7 @@ jobs: steps: - name: Checkout code uses: actions/checkout@v2 - - uses: actions/cache@v2 + - uses: actions/cache@v4 with: path: ~/.cache/pip key: pip-${{ matrix.python-version }}-${{ matrix.install-type }}-${{ hashFiles('**/poetry.lock') From efa8a2408dacd6ddff8f21512bfd4e65f511f1bf Mon Sep 17 00:00:00 2001 From: Davis Rens <166565828+davrens101@users.noreply.github.com> Date: Thu, 5 Nov 2026 01:20:28 -0800 Subject: [PATCH 3/3] Keep generated CI workflow action versions in sync --- .github/workflows_source/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows_source/ci.yml b/.github/workflows_source/ci.yml index 06da4445..af95524c 100644 --- a/.github/workflows_source/ci.yml +++ b/.github/workflows_source/ci.yml @@ -14,7 +14,7 @@ _anchors: # sudo apt install -qq liblapack3 liblapack-dev pip-cache: &PIP_CACHE - uses: actions/cache@v2 + uses: actions/cache@v4 with: path: ~/.cache/pip key: pip-${{ matrix.python-version }}-${{ matrix.install-type }}-${{ hashFiles('**/poetry.lock') }} @@ -47,7 +47,7 @@ jobs: - *INSTALL_PROJECT - name: Build documentation run: poetry run make SPHINXOPTS='-nW' -C doc html - - uses: actions/upload-artifact@v2 + - uses: actions/upload-artifact@v4 with: name: doc-build path: doc/build/html