def test_hex():
    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_javelin = Structure(unitcell=unitcell,
                            symbols=symbols,
                            positions=positions)

    hex_ase = ase.Atoms(symbols=symbols, scaled_positions=positions, cell=hex_javelin.unitcell.Binv)

    assert len(hex_ase) == 6

    # unitcell
    assert_array_equal(hex_javelin.unitcell.cell,
                       ase.geometry.cell_to_cellpar(hex_ase.cell))
    # get_atomic_numbers
    assert_array_equal(hex_javelin.get_atomic_numbers(),
                       hex_ase.get_atomic_numbers())
    # get_positions
    assert_array_almost_equal(hex_javelin.get_positions(),
                              hex_ase.get_positions())
    # get_scaled_positions
    assert_array_almost_equal(hex_javelin.get_scaled_positions(),
                              hex_ase.get_scaled_positions())
示例#2
0
def test_hex():
    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_javelin = Structure(unitcell=unitcell,
                            symbols=symbols,
                            positions=positions)

    hex_ase = ase.Atoms(symbols=symbols,
                        scaled_positions=positions,
                        cell=hex_javelin.unitcell.Binv)

    assert len(hex_ase) == 6

    # unitcell
    assert_array_equal(hex_javelin.unitcell.cell,
                       ase.geometry.cell_to_cellpar(hex_ase.cell))
    # get_atomic_numbers
    assert_array_equal(hex_javelin.get_atomic_numbers(),
                       hex_ase.get_atomic_numbers())
    # get_positions
    assert_array_almost_equal(hex_javelin.get_positions(),
                              hex_ase.get_positions())
    # get_scaled_positions
    assert_array_almost_equal(hex_javelin.get_scaled_positions(),
                              hex_ase.get_scaled_positions())
示例#3
0
def test_average():
    structure = Structure(symbols=['C', 'O'], positions=[(0, 0, 0), (0.5, 0, 0)], unitcell=5)
    structure.repeat(5)

    four = Fourier()
    four.grid.bins = [2, 21]
    four.grid.set_corners(lr=[4.0, 0.0, 0.0],
                          ul=[0.0, 2.0, 0.0])
    four.structure = structure
    four._average = True
    results = four.calc()

    expected_result = [1.32348898e-23, 5.71594728e-24, 8.12875148e-56,
                       8.37681929e-25, 6.37686377e-56, 5.46068451e-25,
                       2.54939667e-55, 8.37681929e-25, 7.16891360e-55,
                       5.21188409e-24, 1.32348898e-23, 5.71594728e-24,
                       3.46656800e-54, 8.37681929e-25, 5.74150325e-55,
                       5.46068451e-25, 2.74171118e-55, 8.37681929e-25,
                       1.00428514e-54, 5.21188409e-24, 1.32348898e-23]
    assert_array_almost_equal(results[0, :, 0], expected_result)

    # Random move
    structure.rattle(seed=0)
    results = four.calc()

    expected_result = [1.32348898e-23, 8.88939203e-04, 7.27741076e-03,
                       1.91258313e-02, 5.94954973e-02, 1.26500450e-01,
                       1.34462469e-01, 1.04512735e-01, 1.17228978e-01,
                       7.24444566e-02, 5.87435134e-26, 1.06907925e-01,
                       2.60217008e-01, 3.57817285e-01, 7.25525103e-01,
                       1.13842501e+00, 9.60372378e-01, 6.18668588e-01,
                       5.97492048e-01, 3.24844518e-01, 2.34454638e-25]
    assert_array_almost_equal(results[0, :, 0], expected_result)
示例#4
0
def test_except():
    with pytest.raises(ValueError):
        Structure(symbols=['U'], positions=[[0, 0, 0]], ncells=[1, 1, 1, 2])

    structure = Structure()
    with pytest.raises(ValueError):
        structure.add_atom(symbol='U')
示例#5
0
def test_reindex():
    structure = Structure(unitcell=5, symbols=['Au', 'Ag', 'Pt', 'Pb'], positions=[[0, 0, 0],
                                                                                   [0, 0, 0],
                                                                                   [0, 0, 0],
                                                                                   [0, 0, 0]])

    assert_array_equal(structure.atoms.index.tolist(), [(0, 0, 0, 0),
                                                        (0, 0, 0, 1),
                                                        (0, 0, 0, 2),
                                                        (0, 0, 0, 3)])
    assert_array_almost_equal(structure.xyz_cartn, [[0., 0., 0.],
                                                    [0., 0., 0.],
                                                    [0., 0., 0.],
                                                    [0., 0., 0.]])
    structure.reindex([2, 2, 1, 1])
    assert_array_equal(structure.atoms.index.tolist(), [(0, 0, 0, 0),
                                                        (0, 1, 0, 0),
                                                        (1, 0, 0, 0),
                                                        (1, 1, 0, 0)])
    assert_array_almost_equal(structure.xyz_cartn, [[0., 0., 0.],
                                                    [0., 5., 0.],
                                                    [5., 0., 0.],
                                                    [5., 5., 0.]])
    structure.reindex([1, 1, 2, 2])
    assert_array_equal(structure.atoms.index.tolist(), [(0, 0, 0, 0),
                                                        (0, 0, 0, 1),
                                                        (0, 0, 1, 0),
                                                        (0, 0, 1, 1)])
    assert_array_almost_equal(structure.xyz_cartn, [[0., 0., 0.],
                                                    [0., 0., 0.],
                                                    [0., 0., 5.],
                                                    [0., 0., 5.]])
