Coverage for ase/io/jsv.py: 83.18%

107 statements  

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

1""" 

2A module for reading and writing crystal structures from JSV 

3See http://www.jcrystal.com/steffenweber/JAVA/JSV/jsv.html 

4 

5By Jesper Friis, Jan. 2012 

6""" 

7 

8import re 

9 

10import numpy as np 

11 

12import ase 

13from ase.geometry import cell_to_cellpar, cellpar_to_cell 

14from ase.spacegroup import Spacegroup, crystal 

15 

16 

17def read_jsv(f): 

18 """Reads a JSV file.""" 

19 natom = nbond = npoly = 0 

20 symbols = [] 

21 labels = [] 

22 cellpar = basis = title = bonds = poly = origin = shell_numbers = None 

23 spacegroup = 1 

24 

25 headline = f.readline().strip() 

26 

27 while True: 

28 line = f.readline() 

29 if not line: 

30 break 

31 line = line.strip() 

32 m = re.match(r'^\[([^]]+)\]\s*(.*)', line) 

33 if m is None or not line: 

34 continue 

35 tag = m.groups()[0].lower() 

36 

37 if len(m.groups()) > 1: 

38 args = m.groups()[1].split() 

39 else: 

40 args = [] 

41 

42 if tag == 'cell': 

43 cellpar = [float(x) for x in args] 

44 elif tag == 'natom': 

45 natom = int(args[0]) 

46 elif tag == 'nbond': 

47 nbond = int(args[0]) 

48 # optional margin of the bondlengths 

49 elif tag == 'npoly': 

50 npoly = int(args[0]) 

51 elif tag == 'space_group': 

52 spacegroup = Spacegroup(*tuple(int(x) for x in args)) 

53 elif tag == 'title': 

54 title = m.groups()[1] 

55 elif tag == 'atoms': 

56 symbols = [] 

57 basis = np.zeros((natom, 3), dtype=float) 

58 shell_numbers = -np.ones((natom,), dtype=int) # float? 

59 for i in range(natom): 

60 tokens = f.readline().strip().split() 

61 labels.append(tokens[0]) 

62 symbols.append(ase.data.chemical_symbols[int(tokens[1])]) 

63 basis[i] = [float(x) for x in tokens[2:5]] 

64 if len(tokens) > 5: 

65 shell_numbers[i] = float(tokens[5]) # float? 

66 elif tag == 'bonds': 

67 for _ in range(nbond): 

68 f.readline() 

69 bonds = NotImplemented 

70 elif tag == 'poly': 

71 for _ in range(npoly): 

72 f.readline() 

73 poly = NotImplemented 

74 elif tag == 'origin': 

75 origin = NotImplemented 

76 else: 

77 raise ValueError(f'Unknown tag: "{tag}"') 

78 

79 if headline == 'asymmetric_unit_cell': 

80 atoms = crystal( 

81 symbols=symbols, 

82 basis=basis, 

83 spacegroup=spacegroup, 

84 cellpar=cellpar, 

85 ) 

86 elif headline == 'full_unit_cell': 

87 atoms = ase.Atoms( 

88 symbols=symbols, 

89 scaled_positions=basis, 

90 cell=cellpar_to_cell(cellpar), 

91 ) 

92 atoms.info['spacegroup'] = Spacegroup(spacegroup) 

93 elif headline == 'cartesian_cell': 

94 atoms = ase.Atoms( 

95 symbols=symbols, 

96 positions=basis, 

97 cell=cellpar_to_cell(cellpar), 

98 ) 

99 atoms.info['spacegroup'] = Spacegroup(spacegroup) 

100 else: 

101 raise ValueError(f'Invalid JSV file type: "{headline}"') 

102 

103 atoms.info['title'] = title 

104 atoms.info['labels'] = labels 

105 if bonds is not None: 

106 atoms.info['bonds'] = bonds 

107 if poly is not None: 

108 atoms.info['poly'] = poly 

109 if origin is not None: 

110 atoms.info['origin'] = origin 

111 if shell_numbers is not None: 

112 atoms.info['shell_numbers'] = shell_numbers 

113 

114 return atoms 

115 

116 

117def write_jsv(fd, atoms): 

118 """Writes JSV file.""" 

119 fd.write('asymmetric_unit_cell\n') 

120 

121 fd.write('[cell]') 

122 for v in cell_to_cellpar(atoms.cell): 

123 fd.write(' %g' % v) 

124 fd.write('\n') 

125 

126 fd.write('[natom] %d\n' % len(atoms)) 

127 fd.write('[nbond] 0\n') # FIXME 

128 fd.write('[npoly] 0\n') # FIXME 

129 

130 if 'spacegroup' in atoms.info: 

131 sg = Spacegroup(atoms.info['spacegroup']) 

132 fd.write('[space_group] %d %d\n' % (sg.no, sg.setting)) 

133 else: 

134 fd.write('[space_group] 1 1\n') 

135 

136 fd.write('[title] %s\n' % atoms.info.get('title', 'untitled')) 

137 

138 fd.write('\n') 

139 fd.write('[atoms]\n') 

140 if 'labels' in atoms.info: 

141 labels = atoms.info['labels'] 

142 else: 

143 labels = [ 

144 '%s%d' % (s, i + 1) 

145 for i, s in enumerate(atoms.get_chemical_symbols()) 

146 ] 

147 numbers = atoms.get_atomic_numbers() 

148 scaled = atoms.get_scaled_positions() 

149 for label, n, p in zip(labels, numbers, scaled): 

150 fd.write( 

151 '%-4s %2d %9.6f %9.6f %9.6f\n' % (label, n, p[0], p[1], p[2]) 

152 ) 

153 

154 fd.write('Label AtomicNumber x y z (repeat natom times)\n') 

155 

156 fd.write('\n') 

157 fd.write('[bonds]\n') 

158 

159 fd.write('\n') 

160 fd.write('[poly]\n') 

161 

162 fd.write('\n')