Coverage for gpaw/test/symmetry/test_symmetry.py: 100%

69 statements  

« prev     ^ index     » next       coverage.py v7.7.1, created at 2025-07-12 00:18 +0000

1from math import sqrt 

2import numpy as np 

3 

4from gpaw.new.symmetry import Symmetries 

5from gpaw.new.brillouin import MonkhorstPackKPoints 

6 

7 

8def test_si(): 

9 """Primitive diamond lattice, with Si lattice parameter.""" 

10 a = 5.475 

11 cell_cv = .5 * a * np.array([(1, 1, 0), (1, 0, 1), (0, 1, 1)]) 

12 spos_ac = np.array([(.00, .00, .00), 

13 (.25, .25, .25)]) 

14 id_a = [1, 1] # two identical atoms 

15 pbc_c = np.ones(3, bool) 

16 mp = MonkhorstPackKPoints((4, 4, 4)) 

17 

18 # Do check 

19 symm = Symmetries.from_cell(cell_cv, pbc=pbc_c) 

20 symm = symm.analyze_positions(spos_ac, id_a) 

21 assert len(symm) == 24 

22 ibz = mp.reduce(symm, strict=False) 

23 assert len(ibz) == 10 

24 a = 3 / 32 

25 b = 1 / 32 

26 c = 6 / 32 

27 assert np.all(ibz.weight_k == [a, b, a, c, c, a, a, a, a, b]) 

28 assert not symm.rotation_scc.sum(0).any() 

29 

30 # Rotate unit cell and check again: 

31 cell_cv = a / sqrt(2) * np.array([(1, 0, 0), 

32 (0.5, sqrt(3) / 2, 0), 

33 (0.5, sqrt(3) / 6, sqrt(2.0 / 3))]) 

34 symm = Symmetries.from_cell(cell_cv, pbc=pbc_c) 

35 symm = symm.analyze_positions(spos_ac, id_a) 

36 assert len(symm) == 24 

37 ibz2 = mp.reduce(symm, strict=False) 

38 assert len(ibz) == 10 

39 assert abs(ibz.weight_k - ibz2.weight_k).sum() < 1e-14 

40 assert abs(ibz.kpt_kc - ibz2.kpt_kc).sum() < 1e-14 

41 assert not symm.rotation_scc.sum(0).any() 

42 

43 mp = MonkhorstPackKPoints((3, 3, 3)) 

44 ibz = mp.reduce(symm) 

45 assert len(ibz) == 4 

46 assert abs(ibz.weight_k * 27 - (1, 12, 6, 8)).sum() < 1e-14 

47 

48 

49def test_h4(): 

50 # Linear chain of four atoms, with H lattice parameter 

51 cell_cv = np.diag((8., 5., 5.)) 

52 spos_ac = np.array([[0.125, 0.5, 0.5], 

53 [0.375, 0.5, 0.5], 

54 [0.625, 0.5, 0.5], 

55 [0.875, 0.5, 0.5]]) 

56 id_a = [1, 1, 1, 1] # four identical atoms 

57 pbc_c = np.array([1, 0, 0], bool) 

58 

59 # Do check 

60 symm = Symmetries.from_cell(cell_cv, pbc=pbc_c) 

61 symm = symm.analyze_positions(spos_ac, id_a) 

62 assert len(symm) == 16 

63 mp = MonkhorstPackKPoints((3, 1, 1)) 

64 ibz = mp.reduce(symm) 

65 assert len(ibz) == 2 

66 assert np.all(ibz.weight_k == [1 / 3., 2 / 3.]) 

67 

68 

69def test_2(): 

70 # Rocksalt Ni2O2 

71 a = 7.92 

72 x = 2. * np.sqrt(1 / 3) 

73 y = np.sqrt(1 / 8) 

74 z1 = np.sqrt(1 / 24) 

75 z2 = np.sqrt(1 / 6) 

76 cell_cv = a * np.array([(x, y, -z1), (x, -y, -z1), (x, 0., z2)]) 

77 spos_ac = np.array([[0., 0., 0.], 

78 [1. / 2., 1. / 2., 1. / 2.], 

79 [1. / 4., 1. / 4., 1. / 4.], 

80 [3. / 4., 3. / 4., 3. / 4.]]) 

81 id_a = [1, 2, 3, 3] 

82 pbc_c = np.array([1, 1, 1], bool) 

83 

84 # Do check 

85 symm = Symmetries.from_cell(cell_cv, pbc=pbc_c) 

86 symm = symm.analyze_positions(spos_ac, id_a) 

87 assert len(symm) == 12 

88 mp = MonkhorstPackKPoints((2, 2, 2)) 

89 ibz = mp.reduce(symm) 

90 assert len(ibz) == 2 

91 assert np.all(ibz.weight_k == [3 / 4, 1 / 4]) 

92 

93 

94def test_new(): 

95 sym = Symmetries.from_cell([1, 2, 3]) 

96 assert sym.has_inversion 

97 assert len(sym) == 8 

98 sym2 = sym.analyze_positions([[0, 0, 0], [0, 0, 0.5]], 

99 ids=[1, 2]) 

100 assert len(sym2) == 8