Browse code

replace '_count_norbs' with 'site_ranges'

The removed logic is an artifact from when vectorized and
unvectorized systems shared the same logic for 'hamiltonian_submatrix'.
This was accidentally left in when we reverted to the old logic
for unvectorized systems.

Joseph Weston authored on 26/11/2019 14:56:59
Showing 1 changed files
... ...
@@ -453,68 +453,6 @@ def _vectorized_make_dense(subgraphs, hams, long [:] norbs, long [:] orb_offsets
453 453
     return mat
454 454
 
455 455
 
456
-def _count_norbs(syst, site_offsets, hams, args=(), params=None):
457
-    """Return the norbs and orbital offset of each site family in 'syst'
458
-
459
-    Parameters
460
-    ----------
461
-    syst : `kwant.system.System`
462
-    site_offsets : sequence of int
463
-        Contains the index of the first site for each site array
464
-        in 'syst.site_arrays'.
465
-    hams : sequence of arrays or 'None'
466
-        The Hamiltonian for each term in 'syst.terms'. 'None'
467
-        if the corresponding term has not been evaluated.
468
-    args, params : positional and keyword arguments to the system Hamiltonian
469
-    """
470
-    site_ranges = syst.site_ranges
471
-    if site_ranges is None:
472
-        # NOTE: Remove this branch once we can rely on
473
-        #       site families storing the norb information.
474
-
475
-        site_arrays = syst.site_arrays
476
-        family_norbs = {s.family: None for s in site_arrays}
477
-
478
-        # Compute the norbs per site family using already evaluated
479
-        # Hamiltonian terms where possible
480
-        for ham, t in zip(hams, syst.terms):
481
-            (to_w, from_w), _ = syst.subgraphs[t.subgraph]
482
-            if ham is not None:
483
-                family_norbs[site_arrays[to_w].family] = ham.shape[1]
484
-                family_norbs[site_arrays[from_w].family] = ham.shape[2]
485
-
486
-        # Evaluate Hamiltonian terms where we do not already have them
487
-        for n, t in enumerate(syst.terms):
488
-            (to_w, from_w), _ = syst.subgraphs[t.subgraph]
489
-            to_fam = site_arrays[to_w].family
490
-            from_fam = site_arrays[from_w].family
491
-            if family_norbs[to_fam] is None or family_norbs[from_fam] is None:
492
-                ham = syst.hamiltonian_term(n, args=args, params=params)
493
-                family_norbs[to_fam] = ham.shape[1]
494
-                family_norbs[from_fam] = ham.shape[2]
495
-
496
-        # This case is technically allowed by the format (some sites present
497
-        # but no hamiltonian terms that touch them) but is very unlikely.
498
-        if any(norbs is None for norbs in family_norbs.values()):
499
-            raise ValueError("Cannot determine the number of orbitals for "
500
-                             "some site families.")
501
-
502
-        orb_offset = 0
503
-        site_ranges = []
504
-        for start, site_array in zip(site_offsets, syst.site_arrays):
505
-            norbs = family_norbs[site_array.family]
506
-            site_ranges.append((start, norbs, orb_offset))
507
-            orb_offset += len(site_array) * norbs
508
-        site_ranges.append((site_offsets[-1], 0, orb_offset))
509
-        site_ranges = np.array(site_ranges)
510
-
511
-    _, norbs, orb_offsets = site_ranges.transpose()
512
-    # The final (extra) element in orb_offsets is the total number of orbitals
513
-    assert len(orb_offsets) == len(syst.site_arrays) + 1
514
-
515
-    return norbs, orb_offsets
516
-
517
-
518 456
 def _expand_norbs(compressed_norbs, site_offsets):
519 457
     "Return norbs per site, given norbs per site array."
520 458
     norbs = np.empty(site_offsets[-1], int)
... ...
@@ -586,8 +524,7 @@ def vectorized_hamiltonian_submatrix(self, args=(), sparse=False,
586 524
         if t.hermitian
587 525
     ]
588 526
 
589
-    norbs, orb_offsets = _count_norbs(self, site_offsets, hams,
590
-                                      args=args, params=params)
527
+    _, norbs, orb_offsets = self.site_ranges.transpose()
591 528
 
592 529
     func = _vectorized_make_sparse if sparse else _vectorized_make_dense
593 530
     mat = func(subgraphs, hams, norbs, orb_offsets, site_offsets)
... ...
@@ -642,13 +579,7 @@ def vectorized_cell_hamiltonian(self, args=(), sparse=False, *, params=None):
642 579
         if self.terms[n].hermitian
643 580
     ]
644 581
 
645
-    # _count_norbs requires passing hamiltonians for all terms, or 'None'
646
-    # if it has not been evaluated
647
-    all_hams = [None] * len(self.terms)
648
-    for n, ham in zip(cell_terms, hams):
649
-        all_hams[n] = ham
650
-    norbs, orb_offsets = _count_norbs(self, site_offsets, all_hams,
651
-                                      args=args, params=params)
582
+    _, norbs, orb_offsets = self.site_ranges.transpose()
652 583
 
653 584
     shape = (orb_offsets[next_cell], orb_offsets[next_cell])
654 585
     func = _vectorized_make_sparse if sparse else _vectorized_make_dense
... ...
@@ -719,17 +650,7 @@ def vectorized_inter_cell_hopping(self, args=(), sparse=False, *, params=None):
719 650
         for (to_sa, from_sa), (to_off, from_off) in subgraphs
720 651
     ]
721 652
 
722
-    # _count_norbs requires passing hamiltonians for all terms, or 'None'
723
-    # if it has not been evaluated
724
-    all_hams = [None] * len(self.terms)
725
-    for n, ham in zip(inter_cell_hopping_terms, inter_cell_hams):
726
-        all_hams[n] = ham
727
-    for n, ham in zip(reversed_inter_cell_hopping_terms,
728
-                      reversed_inter_cell_hams):
729
-        # Transpose to get back correct shape wrt. original term
730
-        all_hams[n] = ham.transpose(0, 2, 1)
731
-    norbs, orb_offsets = _count_norbs(self, site_offsets, all_hams,
732
-                                      args=args, params=params)
653
+    _, norbs, orb_offsets = self.site_ranges.transpose()
733 654
 
734 655
     shape = (orb_offsets[next_cell],
735 656
              orb_offsets[len(self.site_arrays) - next_cell])