def test_superindex_from_index(): latt = Lattice(np.eye(2)) latt.add_atom() latt.add_connections() latt.build((4, 4), primitive=False) for i in range(latt.num_sites): ind = latt.indices[i] assert latt.superindex_from_index(ind) == i
def test_add_connection(): latt = Lattice(np.eye(2)) latt.add_atom([0.0, 0.0], atom="A") latt.add_atom([0.5, 0.5], atom="B") latt.add_connection("A", "A", 1) latt.add_connection("A", "B", 1) latt.analyze() # Assert neighbor atom index is right assert all(latt.get_neighbors(alpha=0, distidx=0)[:, -1] == 1) assert all(latt.get_neighbors(alpha=0, distidx=1)[:, -1] == 0) assert all(latt.get_neighbors(alpha=1, distidx=0)[:, -1] == 0)
def test_build_min_neighbors(): latt = Lattice(np.eye(2)) latt.add_atom() latt.add_connections() shape = Circle([0, 0], radius=5) latt.build(shape, min_neighbors=2) assert_array_equal(latt.data.get_limits(), [[-4.0, -4.0], [4.0, 4.0]])
def test_analyze_exceptions(): latt = Lattice(np.eye(2)) with pytest.raises(NoAtomsError): latt.analyze() latt.add_atom() with pytest.raises(NoConnectionsError): latt.analyze()
def test_get_alpha(): latt = Lattice(np.eye(2)) at1 = latt.add_atom([0.0, 0.0], atom="A") at2 = latt.add_atom([0.5, 0.5], atom="B") assert latt.get_alpha("A") == [0] assert latt.get_alpha(at1) == 0 assert latt.get_alpha("B") == [1] assert latt.get_alpha(at2) == 1 latt = Lattice(np.eye(2)) latt.add_atom([0.0, 0.0], atom="A") latt.add_atom([0.5, 0.5], atom="A") assert latt.get_alpha("A") == [0, 1]
def test_fromdict(latt): d = latt.todict() latt2 = Lattice.fromdict(d) assert_array_equal(latt.vectors, latt2.vectors) assert_array_equal(latt.atom_positions, latt2.atom_positions) assert_array_equal(latt._connections, latt2._connections) assert_array_equal(latt.num_neighbors, latt2.num_neighbors)
def test_brillouin_zone(): latt = Lattice.square() bz = latt.brillouin_zone() expected = [[-1.0, -1.0], [1.0, -1.0], [-1.0, 1.0], [1.0, 1.0]] assert_array_equal(bz.vertices / np.pi, expected) expected = [[0, 1], [0, 2], [1, 3], [2, 3]] assert_array_equal(bz.edges, expected)
def test_add_atom(dim): latt = Lattice(np.eye(dim)) latt.add_atom() assert_array_equal(latt.atom_positions[0], np.zeros(dim)) with pytest.raises(SiteOccupiedError): latt.add_atom() with pytest.raises(ValueError): latt.add_atom(np.zeros(4))
def _benchmark_build_periodic(latt: lp.Lattice, sizes, runs, **kwargs): profiler = Profiler() data = np.zeros((len(sizes), 4)) for i, size in enumerate(sizes): t, mem, per = 0.0, 0.0, 0.0 for _ in range(runs): profiler.start() latt.build(np.ones(latt.dim) * size, **kwargs) t += profiler.seconds mem += profiler.memory profiler.start() latt.set_periodic(True) per += profiler.seconds data[i, 0] = latt.num_sites data[i, 1] = t / runs data[i, 2] = mem / runs data[i, 3] = per / runs return data
def test_get_atom(): latt = Lattice(np.eye(2)) at1 = latt.add_atom([0.0, 0.0], atom="A") at2 = latt.add_atom([0.5, 0.5], atom="B") assert latt.get_atom("A") == at1 assert latt.get_atom(0) == at1 assert latt.get_atom("B") == at2 assert latt.get_atom(1) == at2
def test_get_base_atom_dict(): latt = Lattice(np.eye(2)) ata = latt.add_atom([0, 0], atom="A") atb = latt.add_atom([0.5, 0], atom="B") latt.add_atom([0.5, 0.5], atom="B") result = latt.get_base_atom_dict() assert len(result) == 2 assert_array_equal(result[ata], [[0, 0]]) assert_array_equal(result[atb], [[0.5, 0.0], [0.5, 0.5]])
def test_get_position(nvec, alpha): latt = Lattice(np.eye(2)) latt.add_atom([0.0, 0.0], atom="A") latt.add_atom([0.5, 0.5], atom="B") pos = latt.translate(nvec, latt.atom_positions[alpha]) assert_allclose(latt.get_position(nvec, alpha), pos)
def test_to_dict(): latt = Lattice(np.eye(2)) latt.add_atom() latt.add_connections() latt.build((5, 5)) d = latt.todict() expected = [[1.0, 0.0], [0.0, 1.0]] assert_array_equal(d["vectors"], expected) expected = [[0.0, 0.0]] assert_array_equal(d["positions"], expected) expected = [[1]] assert_array_equal(d["connections"], expected) expected = [5.0, 5.0] assert_array_equal(d["shape"], expected)
def test_get_positions(nvecs, alpha): latt = Lattice(np.eye(2)) latt.add_atom([0.0, 0.0], atom="A") latt.add_atom([0.5, 0.5], atom="B") indices = np.array([[*nvec, alpha] for nvec in nvecs]) results = latt.get_positions(indices) for res, nvec in zip(results, nvecs): pos = latt.translate(nvec, latt.atom_positions[alpha]) assert_allclose(res, pos)
def test_alpha(): latt = Lattice(np.eye(2)) latt.add_atom([0.0, 0.0], "A") latt.add_atom([0.5, 0.5], "B") latt.add_connection("A", "A", 1) latt.add_connection("A", "B", 1) latt.analyze() latt.build((4, 4), primitive=False) assert latt.alpha(0) == 0 assert latt.alpha(1) == 1 assert latt.alpha(2) == 0 assert latt.alpha(3) == 1
def test_append(): latt = Lattice(np.eye(2)) latt.add_atom() latt.add_connections() latt.build((4, 4), primitive=False) latt2 = latt.copy() latt.append(latt2) assert_elements_equal1d(latt.nearest_neighbors(20), [15, 21, 25]) assert_elements_equal1d(latt.nearest_neighbors(21), [16, 20, 22, 26]) assert_elements_equal1d(latt.nearest_neighbors(22), [17, 21, 23, 27]) assert_elements_equal1d(latt.nearest_neighbors(23), [18, 22, 24, 28]) assert_elements_equal1d(latt.nearest_neighbors(24), [19, 23, 29])
def test_compute_connections(): latt = Lattice(np.eye(2)) latt.add_atom() latt.add_connections() latt.build((4, 4), primitive=False) latt2 = Lattice(np.eye(2)) latt2.add_atom() latt2.add_connections() latt2.build((4, 4), pos=(5, 0), primitive=False) pairs, dists = latt.compute_connections(latt2) expected = [[20, 0], [21, 1], [22, 2], [23, 3], [24, 4]] assert_array_equal(pairs, expected) assert np.all(dists) == 1 latt2.build((4, 4), pos=(5, 1), primitive=False) pairs, dists = latt.compute_connections(latt2) expected = [[21, 0], [22, 1], [23, 2], [24, 3]] assert_array_equal(pairs, expected) assert np.all(dists) == 1
def test_build_exceptions(): latt = Lattice(np.eye(2)) latt.add_atom() with pytest.raises(NoConnectionsError): latt.build((5, 5)) latt.add_connections(analyze=False) with pytest.raises(NotAnalyzedError): latt.build((5, 5)) latt.analyze() with pytest.raises(ValueError): latt.build((5, 5, 5))
SiteOccupiedError, NoAtomsError, NoConnectionsError, NotAnalyzedError, ) from lattpy import Lattice, Circle, Atom import lattpy as lp settings.load_profile("lattpy") atom = Atom() PI = np.pi TWOPI = 2 * np.pi chain = Lattice.chain(a=1.0) rchain = Lattice(TWOPI) square = Lattice.square(a=1.0) rsquare = Lattice(TWOPI * np.eye(2)) rect = Lattice.rectangular(a1=2.0, a2=1.0) rrect = Lattice(PI * np.array([[1, 0], [0, 2]])) hexagonal = Lattice.hexagonal(a=1) rhexagonal = Lattice( np.array([[+2.0943951, +3.62759873], [+2.0943951, -3.62759873]])) sc = Lattice.sc(a=1.0) rsc = Lattice(TWOPI * np.eye(3)) fcc = Lattice.fcc(a=1.0)
def test_neighbor_pairs_unique(latt: lp.Lattice): pairs, distindices = latt.neighbor_pairs(unique=True) for (i, j), distidx in zip(pairs, distindices): # noqa assert i < j neighbors = latt.neighbors(i, distidx=distidx) assert j in neighbors
def test_hash(): latt = Lattice(np.eye(2)) latt.add_atom(atom=atom) latt.add_connections() latt.build((5, 5)) hash1 = latt.__hash__() latt.build((6, 5)) assert latt.__hash__() != hash1
def test_volume(): latt = Lattice(np.eye(2)) latt.add_atom() latt.add_connections() latt.build((4, 4), primitive=False) assert latt.volume() == 25
def test_neighbor_pairs(latt: lp.Lattice): pairs, distindices = latt.neighbor_pairs() for (i, j), distidx in zip(pairs, distindices): # noqa neighbors = latt.neighbors(i, distidx=distidx) assert j in neighbors
def test_atom(): latt = Lattice(np.eye(2)) at1 = latt.add_atom([0.0, 0.0], "A") at2 = latt.add_atom([0.5, 0.5], "B") latt.add_connection("A", "A", 1) latt.add_connection("A", "B", 1) latt.analyze() latt.build((4, 4), primitive=False) assert latt.atom(0) == at1 assert latt.atom(1) == at2 assert latt.atom(2) == at1 assert latt.atom(3) == at2
def test_adjacency_matrix(latt: lp.Lattice): adjmat = latt.adjacency_matrix() for i in range(latt.num_sites): for distidx, neighbors in latt.iter_neighbors(i): for j in neighbors: assert adjmat[i, j] == distidx + 1
def test_position(): latt = Lattice(np.eye(2)) latt.add_atom([0.0, 0.0], "A") latt.add_atom([0.5, 0.5], "B") latt.add_connection("A", "A", 1) latt.add_connection("A", "B", 1) latt.analyze() latt.build((4, 4), primitive=False) assert_array_equal(latt.position(0), [0.0, 0.0]) assert_array_equal(latt.position(1), [0.5, 0.5]) assert_array_equal(latt.position(2), [0.0, 1.0]) assert_array_equal(latt.position(3), [0.5, 1.5])