From 9dc26f75462732ca76c27ef6475a1b221efc31ca Mon Sep 17 00:00:00 2001 From: Atsushi Togo Date: Sun, 28 Aug 2016 11:25:33 +0900 Subject: [PATCH] A test for Symmetry class is added. --- phonopy/interface/phonopy_yaml.py | 13 ++-- phonopy/structure/symmetry.py | 41 +---------- .../{harmonic/POSCAR.yaml => NaCl.yaml} | 0 test/phonopy/{interface => }/POSCAR_NaCl | 0 test/phonopy/harmonic/test_dynmat_to_fc.py | 2 +- test/phonopy/interface/test_phonopy_yaml.py | 2 +- test/phonopy/structure/Cr.yaml | 11 +++ test/phonopy/structure/test_symmetry.py | 71 +++++++++++++++++++ test/phonopy/unfolding/POSCAR | 15 ---- test/phonopy/unfolding/test_unfolding.py | 2 +- 10 files changed, 96 insertions(+), 61 deletions(-) rename test/phonopy/{harmonic/POSCAR.yaml => NaCl.yaml} (100%) rename test/phonopy/{interface => }/POSCAR_NaCl (100%) create mode 100644 test/phonopy/structure/Cr.yaml create mode 100644 test/phonopy/structure/test_symmetry.py delete mode 100644 test/phonopy/unfolding/POSCAR diff --git a/phonopy/interface/phonopy_yaml.py b/phonopy/interface/phonopy_yaml.py index 2c1171f7..5fff24bb 100644 --- a/phonopy/interface/phonopy_yaml.py +++ b/phonopy/interface/phonopy_yaml.py @@ -69,13 +69,17 @@ class PhonopyYaml: self._supercell_matrix = None self._primitive_matrix = None self._force_constants = None - self._p2s_map = None self._s2p_map = None - self._p2p_map = None + self._u2p_map = None + self._nac_params = None + self._version = None def get_unitcell(self): return self._unitcell + def set_unitcell(self, cell): + self._unitcell = cell + def get_primitive(self): return self._primitive @@ -113,8 +117,9 @@ class PhonopyYaml: nac_factor = self._nac_params['factor'] dielectric = self._nac_params['dielectric'] - lines.append("phonopy:") - lines.append(" version: %s" % self._version) + if self._version: + lines.append("phonopy:") + lines.append(" version: %s" % self._version) if self._calculator: lines.append(" calculator: %s" % self._calculator) if self._nac_params: diff --git a/phonopy/structure/symmetry.py b/phonopy/structure/symmetry.py index 38fc9f97..70e7cb29 100644 --- a/phonopy/structure/symmetry.py +++ b/phonopy/structure/symmetry.py @@ -212,7 +212,8 @@ class Symmetry: zip(ops['rotations'], ops['translations'])): diff = np.dot(pos[i], r.T) + t - pos[eq_atom] - if (abs(diff - np.rint(diff)) < self._symprec).all(): + diff -= np.rint(diff) + if np.linalg.norm(diff) < self._symprec: map_operations[i] = j break @@ -289,42 +290,4 @@ def get_lattice_vector_equivalence(point_symmetry): equivalence[0] = True return equivalence - - -if __name__ == '__main__': - from phonopy.structure.symmetry import Symmetry - from phonopy.interface.vasp import read_vasp - def get_magmom(text): - magmom = [] - for numxmag in text.split(): - if '*' in numxmag: - num, mag = numxmag.split('*') - magmom += [float(mag)] * int(num) - else: - magmom.append(float(numxmag)) - return magmom - - def parse_incar(filename): - for line in open(filename): - for conf in line.split(';'): - if 'MAGMOM' in conf: - return get_magmom(conf.split('=')[1]) - - cell = read_vasp("POSCAR") - symmetry = Symmetry(cell, symprec=1e-3) - map_nonspin = symmetry.get_map_atoms() - print("Number of operations w/o spin %d" % - len(symmetry.get_symmetry_operations()['rotations'])) - magmoms = parse_incar("INCAR") - cell.set_magnetic_moments(magmoms) - symmetry = Symmetry(cell, symprec=1e-3) - print("Number of operations w spin %d" % - len(symmetry.get_symmetry_operations()['rotations'])) - map_withspin = symmetry.get_map_atoms() - if ((map_nonspin - map_withspin) == 0).all(): - print(True) - else: - print(False) - print(map_nonspin) - print(map_withspin) diff --git a/test/phonopy/harmonic/POSCAR.yaml b/test/phonopy/NaCl.yaml similarity index 100% rename from test/phonopy/harmonic/POSCAR.yaml rename to test/phonopy/NaCl.yaml diff --git a/test/phonopy/interface/POSCAR_NaCl b/test/phonopy/POSCAR_NaCl similarity index 100% rename from test/phonopy/interface/POSCAR_NaCl rename to test/phonopy/POSCAR_NaCl diff --git a/test/phonopy/harmonic/test_dynmat_to_fc.py b/test/phonopy/harmonic/test_dynmat_to_fc.py index 3e277da7..69ac88d8 100644 --- a/test/phonopy/harmonic/test_dynmat_to_fc.py +++ b/test/phonopy/harmonic/test_dynmat_to_fc.py @@ -7,7 +7,7 @@ from phonopy.structure.cells import get_supercell, get_primitive class TestDynmatToFc(unittest.TestCase): def setUp(self): - filename = "POSCAR.yaml" + filename = "../NaCl.yaml" self._cell = get_unitcell_from_phonopy_yaml(filename) def tearDown(self): diff --git a/test/phonopy/interface/test_phonopy_yaml.py b/test/phonopy/interface/test_phonopy_yaml.py index ad1f3d4e..ac85db2c 100644 --- a/test/phonopy/interface/test_phonopy_yaml.py +++ b/test/phonopy/interface/test_phonopy_yaml.py @@ -35,7 +35,7 @@ class TestPhonopyYaml(unittest.TestCase): # print(unitcell) def _get_phonon(self): - cell = read_vasp("POSCAR_NaCl") + cell = read_vasp("../POSCAR_NaCl") phonon = Phonopy(cell, np.diag([2, 2, 2]), primitive_matrix=[[0, 0.5, 0.5], diff --git a/test/phonopy/structure/Cr.yaml b/test/phonopy/structure/Cr.yaml new file mode 100644 index 00000000..c3697942 --- /dev/null +++ b/test/phonopy/structure/Cr.yaml @@ -0,0 +1,11 @@ +lattice: +- [ 2.812696943681890, 0.000000000000000, 0.000000000000000 ] # a +- [ 0.000000000000000, 2.812696943681890, 0.000000000000000 ] # b +- [ 0.000000000000000, 0.000000000000000, 2.812696943681890 ] # c +points: +- symbol: Cr # 1 + coordinates: [ 0.000000000000000, 0.000000000000000, 0.000000000000000 ] + mass: 51.996100 +- symbol: Cr # 2 + coordinates: [ 0.500000000000000, 0.500000000000000, 0.500000000000000 ] + mass: 51.996100 diff --git a/test/phonopy/structure/test_symmetry.py b/test/phonopy/structure/test_symmetry.py new file mode 100644 index 00000000..1702ce11 --- /dev/null +++ b/test/phonopy/structure/test_symmetry.py @@ -0,0 +1,71 @@ +import unittest + +import numpy as np +import time + +from phonopy.structure.symmetry import Symmetry +from phonopy.structure.cells import get_supercell +from phonopy.interface.phonopy_yaml import get_unitcell_from_phonopy_yaml + +class TestSymmetry(unittest.TestCase): + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_get_map_operations(self): + symprec = 1e-5 + cell = get_unitcell_from_phonopy_yaml("../NaCl.yaml") + scell = get_supercell(cell, np.diag([2, 2, 2]), symprec=symprec) + symmetry = Symmetry(scell, symprec=symprec) + + map_ops_cmp = [ 0, 2, 51, 53, 8, 1, 65, 98, 3, 14, + 34, 24, 76, 69, 54, 110, 576, 198, 229, 208, + 201, 192, 210, 294, 5, 67, 36, 57, 18, 90, + 23, 114, 0, 9, 1, 96, 6, 323, 98, 326, + 3, 42, 17, 293, 16, 130, 99, 337, 192, 194, + 202, 199, 205, 196, 200, 193, 12, 5, 30, 108, + 37, 128, 291, 302] + start = time.time() + symmetry._set_map_operations() + end = time.time() + # print(end - start) + map_ops = symmetry.get_map_operations() + self.assertTrue((map_ops_cmp == map_ops).all()) + + def test_magmom(self): + symprec = 1e-5 + cell = get_unitcell_from_phonopy_yaml("Cr.yaml") + symmetry_nonspin = Symmetry(cell, symprec=symprec) + atom_map_nonspin = symmetry_nonspin.get_map_atoms() + len_sym_nonspin = len( + symmetry_nonspin.get_symmetry_operations()['rotations']) + + spin = [1, -1] + cell_withspin = cell.copy() + cell_withspin.set_magnetic_moments(spin) + symmetry_withspin = Symmetry(cell_withspin, symprec=symprec) + atom_map_withspin = symmetry_withspin.get_map_atoms() + len_sym_withspin = len( + symmetry_withspin.get_symmetry_operations()['rotations']) + + broken_spin = [1, -2] + cell_brokenspin = cell.copy() + cell_brokenspin = cell.copy() + cell_brokenspin.set_magnetic_moments(broken_spin) + symmetry_brokenspin = Symmetry(cell_brokenspin, symprec=symprec) + atom_map_brokenspin = symmetry_brokenspin.get_map_atoms() + len_sym_brokenspin = len( + symmetry_brokenspin.get_symmetry_operations()['rotations']) + + self.assertTrue((atom_map_nonspin == atom_map_withspin).all()) + self.assertFalse((atom_map_nonspin == atom_map_brokenspin).all()) + self.assertTrue(len_sym_nonspin == len_sym_withspin) + self.assertFalse(len_sym_nonspin == len_sym_brokenspin) + +if __name__ == '__main__': + suite = unittest.TestLoader().loadTestsFromTestCase(TestSymmetry) + unittest.TextTestRunner(verbosity=2).run(suite) + # unittest.main() diff --git a/test/phonopy/unfolding/POSCAR b/test/phonopy/unfolding/POSCAR deleted file mode 100644 index e7e01402..00000000 --- a/test/phonopy/unfolding/POSCAR +++ /dev/null @@ -1,15 +0,0 @@ -Na Cl - 1.00000000000000 - 5.6903014761756712 0.0000000000000000 0.0000000000000000 - 0.0000000000000000 5.6903014761756712 0.0000000000000000 - 0.0000000000000000 0.0000000000000000 5.6903014761756712 - 4 4 -Direct - 0.0000000000000000 0.0000000000000000 0.0000000000000000 - 0.0000000000000000 0.5000000000000000 0.5000000000000000 - 0.5000000000000000 0.0000000000000000 0.5000000000000000 - 0.5000000000000000 0.5000000000000000 0.0000000000000000 - 0.5000000000000000 0.5000000000000000 0.5000000000000000 - 0.5000000000000000 0.0000000000000000 0.0000000000000000 - 0.0000000000000000 0.5000000000000000 0.0000000000000000 - 0.0000000000000000 0.0000000000000000 0.5000000000000000 diff --git a/test/phonopy/unfolding/test_unfolding.py b/test/phonopy/unfolding/test_unfolding.py index a13f60ca..bf8350fa 100644 --- a/test/phonopy/unfolding/test_unfolding.py +++ b/test/phonopy/unfolding/test_unfolding.py @@ -12,7 +12,7 @@ from phonopy.structure.atoms import PhonopyAtoms as Atoms class TestUnfolding(unittest.TestCase): def setUp(self): - self._cell = read_vasp("POSCAR") + self._cell = read_vasp("../POSCAR_NaCl") # print(self._cell) self._unfolding = None