Пример #1
0
def test_hex():
    from javelin.unitcell import UnitCell
    positions = [[0, 1, 0], [1, 1, 0], [1, 0, 0], [0, -1, 0], [-1, -1, 0],
                 [-1, 0, 0]]
    symbols = ['C'] * 6
    unitcell = (1.4, 1.4, 1, 90, 90, 120)

    hex_cell = Structure(unitcell=UnitCell(unitcell),
                         symbols=symbols,
                         positions=positions)

    assert hex_cell.number_of_atoms == 6
    assert_array_equal(hex_cell.element, ['C', 'C', 'C', 'C', 'C', 'C'])
    assert_array_equal(hex_cell.get_atom_symbols(), ['C'])
    assert_array_equal(hex_cell.get_atom_count(), 6)
    assert_array_equal(hex_cell.get_chemical_symbols(),
                       ['C', 'C', 'C', 'C', 'C', 'C'])
    assert_array_equal(hex_cell.get_atom_Zs(), [6])
    assert_array_equal(hex_cell.get_atomic_numbers(), [6, 6, 6, 6, 6, 6])
    assert_array_equal(hex_cell.x, [0, 1, 1, 0, -1, -1])
    assert_array_equal(hex_cell.y, [1, 1, 0, -1, -1, 0])
    assert_array_equal(hex_cell.z, [0, 0, 0, 0, 0, 0])
    assert_array_equal(hex_cell.xyz, positions)
    assert_array_equal(hex_cell.get_scaled_positions(), positions)
    real_positions = [[0, 1.4, 0], [1.21243557, 0.7, 0], [1.21243557, -0.7, 0],
                      [0, -1.4, 0], [-1.21243557, -0.7, 0],
                      [-1.21243557, 0.7, 0]]
    assert_array_almost_equal(hex_cell.xyz_cartn, real_positions)
    assert_array_almost_equal(hex_cell.get_positions(), real_positions)
    assert_array_almost_equal(hex_cell.unitcell.cell, unitcell)
Пример #2
0
def test_hex():
    from javelin.unitcell import UnitCell
    positions = [[0, 1, 0], [1, 1, 0], [1, 0, 0], [0, -1, 0], [-1, -1, 0], [-1, 0, 0]]
    symbols = ['C']*6
    unitcell = (1.4, 1.4, 1, 90, 90, 120)

    hex_cell = Structure(unitcell=UnitCell(unitcell),
                         symbols=symbols,
                         positions=positions)

    assert hex_cell.number_of_atoms == 6
    assert_array_equal(hex_cell.element, ['C', 'C', 'C', 'C', 'C', 'C'])
    assert_array_equal(hex_cell.get_atom_symbols(), ['C'])
    assert_array_equal(hex_cell.get_atom_count(), 6)
    assert_array_equal(hex_cell.get_chemical_symbols(), ['C', 'C', 'C', 'C', 'C', 'C'])
    assert_array_equal(hex_cell.get_atom_Zs(), [6])
    assert_array_equal(hex_cell.get_atomic_numbers(), [6, 6, 6, 6, 6, 6])
    assert_array_equal(hex_cell.x, [0,  1,  1,  0, -1, -1])
    assert_array_equal(hex_cell.y, [1,  1,  0, -1, -1,  0])
    assert_array_equal(hex_cell.z, [0, 0, 0, 0, 0, 0])
    assert_array_equal(hex_cell.xyz, positions)
    assert_array_equal(hex_cell.get_scaled_positions(), positions)
    real_positions = [[0, 1.4, 0],
                      [1.21243557, 0.7, 0],
                      [1.21243557, -0.7, 0],
                      [0, -1.4, 0],
                      [-1.21243557, -0.7, 0],
                      [-1.21243557, 0.7, 0]]
    assert_array_almost_equal(hex_cell.xyz_cartn, real_positions)
    assert_array_almost_equal(hex_cell.get_positions(), real_positions)
    assert_array_almost_equal(hex_cell.unitcell.cell, unitcell)
