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_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_superindex_from_position(): latt = Lattice(np.eye(2)) latt.add_atom() latt.add_connections() latt.build((4, 4), primitive=False) for i in range(latt.num_sites): pos = latt.position(i) assert latt.superindex_from_pos(pos) == i
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_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_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])
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_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_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 _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_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))
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_volume(): latt = Lattice(np.eye(2)) latt.add_atom() latt.add_connections() latt.build((4, 4), primitive=False) assert latt.volume() == 25