Coverage for dynasor/qpoints/tools.py: 97%

52 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-11-09 14:30 +0000

1"""Exact integer and rational arithmetic on the repetition matrix `P`. 

2 

3The exactness is what keeps commensurate q-points and their `-q` partners 

4consistent for non-diagonal supercells. 

5""" 

6 

7from fractions import Fraction 

8from typing import Optional 

9 

10import numpy as np 

11from numpy.typing import NDArray 

12 

13 

14def exact_inverse(P: NDArray[int]) -> NDArray[Fraction]: 

15 """Return the inverse of a nonsingular integer `3x3` matrix as exact fractions. 

16 

17 Unlike :func:`numpy.linalg.inv` the result is exact, which matters when the 

18 inverse is used to decide whether a lattice point falls inside a cell. 

19 

20 Parameters 

21 ---------- 

22 P 

23 Nonsingular integer `3x3` matrix; the determinant may be negative. 

24 """ 

25 a, b, c = P 

26 adjugate = np.array([np.cross(b, c), np.cross(c, a), np.cross(a, b)]).T 

27 det = int(a @ np.cross(b, c)) 

28 P_inv = np.array([Fraction(int(n), det) for n in adjugate.flat]).reshape(3, 3) 

29 assert np.all(P @ P_inv == np.eye(3)) 

30 return P_inv 

31 

32 

33def _get_commensurate_points(P: NDArray[int]) -> NDArray[Fraction]: 

34 """Return the points commensurate with `P` in reduced coordinates, exactly. 

35 

36 Wanted are all `f` in `[0, 1)` for which `f P` is an integer vector, i.e. the 

37 cosets of the lattice spanned by the rows of `P`. There are exactly 

38 `abs(det(P))` of them and they are enumerated as such: starting from the 

39 origin, adding a row of `inv(P)` and reducing modulo 1 steps between cosets, 

40 so the group closes after `abs(det(P))` points. 

41 

42 Enumerating the integer points of the bounding box instead would be 

43 proportional to the shear of `P` rather than to its determinant, which for a 

44 strongly sheared cell means allocating billions of candidates to keep a handful. 

45 

46 Parameters 

47 ---------- 

48 P 

49 The repetition matrix relating the primitive and supercell. 

50 

51 Returns 

52 ------- 

53 The commensurate points, sorted, as exact fractions. 

54 """ 

55 generators = [tuple(row) for row in exact_inverse(P)] 

56 

57 points, stack = set(), [(Fraction(0),) * 3] 

58 while stack: 

59 f = stack.pop() 

60 if f in points: 

61 continue 

62 points.add(f) 

63 for g in generators: 

64 stack.append(tuple((a + b) % 1 for a, b in zip(f, g))) 

65 

66 return np.array(sorted(points)) 

67 

68 

69def _find_on_line( 

70 start: NDArray[float], 

71 stop: NDArray[float], 

72 P: NDArray[int], 

73) -> list[Fraction]: 

74 """Find fractional distances between start and stop compatible with P. 

75 

76 A supercell is defined by `P @ c = S` for some repetition matrix `P` and we 

77 want to find fractions so that:: 

78 

79 [start + f * (stop - start)] @ P = n 

80 

81 Parameters 

82 ---------- 

83 start 

84 Start of line in reduced supercell coordinates. 

85 stop 

86 End of line in reduced supercell coordinates. 

87 P 

88 Repetition matrix defining the supercell. 

89 """ 

90 

91 if np.allclose(start, stop): 91 ↛ 92line 91 didn't jump to line 92 because the condition on line 91 was never true

92 return [Fraction(0, 1)] 

93 

94 start = np.array([Fraction(s).limit_denominator() for s in start]) 

95 stop = np.array([Fraction(s).limit_denominator() for s in stop]) 

96 

97 A = start @ P 

98 B = (stop - start) @ P 

99 

100 fracs = None 

101 for a, b in zip(A, B): 

102 fs = _solve_diophantine(a, b) 

103 if fs is None: # "inf" solutions 

104 continue 

105 elif fs == []: # No solutions 

106 return [] 

107 fracs = set(fs) if fracs is None else fracs.intersection(fs) 

108 return sorted(fracs) 

109 

110 

111def _solve_diophantine(a: Fraction, b: Fraction) -> Optional[list[Fraction]]: 

112 """Solve n = a + xb for all n in Z and a,b in Q such that 0 <= x <= 1. 

113 

114 Returns `None` when there are infinitely many solutions. 

115 """ 

116 

117 if b == 0: 

118 if a.denominator == 1: 

119 return None 

120 else: 

121 return [] 

122 

123 if b < 0: 

124 right = np.ceil(a) 

125 left = np.floor(a + b) 

126 else: 

127 left = np.floor(a) 

128 right = np.ceil(a + b) 

129 

130 ns = np.arange(left, right + 1) 

131 fracs = [Fraction(n - a, b) for n in ns] 

132 fracs = [f for f in fracs if 0 <= f <= 1] 

133 

134 return fracs