Coverage for tests/test_make_lattice.py: 100%

48 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-14 15:55 +0200

1import numpy as np 

2 

3import gamdpy as gp 

4 

5# cells = [2, 2, 2] 

6EXPECTED_FCC_POSITIONS = np.array( 

7 [ 

8 [-1.0, -1.0, -1.0], 

9 [-0.5, -0.5, -1.0], 

10 [-0.5, -1.0, -0.5], 

11 [-1.0, -0.5, -0.5], 

12 [-1.0, -1.0, 0.0], 

13 [-0.5, -0.5, 0.0], 

14 [-0.5, -1.0, 0.5], 

15 [-1.0, -0.5, 0.5], 

16 [-1.0, 0.0, -1.0], 

17 [-0.5, 0.5, -1.0], 

18 [-0.5, 0.0, -0.5], 

19 [-1.0, 0.5, -0.5], 

20 [-1.0, 0.0, 0.0], 

21 [-0.5, 0.5, 0.0], 

22 [-0.5, 0.0, 0.5], 

23 [-1.0, 0.5, 0.5], 

24 [ 0.0, -1.0, -1.0], 

25 [ 0.5, -0.5, -1.0], 

26 [ 0.5, -1.0, -0.5], 

27 [ 0.0, -0.5, -0.5], 

28 [ 0.0, -1.0, 0.0], 

29 [ 0.5, -0.5, 0.0], 

30 [ 0.5, -1.0, 0.5], 

31 [ 0.0, -0.5, 0.5], 

32 [ 0.0, 0.0, -1.0], 

33 [ 0.5, 0.5, -1.0], 

34 [ 0.5, 0.0, -0.5], 

35 [ 0.0, 0.5, -0.5], 

36 [ 0.0, 0.0, 0.0], 

37 [ 0.5, 0.5, 0.0], 

38 [ 0.5, 0.0, 0.5], 

39 [ 0.0, 0.5, 0.5] 

40 ] 

41) 

42 

43# cells = [2, 2, 2] 

44EXPECTED_BCC_POSITIONS = np.array([ 

45 [-1.0, -1.0, -1.0], 

46 [-0.5, -0.5, -0.5], 

47 [-1.0, -1.0, 0.0], 

48 [-0.5, -0.5, 0.5], 

49 [-1.0, 0.0, -1.0], 

50 [-0.5, 0.5, -0.5], 

51 [-1.0, 0.0, 0.0], 

52 [-0.5, 0.5, 0.5], 

53 [ 0.0, -1.0, -1.0], 

54 [ 0.5, -0.5, -0.5], 

55 [ 0.0, -1.0, 0.0], 

56 [ 0.5, -0.5, 0.5], 

57 [ 0.0, 0.0, -1.0], 

58 [ 0.5, 0.5, -0.5], 

59 [ 0.0, 0.0, 0.0], 

60 [ 0.5, 0.5, 0.5] 

61]) 

62 

63# cells = [4, 2] 

64EXPECTED_HEXAGONAL_POSITIONS = np.array([ 

65 [-2.0, -1.7320508], 

66 [-1.5, -0.8660254], 

67 [-2.0, 0.0], 

68 [-1.5, 0.8660254], 

69 [-1.0, -1.7320508], 

70 [-0.5, -0.8660254], 

71 [-1.0, 0.0], 

72 [-0.5, 0.8660254], 

73 [ 0.0, -1.7320508], 

74 [ 0.5, -0.8660254], 

75 [ 0.0, 0.0], 

76 [ 0.5, 0.8660254], 

77 [ 1.0, -1.7320508], 

78 [ 1.5, -0.8660254], 

79 [ 1.0, 0.0], 

80 [ 1.5, 0.8660254] 

81]) 

82 

83 

84def test_fcc_lattice(): 

85 # verbose = False 

86 # plot = False 

87 

88 cells = [2, 2, 2] 

89 positions, box_vector = gp.configuration.make_lattice(gp.unit_cells.FCC, cells) 

90 configuration = gp.Configuration(D=3) 

91 configuration['r'] = positions 

92 configuration.simbox = gp.Orthorhombic(configuration.D, box_vector) 

93 expected_box_vector = np.array([2.0, 2.0, 2.0]) 

94 assert np.allclose(configuration.simbox.get_lengths(), expected_box_vector) 

