From b0211f1096092d26a416511114b4884bad4d1675 Mon Sep 17 00:00:00 2001
From: Rafal Skolasinski <r.j.skolasinski@gmail.com>
Date: Wed, 11 Jul 2018 18:21:36 +0200
Subject: [PATCH] take site position from underlying discretizer lattice

Previously we were manually computing the position from
the site tag an an extra 'grid_spacing' parameter. Even
though we now have one additional Python function call,
using the position as determined by the underlying lattice
is less susceptible to bugs.

Closes #199.
---
 kwant/continuum/discretizer.py            | 14 ++++----------
 kwant/continuum/tests/test_discretizer.py | 10 ++++++++++
 2 files changed, 14 insertions(+), 10 deletions(-)

diff --git a/kwant/continuum/discretizer.py b/kwant/continuum/discretizer.py
index bd05342b..baef963c 100644
--- a/kwant/continuum/discretizer.py
+++ b/kwant/continuum/discretizer.py
@@ -582,16 +582,13 @@ def _return_string(expr, coords):
     return 'return {}'.format(output), map_func_calls, const_symbols, _cache
 
 
-def _assign_symbols(map_func_calls, grid_spacing,
-                    coords, onsite):
+def _assign_symbols(map_func_calls, coords, onsite):
     """Generate a series of assignments.
 
     Parameters
     ----------
     map_func_calls : dict
         mapping of function calls to assigned constants.
-    grid_spacing : int or float
-        Used to get site.pos from site.tag
     coords : sequence of strings
         If left as None coordinates will not be read from a site.
     onsite : bool
@@ -606,8 +603,8 @@ def _assign_symbols(map_func_calls, grid_spacing,
 
     if coords:
         site = 'site' if onsite else 'site1'
-        args = ', '.join(coords), str(grid_spacing), site
-        lines.append('({}, ) = {} * {}.tag'.format(*args))
+        args = ', '.join(coords), site
+        lines.append('({}, ) = {}.pos'.format(*args))
 
     for k, v in map_func_calls.items():
         lines.append("{} = {}".format(v, _print_sympy(k)))
@@ -667,10 +664,7 @@ def _builder_value(expr, coords, grid_spacing, onsite,
         else:
             return complex(expr)
 
-    lines = _assign_symbols(map_func_calls, onsite=onsite,
-                            grid_spacing=grid_spacing,
-                            coords=coords)
-
+    lines = _assign_symbols(map_func_calls, onsite=onsite, coords=coords)
     lines.append(return_string)
 
     separator = '\n    '
diff --git a/kwant/continuum/tests/test_discretizer.py b/kwant/continuum/tests/test_discretizer.py
index 19ec7454..54c8da65 100644
--- a/kwant/continuum/tests/test_discretizer.py
+++ b/kwant/continuum/tests/test_discretizer.py
@@ -563,6 +563,16 @@ def test_grid_input(ham, grid_offset, offset, norbs):
     assert tmp.lattice.norbs == norbs
 
 
+def test_grid_offset_passed_to_functions():
+    V = lambda x: x
+    grid = Monatomic([[1, ]], offset=[0.5, ])
+    tb = discretize('V(x)', 'x', grid=grid)
+    onsite = tb[tb.lattice(0)]
+    bools = [np.allclose(onsite(tb.lattice(i), V), V(tb.lattice(i).pos))
+             for i in [0, 1, 5]]
+    assert all(bools)
+
+
 @pytest.mark.parametrize("ham, coords, grid", [
     ("k_x", None, Monatomic([[1, 0]])),
     ("k_x", 'xy', Monatomic([[1, 0]])),
-- 
GitLab