Coverage for ase/io/v_sim.py: 86.84%

76 statements  

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

1""" 

2This module contains functionality for reading and writing an ASE 

3Atoms object in V_Sim 3.5+ ascii format. 

4 

5""" 

6 

7import numpy as np 

8 

9from ase.utils import reader, writer 

10 

11 

12@reader 

13def read_v_sim(fd): 

14 """Import V_Sim input file. 

15 

16 Reads cell, atom positions, etc. from v_sim ascii file. 

17 V_sim format is specified here: 

18 https://l_sim.gitlab.io/v_sim/sample.html#sample_ascii 

19 """ 

20 

21 import re 

22 

23 from ase import Atoms, units 

24 from ase.geometry import cellpar_to_cell 

25 

26 # Read comment: 

27 fd.readline() 

28 

29 line = fd.readline() + ' ' + fd.readline() 

30 box = line.split() 

31 for i in range(len(box)): 

32 box[i] = float(box[i]) 

33 

34 keywords = [] 

35 positions = [] 

36 symbols = [] 

37 unit = 1.0 

38 

39 re_comment = re.compile(r'^\s*[#!]') 

40 re_node = re.compile(r'^\s*\S+\s+\S+\s+\S+\s+\S+') 

41 

42 while True: 

43 line = fd.readline() 

44 

45 if line == '': 

46 break # EOF 

47 

48 p = re_comment.match(line) 

49 if p is not None: 

50 # remove comment character at the beginning of line 

51 line = line[p.end() :].replace(',', ' ').lower() 

52 if line[:8] == 'keyword:': 

53 keywords.extend(line[8:].split()) 

54 

55 elif re_node.match(line): 

56 unit = 1.0 

57 if 'reduced' not in keywords: 

58 if ( 

59 ('bohr' in keywords) 

60 or ('bohrd0' in keywords) 

61 or ('atomic' in keywords) 

62 or ('atomicd0' in keywords) 

63 ): 

64 unit = units.Bohr 

65 

66 fields = line.split() 

67 positions.append( 

68 [ 

69 unit * float(fields[0]), 

70 unit * float(fields[1]), 

71 unit * float(fields[2]), 

72 ] 

73 ) 

74 symbols.append(fields[3]) 

75 

76 # create atoms object based on the information 

77 if 'angdeg' in keywords: 

78 cell = cellpar_to_cell(box) 

79 else: 

80 unit = 1.0 

81 if ( 

82 ('bohr' in keywords) 

83 or ('bohrd0' in keywords) 

84 or ('atomic' in keywords) 

85 or ('atomicd0' in keywords) 

86 ): 

87 unit = units.Bohr 

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

89 cell.flat[[0, 3, 4, 6, 7, 8]] = box[:6] 

90 cell *= unit 

91 

92 if 'reduced' in keywords: 

93 atoms = Atoms(cell=cell, scaled_positions=positions) 

94 else: 

95 atoms = Atoms(cell=cell, positions=positions) 

96 

97 if 'periodic' in keywords: 

98 atoms.pbc = [True, True, True] 

99 elif 'freebc' in keywords: 

100 atoms.pbc = [False, False, False] 

101 elif 'surface' in keywords: 

102 atoms.pbc = [True, False, True] 

103 else: # default is periodic boundary conditions 

104 atoms.pbc = [True, True, True] 

105 

106 atoms.set_chemical_symbols(symbols) 

107 return atoms 

108 

109 

110@writer 

111def write_v_sim(fd, atoms): 

112 """Write V_Sim input file. 

113 

114 Writes the atom positions and unit cell. 

115 V_sim format is specified here: 

116 https://l_sim.gitlab.io/v_sim/sample.html#sample_ascii 

117 """ 

118 from ase.geometry import cell_to_cellpar, cellpar_to_cell 

119 

120 # Convert the lattice vectors to triangular matrix by converting 

121 # to and from a set of lengths and angles 

122 cell = cellpar_to_cell(cell_to_cellpar(atoms.cell)) 

123 dxx = cell[0, 0] 

124 dyx, dyy = cell[1, 0:2] 

125 dzx, dzy, dzz = cell[2, 0:3] 

126 

127 fd.write( 

128 '===== v_sim input file created using the' 

129 ' Atomic Simulation Environment (ASE) ====\n' 

130 ) 

131 fd.write(f'{dxx} {dyx} {dyy}\n') 

132 fd.write(f'{dzx} {dzy} {dzz}\n') 

133 

134 # Use v_sim 3.5 keywords to indicate scaled positions, etc. 

135 fd.write('#keyword: reduced\n') 

136 fd.write('#keyword: angstroem\n') 

137 if np.all(atoms.pbc): 

138 fd.write('#keyword: periodic\n') 

139 elif not np.any(atoms.pbc): 

140 fd.write('#keyword: freeBC\n') 

141 elif np.array_equiv(atoms.pbc, [True, False, True]): 

142 fd.write('#keyword: surface\n') 

143 else: 

144 raise Exception( 

145 'Only supported boundary conditions are full PBC,' 

146 ' no periodic boundary, and surface which is free in y direction' 

147 ' (i.e. Atoms.pbc = [True, False, True]).' 

148 ) 

149 

150 # Add atoms (scaled positions) 

151 for position, symbol in zip( 

152 atoms.get_scaled_positions(), atoms.get_chemical_symbols() 

153 ): 

154 fd.write( 

155 '{} {} {} {}\n'.format( 

156 position[0], position[1], position[2], symbol 

157 ) 

158 )