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
9 changes: 9 additions & 0 deletions doc/source/whatsnew.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ 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
'''''''''

- Default origins in ``change_unit_cell`` and ``fold_model`` now match the
model dimension, allowing 1D and 2D models to use the zero-origin default.

What's new in TBmodels 1.4
--------------------------

Expand Down
22 changes: 14 additions & 8 deletions src/tbmodels/_tb_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1555,7 +1555,7 @@ def change_unit_cell( # pylint: disable=too-many-branches
self,
*,
uc: ty.Optional[ty.Sequence[ty.Sequence[float]]] = None,
offset: ty.Sequence[float] = (0, 0, 0),
offset: ty.Optional[ty.Sequence[float]] = None,
cartesian: bool = False,
) -> Model:
"""Return a model with a different unit cell of the same volume.
Expand All @@ -1572,7 +1572,7 @@ def change_unit_cell( # pylint: disable=too-many-branches
current unit cell shape is kept.
offset :
The position of the new unit cell origin, relative to the old
one.
one. If omitted, a zero vector of the model dimension is used.
cartesian :
Specifies if the offset and unit cell are in cartesian or
reduced coordinates. Reduced coordinates are with respect to
Expand All @@ -1584,6 +1584,7 @@ def change_unit_cell( # pylint: disable=too-many-branches
raise ValueError(
"Cannot change the unit cell: model positions are not defined."
)
offset_array = np.zeros(self.dim) if offset is None else np.array(offset)

if cartesian:
if self.uc is None:
Expand All @@ -1597,7 +1598,7 @@ def change_unit_cell( # pylint: disable=too-many-branches
else:
new_uc = np.array(uc)
uc_reduced = la.solve(self.uc.T, new_uc.T).T
offset_reduced = la.solve(self.uc.T, np.array(offset).T).T
offset_reduced = la.solve(self.uc.T, offset_array.T).T
else:
if uc is None:
uc_reduced = np.eye(self.dim)
Expand All @@ -1607,7 +1608,7 @@ def change_unit_cell( # pylint: disable=too-many-branches
new_uc = None
else:
new_uc = (self.uc.T @ uc_reduced.T).T
offset_reduced = np.array(offset)
offset_reduced = offset_array

# check that the reduced unit cell is compatible with the
# current lattice
Expand Down Expand Up @@ -1727,7 +1728,7 @@ def fold_model( # pylint: disable=too-many-locals,too-many-branches,too-many-st
self,
*,
new_unit_cell: ty.Sequence[ty.Sequence[float]],
unit_cell_offset: ty.Sequence[float] = (0, 0, 0),
unit_cell_offset: ty.Optional[ty.Sequence[float]] = None,
position_tolerance: float = 1e-3,
unmatched_position_threshold: float = float("inf"),
orbital_labels: ty.Sequence[ty.Hashable],
Expand All @@ -1754,7 +1755,7 @@ def fold_model( # pylint: disable=too-many-locals,too-many-branches,too-many-st
lie completely within the old unit cell.
unit_cell_offset :
Position (in cartesian coordinates) at which the new unit cell
is based.
is based. If omitted, a zero vector of the model dimension is used.
position_tolerance :
Tolerance used when determining if a position mapped into the
new unit cell is the same as an existing orbital position.
Expand Down Expand Up @@ -1809,9 +1810,14 @@ def fold_model( # pylint: disable=too-many-locals,too-many-branches,too-many-st
"Unit cell and positions must be specified for model folding."
)

offset_array = (
np.zeros(self.dim)
if unit_cell_offset is None
else np.array(unit_cell_offset)
)
new_uc = np.array(new_unit_cell)
# Check that the new unit cell lies within the current one
new_uc_vertices = unit_cell_offset + np.array(
new_uc_vertices = offset_array + np.array(
[
sum(mult * a_i for mult, a_i in zip(multipliers, new_uc))
for multipliers in itertools.product([0, 1], repeat=self.dim)
Expand All @@ -1831,7 +1837,7 @@ def fold_model( # pylint: disable=too-many-locals,too-many-branches,too-many-st
)

positions_cartesian = (self.uc.T @ self.pos.T).T
pos_cartesian_relative = positions_cartesian - unit_cell_offset
pos_cartesian_relative = positions_cartesian - offset_array
pos_reduced_new = la.solve(new_uc.T, pos_cartesian_relative.T).T

# Check and warn if positions are at the edge of the new unit cell.
Expand Down
19 changes: 19 additions & 0 deletions tests/test_change_unit_cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

from parameters import T_VALUES

import tbmodels

# pylint: disable=invalid-name


Expand Down Expand Up @@ -193,3 +195,20 @@ 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("dim", [1, 2])
@pytest.mark.parametrize("cartesian", [True, False])
def test_default_origin_lower_dimensions(dim, cartesian, sparse, models_close):
"""Omitting the origin preserves a lower-dimensional model."""
model = tbmodels.Model(
dim=dim,
pos=[[0.2] * dim, [0.7] * dim],
uc=np.diag(np.arange(1, dim + 1)),
on_site=[-0.4, 0.4],
occ=1,
sparse=sparse,
)
model.add_hop(0.3 + 0.1j, 0, 1, [1] + [0] * (dim - 1))
changed = model.change_unit_cell(cartesian=cartesian)
assert models_close(model, changed)
19 changes: 19 additions & 0 deletions tests/test_folding.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

from parameters import T_VALUES

import tbmodels


@pytest.mark.parametrize("t_values", T_VALUES)
@pytest.mark.parametrize("supercell_size", [(1, 1, 1), (2, 1, 1), (2, 1, 3)])
Expand Down Expand Up @@ -257,3 +259,20 @@ def test_invalid_order_by(get_model):
assert "Invalid input" in str(excinfo.value)
assert "order_by" in str(excinfo.value)
assert "spam" in str(excinfo.value)


@pytest.mark.parametrize("dim", [1, 2])
def test_fold_lower_dim_supercell(dim, sparse, models_close):
"""The default origin permits supercell/folding roundtrips in 1D and 2D."""
model = tbmodels.Model(
dim=dim,
pos=[[0.2] * dim, [0.7] * dim],
uc=np.diag(np.arange(1, dim + 1)),
on_site=[-0.4, 0.4],
occ=1,
sparse=sparse,
)
model.add_hop(0.3 + 0.1j, 0, 1, [1] + [0] * (dim - 1))
supercell = model.supercell([2] + [1] * (dim - 1))
folded = supercell.fold_model(new_unit_cell=model.uc, orbital_labels=["a", "b"] * 2)
assert models_close(model, folded)
Loading