Example #1
0
def test_euclidian_lattice_equality():
    """ Test equality between identical Lattice instances and copies """
    lattice = Lattice(np.eye(3))
    assert lattice == lattice
    assert lattice == deepcopy(lattice)

    assert lattice != Lattice(2 * np.eye(3))
Example #2
0
def test_lattice_frac_mesh_two_arr():
    """ Test that Lattice.frac_mesh is raising an exception for two input vectors """
    lattice = Lattice(np.eye(3))
    x = np.linspace(0, 1, num=2)

    with pytest.raises(ValueError):
        lattice.frac_mesh(x, x)
Example #3
0
def test_lattice_frac_mesh():
    """ Test that Lattice.frac_mesh is working as expected compared to numpy.meshgrid """
    lattice = Lattice(np.eye(3))
    x = np.linspace(0, 1, num=8)

    for out, n in zip(lattice.frac_mesh(x), np.meshgrid(x, x, x)):
        assert np.allclose(out, n)
Example #4
0
def test_lattice_real_mesh_trivial():
    """ Test that Lattice.mesh works identically to Lattice.frac_mesh for trivial lattice """
    lattice = Lattice(np.eye(3))
    x = np.linspace(0, 1, num=8)

    for frac, real in zip(lattice.frac_mesh(x), lattice.mesh(x)):
        assert np.allclose(frac, real)
Example #5
0
def test_lattice_real_mesh():
    """ Test that Lattice.mesh works as expected """
    lattice = Lattice(2 * np.eye(3))
    x = np.linspace(0, 1, num=8)

    # since lattice is a stretched euclidian lattice, we expect
    # a maximum length of 2
    assert np.max(lattice.mesh(x)[0]) == 2
Example #6
0
def test_back_and_forth():
    """Test that Lattice.miller_indices and Lattice.scattering_vector are
    reciprocal to each other"""
    lattice = Lattice(np.random.random((3, 3)))
    h, k, l = np.random.randint(-10, 10, size=(3, ))
    vector = lattice.scattering_vector((h, k, l))
    hp, kp, lp = lattice.miller_indices(vector)

    assert round(abs(h - float(hp)), 7) == 0
    assert round(abs(k - float(kp)), 7) == 0
    assert round(abs(l - float(lp)), 7) == 0
Example #7
0
def test_atomic_distance_different_lattice():
    """Test that fractional and cartesian distances
    between atoms in different lattices raises an error."""
    lattice1 = Lattice(np.eye(3))
    lattice2 = Lattice(2 * np.eye(3))

    atm1 = Atom("He", [0, 0, 0], lattice=lattice1)
    atm2 = Atom("He", [1, 0, 0], lattice=lattice2)

    with pytest.raises(RuntimeError):
        distance_fractional(atm1, atm2)

    with pytest.raises(RuntimeError):
        distance_cartesian(atm1, atm2)
Example #8
0
def test_monoclinic_lattice_system():
    """Test that the Lattice.lattice_system attribute is working properly for monoclinic lattice
    including all possible permutations."""
    parameters = (1, 2, 3, 90, 115, 90)
    l = Lattice.from_parameters(*parameters)
    assert l.lattice_system == LatticeSystem.monoclinic

    parameters = (2, 3, 1, 115, 90, 90)
    l = Lattice.from_parameters(*parameters)
    assert l.lattice_system == LatticeSystem.monoclinic

    parameters = (3, 1, 2, 90, 90, 115)
    l = Lattice.from_parameters(*parameters)
    assert l.lattice_system == LatticeSystem.monoclinic
Example #9
0
def test_euclidian_lattice():
    lattice = Lattice(np.eye(3))
    assert np.array_equal(lattice.a1, [1, 0, 0])
    assert np.array_equal(lattice.a2, [0, 1, 0])
    assert np.array_equal(lattice.a3, [0, 0, 1])

    assert lattice.volume == 1
