diff --git a/kwant/builder.py b/kwant/builder.py index 5b053b01b9ac0436d7a92ffeba2886716d6dfb3d..93e7e2b8f5b3fed165dbd61312c6ba2e10723876 100644 --- a/kwant/builder.py +++ b/kwant/builder.py @@ -442,6 +442,17 @@ class HoppingKind(tuple): else: ensure_isinstance(family_b, SiteFamily) family_b = family_b + + try: + Site(family_b, family_a.normalize_tag(delta) - delta) + except Exception as e: + same_fams = family_b is family_a + msg = (str(family_a), + 'and {} are'.format(family_b) if not same_fams else ' is', + 'not compatible with delta={}'.format(delta), + ) + raise ValueError(' '.join(msg)) from e + return tuple.__new__(cls, (delta, family_a, family_b)) def __call__(self, builder): diff --git a/kwant/tests/test_builder.py b/kwant/tests/test_builder.py index bc61f971cd644a20abea135e97a5321def8f57f1..9bd369d66a3154ea1406abd013e6c7fb2110127d 100644 --- a/kwant/tests/test_builder.py +++ b/kwant/tests/test_builder.py @@ -1011,9 +1011,9 @@ def test_HoppingKind(): assert a.tag - b.tag == delta # test hashability and equality - hk = builder.HoppingKind((1, 0), g) - hk2 = builder.HoppingKind((1, 0), g) - hk3 = builder.HoppingKind((1, 0), g, h) + hk = builder.HoppingKind((1, 0, 0), g) + hk2 = builder.HoppingKind((1, 0, 0), g) + hk3 = builder.HoppingKind((1, 0, 0), g, h) assert hk == hk2 assert hash(hk) == hash(hk2) assert hk != hk3 @@ -1021,6 +1021,21 @@ def test_HoppingKind(): assert len({hk: 0, hk2:1, hk3: 2}) == 2 +def test_invalid_HoppingKind(): + g = kwant.lattice.general(ta.identity(3)) + h = kwant.lattice.general(np.identity(3)[:-1]) # 2D lattice in 3D + + delta = (1, 0, 0) + + # families have incompatible tags + with raises(ValueError): + builder.HoppingKind(delta, g, h) + + # delta is incompatible with tags + with raises(ValueError): + builder.HoppingKind(delta, h) + + def test_ModesLead_and_SelfEnergyLead(): lat = builder.SimpleSiteFamily() hoppings = [builder.HoppingKind((1, 0), lat),