Coverage for ase/io/nwchem/nwreader_in.py: 80.56%

72 statements  

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

1import re 

2 

3import numpy as np 

4 

5from ase import Atoms 

6from ase.geometry import cellpar_to_cell 

7 

8from .parser import _define_pattern 

9 

10# Geometry block parser 

11_geom = _define_pattern( 

12 r'^[ \t]*geometry[ \t\S]*\n' 

13 r'((?:^[ \t]*[\S]+[ \t\S]*\n)+)' 

14 r'^[ \t]*end\n\n', 

15 """\ 

16geometry units angstrom nocenter noautosym noautoz 

17 system crystal units angstrom 

18 lattice_vectors 

19 4.0000000000000000e+00 0.0000000000000000e+00 0.0000000000000000e+00 

20 0.0000000000000000e+00 5.5264780000000000e+00 0.0000000000000000e+00 

21 0.0000000000000000e+00 0.0000000000000000e+00 4.5963089999999998e+00 

22 end 

23 O 5.0000000000000000e-01 5.0000000000000011e-01 5.6486824536818558e-01 

24 H 5.0000000000000000e-01 6.3810586054988372e-01 4.3513175463181430e-01 

25 H 5.0000000000000000e-01 3.6189413945011639e-01 4.3513175463181430e-01 

26end 

27 

28""", 

29 re.M, 

30) 

31 

32# Finds crystal specification 

33_crystal = _define_pattern( 

34 r'^[ \t]*system crystal[ \t\S]*\n' 

35 r'((?:[ \t]*[\S]+[ \t\S]*\n)+?)' 

36 r'^[ \t]*end[ \t]*\n', 

37 """\ 

38 system crystal units angstrom 

39 lattice_vectors 

40 4.0000000000000000e+00 0.0000000000000000e+00 0.0000000000000000e+00 

41 0.0000000000000000e+00 5.5264780000000000e+00 0.0000000000000000e+00 

42 0.0000000000000000e+00 0.0000000000000000e+00 4.5963089999999998e+00 

43 end 

44""", 

45 re.M, 

46) 

47 

48# Finds 3d-periodic unit cell 

49_cell_3d = _define_pattern( 

50 r'^[ \t]*lattice_vectors[ \t]*\n' 

51 r'^((?:(?:[ \t]+[\S]+){3}\n){3})', 

52 """\ 

53 lattice_vectors 

54 4.0000000000000000e+00 0.0000000000000000e+00 0.0000000000000000e+00 

55 0.0000000000000000e+00 5.5264780000000000e+00 0.0000000000000000e+00 

56 0.0000000000000000e+00 0.0000000000000000e+00 4.5963089999999998e+00 

57""", 

58 re.M, 

59) 

60 

61# Extracts chemical species from a geometry block 

62_species = _define_pattern( 

63 r'^[ \t]*[A-Z][a-z]?(?:[ \t]+[\S]+){3}\n', 

64 ' O 0.0 0.0 0.0\n', 

65 re.M, 

66) 

67 

68 

69def read_nwchem_in(fobj, index=-1): 

70 text = ''.join(fobj.readlines()) 

71 atomslist = [] 

72 for match in _geom.findall(text): 

73 symbols = [] 

74 positions = [] 

75 for atom in _species.findall(match): 

76 atom = atom.split() 

77 symbols.append(atom[0]) 

78 positions.append([float(x) for x in atom[1:]]) 

79 positions = np.array(positions) 

80 atoms = Atoms(symbols) 

81 cell, pbc = _get_cell(text) 

82 pos = np.zeros_like(positions) 

83 for dim, ipbc in enumerate(pbc): 

84 if ipbc: 

85 pos += np.outer(positions[:, dim], cell[dim, :]) 

86 else: 

87 pos[:, dim] = positions[:, dim] 

88 atoms.set_cell(cell) 

89 atoms.pbc = pbc 

90 atoms.set_positions(pos) 

91 atomslist.append(atoms) 

92 return atomslist[index] 

93 

94 

95def _get_cell(text): 

96 # first check whether there is a lattice definition 

97 cell = np.zeros((3, 3)) 

98 lattice = _cell_3d.findall(text) 

99 if lattice: 

100 pbc = [True, True, True] 

101 for i, row in enumerate(lattice[0].strip().split('\n')): 

102 cell[i] = [float(x) for x in row.split()] 

103 return cell, pbc 

104 pbc = [False, False, False] 

105 lengths = [None, None, None] 

106 angles = [None, None, None] 

107 for row in text.strip().split('\n'): 

108 row = row.strip().lower() 

109 for dim, vecname in enumerate(['a', 'b', 'c']): 

110 if row.startswith(f'lat_{vecname}'): 

111 pbc[dim] = True 

112 lengths[dim] = float(row.split()[1]) 

113 for i, angle in enumerate(['alpha', 'beta', 'gamma']): 

114 if row.startswith(angle): 

115 angles[i] = float(row.split()[1]) 

116 

117 if not np.any(pbc): 

118 return None, pbc 

119 

120 for i in range(3): 

121 a, b, c = np.roll(np.array([0, 1, 2]), i) 

122 if pbc[a] and pbc[b]: 

123 assert angles[c] is not None 

124 if angles[c] is not None: 

125 assert pbc[a] and pbc[b] 

126 

127 # The easiest case: all three lattice vectors and angles are specified 

128 if np.all(pbc): 

129 return cellpar_to_cell(lengths + angles), pbc 

130 

131 # Next easiest case: exactly one lattice vector has been specified 

132 if np.sum(pbc) == 1: 

133 dim = np.argmax(pbc) 

134 cell[dim, dim] = lengths[dim] 

135 return cell, pbc 

136 

137 # Hardest case: two lattice vectors are specified. 

138 dim1, dim2 = (dim for dim, ipbc in enumerate(pbc) if ipbc) 

139 angledim = np.argmin(pbc) 

140 cell[dim1, dim1] = lengths[dim1] 

141 cell[dim2, dim2] = lengths[dim2] * np.sin(angles[angledim]) 

142 cell[dim2, dim1] = lengths[dim2] * np.cos(angles[angledim]) 

143 return cell, pbc