def test_closest(): rng = ensure_rng(4) lat = lattice.general(((1, 0), (0.5, sqrt(3) / 2)), norbs=1) for i in range(50): point = 20 * rng.random_sample(2) closest = lat(*lat.closest(point)).pos assert np.linalg.norm(point - closest) <= 1 / sqrt(3) lat = lattice.general(rng.randn(3, 3), norbs=1) for i in range(50): tag = rng.randint(10, size=(3, )) assert lat.closest(lat(*tag).pos) == tag
def test_closest(): np.random.seed(4) lat = lattice.general(((1, 0), (0.5, sqrt(3) / 2))) for i in range(50): point = 20 * np.random.rand(2) closest = lat(*lat.closest(point)).pos assert np.linalg.norm(point - closest) <= 1 / sqrt(3) lat = lattice.general(np.random.randn(3, 3)) for i in range(50): tag = np.random.randint(10, size=(3,)) assert_equal(lat.closest(lat(*tag).pos), tag)
def test_general(): for lat in (lattice.general(((1, 0), (0.5, 0.5))), lattice.general(((1, 0), (0.5, sqrt(3)/2)), ((0, 0), (0, 1/sqrt(3))))): for sl in lat.sublattices: tag = (-5, 33) site = sl(*tag) assert tag == sl.closest(site.pos) # Test 2D lattice with 1 vector. lat = lattice.general([[1, 0]]) site = lat(0) raises(ValueError, lat, 0, 1)
def test_general(): for lat in (lattice.general(((1, 0), (0.5, 0.5))), lattice.general(((1, 0), (0.5, sqrt(3) / 2)), ((0, 0), (0, 1 / sqrt(3))))): for sl in lat.sublattices: tag = (-5, 33) site = sl(*tag) assert tag == sl.closest(site.pos) # Test 2D lattice with 1 vector. lat = lattice.general([[1, 0]]) site = lat(0) raises(ValueError, lat, 0, 1)
def test_norbs(): id_mat = np.identity(2) # Monatomic lattices assert lattice.general(id_mat).norbs == None assert lattice.general(id_mat, norbs=2).norbs == 2 # Polyatomic lattices lat = lattice.general(id_mat, basis=id_mat, norbs=None) for l in lat.sublattices: assert l.norbs == None lat = lattice.general(id_mat, basis=id_mat, norbs=2) for l in lat.sublattices: assert l.norbs == 2 lat = lattice.general(id_mat, basis=id_mat, norbs=[1, 2]) for l, n in zip(lat.sublattices, [1, 2]): assert l.norbs == n # should raise ValueError for # of norbs different to length of `basis` raises(ValueError, lattice.general, id_mat, id_mat, norbs=[]) raises(ValueError, lattice.general, id_mat, id_mat, norbs=[1, 2, 3]) # TypeError if Monatomic lattice raises(TypeError, lattice.general, id_mat, norbs=[]) # should raise ValueError if norbs not an integer raises(ValueError, lattice.general, id_mat, norbs=1.5) raises(ValueError, lattice.general, id_mat, id_mat, norbs=1.5) raises(ValueError, lattice.general, id_mat, id_mat, norbs=[1.5, 1.5]) # test that lattices with different norbs are compared `not equal` lat = lattice.general(id_mat, basis=id_mat, norbs=None) lat1 = lattice.general(id_mat, basis=id_mat, norbs=1) lat2 = lattice.general(id_mat, basis=id_mat, norbs=2) assert lat != lat1 assert lat != lat2 assert lat1 != lat2
def test_neighbors(): lat = lattice.honeycomb(1e-10, norbs=1) num_nth_nearest = [len(lat.neighbors(n)) for n in range(5)] assert num_nth_nearest == [2, 3, 6, 3, 6] lat = lattice.general([(0, 1e8, 0, 0), (0, 0, 1e8, 0)], norbs=1) num_nth_nearest = [len(lat.neighbors(n)) for n in range(5)] assert num_nth_nearest == [1, 2, 2, 2, 4] lat = lattice.chain(1e-10, norbs=1) num_nth_nearest = [len(lat.neighbors(n)) for n in range(5)] assert num_nth_nearest == 5 * [1]
def test_wire(): rng = ensure_rng(5) vecs = rng.randn(3, 3) vecs[0] = [1, 0, 0] center = rng.randn(3) lat = lattice.general(vecs, rng.randn(4, 3), norbs=1) syst = builder.Builder(lattice.TranslationalSymmetry((2, 0, 0))) def wire_shape(pos): pos = np.array(pos) return np.linalg.norm(pos[1:] - center[1:])**2 <= 8.6**2 syst[lat.shape(wire_shape, center)] = 0 sites2 = set(syst.sites()) syst = builder.Builder(lattice.TranslationalSymmetry((2, 0, 0))) syst[lat.wire(center, 8.6)] = 1 sites1 = set(syst.sites()) assert sites1 == sites2
def test_wire(): np.random.seed(5) vecs = np.random.randn(3, 3) vecs[0] = [1, 0, 0] center = np.random.randn(3) lat = lattice.general(vecs, np.random.randn(4, 3)) syst = builder.Builder(lattice.TranslationalSymmetry((2, 0, 0))) def wire_shape(pos): pos = np.array(pos) return np.linalg.norm(pos[1:] - center[1:])**2 <= 8.6**2 syst[lat.shape(wire_shape, center)] = 0 sites2 = set(syst.sites()) syst = builder.Builder(lattice.TranslationalSymmetry((2, 0, 0))) syst[lat.wire(center, 8.6)] = 1 sites1 = set(syst.sites()) assert sites1 == sites2
def test_translational_symmetry_reversed(): np.random.seed(30) lat = lattice.general(np.identity(3)) sites = [lat(i, j, k) for i in range(-2, 6) for j in range(-2, 6) for k in range(-2, 6)] for i in range(4): periods = np.random.randint(-5, 5, (3, 3)) try: sym = lattice.TranslationalSymmetry(*periods) rsym = sym.reversed() for site in sites: assert_equal(sym.to_fd(site), rsym.to_fd(site)) assert_equal(sym.which(site), -rsym.which(site)) vec = np.array([1, 1, 1]) assert_equal(sym.act(vec, site), rsym.act(-vec, site)) except ValueError: pass
def test_translational_symmetry_reversed(): rng = ensure_rng(30) lat = lattice.general(np.identity(3), norbs=1) sites = [lat(i, j, k) for i in range(-2, 6) for j in range(-2, 6) for k in range(-2, 6)] for i in range(4): periods = rng.randint(-5, 5, (3, 3)) try: sym = lattice.TranslationalSymmetry(*periods) rsym = sym.reversed() for site in sites: assert sym.to_fd(site) == rsym.to_fd(site) assert sym.which(site) == -rsym.which(site) vec = np.array([1, 1, 1]) assert sym.act(vec, site), rsym.act(-vec == site) except ValueError: pass
def test_norbs(): id_mat = np.identity(2) # Monatomic lattices # Catch deprecation warning with warnings.catch_warnings(): warnings.simplefilter("ignore") assert lattice.general(id_mat).norbs is None assert lattice.general(id_mat, norbs=2).norbs == 2 # Polyatomic lattices # Catch deprecation warning with warnings.catch_warnings(): warnings.simplefilter("ignore") lat = lattice.general(id_mat, basis=id_mat, norbs=None) for l in lat.sublattices: assert l.norbs is None lat = lattice.general(id_mat, basis=id_mat, norbs=2) for l in lat.sublattices: assert l.norbs == 2 lat = lattice.general(id_mat, basis=id_mat, norbs=[1, 2]) for l, n in zip(lat.sublattices, [1, 2]): assert l.norbs == n # should raise ValueError for # of norbs different to length of `basis` raises(ValueError, lattice.general, id_mat, id_mat, norbs=[]) raises(ValueError, lattice.general, id_mat, id_mat, norbs=[1, 2, 3]) # TypeError if Monatomic lattice raises(TypeError, lattice.general, id_mat, norbs=[]) # should raise ValueError if norbs not an integer raises(ValueError, lattice.general, id_mat, norbs=1.5) raises(ValueError, lattice.general, id_mat, id_mat, norbs=1.5) raises(ValueError, lattice.general, id_mat, id_mat, norbs=[1.5, 1.5]) # should raise ValueError if norbs is <= 0 raises(ValueError, lattice.general, id_mat, norbs=0) raises(ValueError, lattice.general, id_mat, norbs=-1) # test that lattices with different norbs are compared `not equal` # Catch deprecation warning with warnings.catch_warnings(): warnings.simplefilter("ignore") lat = lattice.general(id_mat, basis=id_mat, norbs=None) lat1 = lattice.general(id_mat, basis=id_mat, norbs=1) lat2 = lattice.general(id_mat, basis=id_mat, norbs=2) assert lat != lat1 assert lat != lat2 assert lat1 != lat2
def model_to_builder(model, norbs, lat_vecs, atom_coords, *, coeffs=None): """Make a `~kwant.builder.Builder` out of qsymm.Models or qsymm.BlochModels. Parameters ---------- model : qsymm.Model, qsymm.BlochModel, or an iterable thereof The Hamiltonian (or terms of the Hamiltonian) to convert to a Builder. norbs : OrderedDict or sequence of pairs Maps sites to the number of orbitals per site in a unit cell. lat_vecs : list of arrays Lattice vectors of the underlying tight binding lattice. atom_coords : list of arrays Positions of the sites (or atoms) within a unit cell. The ordering of the atoms is the same as in norbs. coeffs : list of sympy.Symbol, default None. Constant prefactors for the individual terms in model, if model is a list of multiple objects. If model is a single Model or BlochModel object, this argument is ignored. By default assigns the coefficient c_n to element model[n]. Returns ------- syst : `~kwant.builder.Builder` The unfinalized Kwant system representing the qsymm Model(s). Notes ----- Onsite terms that are not provided in the input model are set to zero by default. The input model(s) representing the tight binding Hamiltonian in Bloch form should follow the convention where the difference in the real space atomic positions appear in the Bloch factors. """ def make_int(R): # If close to an integer array convert to integer tinyarray, else # return None R_int = ta.array(np.round(R), int) if qsymm.linalg.allclose(R, R_int): return R_int else: return None def term_onsite(onsites_dict, hopping_dict, hop_mat, atoms, sublattices, coords_dict): """Find the Kwant onsites and hoppings in a qsymm.BlochModel term that has no lattice translation in the Bloch factor. """ for atom1, atom2 in it.product(atoms, atoms): # Subblock within the same sublattice is onsite hop = hop_mat[ranges[atom1], ranges[atom2]] if sublattices[atom1] == sublattices[atom2]: onsites_dict[atom1] += Model({coeff: hop}, momenta=momenta) # Blocks between sublattices are hoppings between sublattices # at the same position. # Only include nonzero hoppings elif not allclose(hop, 0): if not allclose(np.array(coords_dict[atom1]), np.array(coords_dict[atom2])): raise ValueError( "Position of sites not compatible with qsymm model.") lat_basis = np.array(zer) hop = Model({coeff: hop}, momenta=momenta) hop_dir = builder.HoppingKind(-lat_basis, sublattices[atom1], sublattices[atom2]) hopping_dict[hop_dir] += hop return onsites_dict, hopping_dict def term_hopping(hopping_dict, hop_mat, atoms, sublattices, coords_dict): """Find Kwant hoppings in a qsymm.BlochModel term that has a lattice translation in the Bloch factor. """ # Iterate over combinations of atoms, set hoppings between each for atom1, atom2 in it.product(atoms, atoms): # Take the block from atom1 to atom2 hop = hop_mat[ranges[atom1], ranges[atom2]] # Only include nonzero hoppings if allclose(hop, 0): continue # Adjust hopping vector to Bloch form basis r_lattice = ( r_vec + np.array(coords_dict[atom1]) - np.array(coords_dict[atom2]) ) # Bring vector to basis of lattice vectors lat_basis = np.linalg.solve(np.vstack(lat_vecs).T, r_lattice) lat_basis = make_int(lat_basis) # Should only have hoppings that are integer multiples of # lattice vectors if lat_basis is not None: hop_dir = builder.HoppingKind(-lat_basis, sublattices[atom1], sublattices[atom2]) # Set the hopping as the matrix times the hopping amplitude hopping_dict[hop_dir] += Model({coeff: hop}, momenta=momenta) else: raise RuntimeError('A nonzero hopping not matching a ' 'lattice vector was found.') return hopping_dict # Disambiguate single model instances from iterables thereof. Because # Model is itself iterable (subclasses dict) this is a bit cumbersome. if isinstance(model, Model): # BlochModel can't yet handle getting a Blochmodel as input if not isinstance(model, BlochModel): model = BlochModel(model) else: model = BlochModel(hamiltonian_from_family( model, coeffs=coeffs, nsimplify=False, tosympy=False)) # 'momentum' and 'zer' are used in the closures defined above, so don't # move these declarations down. momenta = model.momenta if len(momenta) != len(lat_vecs): raise ValueError("Dimension of the lattice and number of " "momenta do not match.") zer = [0] * len(momenta) # Subblocks of the Hamiltonian for different atoms. N = 0 if not any([isinstance(norbs, OrderedDict), isinstance(norbs, list), isinstance(norbs, tuple)]): raise ValueError('norbs must be OrderedDict, tuple, or list.') else: norbs = OrderedDict(norbs) ranges = dict() for a, n in norbs.items(): ranges[a] = slice(N, N + n) N += n # Extract atoms and number of orbitals per atom, # store the position of each atom atoms, orbs = zip(*norbs.items()) coords_dict = dict(zip(atoms, atom_coords)) # Make the kwant lattice lat = lattice.general(lat_vecs, atom_coords, norbs=orbs) # Store sublattices by name sublattices = dict(zip(atoms, lat.sublattices)) # Keep track of the hoppings and onsites by storing those # which have already been set. hopping_dict = defaultdict(dict) onsites_dict = defaultdict(dict) # Iterate over all terms in the model. for key, hop_mat in model.items(): # Determine whether this term is an onsite or a hopping, extract # overall symbolic coefficient if any, extract the exponential # part describing the hopping if present. r_vec, coeff = key # Onsite term; modifies onsites_dict and hopping_dict in-place if allclose(r_vec, 0): term_onsite( onsites_dict, hopping_dict, hop_mat, atoms, sublattices, coords_dict) # Hopping term; modifies hopping_dict in-place else: term_hopping(hopping_dict, hop_mat, atoms, sublattices, coords_dict) # If some onsite terms are not set, we set them to zero. for atom in atoms: if atom not in onsites_dict: onsites_dict[atom] = Model( {sympy.numbers.One(): np.zeros((norbs[atom], norbs[atom]))}, momenta=momenta) # Make the Kwant system, and set all onsites and hoppings. sym = lattice.TranslationalSymmetry(*lat_vecs) syst = builder.Builder(sym) # Iterate over all onsites and set them for atom, onsite in onsites_dict.items(): syst[sublattices[atom](*zer)] = onsite.lambdify(onsite=True) # Finally, iterate over all the hoppings and set them for direction, hopping in hopping_dict.items(): syst[direction] = hopping.lambdify(hopping=True) return syst
def test_lattice_constraints(prim_vecs, basis): with pytest.raises(ValueError): lattice.general(prim_vecs, basis, norbs=1)
def test_monatomic_lattice(): lat = lattice.square(norbs=1) lat2 = lattice.general(np.identity(2), norbs=1) lat3 = lattice.square(name='no', norbs=1) assert len(set([lat, lat2, lat3, lat(0, 0), lat2(0, 0), lat3(0, 0)])) == 4
def test_translational_symmetry(): ts = lattice.TranslationalSymmetry f2 = lattice.general(np.identity(2), norbs=1) f3 = lattice.general(np.identity(3), norbs=1) shifted = lambda site, delta: site.family(*ta.add(site.tag, delta)) raises(ValueError, ts, (0, 0, 4), (0, 5, 0), (0, 0, 2)) sym = ts((3.3, 0)) raises(ValueError, sym.add_site_family, f2) # Test lattices with dimension smaller than dimension of space. f2in3 = lattice.general([[4, 4, 0], [4, -4, 0]], norbs=1) sym = ts((8, 0, 0)) sym.add_site_family(f2in3) sym = ts((8, 0, 1)) raises(ValueError, sym.add_site_family, f2in3) # Test automatic fill-in of transverse vectors. sym = ts((1, 2)) sym.add_site_family(f2) assert sym.site_family_data[f2][2] != 0 sym = ts((1, 0, 2), (3, 0, 2)) sym.add_site_family(f3) assert sym.site_family_data[f3][2] != 0 transl_vecs = np.array([[10, 0], [7, 7]], dtype=int) sym = ts(*transl_vecs) assert sym.num_directions == 2 sym2 = ts(*transl_vecs[:1, :]) sym2.add_site_family(f2, transl_vecs[1:, :]) for site in [f2(0, 0), f2(4, 0), f2(2, 1), f2(5, 5), f2(15, 6)]: assert sym.in_fd(site) assert sym2.in_fd(site) assert sym.which(site) == (0, 0) assert sym2.which(site) == (0, ) for v in [(1, 0), (0, 1), (-1, 0), (0, -1), (5, 10), (-111, 573)]: site2 = shifted(site, np.dot(v, transl_vecs)) assert not sym.in_fd(site2) assert (v[0] != 0) != sym2.in_fd(site2) assert sym.to_fd(site2) == site assert (v[1] == 0) == (sym2.to_fd(site2) == site) assert sym.which(site2) == v assert sym2.which(site2) == v[:1] for hop in [(0, 0), (100, 0), (0, 5), (-2134, 3213)]: assert (sym.to_fd(site2, shifted(site2, hop)) == (site, shifted(site, hop))) # Test act for hoppings belonging to different lattices. f2p = lattice.general(2 * np.identity(2), norbs=1) sym = ts(*(2 * np.identity(2))) assert sym.act((1, 1), f2(0, 0), f2p(0, 0)) == (f2(2, 2), f2p(1, 1)) assert sym.act((1, 1), f2p(0, 0), f2(0, 0)) == (f2p(1, 1), f2(2, 2)) # Test add_site_family on random lattices and symmetries by ensuring that # it's possible to add site groups that are compatible with a randomly # generated symmetry with proper vectors. rng = ensure_rng(30) vec = rng.randn(3, 5) lat = lattice.general(vec, norbs=1) total = 0 for k in range(1, 4): for i in range(10): sym_vec = rng.randint(-10, 10, size=(k, 3)) if np.linalg.matrix_rank(sym_vec) < k: continue total += 1 sym_vec = np.dot(sym_vec, vec) sym = ts(*sym_vec) sym.add_site_family(lat) assert total > 20
def test_translational_symmetry(): ts = lattice.TranslationalSymmetry f2 = lattice.general(np.identity(2)) f3 = lattice.general(np.identity(3)) shifted = lambda site, delta: site.family(*ta.add(site.tag, delta)) assert_raises(ValueError, ts, (0, 0, 4), (0, 5, 0), (0, 0, 2)) sym = ts((3.3, 0)) assert_raises(ValueError, sym.add_site_family, f2) # Test lattices with dimension smaller than dimension of space. f2in3 = lattice.general([[4, 4, 0], [4, -4, 0]]) sym = ts((8, 0, 0)) sym.add_site_family(f2in3) sym = ts((8, 0, 1)) assert_raises(ValueError, sym.add_site_family, f2in3) # Test automatic fill-in of transverse vectors. sym = ts((1, 2)) sym.add_site_family(f2) assert_not_equal(sym.site_family_data[f2][2], 0) sym = ts((1, 0, 2), (3, 0, 2)) sym.add_site_family(f3) assert_not_equal(sym.site_family_data[f3][2], 0) transl_vecs = np.array([[10, 0], [7, 7]], dtype=int) sym = ts(*transl_vecs) assert_equal(sym.num_directions, 2) sym2 = ts(*transl_vecs[:1, :]) sym2.add_site_family(f2, transl_vecs[1:, :]) for site in [f2(0, 0), f2(4, 0), f2(2, 1), f2(5, 5), f2(15, 6)]: assert sym.in_fd(site) assert sym2.in_fd(site) assert_equal(sym.which(site), (0, 0)) assert_equal(sym2.which(site), (0,)) for v in [(1, 0), (0, 1), (-1, 0), (0, -1), (5, 10), (-111, 573)]: site2 = shifted(site, np.dot(v, transl_vecs)) assert not sym.in_fd(site2) assert (v[0] != 0) != sym2.in_fd(site2) assert_equal(sym.to_fd(site2), site) assert (v[1] == 0) == (sym2.to_fd(site2) == site) assert_equal(sym.which(site2), v) assert_equal(sym2.which(site2), v[:1]) for hop in [(0, 0), (100, 0), (0, 5), (-2134, 3213)]: assert_equal(sym.to_fd(site2, shifted(site2, hop)), (site, shifted(site, hop))) # Test act for hoppings belonging to different lattices. f2p = lattice.general(2 * np.identity(2)) sym = ts(*(2 * np.identity(2))) assert sym.act((1, 1), f2(0, 0), f2p(0, 0)) == (f2(2, 2), f2p(1, 1)) assert sym.act((1, 1), f2p(0, 0), f2(0, 0)) == (f2p(1, 1), f2(2, 2)) # Test add_site_family on random lattices and symmetries by ensuring that # it's possible to add site groups that are compatible with a randomly # generated symmetry with proper vectors. np.random.seed(30) vec = np.random.randn(3, 5) lat = lattice.general(vec) total = 0 for k in range(1, 4): for i in range(10): sym_vec = np.random.randint(-10, 10, size=(k, 3)) if np.linalg.matrix_rank(sym_vec) < k: continue total += 1 sym_vec = np.dot(sym_vec, vec) sym = ts(*sym_vec) sym.add_site_family(lat) assert total > 20
def test_monatomic_lattice(): lat = lattice.square() lat2 = lattice.general(np.identity(2)) lat3 = lattice.square(name="no") assert len(set([lat, lat2, lat3, lat(0, 0), lat2(0, 0), lat3(0, 0)])) == 4