def test_supercell(): from numpy import array, all, abs from pylada.dftcrystal import Supercell, Crystal from pylada.error import ValueError structure = Crystal(136, 4.63909875, 2.97938395, \ ifhr=0, \ shift=0)\ .add_atom(0, 0, 0, 'Ti')\ .add_atom(0.306153, 0.306153, 0, 'O') \ .append(Supercell([[2,0,0],[0,1,0],[0,0,1]])) assert len(structure.eval()) == 2 * 6 structure[-1].matrix[2,2] = 2 assert len(structure.eval()) == 2 * 2 * 6 map = structure[-1].output_map() assert 'supercel' in map a = Supercell() a.read_input(map['supercel']) assert all(abs(a.matrix - structure[-1].matrix) < 1e-8) assert all(abs(eval(repr(a), {'Supercell': Supercell}).matrix - a.matrix) < 1e-8) # test errors try: structure[-1].matrix = array( [[0]*3]*3 ) except ValueError: pass else: raise Exception() structure[-1].matrix = array([[1,0],[0,1]]) try: structure.print_input() except ValueError: pass else: raise Exception() map = structure[-1].output_map() assert 'supercel' in map a = Supercell() a.read_input(map['supercel']) assert all(abs(a.matrix - structure[-1].matrix) < 1e-8) assert all(abs(eval(repr(a), {'Supercell': Supercell}).matrix - a.matrix) < 1e-8)
def test_crystal(): from numpy import array from pylada.dftcrystal.crystal import Crystal structure = Crystal(136, 4.63909875, 2.97938395, \ ifhr=0, \ shift=0)\ .add_atom(0, 0, 0, 'Ti')\ .add_atom(0.306153, 0.306153, 0, 'O') assert structure.print_input() is not None assert len(structure.print_input().split('\n')) == 9 assert all(abs(array(structure.print_input().split()[1:-2], dtype='float64') \ - [0, 0, 0, 136, 4.63909875, 2.97938395, 2, 22, 0, 0, 0, 8, 0.306153, 0.306153, 0]) < 1e-8) assert structure.print_input().split()[0] == 'CRYSTAL' assert structure.print_input().split()[-2] == 'END' assert structure.print_input().split()[-1] == 'CRYSTAL'
def test_elastic(): """ Test elastic keyword. """ from pickle import loads, dumps from numpy import array, all, abs, identity, dot from pylada.dftcrystal import Crystal, Elastic epsilon = array([[0, 0, 0], [0, 0, 0], [0, 0, 0.1]]) crystal = Crystal(227, 5.43).add_atom(0.125, 0.125, 0.125, 'Si') structure = crystal.eval() crystal.append(Elastic([[0, 0, 0], [0, 0, 0], [0, 0, 0.1]])) assert all(abs(dot(identity(3) + epsilon, structure.cell) - crystal.eval().cell) < 1e-8) assert all(abs(crystal.eval()[0].pos - dot(identity(3) + epsilon, structure[0].pos)) < 1e-8) assert all(abs(crystal.eval()[1].pos - dot(identity(3) + epsilon, structure[1].pos)) < 1e-8) assert abs(crystal.eval()[0].pos[1] - crystal.eval()[0].pos[0]) < 1e-8 assert abs(crystal.eval()[1].pos[2] / crystal.eval()[1].pos[1] - 1.1) < 1e-8 assert abs(crystal.eval()[1].pos[1] - crystal.eval()[1].pos[0]) < 1e-8 a = Elastic() a.raw = crystal[-1].raw assert a.is_epsilon == crystal[-1].is_epsilon assert all(abs(a.matrix - crystal[-1].matrix) < 1e-8) assert all(abs(eval(repr(a), {'Elastic': Elastic}).matrix - a.matrix) < 1e-8) assert eval(repr(a), {'Elastic': Elastic}).is_epsilon == a.is_epsilon assert loads(dumps(a)).is_epsilon == a.is_epsilon assert all(abs(loads(dumps(a)).matrix - a.matrix) < 1e-8) z = dot(epsilon, structure.cell) crystal[-1].matrix = z crystal[-1].is_epsilon = False assert all(abs(dot(identity(3) + epsilon, structure.cell) - crystal.eval().cell) < 1e-8) assert all(abs(crystal.eval()[0].pos - dot(identity(3) + epsilon, structure[0].pos)) < 1e-8) assert all(abs(crystal.eval()[1].pos - dot(identity(3) + epsilon, structure[1].pos)) < 1e-8) a = Elastic() a.raw = crystal[-1].raw assert a.is_epsilon == crystal[-1].is_epsilon assert all(abs(a.matrix - crystal[-1].matrix) < 1e-8) assert all(abs(eval(repr(a), {'Elastic': Elastic}).matrix - a.matrix) < 1e-8) assert eval(repr(a), {'Elastic': Elastic}).is_epsilon == a.is_epsilon assert loads(dumps(a)).is_epsilon == a.is_epsilon assert all(abs(loads(dumps(a)).matrix - a.matrix) < 1e-8) epsilon = array([[0, 0, 0], [0, 0, 0], [0, 0.1, 0]]) crystal[-1].matrix = epsilon crystal[-1].is_epsilon = True assert all(abs(dot(identity(3) + epsilon, structure.cell) - crystal.eval().cell) < 1e-8) assert all(abs(crystal.eval()[0].pos - dot(identity(3) + epsilon, structure[0].pos)) < 1e-8) assert all(abs(crystal.eval()[1].pos - dot(identity(3) + epsilon, structure[1].pos)) < 1e-8) a = Elastic() a.raw = crystal[-1].raw assert a.is_epsilon == crystal[-1].is_epsilon assert all(abs(a.matrix - crystal[-1].matrix) < 1e-8) assert all(abs(eval(repr(a), {'Elastic': Elastic}).matrix - a.matrix) < 1e-8) assert eval(repr(a), {'Elastic': Elastic}).is_epsilon == a.is_epsilon assert loads(dumps(a)).is_epsilon == a.is_epsilon assert all(abs(loads(dumps(a)).matrix - a.matrix) < 1e-8) z = dot(epsilon, structure.cell) crystal[-1].matrix = z crystal[-1].is_epsilon = False assert all(abs(dot(identity(3) + epsilon, structure.cell) - crystal.eval().cell) < 1e-8) assert all(abs(crystal.eval()[0].pos - dot(identity(3) + epsilon, structure[0].pos)) < 1e-8) assert all(abs(crystal.eval()[1].pos - dot(identity(3) + epsilon, structure[1].pos)) < 1e-8) a = Elastic() a.raw = crystal[-1].raw assert a.is_epsilon == crystal[-1].is_epsilon assert all(abs(a.matrix - crystal[-1].matrix) < 1e-8) assert all(abs(eval(repr(a), {'Elastic': Elastic}).matrix - a.matrix) < 1e-8) assert eval(repr(a), {'Elastic': Elastic}).is_epsilon == a.is_epsilon assert loads(dumps(a)).is_epsilon == a.is_epsilon assert all(abs(loads(dumps(a)).matrix - a.matrix) < 1e-8)
def test_crystal(): from numpy import all, abs, array from pylada.dftcrystal.crystal import Crystal a = Crystal() a.raw = '0 0 0\n'\ '136\n'\ '4.63909875 2.97938395\n'\ '2\n'\ '22 0.0 0.0 0.0\n'\ '8 3.061526467783E-01 3.061526467783E-01 0.0\n' assert a.symmgroup == 136 assert a.shift == 0 assert a.ifhr == 0 assert len(a.atoms) == 2 assert a.atoms[0].type == 'Ti' and all(abs(a.atoms[0].pos) < 1e-8) assert a.atoms[1].type == 'O' and all(abs(a.atoms[1].pos-[0.30615265, 0.30615265, 0.0]) < 1e-8) a.raw = a.raw assert a.symmgroup == 136 assert a.shift == 0 assert a.ifhr == 0 assert len(a.atoms) == 2 assert a.atoms[0].type == 'Ti' and all(abs(a.atoms[0].pos) < 1e-8) assert a.atoms[1].type == 'O' and all(abs(a.atoms[1].pos-[0.30615265, 0.30615265, 0.0]) < 1e-8) a.raw = '1 0 0\n'\ 'P 42/M N M\n'\ '4.63909875 2.97938395\n'\ '2\n'\ '22 0.0 0.0 0.0\n'\ '8 3.061526467783E-01 3.061526467783E-01 0.0\n' assert a.symmgroup == 'P 42/M N M' a.raw = a.raw assert a.symmgroup == 136 a.raw = '1 1 0\n'\ 'P 42/M N M\n'\ '4.63909875 2.97938395\n'\ '2\n'\ '22 0.0 0.0 0.0\n'\ '8 3.061526467783E-01 3.061526467783E-01 0.0\n' assert a.ifhr == 1 a.raw = a.raw assert a.ifhr == 1 a.raw = '1 0 1\n'\ 'P 42/M N M\n'\ '4.63909875 2.97938395\n'\ '2\n'\ '22 0.0 0.0 0.0\n'\ '8 3.061526467783E-01 3.061526467783E-01 0.0\n' assert a.shift == 1 a.raw = a.raw assert a.shift == 1 a.raw = '1 0 2\n'\ 'P 42/M N M\n'\ '5 5 5\n'\ '4.63909875 2.97938395\n'\ '2\n'\ '22 0.0 0.0 0.0\n'\ '8 3.061526467783E-01 3.061526467783E-01 0.0\n' assert all(abs(a.shift - 5/24.0) < 1e-8) a.raw = a.raw assert all(abs(a.shift - 5/24.0) < 1e-8) b = eval(repr(a), {'Crystal': Crystal, 'array': array}) assert b.symmgroup == a.symmgroup assert all(abs(array(b.params) - a.params) < 1e-8) assert len(b) == len(a) assert len(b.atoms) == len(a.atoms)