Example #10
0
    def test_distance_different_lattice(self):
        """ Test that fractional and cartesian distances 
        between atoms in different lattices raises an error. """
        lattice1 = Lattice(np.eye(3))
        lattice2 = Lattice(2 * np.eye(3))

        atm1 = Atom("He", [0, 0, 0], lattice=lattice1)
        atm2 = Atom("He", [1, 0, 0], lattice=lattice2)

        with self.subTest("Fractional distance"):
            with self.assertRaises(RuntimeError):
                distance_fractional(atm1, atm2)

        with self.subTest("Cartesian distance"):
            with self.assertRaises(RuntimeError):
                distance_cartesian(atm1, atm2)
Example #11
0
def test_lattice_parameters_reciprocal_and_back():
    """Create lattice from parameters, take reciprocal twice,
    and see if the parameters have changed."""
    triclinic = (3, 4, 20, 45, 90, 126)
    triclinic2 = Lattice.from_parameters(
        *triclinic).reciprocal.reciprocal.lattice_parameters
    assert np.allclose(triclinic, triclinic2)
Example #12
0
    def test_distance_cartesian(self):
        """ Test the cartesian distance between atom """
        lattice = Lattice(4 * np.eye(3))  # Cubic lattice side length 4 angs

        atm1 = Atom("He", [0, 0, 0], lattice=lattice)
        atm2 = Atom("He", [1, 0, 0], lattice=lattice)

        self.assertEqual(distance_cartesian(atm1, atm2), 4.0)
Example #13
0
def test_atomic_distance_cartesian():
    """Test the cartesian distance between atom"""
    lattice = Lattice(4 * np.eye(3))  # Cubic lattice side length 4 angs

    atm1 = Atom("He", [0, 0, 0], lattice=lattice)
    atm2 = Atom("He", [1, 0, 0], lattice=lattice)

    assert distance_cartesian(atm1, atm2) == 4.0
Example #14
0
def test_hexagonal_lattice_system():
    """Test that the Lattice.lattice_system attribute is working properly for hexagonal lattice,
    including all possible permutations of lattice parameters."""
    parameters = (2, 2, 3, 90, 90, 120)
    l = Lattice.from_parameters(*parameters)
    assert l.lattice_system == LatticeSystem.hexagonal

    parameters = (3, 2, 2, 120, 90, 90)
    l = Lattice.from_parameters(*parameters)
    assert l.lattice_system == LatticeSystem.hexagonal

    parameters = (2, 3, 2, 90, 120, 90)
    l = Lattice.from_parameters(*parameters)
    assert l.lattice_system == LatticeSystem.hexagonal

    parameters = (2, 2, 2, 90, 120, 90)
    l = Lattice.from_parameters(*parameters)
    assert l.lattice_system == LatticeSystem.hexagonal
Example #15
0
def test_lattice_parameters_triclinic():
    """ alpha, beta, gama =/= 90 """
    a1, a2, a3 = Lattice.from_parameters(1, 2, 3, 75, 40, 81).lattice_vectors

    assert round(abs(norm(a1) - 1), 7) == 0
    assert round(abs(norm(a2) - 2), 7) == 0
    assert round(abs(norm(a3) - 3), 7) == 0

    assert round(abs(angle_between(a2, a3) - 75), 7) == 0
    assert round(abs(angle_between(a3, a1) - 40), 7) == 0
    assert round(abs(angle_between(a1, a2) - 81), 7) == 0
Example #16
0
def test_lattice_parameters_monoclinic():
    """ beta =/= 90 """
    a1, a2, a3 = Lattice.from_parameters(1, 2, 3, 90, 120, 90).lattice_vectors

    assert round(abs(norm(a1) - 1), 7) == 0
    assert round(abs(norm(a2) - 2), 7) == 0
    assert round(abs(norm(a3) - 3), 7) == 0

    assert round(abs(angle_between(a2, a3) - 90), 7) == 0
    assert round(abs(angle_between(a3, a1) - 120), 7) == 0
    assert round(abs(angle_between(a1, a2) - 90), 7) == 0
