Coverage for ase/lattice/cubic.py: 88.71%
62 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-27 15:21 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-27 15:21 +0000
1"""Function-like objects creating cubic lattices (SC, FCC, BCC and Diamond).
3The following lattice creators are defined:
4 SimpleCubic
5 FaceCenteredCubic
6 BodyCenteredCubic
7 Diamond
8"""
10import numpy as np
12from ase.data import reference_states as _refstate
13from ase.lattice.bravais import Bravais, reduceindex
16class SimpleCubicFactory(Bravais):
17 "A factory for creating simple cubic lattices."
19 # The name of the crystal structure in ChemicalElements
20 xtal_name = 'sc'
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
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
30 # For checking the basis volume
31 atoms_in_unit_cell = 1
33 def get_lattice_constant(self):
34 "Get the lattice constant of an element with cubic crystal structure."
35 if _refstate[self.atomicnumber]['symmetry'] != self.xtal_name:
36 raise ValueError(
37 (
38 'Cannot guess the %s lattice constant of'
39 + ' an element with crystal structure %s.'
40 )
41 % (self.xtal_name, _refstate[self.atomicnumber]['symmetry'])
42 )
43 return _refstate[self.atomicnumber]['a']
45 def make_crystal_basis(self):
46 """Make the basis matrix for the crystal unit cell and the
47 system unit cell."""
48 self.crystal_basis = (
49 self.latticeconstant * self.basis_factor * self.int_basis
50 )
51 self.miller_basis = self.latticeconstant * np.identity(3)
52 self.basis = np.dot(self.directions, self.crystal_basis)
53 self.check_basis_volume()
55 def check_basis_volume(self):
56 "Check the volume of the unit cell."
57 vol1 = abs(np.linalg.det(self.basis))
58 cellsize = self.atoms_in_unit_cell
59 if self.bravais_basis is not None:
60 cellsize *= len(self.bravais_basis)
61 vol2 = self.calc_num_atoms() * self.latticeconstant**3 / cellsize
62 assert abs(vol1 - vol2) < 1e-5
64 def find_directions(self, directions, miller):
65 "Find missing directions and miller indices from the specified ones."
66 directions = list(directions)
67 miller = list(miller)
68 # Process keyword "orthogonal"
69 self.find_ortho(directions)
70 self.find_ortho(miller)
71 Bravais.find_directions(self, directions, miller)
73 def find_ortho(self, idx):
74 "Replace keyword 'ortho' or 'orthogonal' with a direction."
75 for i in range(3):
76 if isinstance(idx[i], str) and (
77 idx[i].lower() == 'ortho' or idx[i].lower() == 'orthogonal'
78 ):
79 if self.debug:
80 print('Calculating orthogonal direction', i)
81 print(idx[i - 2], 'X', idx[i - 1], end=' ')
82 idx[i] = reduceindex(np.cross(idx[i - 2], idx[i - 1]))
83 if self.debug:
84 print('=', idx[i])
87SimpleCubic = SimpleCubicFactory()
90class FaceCenteredCubicFactory(SimpleCubicFactory):
91 "A factory for creating face-centered cubic lattices."
93 xtal_name = 'fcc'
94 int_basis = np.array([[0, 1, 1], [1, 0, 1], [1, 1, 0]])
95 basis_factor = 0.5
96 inverse_basis = np.array([[-1, 1, 1], [1, -1, 1], [1, 1, -1]])
97 inverse_basis_factor = 1.0
99 atoms_in_unit_cell = 4
102FaceCenteredCubic = FaceCenteredCubicFactory()
105class BodyCenteredCubicFactory(SimpleCubicFactory):
106 "A factory for creating body-centered cubic lattices."
108 xtal_name = 'bcc'
109 int_basis = np.array([[-1, 1, 1], [1, -1, 1], [1, 1, -1]])
110 basis_factor = 0.5
111 inverse_basis = np.array([[0, 1, 1], [1, 0, 1], [1, 1, 0]])
112 inverse_basis_factor = 1.0
114 atoms_in_unit_cell = 2
117BodyCenteredCubic = BodyCenteredCubicFactory()
120class DiamondFactory(FaceCenteredCubicFactory):
121 "A factory for creating diamond lattices."
123 xtal_name = 'diamond'
124 bravais_basis = [[0, 0, 0], [0.25, 0.25, 0.25]]
127Diamond = DiamondFactory()