95 expected_number_of_particles = 32 

96 assert configuration['r'].shape[0] == expected_number_of_particles 

97 # if verbose: 

98 # print(' FCC lattice') 

99 # print("positions:", configuration['r']) 

100 # print("box_vector:", configuration.simbox.get_lengths()) 

101 # if plot: 

102 # import matplotlib.pyplot as plt 

103 # fig = plt.figure() 

104 # ax = fig.add_subplot(111, projection='3d') 

105 # ax.scatter(configuration['r'][:, 0], configuration['r'][:, 1], configuration['r'][:, 2]) 

106 # plt.show() 

107 

108def test_fcc_lattice_method(): 

109 print(" FCC lattice usign conf.make_lattice") 

110 conf = gp.Configuration(D=3) 

111 conf.make_lattice(gp.unit_cells.FCC, [2, 2, 2]) 

112 positions = conf['r'] 

113 print("positions:", positions) 

114 box_vector = conf.simbox.get_lengths() 

115 expected_box_vector = np.array([2.0, 2.0, 2.0]) 

116 assert np.allclose(positions, EXPECTED_FCC_POSITIONS, rtol=1e-4) 

117 assert np.allclose(conf.simbox.get_lengths(), expected_box_vector) 

118 print("positions:", positions) 

119 

120 

121def test_bcc_lattice(): 

122 cells = [2, 2, 2] 

123 positions, box_vector = gp.configuration.make_lattice(gp.unit_cells.BCC, cells) 

124 configuration = gp.Configuration(D=3) 

125 configuration['r'] = positions 

126 configuration.simbox = gp.Orthorhombic(configuration.D, box_vector) 

127 expected_number_of_particles = 16 

128 assert configuration['r'].shape[0] == expected_number_of_particles 

129 assert np.allclose(configuration['r'], EXPECTED_BCC_POSITIONS) 

130 expected_box_vector = np.array([2.0, 2.0, 2.0]) 

131 assert np.allclose(configuration.simbox.get_lengths(), expected_box_vector) 

132 # if verbose: 

133 # print(" BCC lattice") 

134 # print("positions:", configuration['r']) 

135 # print("box_vector:", configuration.simbox.get_lengths()) 

136 # if plot: 

137 # import matplotlib.pyplot as plt 

138 # fig = plt.figure() 

139 # ax = fig.add_subplot(111, projection='3d') 

140 # ax.scatter(configuration['r'][:, 0], configuration['r'][:, 1], configuration['r'][:, 2]) 

141 # plt.show() 

142 

143 

144def test_hexagonal(): 

145 # verbose = False 

146 # plot = False 

147 

148 cells = [4, 2] 

149 positions, box_vector = gp.configuration.make_lattice(gp.unit_cells.HEXAGONAL, cells=cells) 

150 configuration = gp.Configuration(D=2) 

151 configuration['r'] = positions 

152 configuration.simbox = gp.Orthorhombic(configuration.D, box_vector) 

153 expected_dimensions_of_space = 2 

154 assert configuration['r'].shape[1] == expected_dimensions_of_space 

155 expected_number_of_particles = 16 

156 assert configuration['r'].shape[0] == expected_number_of_particles 

157 

158 assert np.allclose(configuration['r'], EXPECTED_HEXAGONAL_POSITIONS, rtol=1e-4) 

159 

160 # if verbose: 

161 # print(' Hexagonal lattice') 

162 # print("positions:", configuration['r']) 

163 # print("box_vector:", configuration.simbox.get_lengths()) 

164 # if plot: 

165 # import matplotlib.pyplot as plt 

166 # plt.figure() 

167 # plt.title("Hexagonal lattice") 

168 # plt.scatter(configuration['r'][:, 0], configuration['r'][:, 1]) 

169 # # Plot the box 

170 # L_x, L_y = box_vector 

171 # box = plt.Rectangle([-L_x/2, -L_y/2], L_x, L_y, fill=False) 

172 # plt.gca().add_patch(box) 

173 # plt.axis('equal') 

174 # plt.show() 

175 

176 

177if __name__ == "__main__": # pragma: no cover 

178 test_hexagonal() 

179 test_fcc_lattice() 

180 test_fcc_lattice_method() 

181 test_bcc_lattice()