Пример #3
0
def test_empty():
    structure = Structure()
    assert structure.number_of_atoms == 0
    assert_array_equal(structure.element, np.empty(0))
    assert_array_equal(structure.get_atom_symbols(), np.empty(0))
    assert_array_equal(structure.get_atom_count(), 0)
    assert_array_equal(structure.get_chemical_symbols(), np.empty(0))
    assert_array_equal(structure.get_atom_Zs(), np.empty(0))
    assert_array_equal(structure.get_atomic_numbers(), np.empty(0))
    assert_array_equal(structure.x, np.empty(0))
    assert_array_equal(structure.y, np.empty(0))
    assert_array_equal(structure.z, np.empty(0))
    assert_array_equal(structure.xyz, np.empty((0, 3)))
    assert_array_equal(structure.get_scaled_positions(), np.empty((0, 3)))
    assert_array_equal(structure.xyz_cartn, np.empty((0, 3)))
    assert_array_equal(structure.get_positions(), np.empty((0, 3)))
    assert_array_equal(structure.unitcell.cell, (1.0, 1.0, 1.0, 90.0, 90.0, 90.0))
Пример #4
0
def test_one_atom_init():
    structure = Structure(symbols=['Au'], positions=[[0.5, 0, 0.25]], unitcell=(3, 4, 5))
    assert structure.number_of_atoms == 1
    assert_array_equal(structure.element, ['Au'])
    assert_array_equal(structure.get_atom_symbols(), ['Au'])
    assert_array_equal(structure.get_atom_count(), 1)
    assert_array_equal(structure.get_chemical_symbols(), ['Au'])
    assert_array_equal(structure.get_atom_Zs(), [79])
    assert_array_equal(structure.get_atomic_numbers(), [79])
    assert_array_equal(structure.x, [0.5])
    assert_array_equal(structure.y, [0])
    assert_array_equal(structure.z, [0.25])
    assert_array_equal(structure.xyz, [[0.5, 0, 0.25]])
    assert_array_equal(structure.get_scaled_positions(), [[0.5, 0, 0.25]])
    assert_array_almost_equal(structure.xyz_cartn, [[1.5, 0, 1.25]])
    assert_array_almost_equal(structure.get_positions(), [[1.5, 0, 1.25]])
    assert_array_equal(structure.unitcell.cell, (3.0, 4.0, 5.0, 90.0, 90.0, 90.0))
Пример #5
0
def test_empty():
    structure = Structure()
    assert structure.number_of_atoms == 0
    assert_array_equal(structure.element, np.empty(0))
    assert_array_equal(structure.get_atom_symbols(), np.empty(0))
    assert_array_equal(structure.get_atom_count(), 0)
    assert_array_equal(structure.get_chemical_symbols(), np.empty(0))
    assert_array_equal(structure.get_atom_Zs(), np.empty(0))
    assert_array_equal(structure.get_atomic_numbers(), np.empty(0))
    assert_array_equal(structure.x, np.empty(0))
    assert_array_equal(structure.y, np.empty(0))
    assert_array_equal(structure.z, np.empty(0))
    assert_array_equal(structure.xyz, np.empty((0, 3)))
    assert_array_equal(structure.get_scaled_positions(), np.empty((0, 3)))
    assert_array_equal(structure.xyz_cartn, np.empty((0, 3)))
    assert_array_equal(structure.get_positions(), np.empty((0, 3)))
    assert_array_equal(structure.unitcell.cell,
                       (1.0, 1.0, 1.0, 90.0, 90.0, 90.0))
