From 6deb130f9d2664fbd234c7c9a27ca8c34c618ab3 Mon Sep 17 00:00:00 2001 From: Anton Akhmerov <anton.akhmerov@gmail.com> Date: Tue, 10 Jul 2012 00:41:30 +0200 Subject: [PATCH] catch bug due to numpy multicasting, write test for it --- kwant/system.py | 10 +++++----- kwant/tests/test_system.py | 4 ++++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/kwant/system.py b/kwant/system.py index ed15e868..e1ca468e 100644 --- a/kwant/system.py +++ b/kwant/system.py @@ -125,12 +125,12 @@ class System(object): n_j = to_coord.get(j) if n_j is None: continue - try: - h_sub[to_off[n_j] : to_off[n_j + 1], - from_off[n_i] : from_off[n_i + 1]] = \ - ham(j, i) if j != i else diag[i] - except ValueError: + h = ham(j, i) if j != i else diag[i] + shape = (1, 1) if np.isscalar(h) else h.shape + if shape != (to_norb[n_j], from_norb[n_i]): raise ValueError(msg.format(i, j)) + h_sub[to_off[n_j] : to_off[n_j + 1], + from_off[n_i] : from_off[n_i + 1]] = h return h_sub gr = self.graph diff --git a/kwant/tests/test_system.py b/kwant/tests/test_system.py index 2411b0d7..893b4637 100644 --- a/kwant/tests/test_system.py +++ b/kwant/tests/test_system.py @@ -54,6 +54,10 @@ def test_hamiltonian_submatrix(): sys2 = sys.finalized() assert_raises(ValueError, sys2.hamiltonian_submatrix) assert_raises(ValueError, sys2.hamiltonian_submatrix, None, None, True) + sys[(0,), (2,)] = 1 + sys2 = sys.finalized() + assert_raises(ValueError, sys2.hamiltonian_submatrix) + assert_raises(ValueError, sys2.hamiltonian_submatrix, None, None, True) def test_energies(): sys = kwant.Builder(kwant.TranslationalSymmetry([(-1, 0)])) -- GitLab