Example #17
0
def _generate_unitcell_atoms(crystal: Crystal):
    for atm in crystal:
        for factors in product(range(-2, 2), range(-2, 2), range(-2, 2)):
            coords = atm.coords_fractional + np.asarray(factors)
            if np.all(coords <= 1.1) and np.all(coords >= -0.1):
                yield Atom(
                    element=atm.element,
                    coords=coords,
                    lattice=Lattice(crystal.lattice_vectors),
                    displacement=atm.displacement,
                    magmom=atm.magmom,
                    occupancy=atm.occupancy,
                )
Example #18
0
def test_cubic_lattice_system():
    """ Test that the Lattice.lattice_system attribute is working properly for cubic lattice """
    assert Lattice(2 * np.eye(3)).lattice_system == LatticeSystem.cubic
Example #19
0
def test_miller_indices_trivial():
    """ Test that Lattice.miller_indices is working """
    lattice = Lattice(np.random.random((3, 3)))
    assert np.allclose(lattice.miller_indices((0, 0, 0)), ((0, 0, 0)))
Example #20
0
def test_lattice_parameters_orthorombic():
    """ alpha = beta = gamma = 90"""
    a1, a2, a3 = Lattice.from_parameters(2, 1, 5, 90, 90, 90).lattice_vectors
    assert np.allclose(a1, [2, 0, 0])
    assert np.allclose(a2, [0, 1, 0])
    assert np.allclose(a3, [0, 0, 5])
Example #21
0
def test_tetragonal_lattice_system():
    """ Test that the Lattice.lattice_system attribute is working properly for tetragonal lattice """
    parameters = (2, 2, 3, 90, 90, 90)
    l = Lattice.from_parameters(*parameters)
    assert l.lattice_system == LatticeSystem.tetragonal
Example #22
0
def test_triclinic_lattice_system():
    """ Test that the Lattice.lattice_system attribute is working properly for triclinic lattice """
    l = Lattice.from_parameters(1, 2, 3, 75, 40, 81)
    assert l.lattice_system == LatticeSystem.triclinic
Example #23
0
def test_rhombohedral_lattice_system():
    """ Test that the Lattice.lattice_system attribute is working properly for rhombohedral lattice """
    parameters = (1, 1, 1, 87, 87, 87)
    l = Lattice.from_parameters(*parameters)
    assert l.lattice_system == LatticeSystem.rhombohedral
Example #24
0
def test_scattering_vector_trivial():
    """ Test that Lattice.scattering_vectors is working """
    lattice = Lattice(np.random.random((3, 3)))
    assert np.allclose(lattice.scattering_vector((0, 0, 0)), ((0, 0, 0)))
Example #25
0
def test_scattering_vector_table():
    """ Test that Lattice.scattering_vector is working on tables of reflections """
    lattice = Lattice(2 * np.pi * np.eye(3))
    vectors = np.random.random(size=(10, 3))
    assert np.allclose(lattice.scattering_vector(vectors), vectors)
Example #26
0
def test_lattice_array_dtype():
    """ Test that the data-type of array(Lattice(...)) is respected """
    arr = np.random.random(size=(3, 3))
    lattice = Lattice(arr)
    assert np.array(lattice, dtype=np.int32).dtype, np.int32
Example #27
0
def test_lattice_array_shape():
    """ Test that array(Lattice(...)) is always 3x3 """
    arr = np.random.random(size=(3, 3))
    lattice = Lattice(arr)
    assert np.allclose(arr, np.array(lattice))
Example #28
0
def test_miller_indices_table():
    """ Test that Lattice.miller_indices is working on tables of vectors """
    lattice = Lattice(2 * np.pi * np.eye(3))
    vectors = np.random.random(size=(10, 3))
    assert np.allclose(lattice.miller_indices(vectors), vectors)