Example #1
0
def test_simple_square():
    latt = lp.simple_square(a=1, neighbors=1)
    latt.build((2, 2))

    # Check correct building
    assert latt.num_sites == 9

    # Check nearest neighbors
    expected = [1, 3, 5, 7]
    actual = latt.nearest_neighbors(4)
    assert_array_equal(expected, sorted(actual))

    # Check periodic boundary conditions
    latt.set_periodic(0)
    expected = [1, 3, 6]
    actual = latt.nearest_neighbors(0)
    assert_array_equal(expected, sorted(actual))

    expected = [0, 2, 4, 7]
    actual = latt.nearest_neighbors(1)
    assert_array_equal(expected, sorted(actual))

    latt.set_periodic(1)
    expected = [1, 2, 3]
    actual = latt.nearest_neighbors(0)
    assert_array_equal(expected, sorted(actual))

    # Check next nearest neighbors
    latt = lp.simple_square(a=1, neighbors=2)
    latt.build((2, 2))

    expected = [0, 2, 6, 8]
    actual = latt.neighbors(4, distidx=1)
    assert_array_equal(expected, sorted(actual))
Example #2
0
def test_extend():
    # Only check size, connections are handled by append
    latt = lp.simple_chain()
    latt.build(4, primitive=False)
    latt.extend(2)
    assert latt.num_sites == 8

    latt = lp.simple_square()
    latt.build((4, 4), primitive=False)
    latt.extend(2, ax=0)
    assert_array_equal(latt.shape, (7, 4))

    latt = lp.simple_square()
    latt.build((4, 4), primitive=False)
    latt.extend(2, ax=1)
    assert_array_equal(latt.shape, (4, 7))
Example #3
0
def test_repeat():
    # Only check size, connections are handled by append
    latt = lp.simple_chain()
    latt.build(4, primitive=False)
    latt.repeat()
    assert latt.num_sites == 10

    latt = lp.simple_square()
    latt.build((4, 4), primitive=False)
    latt.repeat(1, ax=0)
    assert_array_equal(latt.shape, (9, 4))

    latt = lp.simple_square()
    latt.build((4, 4), primitive=False)
    latt.repeat(1, ax=1)
    assert_array_equal(latt.shape, (4, 9))
Example #4
0
def test_periodic_next_nearest():
    # Lattice chain
    latt = lp.simple_chain(neighbors=2)
    latt.build(9)
    latt.set_periodic(0)
    assert 8 in latt.neighbors(0, distidx=1)

    # Square lattice
    latt = lp.simple_square(neighbors=2)
    latt.build((4, 4))
    latt.set_periodic(0)
    assert_elements_equal1d(latt.neighbors(0, 1), [6, 21])
    assert_elements_equal1d(latt.neighbors(1, 1), [7, 5, 20, 22])
    assert_elements_equal1d(latt.neighbors(2, 1), [8, 6, 21, 23])
    assert_elements_equal1d(latt.neighbors(3, 1), [9, 7, 22, 24])
    assert_elements_equal1d(latt.neighbors(4, 1), [8, 23])
    latt.set_periodic(1)
    assert_elements_equal1d(latt.neighbors(0, 1), [6, 9])
    assert_elements_equal1d(latt.neighbors(5, 1), [1, 4, 11, 14])
    assert_elements_equal1d(latt.neighbors(10, 1), [6, 9, 16, 19])
    assert_elements_equal1d(latt.neighbors(15, 1), [11, 14, 21, 24])
    assert_elements_equal1d(latt.neighbors(20, 1), [16, 19])
    # Only check corners for both axis periodic
    latt.set_periodic([0, 1])
    assert_elements_equal1d(latt.neighbors(0, 1), [6, 9, 21, 24])
    assert_elements_equal1d(latt.neighbors(4, 1), [5, 8, 20, 23])
    assert_elements_equal1d(latt.neighbors(20, 1), [1, 4, 16, 19])
    assert_elements_equal1d(latt.neighbors(23, 1), [0, 3, 15, 18])
