diff --git a/kwant/builder.py b/kwant/builder.py index f960ff1cfb30673c665a54a69efeaa0e0351d9b2..2e1514b8dde403973fe519c6657e3c4608c15a04 100644 --- a/kwant/builder.py +++ b/kwant/builder.py @@ -1260,9 +1260,12 @@ class Builder: self.update(other) return self - def fill(self, template, shape, start, *, overwrite=False, max_sites=10**7): + def fill(self, template, shape, start, *, max_sites=10**7): """Populate builder using another one as a template. + Sites and hoppings that already exist in the target builder are never + overwritten. + Parameters ---------- template : `Builder` instance @@ -1276,10 +1279,6 @@ class Builder: The site(s) at which the the flood-fill starts. If start is an iterable of numbers, the starting site will be ``template.closest(start)``. - overwrite : boolean - Whether existing sites or hoppings in the target builder should be - overwritten. When overwriting is disabled (the default), existing - sites act as boundaries for the flood-fill. max_sites : positive number The maximal number of sites that may be added before ``RuntimeError`` is raised. Used to prevent using up all memory. @@ -1330,7 +1329,7 @@ class Builder: congested = True for s in start: s = to_fd(s) - if overwrite or s not in H: + if s not in H: congested = False if shape(s): active.add(s) @@ -1384,7 +1383,7 @@ class Builder: if not shape(head_fd): continue - if overwrite or head_fd not in H: + if head_fd not in H: # Fill 'head' site. new_active.add(head_fd) H.setdefault(head_fd, [head_fd, None]) diff --git a/kwant/tests/test_builder.py b/kwant/tests/test_builder.py index 51733647414fc426475189a07a70fe9e225e263f..8eb2e58b9ced9e9f32038e3776dacbe737eef4ef 100644 --- a/kwant/tests/test_builder.py +++ b/kwant/tests/test_builder.py @@ -693,14 +693,13 @@ def test_fill(): ## Test filling of infinite builder. for n in [1, 2, 4]: sym_n = kwant.TranslationalSymmetry((n, 0)) - for ow in [False, True]: - for start in [g(0, 0), g(20, 0)]: - target = builder.Builder(sym_n) - sites = target.fill(template_1d, lambda s: True, start, - overwrite=ow, max_sites=10) - assert len(sites) == n - assert len(list(target.hoppings())) == n - assert set(sym_n.to_fd(s) for s in sites) == set(target.sites()) + for start in [g(0, 0), g(20, 0)]: + target = builder.Builder(sym_n) + sites = target.fill(template_1d, lambda s: True, start, + max_sites=10) + assert len(sites) == n + assert len(list(target.hoppings())) == n + assert set(sym_n.to_fd(s) for s in sites) == set(target.sites()) ## test max_sites target = builder.Builder() @@ -716,14 +715,8 @@ def test_fill(): target = builder.Builder() added_sites = target.fill(template_1d, line_200, g(0, 0)) assert len(added_sites) == 200 - ## test overwrite=False with warns(RuntimeWarning): target.fill(template_1d, line_200, g(0, 0)) - ## test overwrite=True - added_sites = target.fill(template_1d, line_200, g(0, 0), - overwrite=True) - assert len(added_sites) == 200 - ## test multiplying unit cell size in 1D n_cells = 10 @@ -784,13 +777,13 @@ def test_fill(): 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 the starting - # site and 'overwrite == False'. + # Refuses to fill the target because target already contains the starting + # site. with warns(RuntimeWarning): target.fill(template, lambda x: True, lat(0)) # should only add a single site (and hopping) - new_sites = target.fill(template, lambda x: True, lat(1), overwrite=False) + new_sites = target.fill(template, lambda x: True, lat(1)) assert target[lat(0)] is None # should not be overwritten by template assert target[lat(-1)] == template[lat(0)] assert len(new_sites) == 1