Coverage for ase/lattice/orthorhombic.py: 45.33%

75 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-11-27 15:21 +0000

1"""Function-like objects creating orthorhombic lattices. 

2 

3The following lattice creators are defined: 

4 SimleOrthorhombic 

5 BaseCenteredOrthorhombic 

6 BodyCenteredOrthorhombic 

7 FaceCenteredOrthorhombic 

8""" 

9 

10import numpy as np 

11 

12from ase.data import reference_states as _refstate 

13from ase.lattice.bravais import Bravais 

14 

15 

16class SimpleOrthorhombicFactory(Bravais): 

17 "A factory for creating simple orthorhombic lattices." 

18 

19 # The name of the crystal structure in ChemicalElements 

20 xtal_name = 'orthorhombic' 

21 

22 # The natural basis vectors of the crystal structure 

23 int_basis = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) 

24 basis_factor = 1.0 

25 

26 # Converts the natural basis back to the crystallographic basis 

27 inverse_basis = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) 

28 inverse_basis_factor = 1.0 

29 

30 def get_lattice_constant(self): 

31 """Get the lattice constant of an element with orhtorhombic 

32 crystal structure.""" 

33 if _refstate[self.atomicnumber]['symmetry'] != self.xtal_name: 

34 raise ValueError( 

35 ( 

36 'Cannot guess the %s lattice constant of' 

37 + ' an element with crystal structure %s.' 

38 ) 

39 % (self.xtal_name, _refstate[self.atomicnumber]['symmetry']) 

40 ) 

41 return _refstate[self.atomicnumber].copy() 

42 

43 def make_crystal_basis(self): 

44 """Make the basis matrix for the crystal unit cell and the system unit 

45 cell.""" 

46 

47 lattice = self.latticeconstant 

48 if isinstance(lattice, type({})): 

49 a = lattice['a'] 

50 try: 

51 b = lattice['b'] 

52 except KeyError: 

53 b = a * lattice['b/a'] 

54 try: 

55 c = lattice['c'] 

56 except KeyError: 

57 c = a * lattice['c/a'] 

58 else: 

59 if len(lattice) == 3: 

60 (a, b, c) = lattice 

61 else: 

62 raise ValueError( 

63 'Improper lattice constants for orthorhombic crystal.' 

64 ) 

65 

66 lattice = np.array([[a, 0, 0], [0, b, 0], [0, 0, c]]) 

67 self.latticeconstant = lattice 

68 self.miller_basis = lattice 

69 self.crystal_basis = self.basis_factor * np.dot(self.int_basis, lattice) 

70 self.basis = np.dot(self.directions, self.crystal_basis) 

71 self.check_basis_volume() 

72 

73 def check_basis_volume(self): 

74 "Check the volume of the unit cell." 

75 vol1 = abs(np.linalg.det(self.basis)) 

76 vol2 = self.calc_num_atoms() * np.linalg.det(self.latticeconstant) 

77 if self.bravais_basis is not None: 

78 vol2 /= len(self.bravais_basis) 

79 if abs(vol1 - vol2) > 1e-5: 

80 print(f'WARNING: Got volume {vol1:f}, expected {vol2:f}') 

81 

82 

83SimpleOrthorhombic = SimpleOrthorhombicFactory() 

84 

85 

86class BaseCenteredOrthorhombicFactory(SimpleOrthorhombicFactory): 

87 "A factory for creating base-centered orthorhombic lattices." 

88 

89 # The natural basis vectors of the crystal structure 

90 int_basis = np.array([[1, -1, 0], [1, 1, 0], [0, 0, 2]]) 

91 basis_factor = 0.5 

92 

93 # Converts the natural basis back to the crystallographic basis 

94 inverse_basis = np.array([[1, 1, 0], [-1, 1, 0], [0, 0, 1]]) 

95 inverse_basis_factor = 1.0 

96 

97 def check_basis_volume(self): 

98 "Check the volume of the unit cell." 

99 vol1 = abs(np.linalg.det(self.basis)) 

100 vol2 = self.calc_num_atoms() * np.linalg.det(self.latticeconstant) / 2.0 

101 if abs(vol1 - vol2) > 1e-5: 

102 print(f'WARNING: Got volume {vol1:f}, expected {vol2:f}') 

103 

104 

105BaseCenteredOrthorhombic = BaseCenteredOrthorhombicFactory() 

106 

107 

108class BodyCenteredOrthorhombicFactory(SimpleOrthorhombicFactory): 

109 "A factory for creating body-centered orthorhombic lattices." 

110 

111 int_basis = np.array([[-1, 1, 1], [1, -1, 1], [1, 1, -1]]) 

112 basis_factor = 0.5 

113 inverse_basis = np.array([[0, 1, 1], [1, 0, 1], [1, 1, 0]]) 

114 inverse_basis_factor = 1.0 

115 

116 def check_basis_volume(self): 

117 "Check the volume of the unit cell." 

118 vol1 = abs(np.linalg.det(self.basis)) 

119 vol2 = self.calc_num_atoms() * np.linalg.det(self.latticeconstant) / 2.0 

120 if abs(vol1 - vol2) > 1e-5: 

121 print(f'WARNING: Got volume {vol1:f}, expected {vol2:f}') 

122 

123 

124BodyCenteredOrthorhombic = BodyCenteredOrthorhombicFactory() 

125 

126 

127class FaceCenteredOrthorhombicFactory(SimpleOrthorhombicFactory): 

128 "A factory for creating face-centered orthorhombic lattices." 

129 

130 int_basis = np.array([[0, 1, 1], [1, 0, 1], [1, 1, 0]]) 

131 basis_factor = 0.5 

132 inverse_basis = np.array([[-1, 1, 1], [1, -1, 1], [1, 1, -1]]) 

133 inverse_basis_factor = 1.0 

134 

135 def check_basis_volume(self): 

136 "Check the volume of the unit cell." 

137 vol1 = abs(np.linalg.det(self.basis)) 

138 vol2 = self.calc_num_atoms() * np.linalg.det(self.latticeconstant) / 4.0 

139 if abs(vol1 - vol2) > 1e-5: 

140 print(f'WARNING: Got volume {vol1:f}, expected {vol2:f}') 

141 

142 

143FaceCenteredOrthorhombic = FaceCenteredOrthorhombicFactory()