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..be86568 100644 --- a/doc/source/whatsnew.rst +++ b/doc/source/whatsnew.rst @@ -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 -------------------------- diff --git a/src/tbmodels/_tb_model.py b/src/tbmodels/_tb_model.py index 03c2d90..df93615 100644 --- a/src/tbmodels/_tb_model.py +++ b/src/tbmodels/_tb_model.py @@ -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. @@ -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 @@ -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: @@ -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) @@ -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 @@ -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], @@ -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. @@ -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) @@ -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. diff --git a/tests/test_change_unit_cell.py b/tests/test_change_unit_cell.py index 571fca3..f06a79a 100644 --- a/tests/test_change_unit_cell.py +++ b/tests/test_change_unit_cell.py @@ -8,6 +8,8 @@ from parameters import T_VALUES +import tbmodels + # pylint: disable=invalid-name @@ -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) diff --git a/tests/test_folding.py b/tests/test_folding.py index 480e173..156bb20 100644 --- a/tests/test_folding.py +++ b/tests/test_folding.py @@ -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)]) @@ -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)