Example #5
0
def test_periodic_small():
    latt = lp.simple_square()
    latt.build((2, 2), primitive=True)
    latt.set_periodic(True)

    assert_elements_equal1d(latt.neighbors(0), [1, 2])
    assert_elements_equal1d(latt.neighbors(1), [2, 3])
Example #6
0
def test_wigner_seitz_linspace():
    latt = simple_square()
    ws = latt.wigner_seitz_cell()

    x, y = ws.linspace(100, endpoint=True)
    assert_allclose(x, np.linspace(-0.5, +0.5, 100))
    assert_allclose(y, np.linspace(-0.5, +0.5, 100))

    x, y = ws.linspace((100, 200), endpoint=True)
    assert_allclose(x, np.linspace(-0.5, +0.5, 100))
    assert_allclose(y, np.linspace(-0.5, +0.5, 200))
Example #7
0
def test_wigner_seitz_arange():
    latt = simple_square()
    ws = latt.wigner_seitz_cell()

    x, y = ws.arange(0.1)
    assert_allclose(x, np.arange(-0.5, +0.5, 0.1))
    assert_allclose(y, np.arange(-0.5, +0.5, 0.1))

    x, y = ws.arange((0.1, 0.2))
    assert_allclose(x, np.arange(-0.5, +0.5, 0.1))
    assert_allclose(y, np.arange(-0.5, +0.5, 0.2))
Example #8
0
def test_periodic_nearest():
    # Lattice chain
    latt = lp.simple_chain()
    latt.build(9)
    latt.set_periodic(0)
    assert 9 in latt.neighbors(0)

    # Square lattice
    latt = lp.simple_square()
    latt.build((4, 4))

    latt.set_periodic(0)
    assert_elements_equal1d(latt.nearest_neighbors(0), [1, 5, 20])
    assert_elements_equal1d(latt.nearest_neighbors(1), [0, 2, 6, 21])
    assert_elements_equal1d(latt.nearest_neighbors(2), [1, 3, 7, 22])
    assert_elements_equal1d(latt.nearest_neighbors(3), [2, 4, 8, 23])
    assert_elements_equal1d(latt.nearest_neighbors(4), [3, 9, 24])
    latt.set_periodic(1)
    assert_elements_equal1d(latt.nearest_neighbors(0), [1, 5, 4])
    assert_elements_equal1d(latt.nearest_neighbors(5), [0, 6, 10, 9])
    assert_elements_equal1d(latt.nearest_neighbors(10), [5, 11, 15, 14])
    assert_elements_equal1d(latt.nearest_neighbors(15), [10, 16, 20, 19])
    assert_elements_equal1d(latt.nearest_neighbors(20), [15, 21, 24])
    # Only check corners for both axis periodic
    latt.set_periodic([0, 1])
    assert_elements_equal1d(latt.nearest_neighbors(0), [1, 5, 20, 24])
    assert_elements_equal1d(latt.nearest_neighbors(4), [3, 9, 20, 24])
    assert_elements_equal1d(latt.nearest_neighbors(20), [0, 15, 21, 24])
    assert_elements_equal1d(latt.nearest_neighbors(24), [4, 19, 20, 23])

    # graphene lattice
    latt = lp.graphene()
    latt.build((5.5, 4.5))

    latt.set_periodic(0)
    assert_elements_equal1d(latt.nearest_neighbors(0), [1, 15])
    assert_elements_equal1d(latt.nearest_neighbors(2), [3, 15, 21])
    assert_elements_equal1d(latt.nearest_neighbors(8), [9, 21, 23])
    latt.set_periodic(1)
    assert_elements_equal1d(latt.nearest_neighbors(1), [0, 4, 16])
    assert_elements_equal1d(latt.nearest_neighbors(6), [5, 7, 17])
    assert_elements_equal1d(latt.nearest_neighbors(7), [6, 14, 22])
    # Only check corners for both axis periodic
    latt.set_periodic([0, 1])
    assert_elements_equal1d(latt.nearest_neighbors(0), [1, 15, 23])
    assert_elements_equal1d(latt.nearest_neighbors(8), [9, 21, 23])
