Skip to content

Commit

Permalink
Added info message when non-periodic and nsc>1
Browse files Browse the repository at this point in the history
This should make users aware of some inconsistencies in
the functionality of the objects.

Signed-off-by: Nick Papior <[email protected]>
  • Loading branch information
zerothi committed Oct 24, 2023
1 parent 8979d25 commit 55cb1bd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/sisl/lattice.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from enum import IntEnum, auto
from numbers import Integral
from pathlib import Path
from typing import TYPE_CHECKING, Optional, Sequence, Tuple, Union
from typing import Optional, Sequence, Tuple, Union

import numpy as np
from numpy import dot, ndarray
Expand All @@ -25,7 +25,7 @@
from ._internal import set_module
from ._lattice import cell_invert, cell_reciprocal
from ._math_small import cross3, dot3
from .messages import SislError, deprecate, deprecate_argument, deprecation
from .messages import SislError, deprecate, deprecate_argument, deprecation, info
from .quaternion import Quaternion
from .shape.prism4 import Cuboid
from .utils.mathematics import fnorm
Expand Down Expand Up @@ -104,7 +104,7 @@ class Lattice(_Dispatchs,
number of supercells along each lattice vector
origin : (3,) of float, optional
the origin of the supercell.
boundary_condition : list of int (3, 2) or (3, ), optional
boundary_condition : int/str or list of int/str (3, 2) or (3, ), optional
the boundary conditions for each of the cell's planes. Defaults to periodic boundary condition.
See `BoundaryCondition` for valid enumerations.
"""
Expand Down Expand Up @@ -221,6 +221,7 @@ def conv(v):

if not hasattr(self, "_bc"):
self._bc = _a.fulli([3, 2], getitem("Unknown"))
old = self._bc.copy()

if not boundary is None:
if isinstance(boundary, (Integral, str, bool)):
Expand All @@ -245,11 +246,15 @@ def conv(v):
self._bc[d, :] = v

# shorthand for bc
for bc in self._bc == BoundaryCondition.PERIODIC:
for nsc, bc, changed in zip(self.nsc, self._bc == BoundaryCondition.PERIODIC, self._bc != old):
if bc.any() and not bc.all():
raise ValueError(f"{self.__class__.__name__}.set_boundary_condition has a one non-periodic and "

Check warning on line 251 in src/sisl/lattice.py

View check run for this annotation

Codecov / codecov/patch

src/sisl/lattice.py#L251

Added line #L251 was not covered by tests
"one periodic direction. If one direction is periodic, both instances "
"must have that BC.")
if changed.any() and (~bc).all() and nsc > 1:
info(f"{self.__class__.__name__}.set_boundary_condition is having image connections (nsc={nsc}>1) "
"while having a non-periodic boundary condition.")


def parameters(self, rad: bool=False) -> Tuple[float, float, float, float, float, float]:
r""" Cell parameters of this cell in 3 lengths and 3 angles
Expand Down
8 changes: 8 additions & 0 deletions src/sisl/tests/test_lattice.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,3 +539,11 @@ def test_lattice_bc_fail():
with pytest.raises(KeyError):
lat.set_boundary_condition(b="eusoatuhesoau")


def test_lattice_info():
lat = Lattice(1, nsc=[3, 3, 3])
with pytest.warns(sisl.SislInfo) as record:
lat.set_boundary_condition(b=Lattice.BC.DIRICHLET)
lat.set_boundary_condition(c=Lattice.BC.PERIODIC)
assert len(record) == 1

0 comments on commit 55cb1bd

Please sign in to comment.