Coverage for ase/io/wannier90.py: 93.88%

49 statements  

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

1"""Read Wannier90 wout format.""" 

2 

3from typing import IO, Any 

4 

5import numpy as np 

6 

7from ase import Atoms 

8 

9 

10def read_wout_all(fileobj: IO[str]) -> dict[str, Any]: 

11 """Read atoms, wannier function centers and spreads.""" 

12 lines = fileobj.readlines() 

13 

14 for n, line in enumerate(lines): 

15 if line.strip().lower().startswith('lattice vectors (ang)'): 

16 break 

17 else: 

18 raise ValueError('Could not fine lattice vectors') 

19 

20 cell = [ 

21 [float(x) for x in line.split()[-3:]] for line in lines[n + 1 : n + 4] 

22 ] 

23 

24 for n, line in enumerate(lines): 

25 if 'cartesian coordinate (ang)' in line.lower(): 

26 break 

27 else: 

28 raise ValueError('Could not find coordinates') 

29 

30 positions = [] 

31 symbols = [] 

32 n += 2 

33 while True: 

34 words = lines[n].split() 

35 if len(words) == 1: 

36 break 

37 positions.append([float(x) for x in words[-4:-1]]) 

38 symbols.append(words[1]) 

39 n += 1 

40 

41 atoms = Atoms(symbols, positions, cell=cell, pbc=True) 

42 

43 n = len(lines) - 1 

44 while n > 0: 

45 if lines[n].strip().lower().startswith('final state'): 

46 break 

47 n -= 1 

48 else: 

49 return { 

50 'atoms': atoms, 

51 'centers': np.zeros((0, 3)), 

52 'spreads': np.zeros((0,)), 

53 } 

54 

55 n += 1 

56 centers = [] 

57 spreads = [] 

58 while True: 

59 line = lines[n].strip() 

60 if line.startswith('WF'): 

61 centers.append( 

62 [float(x) for x in line.split('(')[1].split(')')[0].split(',')] 

63 ) 

64 spreads.append(float(line.split()[-1])) 

65 n += 1 

66 else: 

67 break 

68 

69 return { 

70 'atoms': atoms, 

71 'centers': np.array(centers), 

72 'spreads': np.array(spreads), 

73 } 

74 

75 

76def read_wout( 

77 fileobj: IO[str], include_wannier_function_centers: bool = True 

78) -> Atoms: 

79 """Read atoms and wannier function centers (as symbol X).""" 

80 dct = read_wout_all(fileobj) 

81 atoms = dct['atoms'] 

82 if include_wannier_function_centers: 

83 centers = dct['centers'] 

84 atoms += Atoms(f'X{len(centers)}', centers) 

85 return atoms