Example #9
0
def test_wigner_seitz_meshgrid():
    latt = simple_square()
    ws = latt.wigner_seitz_cell()

    num = 101
    actual = ws.meshgrid(num, endpoint=True)
    x = np.linspace(-0.5, +0.5, num)
    y = np.linspace(-0.5, +0.5, num)
    expected = np.array(np.meshgrid(x, y))
    assert_allclose(actual, expected)

    step = 0.1
    actual = ws.meshgrid(steps=step, endpoint=True)
    x = np.arange(-0.5, +0.5, step)
    y = np.arange(-0.5, +0.5, step)
    expected = np.array(np.meshgrid(x, y))
    assert_allclose(actual, expected)
Example #10
0
def test_minimum_distances_square():
    latt = lp.simple_square()
    latt.build((2, 2))
    sqrt11 = np.sqrt(2)
    sqrt21 = np.sqrt(5)
    sqrt22 = np.sqrt(8)

    # lower left
    dists = [0.0, 1.0, 2.0, 1.0, sqrt11, sqrt21, 2.0, sqrt21, sqrt22]
    assert_allclose(latt.minimum_distances(0), dists)
    # upper left
    dists = [2.0, 1.0, 0.0, sqrt21, sqrt11, 1.0, sqrt22, sqrt21, 2.0]
    assert_allclose(latt.minimum_distances(2), dists)
    # center
    dists = [sqrt11, 1.0, sqrt11, 1.0, 0.0, 1, sqrt11, 1.0, sqrt11]
    assert_allclose(latt.minimum_distances(4), dists)
    # lower right
    dists = [2.0, sqrt21, sqrt22, 1.0, sqrt11, sqrt21, 0.0, 1.0, 2.0]
    assert_allclose(latt.minimum_distances(6), dists)
    # upper right
    dists = [sqrt22, sqrt21, 2.0, sqrt21, sqrt11, 1.0, 2.0, 1.0, 0.0]
    assert_allclose(latt.minimum_distances(8), dists)

    latt.set_periodic(0)
    # lower left
    dists = [0.0, 1.0, 2.0, 1.0, sqrt11, sqrt21, 1.0, sqrt11, sqrt21]
    assert_allclose(latt.minimum_distances(0), dists)
    # upper left
    dists = [2.0, 1.0, 0.0, sqrt21, sqrt11, 1.0, sqrt21, sqrt11, 1.0]
    assert_allclose(latt.minimum_distances(2), dists)
    # center
    dists = [sqrt11, 1.0, sqrt11, 1.0, 0.0, 1, sqrt11, 1.0, sqrt11]
    assert_allclose(latt.minimum_distances(4), dists)
    # lower right
    dists = [1.0, sqrt11, sqrt21, 1.0, sqrt11, sqrt21, 0.0, 1.0, 2.0]
    assert_allclose(latt.minimum_distances(6), dists)
    # upper right
    dists = [sqrt21, sqrt11, 1.0, sqrt21, sqrt11, 1.0, 2.0, 1.0, 0.0]
    assert_allclose(latt.minimum_distances(8), dists)
Example #11
0
def test_periodic_notbuilt():
    latt = lp.simple_square()
    with pytest.raises(lp.NotBuiltError):
        latt.set_periodic(True)
Example #12
0
sc = Lattice.sc(a=1.0)
rsc = Lattice(TWOPI * np.eye(3))

fcc = Lattice.fcc(a=1.0)
rfcc = Lattice(TWOPI * np.array([[+1, +1, -1], [+1, -1, +1], [-1, +1, +1]]))
bcc = Lattice.bcc(a=1.0)
rbcc = Lattice(TWOPI * np.array([[+1, +1, 0], [0, -1, +1], [-1, 0, +1]]))

LATTICES = [chain, square, rect, hexagonal, sc, fcc, bcc]
RLATTICES = [rchain, rsquare, rrect, rhexagonal, rsc, rfcc, rbcc]

STRUCTURES = list()
_latt = lp.simple_chain()
_latt.build(4)
STRUCTURES.append(_latt)
_latt = lp.simple_square()
_latt.build((4, 4))
STRUCTURES.append(_latt)
_latt = lp.simple_cubic()
_latt.build((4, 4, 4))
STRUCTURES.append(_latt)