Пример #6
0
def test_one_atom_add():
    structure = Structure()
    structure.unitcell.cell = (3.0, 4.0, 5.0, 90.0, 90.0, 90.0)
    structure.add_atom(symbol='Au', position=[0.5, 0, 0.25])
    assert structure.number_of_atoms == 1
    assert_array_equal(structure.element, ['Au'])
    assert_array_equal(structure.get_atom_symbols(), ['Au'])
    assert_array_equal(structure.get_atom_count(), 1)
    assert_array_equal(structure.get_chemical_symbols(), ['Au'])
    assert_array_equal(structure.get_atom_Zs(), [79])
    assert_array_equal(structure.get_atomic_numbers(), [79])
    assert_array_equal(structure.x, [0.5])
    assert_array_equal(structure.y, [0])
    assert_array_equal(structure.z, [0.25])
    assert_array_equal(structure.xyz, [[0.5, 0, 0.25]])
    assert_array_equal(structure.get_scaled_positions(), [[0.5, 0, 0.25]])
    assert_array_almost_equal(structure.xyz_cartn, [[1.5, 0, 1.25]])
    assert_array_almost_equal(structure.get_positions(), [[1.5, 0, 1.25]])
    assert_array_equal(structure.unitcell.cell,
                       (3.0, 4.0, 5.0, 90.0, 90.0, 90.0))
Пример #7
0
def test_repeat():
    structure = Structure(unitcell=5, symbols=['Au', 'Ag'], positions=[[0, 0, 0], [0.5, 0.5, 0.5]])

    assert structure.number_of_atoms == 2
    assert_array_equal(structure.get_atom_count(), [1, 1])
    assert_array_equal(structure.get_atom_Zs(), [79, 47])
    assert_array_equal(structure.get_atomic_numbers(), [79, 47])
    assert_array_equal(structure.element, ['Au', 'Ag'])
    assert_array_equal(structure.get_atom_symbols(), ['Au', 'Ag'])
    assert_array_equal(structure.get_chemical_symbols(), ['Au', 'Ag'])
    assert_array_equal(structure.xyz, [[0., 0., 0.],
                                       [0.5, 0.5, 0.5]])
    assert_array_almost_equal(structure.xyz_cartn, [[0., 0., 0.],
                                                    [2.5, 2.5, 2.5]])

    structure.repeat((2, 3, 1))

    assert structure.number_of_atoms == 12
    assert_array_equal(structure.get_atom_count(), [6, 6])
    assert_array_equal(structure.get_atom_Zs(), [79, 47])
    assert_array_equal(structure.get_atomic_numbers(), [79, 47, 79, 47, 79, 47,
                                                        79, 47, 79, 47, 79, 47])
    assert_array_equal(structure.element, ['Au', 'Ag', 'Au', 'Ag', 'Au', 'Ag',
                                           'Au', 'Ag', 'Au', 'Ag', 'Au', 'Ag'])
    assert_array_equal(structure.get_atom_symbols(), ['Au', 'Ag'])
    assert_array_equal(structure.get_chemical_symbols(), ['Au', 'Ag', 'Au', 'Ag', 'Au', 'Ag',
                                                          'Au', 'Ag', 'Au', 'Ag', 'Au', 'Ag'])
    assert_array_equal(structure.xyz, np.tile([[0., 0., 0.],
                                               [0.5, 0.5, 0.5]], (6, 1)))
    assert_array_almost_equal(structure.xyz_cartn, [[0.0,  0.0, 0.0],
                                                    [2.5,  2.5, 2.5],
                                                    [0.0,  5.0, 0.0],
                                                    [2.5,  7.5, 2.5],
                                                    [0.0, 10.0, 0.0],
                                                    [2.5, 12.5, 2.5],
                                                    [5.0,  0.0, 0.0],
                                                    [7.5,  2.5, 2.5],
                                                    [5.0,  5.0, 0.0],
                                                    [7.5,  7.5, 2.5],
                                                    [5.0, 10.0, 0.0],
                                                    [7.5, 12.5, 2.5]])

    structure = Structure(unitcell=5, symbols=['Au', 'Ag'], positions=[[0, 0, 0], [0.5, 0.5, 0.5]])
    structure.repeat(2)

    assert structure.number_of_atoms == 16
    assert_array_equal(structure.get_atom_count(), [8, 8])
    assert_array_equal(structure.get_atom_Zs(), [79, 47])
    assert_array_equal(structure.get_atomic_numbers(), [79, 47, 79, 47, 79, 47, 79, 47,
                                                        79, 47, 79, 47, 79, 47, 79, 47])
    assert_array_equal(structure.element, ['Au', 'Ag', 'Au', 'Ag',
                                           'Au', 'Ag', 'Au', 'Ag',
                                           'Au', 'Ag', 'Au', 'Ag',
                                           'Au', 'Ag', 'Au', 'Ag'])
    assert_array_equal(structure.get_atom_symbols(), ['Au', 'Ag'])
    assert_array_equal(structure.get_chemical_symbols(), ['Au', 'Ag', 'Au', 'Ag',
                                                          'Au', 'Ag', 'Au', 'Ag',
                                                          'Au', 'Ag', 'Au', 'Ag',
                                                          'Au', 'Ag', 'Au', 'Ag'])
    assert_array_equal(structure.xyz, np.tile([[0., 0., 0.],
                                               [0.5, 0.5, 0.5]], (8, 1)))
    assert_array_almost_equal(structure.xyz_cartn, [[0.0, 0.0, 0.0],
                                                    [2.5, 2.5, 2.5],
                                                    [0.0, 0.0, 5.0],
                                                    [2.5, 2.5, 7.5],
                                                    [0.0, 5.0, 0.0],
                                                    [2.5, 7.5, 2.5],
                                                    [0.0, 5.0, 5.0],
                                                    [2.5, 7.5, 7.5],
                                                    [5.0, 0.0, 0.0],
                                                    [7.5, 2.5, 2.5],
                                                    [5.0, 0.0, 5.0],
                                                    [7.5, 2.5, 7.5],
                                                    [5.0, 5.0, 0.0],
                                                    [7.5, 7.5, 2.5],
                                                    [5.0, 5.0, 5.0],
                                                    [7.5, 7.5, 7.5]])
