Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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
Expand All @@ -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')
Expand Down Expand Up @@ -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')
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows_source/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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') }}
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions doc/source/whatsnew.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------------------------

Expand Down
7 changes: 5 additions & 2 deletions src/tbmodels/_tb_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions tests/test_change_unit_cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Loading