@st.composite
def structures(draw):
    return draw(st.sampled_from(STRUCTURES))


def assert_elements_equal1d(actual, expected):
    actual = np.unique(actual)
    expected = np.unique(expected)
Example #13
0
def test_wignerseitz_symmetry_points():
    # Test 1D
    latt = simple_chain()
    ws = latt.wigner_seitz_cell()
    origin, corners, edge_centers, face_centers = ws.symmetry_points()
    assert_array_equal(origin, [0.0])
    assert_array_equal(corners, [[-0.5], [0.5]])
    assert edge_centers is None
    assert face_centers is None

    # Test 2D
    latt = simple_square()
    ws = latt.wigner_seitz_cell()
    origin, corners, edge_centers, face_centers = ws.symmetry_points()

    assert_array_equal(origin, [0.0, 0.0])
    assert_array_equal(
        2 * corners, [[-1.0, -1.0], [1.0, -1.0], [-1.0, 1.0], [1.0, 1.0]]
    )
    assert_array_equal(
        2 * edge_centers, [[0.0, -1.0], [-1.0, 0.0], [1.0, 0.0], [0.0, 1.0]]
    )
    assert face_centers is None

    # Test 3D
    latt = simple_cubic()
    ws = latt.wigner_seitz_cell()
    origin, corners, edge_centers, face_centers = ws.symmetry_points()

    c = [
        [-0.5, -0.5, -0.5],
        [0.5, -0.5, -0.5],
        [-0.5, -0.5, 0.5],
        [0.5, -0.5, 0.5],
        [-0.5, 0.5, -0.5],
        [0.5, 0.5, -0.5],
        [0.5, 0.5, 0.5],
        [-0.5, 0.5, 0.5],
    ]
    e = [
        [-0.5, -0.5, 0.0],
        [0.0, -0.5, -0.5],
        [0.5, -0.5, 0.0],
        [0.0, -0.5, 0.5],
        [-0.5, 0.5, 0.0],
        [0.0, 0.5, 0.5],
        [0.5, 0.5, 0.0],
        [0.0, 0.5, -0.5],
        [0.5, 0.0, 0.5],
        [0.0, 0.5, 0.5],
        [-0.5, 0.0, 0.5],
        [0.0, -0.5, 0.5],
        [-0.5, -0.5, 0.0],
        [-0.5, 0.0, -0.5],
        [-0.5, 0.5, 0.0],
        [-0.5, 0.0, 0.5],
        [0.5, 0.0, -0.5],
        [0.0, 0.5, -0.5],
        [-0.5, 0.0, -0.5],
        [0.0, -0.5, -0.5],
        [0.5, -0.5, 0.0],
        [0.5, 0.0, -0.5],
        [0.5, 0.5, 0.0],
        [0.5, 0.0, 0.5],
    ]

    f = [
        [0.0, -0.5, 0.0],
        [0.0, 0.5, 0.0],
        [0.0, 0.0, 0.5],
        [-0.5, 0.0, 0.0],
        [0.0, 0.0, -0.5],
        [0.5, 0.0, 0.0],
    ]

    assert_array_equal(origin, [0.0, 0.0, 0.0])
    assert_array_equal(corners, c)
    assert_array_equal(edge_centers, e)
    assert_array_equal(face_centers, f)
Example #14
0
import os
import time
import tracemalloc
import numpy as np
import matplotlib.pyplot as plt
import lattpy as lp

MB = 1024 * 1024
MAX_SITES = 10_000_000
RUNS = 3
MAX_POINTS = 30

overwrite = False
latts = {
    "chain": lp.simple_chain(),
    "square": lp.simple_square(),
    "hexagonal": lp.simple_hexagonal(),
    "cubic": lp.simple_cubic(),
}


class Profiler:
    """Profiler object for measuring time, memory and other stats."""

    def __init__(self):
        self._timefunc = time.perf_counter
        self._snapshot = None
        self._t0 = 0
        self._m0 = 0
        self._thread = None