Coverage for /builds/kinetik161/ase/ase/lattice/triclinic.py: 95.92%
49 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-12-10 11:04 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-12-10 11:04 +0000
1"""Function-like object creating triclinic lattices.
3The following lattice creator is defined:
4 Triclinic
5"""
7import numpy as np
9from ase.data import reference_states as _refstate
10from ase.lattice.bravais import Bravais
13class TriclinicFactory(Bravais):
14 "A factory for creating triclinic lattices."
16 # The name of the crystal structure in ChemicalElements
17 xtal_name = "triclinic"
19 # The natural basis vectors of the crystal structure
20 int_basis = np.array([[1, 0, 0],
21 [0, 1, 0],
22 [0, 0, 1]])
23 basis_factor = 1.0
25 # Converts the natural basis back to the crystallographic basis
26 inverse_basis = np.array([[1, 0, 0],
27 [0, 1, 0],
28 [0, 0, 1]])
29 inverse_basis_factor = 1.0
31 def get_lattice_constant(self):
32 """Get the lattice constant of an element with triclinic
33 crystal structure."""
34 if _refstate[self.atomicnumber]['symmetry'] != self.xtal_name:
35 raise ValueError(('Cannot guess the %s lattice constant of'
36 + ' an element with crystal structure %s.')
37 % (self.xtal_name,
38 _refstate[self.atomicnumber]['symmetry']))
39 return _refstate[self.atomicnumber].copy()
41 def make_crystal_basis(self):
42 """Make the basis matrix for the crystal unit cell and the system
43 unit cell."""
44 lattice = self.latticeconstant
45 if isinstance(lattice, type({})):
46 a = lattice['a']
47 try:
48 b = lattice['b']
49 except KeyError:
50 b = a * lattice['b/a']
51 try:
52 c = lattice['c']
53 except KeyError:
54 c = a * lattice['c/a']
55 alpha = lattice['alpha']
56 beta = lattice['beta']
57 gamma = lattice['gamma']
58 else:
59 if len(lattice) == 6:
60 (a, b, c, alpha, beta, gamma) = lattice
61 else:
62 raise ValueError(
63 "Improper lattice constants for triclinic crystal.")
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 [[a, 0, 0],
73 [b * cosg, b * sing, 0],
74 [c * cosb, c * (cosa - cosb * cosg) / sing,
75 c * np.sqrt(sinb**2 - ((cosa - cosb * cosg) / sing)**2)]])
76 self.latticeconstant = lattice
77 self.miller_basis = lattice
78 self.crystal_basis = (self.basis_factor *
79 np.dot(self.int_basis, lattice))
80 self.basis = np.dot(self.directions, self.crystal_basis)
81 assert abs(np.dot(lattice[0], lattice[1]) - a * b * cosg) < 1e-5
82 assert abs(np.dot(lattice[0], lattice[2]) - a * c * cosb) < 1e-5
83 assert abs(np.dot(lattice[1], lattice[2]) - b * c * cosa) < 1e-5
84 assert abs(np.dot(lattice[0], lattice[0]) - a * a) < 1e-5
85 assert abs(np.dot(lattice[1], lattice[1]) - b * b) < 1e-5
86 assert abs(np.dot(lattice[2], lattice[2]) - c * c) < 1e-5
89Triclinic = TriclinicFactory()