Пример #8
0
def test_repeat():
    structure = Structure(unitcell=5,
                          symbols=['Au', 'Ag'],
                          positions=[[0, 0, 0], [0.5, 0.5, 0.5]])

    assert structure.number_of_atoms == 2
    assert_array_equal(structure.get_atom_count(), [1, 1])
    assert_array_equal(structure.get_atom_Zs(), [79, 47])
    assert_array_equal(structure.get_atomic_numbers(), [79, 47])
    assert_array_equal(structure.element, ['Au', 'Ag'])
    assert_array_equal(structure.get_atom_symbols(), ['Au', 'Ag'])
    assert_array_equal(structure.get_chemical_symbols(), ['Au', 'Ag'])
    assert_array_equal(structure.xyz, [[0., 0., 0.], [0.5, 0.5, 0.5]])
    assert_array_almost_equal(structure.xyz_cartn,
                              [[0., 0., 0.], [2.5, 2.5, 2.5]])

    structure.repeat((2, 3, 1))

    assert structure.number_of_atoms == 12
    assert_array_equal(structure.get_atom_count(), [6, 6])
    assert_array_equal(structure.get_atom_Zs(), [79, 47])
    assert_array_equal(structure.get_atomic_numbers(),
                       [79, 47, 79, 47, 79, 47, 79, 47, 79, 47, 79, 47])
    assert_array_equal(structure.element, [
        'Au', 'Ag', 'Au', 'Ag', 'Au', 'Ag', 'Au', 'Ag', 'Au', 'Ag', 'Au', 'Ag'
    ])
    assert_array_equal(structure.get_atom_symbols(), ['Au', 'Ag'])
    assert_array_equal(structure.get_chemical_symbols(), [
        'Au', 'Ag', 'Au', 'Ag', 'Au', 'Ag', 'Au', 'Ag', 'Au', 'Ag', 'Au', 'Ag'
    ])
    assert_array_equal(structure.xyz,
                       np.tile([[0., 0., 0.], [0.5, 0.5, 0.5]], (6, 1)))
    assert_array_almost_equal(
        structure.xyz_cartn,
        [[0.0, 0.0, 0.0], [2.5, 2.5, 2.5], [0.0, 5.0, 0.0], [2.5, 7.5, 2.5],
         [0.0, 10.0, 0.0], [2.5, 12.5, 2.5], [5.0, 0.0, 0.0], [7.5, 2.5, 2.5],
         [5.0, 5.0, 0.0], [7.5, 7.5, 2.5], [5.0, 10.0, 0.0], [7.5, 12.5, 2.5]])

    structure = Structure(unitcell=5,
                          symbols=['Au', 'Ag'],
                          positions=[[0, 0, 0], [0.5, 0.5, 0.5]])
    structure.repeat(2)

    assert structure.number_of_atoms == 16
    assert_array_equal(structure.get_atom_count(), [8, 8])
    assert_array_equal(structure.get_atom_Zs(), [79, 47])
    assert_array_equal(
        structure.get_atomic_numbers(),
        [79, 47, 79, 47, 79, 47, 79, 47, 79, 47, 79, 47, 79, 47, 79, 47])
    assert_array_equal(structure.element, [
        'Au', 'Ag', 'Au', 'Ag', 'Au', 'Ag', 'Au', 'Ag', 'Au', 'Ag', 'Au', 'Ag',
        'Au', 'Ag', 'Au', 'Ag'
    ])
    assert_array_equal(structure.get_atom_symbols(), ['Au', 'Ag'])
    assert_array_equal(structure.get_chemical_symbols(), [
        'Au', 'Ag', 'Au', 'Ag', 'Au', 'Ag', 'Au', 'Ag', 'Au', 'Ag', 'Au', 'Ag',
        'Au', 'Ag', 'Au', 'Ag'
    ])
    assert_array_equal(structure.xyz,
                       np.tile([[0., 0., 0.], [0.5, 0.5, 0.5]], (8, 1)))
    assert_array_almost_equal(
        structure.xyz_cartn,
        [[0.0, 0.0, 0.0], [2.5, 2.5, 2.5], [0.0, 0.0, 5.0], [2.5, 2.5, 7.5],
         [0.0, 5.0, 0.0], [2.5, 7.5, 2.5], [0.0, 5.0, 5.0], [2.5, 7.5, 7.5],
         [5.0, 0.0, 0.0], [7.5, 2.5, 2.5], [5.0, 0.0, 5.0], [7.5, 2.5, 7.5],
         [5.0, 5.0, 0.0], [7.5, 7.5, 2.5], [5.0, 5.0, 5.0], [7.5, 7.5, 7.5]])
