Browse code

Merge branch 'bugfix/norbs-specified-when-vectorized' into 'master'

Require that norbs are specified for abstract vectorized systems.
This was already a de-facto requirement for finalized Builders, and
it should be part of the abstract base class.

Closes #336

See merge request kwant/kwant!334

Joseph Weston authored on 22/11/2019 17:18:36
Showing 2 changed files
... ...
@@ -376,16 +376,16 @@ class VectorizedSystem(System, metaclass=abc.ABCMeta):
376 376
         conjugate to be added when evaluating the Hamiltonian, and 'parameters'
377 377
         contains a list of parameter names used when evaluating this term.
378 378
     site_arrays : sequence of SiteArray
379
-        The sites of the system.
380
-    site_ranges : None or Nx3 integer array
379
+        The sites of the system. The family of each site array must have
380
+        ``norbs`` specified.
381
+    site_ranges : Nx3 integer array
381 382
         Has 1 row per site array, plus one extra row.  Each row consists
382 383
         of ``(first_site, norbs, orb_offset)``: the index of the first
383 384
         site in the site array, the number of orbitals on each site in
384 385
         the site array, and the offset of the first orbital of the first
385 386
         site in the site array.  In addition, the final row has the form
386 387
         ``(len(graph.num_nodes), 0, tot_norbs)`` where ``tot_norbs`` is the
387
-        total number of orbitals in the system.  ``None`` if any site array
388
-        in 'site_arrays' does not have 'norbs' specified. Note 'site_ranges'
388
+        total number of orbitals in the system. Note 'site_ranges'
389 389
         is directly computable from 'site_arrays'.
390 390
     parameters : frozenset of strings
391 391
         The names of the parameters on which the system depends. This attribute
... ...
@@ -431,8 +431,6 @@ class VectorizedSystem(System, metaclass=abc.ABCMeta):
431 431
     def site_ranges(self):
432 432
         site_offsets = np.cumsum([0] + [len(arr) for arr in self.site_arrays])
433 433
         norbs = [arr.family.norbs for arr in self.site_arrays] + [0]
434
-        if any(norb is None for norb in norbs):
435
-            return None
436 434
         orb_offsets = np.cumsum(
437 435
             [0] + [len(arr) * arr.family.norbs for arr in self.site_arrays]
438 436
         )
... ...
@@ -740,15 +740,19 @@ def test_vectorized_hamiltonian_evaluation():
740 740
     )
741 741
 
742 742
 
743
-def test_vectorized_requires_norbs():
743
+@pytest.mark.parametrize("sym", [
744
+    builder.NoSymmetry(),
745
+    kwant.TranslationalSymmetry([-1]),
746
+])
747
+def test_vectorized_requires_norbs(sym):
744 748
 
745 749
     # Catch deprecation warning for lack of norbs
746 750
     with warnings.catch_warnings():
747 751
         warnings.simplefilter("ignore")
748
-        fam = builder.SimpleSiteFamily()
752
+        lat = kwant.lattice.chain()
749 753
 
750
-    syst = builder.Builder(vectorize=True)
751
-    syst[fam(0, 0)] = 1
754
+    syst = builder.Builder(sym, vectorize=True)
755
+    syst[lat(0)] = 1
752 756
 
753 757
     raises(ValueError, syst.finalized)
754 758