From b1879be54deeaad71cef8dcce79e6e6cbe1217bf Mon Sep 17 00:00:00 2001
From: Rafal Skolasinski <r.j.skolasinski@gmail.com>
Date: Wed, 14 Feb 2018 14:42:57 +0100
Subject: [PATCH] fix problem in discretizer that occur when onsite is set to
 "int(1)"

Running "kwant.continuum.discretize('k_x**2 + 1')" was resulting
in an "AssertionError". This happend because "1 * expr = expr"
in SymPy and therefore a wrong argument has been passed to
"_read_offset" function.
---
 kwant/continuum/discretizer.py            | 6 +++---
 kwant/continuum/tests/test_discretizer.py | 4 ++++
 2 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/kwant/continuum/discretizer.py b/kwant/continuum/discretizer.py
index ad79642a..7fcc9e01 100644
--- a/kwant/continuum/discretizer.py
+++ b/kwant/continuum/discretizer.py
@@ -409,10 +409,10 @@ def _discretize_expression(expression, coords):
     def _extract_hoppings(expr):
         """Read hoppings and perform shortening operation."""
         expr = sympy.expand(expr)
-        summands = expr.as_ordered_terms()
+        summands = [e.as_ordered_factors() for e in expr.as_ordered_terms()]
 
-        offset = [_read_offset(s.args[-1]) for s in summands]
-        coeffs = [sympy.Mul(*s.args[:-1]) for s in summands]
+        offset = [_read_offset(s[-1]) for s in summands]
+        coeffs = [sympy.Mul(*s[:-1]) for s in summands]
         offset = np.array(offset, dtype=int)
         # rescale the offsets for each coordinate by their greatest
         # common divisor across the summands. e.g:
diff --git a/kwant/continuum/tests/test_discretizer.py b/kwant/continuum/tests/test_discretizer.py
index 149150d5..8d0fbb1a 100644
--- a/kwant/continuum/tests/test_discretizer.py
+++ b/kwant/continuum/tests/test_discretizer.py
@@ -104,6 +104,10 @@ def test_simple_derivations(commutative):
     kx, ky, kz = sympy.symbols('k_x k_y k_z', commutative=commutative)
     test = {
         kx**2                   : {(0,): 2/a**2, (1,): -1/a**2},
+        kx**2 + 1               : {(0,): 1 + 2/a**2, (1,): -1/a**2},
+        kx**2 + 2               : {(0,): 2 + 2/a**2, (1,): -1/a**2},
+        kx**2 + 1.0             : {(0,): 1.0 + 2/a**2, (1,): -1/a**2},
+        kx**2 + 2.0             : {(0,): 2.0 + 2/a**2, (1,): -1/a**2},
         kx**2 + ky**2           : {(0, 1): -1/a**2, (0, 0): 4/a**2,
                                    (1, 0): -1/a**2},
         kx**2 + ky**2 + kz**2   : {(1, 0, 0): -1/a**2, (0, 0, 1): -1/a**2,
-- 
GitLab