Пример #9
0
def read_stru(filename, starting_cell=(1, 1, 1)):
    """Read in a .stru file saved from DISCUS into a javelin Structure

    If the line ncell is not present in the file all the atoms will be
    read into a single cell."""

    from javelin.structure import Structure
    import numpy as np

    with open(filename) as f:
        lines = f.readlines()

    a = b = c = alpha = beta = gamma = 0

    reading_atom_list = False

    ncell = None
    symbols = []
    x = []
    y = []
    z = []

    for l in lines:
        line = l.replace(',', ' ').split()
        if not reading_atom_list:  # Wait for 'atoms' line before reading atoms
            if line[0] == 'cell':
                a, b, c, alpha, beta, gamma = [
                    float(word) for word in line[1:7]
                ]
            elif line[0] == 'ncell':
                ncell = [int(word) for word in line[1:5]]
            elif line[0] == 'atoms':
                if a == 0:
                    print("Cell not found")
                    a = b = c = 1
                    alpha = beta = gamma = 90
                reading_atom_list = True
        else:
            symbol, xx, yy, zz = line[:4]
            symbols.append(symbol)
            x.append(xx)
            y.append(yy)
            z.append(zz)

    print("Found a = {}, b = {}, c = {}, alpha = {}, beta = {}, gamma = {}".
          format(a, b, c, alpha, beta, gamma))

    x = np.array(x, dtype=np.float64)
    y = np.array(y, dtype=np.float64)
    z = np.array(z, dtype=np.float64)
    symbols = np.array(symbols)

    if ncell is not None:
        x -= np.tile(np.repeat(np.array(range(ncell[0])), ncell[3]),
                     ncell[1] * ncell[2]) + starting_cell[0]
        y -= np.tile(np.repeat(np.array(range(ncell[1])), ncell[0] * ncell[3]),
                     ncell[2]) + starting_cell[1]
        z -= np.repeat(np.array(range(ncell[2])),
                       ncell[0] * ncell[1] * ncell[3]) + starting_cell[2]

        # reorder atom arrays, discus stru files have x increment fastest
        # and z slowest, javelin is the opposite
        x = x.reshape(ncell).transpose((2, 1, 0, 3)).flatten()
        y = y.reshape(ncell).transpose((2, 1, 0, 3)).flatten()
        z = z.reshape(ncell).transpose((2, 1, 0, 3)).flatten()
        symbols = symbols.reshape(ncell).transpose((2, 1, 0, 3)).flatten()

    xyz = np.array((x, y, z)).T

    structure = Structure(unitcell=(a, b, c, alpha, beta, gamma),
                          symbols=symbols,
                          positions=xyz,
                          ncells=ncell)

    print("Read in these atoms:")
    print(structure.get_atom_count())

    return structure
