Coverage for ase/dft/wannierstate.py: 63.79%

58 statements  

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

1import numpy as np 

2from scipy.linalg import qr 

3 

4 

5def random_orthogonal_matrix(dim, rng=np.random, real=False): 

6 """Generate uniformly distributed random orthogonal matrices""" 

7 if real: 

8 from scipy.stats import special_ortho_group 

9 

10 ortho_m = special_ortho_group.rvs(dim=dim, random_state=rng) 

11 else: 

12 # The best method but not supported on old systems 

13 # from scipy.stats import unitary_group 

14 # ortho_m = unitary_group.rvs(dim=dim, random_state=rng) 

15 

16 # Alternative method from https://stackoverflow.com/questions/38426349 

17 H = rng.random((dim, dim)) 

18 Q, R = qr(H) 

19 ortho_m = Q @ np.diag(np.sign(np.diag(R))) 

20 

21 return ortho_m 

22 

23 

24def _empty(): 

25 return np.empty(0, complex) 

26 

27 

28class WannierSpec: 

29 def __init__(self, Nk, Nw, Nb, fixedstates_k): 

30 self.Nk = Nk 

31 self.Nw = Nw 

32 self.Nb = Nb 

33 self.fixedstates_k = fixedstates_k 

34 

35 def _zeros(self): 

36 return np.zeros((self.Nk, self.Nw, self.Nw), complex) 

37 

38 def bloch(self, edf_k): 

39 U_kww = self._zeros() 

40 C_kul = [] 

41 for U, M, L in zip(U_kww, self.fixedstates_k, edf_k): 

42 U[:] = np.identity(self.Nw, complex) 

43 if L > 0: 

44 C_kul.append(np.identity(self.Nb - M, complex)[:, :L]) 

45 else: 

46 C_kul.append(_empty()) 

47 return WannierState(C_kul, U_kww) 

48 

49 def random(self, rng, edf_k): 

50 # Set U and C to random (orthogonal) matrices 

51 U_kww = self._zeros() 

52 C_kul = [] 

53 for U, M, L in zip(U_kww, self.fixedstates_k, edf_k): 

54 U[:] = random_orthogonal_matrix(self.Nw, rng, real=False) 

55 if L > 0: 

56 C_kul.append( 

57 random_orthogonal_matrix(self.Nb - M, rng=rng, real=False)[ 

58 :, :L 

59 ] 

60 ) 

61 else: 

62 C_kul.append(_empty()) 

63 return WannierState(C_kul, U_kww) 

64 

65 def initial_orbitals(self, calc, orbitals, kptgrid, edf_k, spin): 

66 C_kul, U_kww = calc.initial_wannier( 

67 orbitals, kptgrid, self.fixedstates_k, edf_k, spin, self.Nb 

68 ) 

69 return WannierState(C_kul, U_kww) 

70 

71 def initial_wannier(self, calc, method, kptgrid, edf_k, spin): 

72 C_kul, U_kww = calc.initial_wannier( 

73 method, kptgrid, self.fixedstates_k, edf_k, spin, self.Nb 

74 ) 

75 return WannierState(C_kul, U_kww) 

76 

77 def scdm(self, calc, kpt_kc, spin): 

78 from ase.dft.wannier import scdm 

79 

80 # get the size of the grid and check if there are Nw bands: 

81 ps = calc.get_pseudo_wave_function(band=self.Nw, kpt=0, spin=0) 

82 Ng = ps.size 

83 pseudo_nkG = np.zeros((self.Nb, self.Nk, Ng), dtype=np.complex128) 

84 for k in range(self.Nk): 

85 for n in range(self.Nb): 

86 pseudo_nkG[n, k] = calc.get_pseudo_wave_function( 

87 band=n, kpt=k, spin=spin 

88 ).ravel() 

89 

90 # Use initial guess to determine U and C 

91 C_kul, U_kww = scdm( 

92 pseudo_nkG, kpts=kpt_kc, fixed_k=self.fixedstates_k, Nw=self.Nw 

93 ) 

94 return WannierState(C_kul, U_kww) 

95 

96 

97class WannierState: 

98 def __init__(self, C_kul, U_kww): 

99 # Number of u is not always the same, so C_kul is ragged 

100 self.C_kul = [C_ul.astype(complex) for C_ul in C_kul] 

101 self.U_kww = U_kww