def test_update(): lat = builder.SimpleSiteFamily() syst = builder.Builder() syst[[lat(0, ), lat(1, )]] = 1 syst[lat(0, ), lat(1, )] = 1 other_syst = builder.Builder() other_syst[[lat(1, ), lat(2, )]] = 2 other_syst[lat(1, ), lat(2, )] = 1 lead0 = builder.Builder(VerySimpleSymmetry(-1)) lead0[lat(0, )] = 1 lead0[(lat(0, ), lat(1, ))] = 1 lead0 = builder.BuilderLead(lead0, [lat(0, )]) syst.leads.append(lead0) lead1 = builder.Builder(VerySimpleSymmetry(1)) lead1[lat(2, )] = 1 lead1[(lat(2, ), lat(1, ))] = 1 lead1 = builder.BuilderLead(lead1, [lat(2, )]) other_syst.leads.append(lead1) syst.update(other_syst) assert syst.leads == [lead0, lead1] expected = sorted([((0, ), 1), ((1, ), 2), ((2, ), 2)]) assert sorted(((s.tag, v) for s, v in syst.site_value_pairs())) == expected expected = sorted([((0, ), (1, ), 1), ((1, ), (2, ), 1)]) assert (sorted(((a.tag, b.tag, v) for (a, b), v in syst.hopping_value_pairs())) == expected)
def test_neighbors_not_in_single_domain(): sr = builder.Builder() lead = builder.Builder(VerySimpleSymmetry(-1)) fam = builder.SimpleSiteFamily() sr[(fam(x, y) for x in range(3) for y in range(3) if x >= y)] = 0 sr[builder.HoppingKind((1, 0), fam)] = 1 sr[builder.HoppingKind((0, 1), fam)] = 1 lead[(fam(0, y) for y in range(3))] = 0 lead[((fam(0, y), fam(1, y)) for y in range(3))] = 1 lead[((fam(0, y), fam(0, y + 1)) for y in range(2))] = 1 sr.leads.append(builder.BuilderLead(lead, [fam(i, i) for i in range(3)])) raises(ValueError, sr.finalized)
def test_finalization(): """Test the finalization of finite and infinite systems. In order to exactly verify the finalization, low-level features of the build module are used directly. This is not the way one would use a finalized system in normal code. """ def set_sites(dest): while len(dest) < n_sites: site = rng.randrange(size), rng.randrange(size) if site not in dest: dest[site] = random_onsite_hamiltonian(rng) def set_hops(dest, sites): while len(dest) < n_hops: a, b = rng.sample(list(sites), 2) if (a, b) not in dest and (b, a) not in dest: dest[a, b] = random_hopping_integral(rng) rng = Random(123) size = 20 n_sites = 120 n_hops = 500 # Make scattering region blueprint. sr_sites = {} set_sites(sr_sites) sr_hops = {} set_hops(sr_hops, sr_sites) # Make lead blueprint. possible_neighbors = rng.sample(list(sr_sites), n_sites // 2) lead_sites = {} for pn in possible_neighbors: lead_sites[pn] = random_hopping_integral(rng) set_sites(lead_sites) lead_hops = {} # Hoppings within a single lead unit cell set_hops(lead_hops, lead_sites) lead_sites_list = list(lead_sites) neighbors = set() for i in range(n_hops): while True: a = rng.choice(lead_sites_list) b = rng.choice(possible_neighbors) neighbors.add(b) b = b[0] - size, b[1] if rng.randrange(2): a, b = b, a if (a, b) not in lead_hops and (b, a) not in lead_hops: break lead_hops[a, b] = random_hopping_integral(rng) neighbors = sorted(neighbors) # Build scattering region from blueprint and test it. syst = builder.Builder() fam = kwant.lattice.general(ta.identity(2)) for site, value in sr_sites.items(): syst[fam(*site)] = value for hop, value in sr_hops.items(): syst[fam(*hop[0]), fam(*hop[1])] = value fsyst = syst.finalized() check_id_by_site(fsyst) check_onsite(fsyst, sr_sites) check_hoppings(fsyst, sr_hops) # check that sites are sorted assert fsyst.sites == tuple(sorted(fam(*site) for site in sr_sites)) # Build lead from blueprint and test it. lead = builder.Builder(kwant.TranslationalSymmetry((size, 0))) for site, value in lead_sites.items(): shift = rng.randrange(-5, 6) * size site = site[0] + shift, site[1] lead[fam(*site)] = value with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") lead.finalized() # Trigger the warning. assert len(w) == 1 assert issubclass(w[0].category, RuntimeWarning) assert "disconnected" in str(w[0].message) for (a, b), value in lead_hops.items(): shift = rng.randrange(-5, 6) * size a = a[0] + shift, a[1] b = b[0] + shift, b[1] lead[fam(*a), fam(*b)] = value flead = lead.finalized() all_sites = list(lead_sites) all_sites.extend((x - size, y) for (x, y) in neighbors) check_id_by_site(fsyst) check_onsite(flead, all_sites, check_values=False) check_onsite(flead, lead_sites, subset=True) check_hoppings(flead, lead_hops) # Attach lead to system with empty interface. syst.leads.append(builder.BuilderLead(lead, ())) raises(ValueError, syst.finalized) # Attach lead with improper interface. syst.leads[-1] = builder.BuilderLead( lead, 2 * tuple(builder.Site(fam, n) for n in neighbors)) raises(ValueError, syst.finalized) # Attach lead properly. syst.leads[-1] = builder.BuilderLead(lead, (builder.Site(fam, n) for n in neighbors)) fsyst = syst.finalized() assert len(fsyst.lead_interfaces) == 1 assert ([fsyst.sites[i].tag for i in fsyst.lead_interfaces[0]] == neighbors) # test that we cannot finalize a system with a badly sorted interface order raises(ValueError, builder.InfiniteSystem, lead, [builder.Site(fam, n) for n in reversed(neighbors)]) # site ordering independent of whether interface was specified flead_order = builder.InfiniteSystem( lead, [builder.Site(fam, n) for n in neighbors]) assert flead.sites == flead_order.sites syst.leads[-1] = builder.BuilderLead(lead, (builder.Site(fam, n) for n in neighbors)) fsyst = syst.finalized() assert len(fsyst.lead_interfaces) == 1 assert ([fsyst.sites[i].tag for i in fsyst.lead_interfaces[0]] == neighbors) # Add a hopping to the lead which couples two next-nearest cells and check # whether this leads to an error. a = rng.choice(lead_sites_list) b = rng.choice(possible_neighbors) b = b[0] + 2 * size, b[1] lead[fam(*a), fam(*b)] = random_hopping_integral(rng) raises(ValueError, lead.finalized)