Coverage for ase/io/prismatic.py: 98.25%

57 statements  

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

1"""Module to read and write atoms in xtl file format for the prismatic and 

2computem software. 

3 

4See https://prism-em.com/docs-inputs for an example of this format and the 

5documentation of prismatic. 

6 

7See https://sourceforge.net/projects/computem/ for the source code of the 

8computem software. 

9""" 

10 

11import numpy as np 

12 

13from ase.atoms import Atoms, symbols2numbers 

14from ase.utils import reader 

15 

16from .utils import verify_cell_for_export, verify_dictionary 

17 

18 

19@reader 

20def read_prismatic(fd): 

21 r"""Import prismatic and computem xyz input file as an Atoms object. 

22 

23 Reads cell, atom positions, occupancies and Debye Waller factor. 

24 The occupancy values and the Debye Waller factors are obtained using the 

25 `get_array` method and the `occupancies` and `debye_waller_factors` keys, 

26 respectively. The root means square (RMS) values from the 

27 prismatic/computem xyz file are converted to Debye-Waller factors (B) in Ų 

28 by: 

29 

30 .. math:: 

31 

32 B = RMS^2 * 8\pi^2 

33 

34 """ 

35 # Read comment: 

36 fd.readline() 

37 

38 # Read unit cell parameters: 

39 cellpar = [float(i) for i in fd.readline().split()] 

40 

41 # Read all data at once 

42 # Use genfromtxt instead of loadtxt to skip last line 

43 read_data = np.genfromtxt(fname=fd, skip_footer=1) 

44 # Convert from RMS to Debye-Waller factor 

45 RMS = read_data[:, 5] ** 2 * 8 * np.pi**2 

46 

47 atoms = Atoms( 

48 symbols=read_data[:, 0], 

49 positions=read_data[:, 1:4], 

50 cell=cellpar, 

51 ) 

52 atoms.set_array('occupancies', read_data[:, 4]) 

53 atoms.set_array('debye_waller_factors', RMS) 

54 

55 return atoms 

56 

57 

58class XYZPrismaticWriter: 

59 """See the docstring of the `write_prismatic` function.""" 

60 

61 def __init__(self, atoms, debye_waller_factors=None, comments=None): 

62 verify_cell_for_export(atoms.get_cell()) 

63 

64 self.atoms = atoms.copy() 

65 self.atom_types = set(atoms.symbols) 

66 self.comments = comments 

67 

68 self.occupancies = self._get_occupancies() 

69 debye_waller_factors = self._get_debye_waller_factors( 

70 debye_waller_factors 

71 ) 

72 self.RMS = np.sqrt(debye_waller_factors / (8 * np.pi**2)) 

73 

74 def _get_occupancies(self): 

75 if 'occupancies' in self.atoms.arrays: 

76 occupancies = self.atoms.get_array('occupancies', copy=False) 

77 else: 

78 occupancies = np.ones_like(self.atoms.numbers) 

79 

80 return occupancies 

81 

82 def _get_debye_waller_factors(self, DW): 

83 if np.isscalar(DW): 

84 if len(self.atom_types) > 1: 

85 raise ValueError( 

86 'This cell contains more then one type of ' 

87 'atoms and the Debye-Waller factor needs to ' 

88 'be provided for each atom using a ' 

89 'dictionary.' 

90 ) 

91 DW = np.ones_like(self.atoms.numbers) * DW 

92 elif isinstance(DW, dict): 

93 verify_dictionary(self.atoms, DW, 'DW') 

94 # Get the arrays of DW from mapping the DW defined by symbol 

95 DW = {symbols2numbers(k)[0]: v for k, v in DW.items()} 

96 DW = np.vectorize(DW.get)(self.atoms.numbers) 

97 else: 

98 for name in ['DW', 'debye_waller_factors']: 

99 if name in self.atoms.arrays: 

100 DW = self.atoms.get_array(name) 

101 

102 if DW is None: 

103 raise ValueError( 

104 'Missing Debye-Waller factors. It can be ' 

105 'provided as a dictionary with symbols as key or ' 

106 'can be set for each atom by using the ' 

107 '`set_array("debye_waller_factors", values)` of ' 

108 'the `Atoms` object.' 

109 ) 

110 

111 return DW 

112 

113 def _get_file_header(self): 

114 # 1st line: comment line 

115 if self.comments is None: 

116 s = '{} atoms with chemical formula: {}.'.format( 

117 len(self.atoms), self.atoms.get_chemical_formula() 

118 ) 

119 else: 

120 s = self.comments 

121 

122 s = s.strip() 

123 s += ' generated by the ase library.\n' 

124 # 2nd line: lattice parameter 

125 s += '{} {} {}'.format(*self.atoms.cell.cellpar()[:3]) 

126 

127 return s 

128 

129 def write_to_file(self, f): 

130 data_array = np.vstack( 

131 ( 

132 self.atoms.numbers, 

133 self.atoms.positions.T, 

134 self.occupancies, 

135 self.RMS, 

136 ) 

137 ).T 

138 

139 np.savetxt( 

140 fname=f, 

141 X=data_array, 

142 fmt='%.6g', 

143 header=self._get_file_header(), 

144 newline='\n', 

145 footer='-1', 

146 comments='', 

147 ) 

148 

149 

150def write_prismatic(fd, *args, **kwargs): 

151 r"""Write xyz input file for the prismatic and computem software. The cell 

152 needs to be orthorhombric. If the cell contains the `occupancies` and 

153 `debye_waller_factors` arrays (see the `set_array` method to set them), 

154 these array will be written to the file. 

155 If the occupancies is not specified, the default value will be set to 0. 

156 

157 Parameters 

158 ---------- 

159 

160 atoms: Atoms object 

161 

162 debye_waller_factors: float or dictionary of float or None (optional). 

163 If float, set this value to all 

164 atoms. If dictionary, each atom needs to specified with the symbol as 

165 key and the corresponding Debye-Waller factor as value. 

166 If None, the `debye_waller_factors` array of the Atoms object needs to 

167 be set (see the `set_array` method to set them), otherwise raise a 

168 ValueError. Since the prismatic/computem software use root means square 

169 (RMS) displacements, the Debye-Waller factors (B) needs to be provided 

170 in Ų and these values are converted to RMS displacement by: 

171 

172 .. math:: 

173 

174 RMS = \sqrt{\frac{B}{8\pi^2}} 

175 

176 Default is None. 

177 

178 comment: str (optional) 

179 Comments to be written in the first line of the file. If not 

180 provided, write the total number of atoms and the chemical formula. 

181 

182 """ 

183 

184 writer = XYZPrismaticWriter(*args, **kwargs) 

185 writer.write_to_file(fd)