Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

""" 

Module for generating rattled structures. Rattle refers to displacing atoms 

with a normal distribution with zero mean and some standard deviation. 

""" 

 

import numpy as np 

from scipy.special import erf 

from ase.neighborlist import NeighborList 

 

 

def generate_rattled_structures(atoms, n_structures, rattle_std, seed=42): 

"""Returns list of rattled configurations. 

 

Displacements are drawn from normal distributions for each 

Cartesian directions for each atom independently. 

 

Warning 

------- 

Repeatedly calling this function *without* providing different 

seeds will yield identical or correlated results. To avoid this 

behavior it is recommended to specify a different seed for each 

call to this function. 

 

Parameters 

---------- 

atoms : ase.Atoms 

prototype structure 

n_structures : int 

number of structures to generate 

rattle_std : float 

rattle amplitude (standard deviation of the normal distribution) 

seed : int 

seed for setting up NumPy random state from which random numbers are 

generated 

 

Returns 

------- 

list of ase.Atoms 

generated structures 

""" 

rs = np.random.RandomState(seed) 

N = len(atoms) 

atoms_list = [] 

for _ in range(n_structures): 

atoms_tmp = atoms.copy() 

displacements = rs.normal(0.0, rattle_std, (N, 3)) 

atoms_tmp.positions += displacements 

atoms_list.append(atoms_tmp) 

return atoms_list 

 

 

def generate_mc_rattled_structures(atoms, n_configs, rattle_std, d_min, 

seed=42, **kwargs): 

"""Returns list of Monte Carlo rattled configurations. 

 

Rattling atom `i` is carried out as a Monte Carlo move that is 

accepted with a probability determined from the minimum 

interatomic distance :math:`d_{ij}`. If :math`\\min(d_{ij})` is 

smaller than :math:`d_{min}` the move is only accepted with a low 

probability. 

 

This process is repeated for each atom a number of times meaning 

the magnitude of the final displacements is not *directly* 

connected to `rattle_std`. 

 

Warning 

------- 

Repeatedly calling this function *without* providing different 

seeds will yield identical or correlated results. To avoid this 

behavior it is recommended to specify a different seed for each 

call to this function. 

 

Notes 

------ 

The procedure implemented here might not generate a symmetric 

distribution for the displacements `kwargs` will be forwarded to 

`mc_rattle` (see user guide for a detailed explanation) 

 

Parameters 

---------- 

atoms : ase.Atoms 

prototype structure 

n_structures : int 

number of structures to generate 

rattle_std : float 

rattle amplitude (standard deviation in normal distribution); 

note this value is not *directly* connected to the final 

average displacement for the structures 

d_min : float 

interatomic distance used for computing the probability for each rattle 

move 

seed : int 

seed for setting up NumPy random state from which random numbers are 

generated 

 

Returns 

------- 

list of ase.Atoms 

generated structures 

""" 

rs = np.random.RandomState(seed) 

atoms_list = [] 

for _ in range(n_configs): 

atoms_tmp = atoms.copy() 

seed = rs.randint(1, 1000000000) 

displacements = mc_rattle(atoms_tmp, rattle_std, d_min, seed=seed, 

**kwargs) 

atoms_tmp.positions += displacements 

atoms_list.append(atoms_tmp) 

return atoms_list 

 

 

def _probability_mc_rattle(d, d_min, width): 

""" Monte Carlo probability function as an error function. 

 

Parameters 

---------- 

d_min : float 

center value for the error function 

width : float 

width of error function 

""" 

 

return (erf((d-d_min)/width) + 1.0) / 2 

 

 

def mc_rattle(atoms, rattle_std, d_min, width=0.1, n_iter=10, 

max_attempts=5000, max_disp=2.0, active_atoms=None, seed=42): 

"""Generate displacements using the Monte Carlo rattle method 

 

Parameters 

---------- 

atoms : ase.Atoms 

prototype structure 

rattle_std : float 

rattle amplitude (standard deviation in normal distribution) 

d_min : float 

interatomic distance used for computing the probability for each rattle 

move. Center position of the error function 

width : float 

width of the error function 

n_iter : int 

number of Monte Carlo cycle 

max_disp : float 

rattle moves that yields a displacement larger than max_disp will 

always be rejected. This rarley occurs and is more used as a safety net 

for not generating structures where two or more have swapped positions. 

max_attempts : int 

limit for how many attempted rattle moves are allowed a single atom; 

if this limit is reached an `Exception` is raised. 

active_atoms : list 

list of which atomic indices should undergo Monte Carlo rattling 

seed : int 

seed for setting up NumPy random state from which random numbers are 

generated 

 

Returns 

------- 

numpy.ndarray 

atomic displacements (`Nx3`) 

""" 

rs = np.random.RandomState(seed) 

 

164 ↛ 167line 164 didn't jump to line 167, because the condition on line 164 was never false if active_atoms is None: 

active_atoms = range(len(atoms)) 

 

atoms_rattle = atoms.copy() 

reference_positions = atoms_rattle.get_positions() 

nbr_list = NeighborList([d_min]*len(atoms_rattle), skin=0.0, 

self_interaction=False, bothways=True) 

nbr_list.update(atoms_rattle) 

 

# run Monte Carlo 

for _ in range(n_iter): 

for i in active_atoms: 

i_nbrs = np.setdiff1d(nbr_list.get_neighbors(i)[0], [i]) 

177 ↛ 191line 177 didn't jump to line 191, because the loop on line 177 didn't complete for n in range(max_attempts): 

delta_disp = rs.normal(0.0, rattle_std, 3) 

atoms_rattle.positions[i] += delta_disp 

disp_i = atoms_rattle.positions[i] - reference_positions[i] 

181 ↛ 182line 181 didn't jump to line 182, because the condition on line 181 was never true if np.linalg.norm(disp_i) > max_disp: 

continue 

min_distance = np.min(atoms_rattle.get_distances(i, i_nbrs, 

mic=True)) 

185 ↛ 189line 185 didn't jump to line 189, because the condition on line 185 was never false if _probability_mc_rattle(min_distance, d_min, width) > \ 

rs.rand(): # accept disp_i 

break 

else: # revert disp_i 

atoms_rattle[i].position -= delta_disp 

else: 

raise Exception('Maxmium attempts for atom {}'.format(i)) 

displacements = atoms_rattle.positions - reference_positions 

return displacements