Coverage for ase/lattice/triclinic.py: 95.92%

49 statements  

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

1"""Function-like object creating triclinic lattices. 

2 

3The following lattice creator is defined: 

4 Triclinic 

5""" 

6 

7import numpy as np 

8 

9from ase.data import reference_states as _refstate 

10from ase.lattice.bravais import Bravais 

11 

12 

13class TriclinicFactory(Bravais): 

14 "A factory for creating triclinic lattices." 

15 

16 # The name of the crystal structure in ChemicalElements 

17 xtal_name = 'triclinic' 

18 

19 # The natural basis vectors of the crystal structure 

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

21 basis_factor = 1.0 

22 

23 # Converts the natural basis back to the crystallographic basis 

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

25 inverse_basis_factor = 1.0 

26 

27 def get_lattice_constant(self): 

28 """Get the lattice constant of an element with triclinic 

29 crystal structure.""" 

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

31 raise ValueError( 

32 ( 

33 'Cannot guess the %s lattice constant of' 

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

35 ) 

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

37 ) 

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

39 

40 def make_crystal_basis(self): 

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

42 unit cell.""" 

43 lattice = self.latticeconstant 

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

45 a = lattice['a'] 

46 try: 

47 b = lattice['b'] 

48 except KeyError: 

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

50 try: 

51 c = lattice['c'] 

52 except KeyError: 

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

54 alpha = lattice['alpha'] 

55 beta = lattice['beta'] 

56 gamma = lattice['gamma'] 

57 else: 

58 if len(lattice) == 6: 

59 (a, b, c, alpha, beta, gamma) = lattice 

60 else: 

61 raise ValueError( 

62 'Improper lattice constants for triclinic crystal.' 

63 ) 

64 

65 degree = np.pi / 180.0 

66 cosa = np.cos(alpha * degree) 

67 cosb = np.cos(beta * degree) 

68 sinb = np.sin(beta * degree) 

69 cosg = np.cos(gamma * degree) 

70 sing = np.sin(gamma * degree) 

71 lattice = np.array( 

72 [ 

73 [a, 0, 0], 

74 [b * cosg, b * sing, 0], 

75 [ 

76 c * cosb, 

77 c * (cosa - cosb * cosg) / sing, 

78 c * np.sqrt(sinb**2 - ((cosa - cosb * cosg) / sing) ** 2), 

79 ], 

80 ] 

81 ) 

82 self.latticeconstant = lattice 

83 self.miller_basis = lattice 

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

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

86 assert abs(np.dot(lattice[0], lattice[1]) - a * b * cosg) < 1e-5 

87 assert abs(np.dot(lattice[0], lattice[2]) - a * c * cosb) < 1e-5 

88 assert abs(np.dot(lattice[1], lattice[2]) - b * c * cosa) < 1e-5 

89 assert abs(np.dot(lattice[0], lattice[0]) - a * a) < 1e-5 

90 assert abs(np.dot(lattice[1], lattice[1]) - b * b) < 1e-5 

91 assert abs(np.dot(lattice[2], lattice[2]) - c * c) < 1e-5 

92 

93 

94Triclinic = TriclinicFactory()