Пример #10
0
def read_stru(filename, starting_cell=(1, 1, 1)):
    """Read in a .stru file saved from DISCUS into a javelin Structure

    If the line ncell is not present in the file all the atoms will be
    read into a single cell."""

    from javelin.structure import Structure
    import numpy as np

    with open(filename) as f:
        lines = f.readlines()

    a = b = c = alpha = beta = gamma = 0

    reading_atom_list = False

    ncell = None
    symbols = []
    x = []
    y = []
    z = []

    for l in lines:
        line = l.replace(',', ' ').split()
        if not reading_atom_list:  # Wait for 'atoms' line before reading atoms
            if line[0] == 'cell':
                a, b, c, alpha, beta, gamma = [float(word) for word in line[1:7]]
            elif line[0] == 'ncell':
                ncell = [int(word) for word in line[1:5]]
            elif line[0] == 'atoms':
                if a == 0:
                    print("Cell not found")
                    a = b = c = 1
                    alpha = beta = gamma = 90
                reading_atom_list = True
        else:
            symbol, xx, yy, zz = line[:4]
            symbols.append(symbol)
            x.append(xx)
            y.append(yy)
            z.append(zz)

    print("Found a = {}, b = {}, c = {}, alpha = {}, beta = {}, gamma = {}"
          .format(a, b, c, alpha, beta, gamma))

    x = np.array(x, dtype=np.float64)
    y = np.array(y, dtype=np.float64)
    z = np.array(z, dtype=np.float64)
    symbols = np.array(symbols)

    if ncell is not None:
        x -= np.tile(np.repeat(np.array(range(ncell[0])),
                               ncell[3]), ncell[1]*ncell[2]) + starting_cell[0]
        y -= np.tile(np.repeat(np.array(range(ncell[1])),
                               ncell[0]*ncell[3]), ncell[2]) + starting_cell[1]
        z -= np.repeat(np.array(range(ncell[2])),
                       ncell[0]*ncell[1]*ncell[3]) + starting_cell[2]

        # reorder atom arrays, discus stru files have x increment fastest
        # and z slowest, javelin is the opposite
        x = x.reshape(ncell).transpose((2, 1, 0, 3)).flatten()
        y = y.reshape(ncell).transpose((2, 1, 0, 3)).flatten()
        z = z.reshape(ncell).transpose((2, 1, 0, 3)).flatten()
        symbols = symbols.reshape(ncell).transpose((2, 1, 0, 3)).flatten()

    xyz = np.array((x, y, z)).T

    structure = Structure(unitcell=(a, b, c, alpha, beta, gamma),
                          symbols=symbols,
                          positions=xyz,
                          ncells=ncell)

    print("Read in these atoms:")
    print(structure.get_atom_count())

    return structure