From ccb722a9b77c61c2051d1c036e95b7743836197c Mon Sep 17 00:00:00 2001
From: Anton Akhmerov <anton.akhmerov@gmail.com>
Date: Tue, 30 Jul 2013 10:56:43 +0200
Subject: [PATCH] make tolerance in neighbor search relative to the shortest
 lattice vector

---
 kwant/lattice.py            | 10 +++++++---
 kwant/tests/test_lattice.py |  6 +++---
 2 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/kwant/lattice.py b/kwant/lattice.py
index 85d6bca..d50bcfa 100644
--- a/kwant/lattice.py
+++ b/kwant/lattice.py
@@ -251,15 +251,15 @@ class Polyatomic(object):
         return wire_sites
 
     def neighbors(self, n=1, eps=1e-8):
-        """
-        Return n-th nearest neighbor hoppings.
+        """Return n-th nearest neighbor hoppings.
 
         Parameters
         ----------
         n : integer
             Order of the hoppings to return.
         eps : float
-            A cutoff for when to consider lengths to be approximately equal.
+            Tolerance relative to the length of the shortest lattice vector for
+            when to consider lengths to be approximately equal.
 
         Returns
         -------
@@ -276,6 +276,9 @@ class Polyatomic(object):
         # This algorithm is not designed to be fast and can be improved,
         # however there is no real need.
         sls = self.sublattices
+        shortest_hopping = sls[0].n_closest(sls[0](*([0] * sls[0].dim)).pos,
+                                            2)[-1]
+        eps *= np.linalg.norm(self.vec(shortest_hopping))
         nvec = len(self.prim_vecs)
         sublat_pairs = [(i, j) for (i, j) in product(sls, sls)
                         if sls.index(j) >= sls.index(i)]
@@ -289,6 +292,7 @@ class Polyatomic(object):
                     continue
             return True
 
+
         # Find the correct number of neighbors to calculate on each lattice.
         cutoff = n + 2
         while True:
diff --git a/kwant/tests/test_lattice.py b/kwant/tests/test_lattice.py
index 8dc2140..6331520 100644
--- a/kwant/tests/test_lattice.py
+++ b/kwant/tests/test_lattice.py
@@ -44,13 +44,13 @@ def test_general():
 
 
 def test_neighbors():
-    lat = lattice.honeycomb()
+    lat = lattice.honeycomb(1e-10)
     num_nth_nearest = [len(lat.neighbors(n)) for n in range(5)]
     assert num_nth_nearest == [2, 3, 6, 3, 6]
-    lat = lattice.square()
+    lat = lattice.square(1e8)
     num_nth_nearest = [len(lat.neighbors(n)) for n in range(5)]
     assert num_nth_nearest == [1, 2, 2, 2, 4]
-    lat = lattice.chain()
+    lat = lattice.chain(1e-10)
     num_nth_nearest = [len(lat.neighbors(n)) for n in range(5)]
     assert num_nth_nearest == 5 * [1]
 
-- 
GitLab