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')
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())
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)
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.]])
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)
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))
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))
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]])
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