From 1690d19db58d3542f4147ee503a880836edc9903 Mon Sep 17 00:00:00 2001
From: Christoph Groth <christoph.groth@cea.fr>
Date: Mon, 17 Dec 2012 14:30:23 +0100
Subject: [PATCH] fix possible_hoppings, add test

---
 kwant/builder.py            | 21 +++++++++++--------
 kwant/tests/test_builder.py | 41 +++++++++++++++++++++++++++++++++++--
 2 files changed, 51 insertions(+), 11 deletions(-)

diff --git a/kwant/builder.py b/kwant/builder.py
index 8bddf07e..ee88d82c 100644
--- a/kwant/builder.py
+++ b/kwant/builder.py
@@ -889,12 +889,15 @@ class Builder(object):
         self.leads.extend(other_sys.leads)
         return self
 
-    def possible_hoppings(self, delta, group_b, group_a):
+    def possible_hoppings(self, delta, group_a, group_b):
         """Return all matching possible hoppings between existing sites.
 
         A hopping ``(a, b)`` matches precisely when the site group of ``a`` is
-        `group_a` and that of ``b`` is `group_b` and ``(a.tag - b.tag)``
-        (interpreted as vectors) equals to `delta`.
+        `group_a` and that of ``b`` is `group_b` and ``(a.tag - b.tag)`` is
+        equal to `delta`.
+
+        In other words, the matching hoppings have the form:
+        ``(group_a(x + delta), group_b(x))``
 
         Parameters
         ----------
@@ -910,13 +913,13 @@ class Builder(object):
         """
         H = self.H
         symtofd = self.symmetry.to_fd
-        d = -ta.array(delta, int)
-        for site0 in self.H:
-            if site0.group is not group_a:
+        delta = ta.array(delta, int)
+        for a in self.H:
+            if a.group is not group_a:
                 continue
-            site1 = Site(group_b, site0.tag + d, True)
-            if symtofd(site1) in H: # if site1 in self
-                yield site0, site1
+            b = Site(group_b, a.tag - delta, True)
+            if symtofd(b) in H:
+                yield a, b
 
     def attach_lead(self, lead_builder, origin=None):
         """Attach a lead to the builder, possibly adding missing sites.
diff --git a/kwant/tests/test_builder.py b/kwant/tests/test_builder.py
index e3ff2bc1..8138f6a2 100644
--- a/kwant/tests/test_builder.py
+++ b/kwant/tests/test_builder.py
@@ -492,10 +492,47 @@ def test_iadd():
     sys += other_sys
     assert_equal(sys.leads, [lead0, lead1])
     expected = sorted([[(0,), 1], [(1,), 2], [(2,), 2]])
-    assert_equal(sorted(((s.tag, v) for s, v in sys.site_value_pairs())), 
+    assert_equal(sorted(((s.tag, v) for s, v in sys.site_value_pairs())),
                  expected)
     expected = sorted([[(0,), (1,), 1], [(1,), (2,), 1]])
     assert_equal(sorted(((a.tag, b.tag, v)
-                         for (a, b), v in sys.hopping_value_pairs())), 
+                         for (a, b), v in sys.hopping_value_pairs())),
                  expected)
 
+# y=0:    y=1:
+#
+# hhhh    hhhh
+# gggh    gggh
+# hhgh    hhgh
+# ghgh    hhgh
+#
+def test_possible_hoppings():
+    g = kwant.make_lattice(ta.identity(3))
+    h = kwant.make_lattice(ta.identity(3))
+    sym = kwant.TranslationalSymmetry((0, 2, 0))
+    sys = builder.Builder(sym)
+    sys[((h if max(x, y, z) % 2 else g)(x, y, z)
+         for x in range(4) for y in range(2) for z in range(4))] = None
+    for delta, group_a, group_b, n in [((1, 0, 0), g, h, 4),
+                                       ((1, 0, 0), h, g, 7),
+                                       ((0, 1, 0), g, h, 1),
+                                       ((0, 4, 0), h, h, 21),
+                                       ((0, 0, 1), g, h, 4)
+                                       ]:
+        ph = list(sys.possible_hoppings(delta, group_a, group_b))
+        assert_equal(len(ph), n)
+        ph = set(ph)
+        assert_equal(len(ph), n)
+
+        ph2 = list((
+                sym.to_fd(b, a) for a, b in
+                sys.possible_hoppings(ta.negative(delta), group_b, group_a)))
+        assert_equal(len(ph2), n)
+        ph2 = set(ph2)
+        assert_equal(ph2, ph)
+
+        for a, b in ph:
+            assert a.group is group_a
+            assert b.group is group_b
+            assert sym.to_fd(a) == a
+            assert_equal(a.tag - b.tag, delta)
-- 
GitLab