示例#6
0
def test_except():
    with pytest.raises(ValueError):
        Structure(symbols=['U'], positions=[[0, 0, 0]], ncells=[1, 1, 1, 2])

    structure = Structure()
    with pytest.raises(ValueError):
        structure.add_atom(symbol='U')
示例#7
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)
示例#8
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)
示例#9
0
def test_lots():
    import numpy as np
    np.random.seed(42)  # Make random lot selection not random

    structure = Structure(symbols=['C', 'O'], positions=[(0, 0, 0), (0.5, 0, 0)], unitcell=5)
    structure.repeat(5)

    four = Fourier()
    four.grid.bins = [2, 21]
    four.grid.set_corners(lr=[4.0, 0.0, 0.0],
                          ul=[0.0, 2.0, 0.0])
    four.structure = structure
    four.lots = None
    results = four.calc()

    expected_result = [2.42323706e+06, 1.01505872e+06, 1.46887112e-26,
                       1.48095071e+05, 1.16378024e-26, 9.69294822e+04,
                       5.01755975e-26, 1.48095071e+05, 1.35809407e-25,
                       1.01505872e+06, 2.42323706e+06, 1.01505872e+06,
                       6.70205900e-25, 1.48095071e+05, 1.03080955e-25,
                       9.69294822e+04, 5.35400081e-26, 1.48095071e+05,
                       1.77832044e-25, 1.01505872e+06, 2.42323706e+06]
    assert_allclose(results[0, :, 0], expected_result, atol=1e-25)

    four.lots = 3, 3, 3
    four.number_of_lots = 3
    results = four.calc()

    expected_result = [339175.64420172,  202125.69466616,   98663.70718625,
                       61677.5841574,   14394.84088099,   37686.18268908,
                       14394.84088099,   61677.5841574,   98663.70718625,
                       202125.69466616,  339175.64420172,  202125.69466616,
                       98663.70718625,   61677.5841574,   14394.84088099,
                       37686.18268908,   14394.84088099,   61677.5841574,
                       98663.70718625,  202125.69466616,  339175.64420172]
    assert_allclose(results[0, :, 0], expected_result)

    # Random move + average subtraction
    structure.rattle(seed=0)
    four._average = True
    results = four.calc()

    expected_result = [1.63820535e-24, 2.55360350e+05, 2.83431155e+05,
                       5.87063567e+04, 3.06182580e+04, 6.47880007e-03,
                       3.06274940e+04, 5.87242408e+04, 2.83411515e+05,
                       2.55140706e+05, 3.20451398e-01, 2.55556564e+05,
                       2.83427875e+05, 5.86850273e+04, 3.06047773e+04,
                       5.83516439e-02, 3.06324860e+04, 5.87386761e+04,
                       2.83368966e+05, 2.54897708e+05, 1.28156664e+00]
    assert_allclose(results[0, :, 0], expected_result)
def test_hex():
    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_javelin = Structure(unitcell=unitcell,
                            symbols=symbols,
                            positions=positions)

    hex_diffpy = dps.Structure(atoms=[dps.Atom(atype='C', xyz=xyz) for xyz in positions],
                               lattice=dps.Lattice(1.4, 1.4, 1, 90, 90, 120))
    assert len(hex_diffpy) == 6

    # unitcell
    assert_array_almost_equal(hex_javelin.unitcell.cell,
                              hex_diffpy.lattice.abcABG())
    # element
    assert_array_equal(hex_javelin.element,
                       np.array(hex_diffpy.element))
    # xyz
    assert_array_almost_equal(hex_javelin.xyz,
                              hex_diffpy.xyz)
    # xyz_cartn
    assert_array_almost_equal(hex_javelin.xyz_cartn,
                              hex_diffpy.xyz_cartn)
示例#11
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))
示例#12
0
def test_reindex():
    structure = Structure(unitcell=5,
                          symbols=['Au', 'Ag', 'Pt', 'Pb'],
                          positions=[[0, 0, 0], [0, 0, 0], [0, 0, 0],
                                     [0, 0, 0]])

    assert_array_equal(structure.atoms.index.tolist(), [(0, 0, 0, 0),
                                                        (0, 0, 0, 1),
                                                        (0, 0, 0, 2),
                                                        (0, 0, 0, 3)])
    assert_array_almost_equal(
        structure.xyz_cartn,
        [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.], [0., 0., 0.]])
    structure.reindex([2, 2, 1, 1])
    assert_array_equal(structure.atoms.index.tolist(), [(0, 0, 0, 0),
                                                        (0, 1, 0, 0),
                                                        (1, 0, 0, 0),
                                                        (1, 1, 0, 0)])
    assert_array_almost_equal(
        structure.xyz_cartn,
        [[0., 0., 0.], [0., 5., 0.], [5., 0., 0.], [5., 5., 0.]])
    structure.reindex([1, 1, 2, 2])
    assert_array_equal(structure.atoms.index.tolist(), [(0, 0, 0, 0),
                                                        (0, 0, 0, 1),
                                                        (0, 0, 1, 0),
                                                        (0, 0, 1, 1)])
    assert_array_almost_equal(
        structure.xyz_cartn,
        [[0., 0., 0.], [0., 0., 0.], [0., 0., 5.], [0., 0., 5.]])
示例#13
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))
示例#14
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))
示例#15
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))
示例#16
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
示例#17
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]])
示例#18
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]])
示例#19
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