From 5c0fc3a9115372030efa10a2eab5b0a8693dea39 Mon Sep 17 00:00:00 2001
From: Christoph Groth <christoph.groth@cea.fr>
Date: Mon, 15 May 2017 01:01:11 +0200
Subject: [PATCH] add 'lattice' attribute to builders returned by discretize

---
 kwant/continuum/discretizer.py            | 20 ++++++++++++++------
 kwant/continuum/tests/test_discretizer.py |  1 +
 2 files changed, 15 insertions(+), 6 deletions(-)

diff --git a/kwant/continuum/discretizer.py b/kwant/continuum/discretizer.py
index 2ee44857..1d830400 100644
--- a/kwant/continuum/discretizer.py
+++ b/kwant/continuum/discretizer.py
@@ -36,8 +36,9 @@ class _DiscretizedBuilder(builder.Builder):
     """A builder that is made from a discretized model and knows how to
     pretty-print itself."""
 
-    def __init__(self, symmetry=None, coords=[], **kwargs):
+    def __init__(self, coords, lattice, symmetry=None, **kwargs):
         super().__init__(symmetry, **kwargs)
+        self.lattice = lattice
         self._coords = coords
 
     def __str__(self):
@@ -120,8 +121,11 @@ def discretize(hamiltonian, coords=None, *, grid_spacing=1,
     -------
     model : `~kwant.builder.Builder`
         The translationally symmetric builder that corresponds to the provided
-        Hamiltonian.
-
+        Hamiltonian.  This builder instance belongs to a subclass of the
+        standard builder that may be printed to obtain the source code of the
+        value functions.  It also holds the discretization lattice (a
+        `~kwant.lattice.Monatomic` instance with lattice constant
+        `grid_spacing`) in the ``lattice`` attribute.
     """
     tb, coords = discretize_symbolic(hamiltonian, coords, locals=locals)
 
@@ -262,7 +266,11 @@ def build_discretized(tb_hamiltonian, coords, *, grid_spacing=1, locals=None):
     -------
     model : `~kwant.builder.Builder`
         The translationally symmetric builder that corresponds to the provided
-        Hamiltonian.
+        Hamiltonian.  This builder instance belongs to a subclass of the
+        standard builder that may be printed to obtain the source code of the
+        value functions.  It also holds the discretization lattice (a
+        `~kwant.lattice.Monatomic` instance with lattice constant
+        `grid_spacing`) in the ``lattice`` attribute.
 
     """
     if len(coords) == 0:
@@ -301,8 +309,8 @@ def build_discretized(tb_hamiltonian, coords, *, grid_spacing=1, locals=None):
     hoppings = {builder.HoppingKind(tuple(-i for i in d), lat): val
                 for d, val in tb.items()}
 
-    syst = _DiscretizedBuilder(lattice.TranslationalSymmetry(*prim_vecs),
-                               coords)
+    syst = _DiscretizedBuilder(coords, lat,
+                               lattice.TranslationalSymmetry(*prim_vecs))
     syst[lat(*onsite_zeros)] = onsite
     for hop, val in hoppings.items():
         syst[hop] = val
diff --git a/kwant/continuum/tests/test_discretizer.py b/kwant/continuum/tests/test_discretizer.py
index 523a5fa5..98a5cec8 100644
--- a/kwant/continuum/tests/test_discretizer.py
+++ b/kwant/continuum/tests/test_discretizer.py
@@ -308,6 +308,7 @@ def test_numeric_functions_basic_symbolic():
     for i in [0, 1, 3, 5]:
         builder = discretize(i, 'x')
         lat = next(iter(builder.sites()))[0]
+        assert builder.lattice is lat
         assert builder[lat(0)] == i
 
         p = dict(t=i)
-- 
GitLab