diff --git a/kwant/builder.py b/kwant/builder.py index 42c3352a5e31dd6acf6a791e467213a1fe4d2c28..d711463d9de6604aa9634d093e964613de6ad912 100644 --- a/kwant/builder.py +++ b/kwant/builder.py @@ -1252,7 +1252,7 @@ class Builder: yield result def add_site(candidate): - may_add = overwrite or candidate not in self.H + may_add = overwrite or candidate not in self # Delay calling shape because it may raise an error. if not may_add or candidate in all_added: return diff --git a/kwant/tests/test_builder.py b/kwant/tests/test_builder.py index 9774fccb0c8b48a13e373b9bc1818b1ccd51c148..8c0d5747528baae6a5a0ccba99dfad8dc2ea900d 100644 --- a/kwant/tests/test_builder.py +++ b/kwant/tests/test_builder.py @@ -730,6 +730,27 @@ def test_fill(): assert sorted(target.sites()) == sorted(should_be_syst.sites()) assert sorted(target.hoppings()) == sorted(should_be_syst.hoppings()) + ## test that 'fill' respects the symmetry of the target builder + lat = kwant.lattice.chain(a=1) + template = builder.Builder(kwant.TranslationalSymmetry((-1,))) + template[lat(0)] = 2 + template[lat.neighbors()] = -1 + + target = builder.Builder(kwant.TranslationalSymmetry((-2,))) + target[lat(0)] = None + to_target_fd = target.symmetry.to_fd + # refuses to fill the target because target already contains a site + # in template symmetry domain (0,) and 'overwrite == False' + with raises(RuntimeError): + target.fill(template, lambda x: True, (0,)) + + # should only add a single site (and hopping) + new_sites = target.fill(template, lambda x: True, (1,), overwrite=False) + assert target[lat(0)] is None # should not be overwritten by template + assert target[lat(-1)] == template[lat(0)] + assert len(new_sites) == 1 + assert to_target_fd(new_sites[0]) == to_target_fd(lat(-1)) + def test_attach_lead(): fam = builder.SimpleSiteFamily()