diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2a5c362..33be161 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') diff --git a/.github/workflows_source/ci.yml b/.github/workflows_source/ci.yml index 06da444..af95524 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 diff --git a/doc/source/whatsnew.rst b/doc/source/whatsnew.rst index be94bca..24c76e4 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 03c2d90..70fad2c 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 571fca3..d466378 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)