From 0becfa4b1a3db0bc10fa14f48f791bf385aa7602 Mon Sep 17 00:00:00 2001
From: Joseph Weston <joseph.weston08@gmail.com>
Date: Fri, 28 Apr 2017 12:58:02 +0200
Subject: [PATCH] check for site presence modulo target system symmetry in
 'Builder.fill'

Previously 'Builder.fill' did not respect the symmetry of the target
Builder. This change also adds a test to check that target system
symmetry is respected.

Fixes #119
---
 kwant/builder.py            |  2 +-
 kwant/tests/test_builder.py | 21 +++++++++++++++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/kwant/builder.py b/kwant/builder.py
index 42c3352a..d711463d 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 9774fccb..8c0d5747 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()
-- 
GitLab