Coverage for ase/optimize/precon/neighbors.py: 97.30%

37 statements  

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

1import numpy as np 

2 

3from ase.constraints import FixAtoms 

4from ase.filters import Filter 

5from ase.geometry.cell import cell_to_cellpar 

6from ase.neighborlist import neighbor_list 

7 

8 

9def get_neighbours( 

10 atoms, r_cut, self_interaction=False, neighbor_list=neighbor_list 

11): 

12 """Return a list of pairs of atoms within a given distance of each other. 

13 

14 Uses ase.neighborlist.neighbour_list to compute neighbors. 

15 

16 Args: 

17 atoms: ase.atoms object to calculate neighbours for 

18 r_cut: cutoff radius (float). Pairs of atoms are considered neighbours 

19 if they are within a distance r_cut of each other (note that this 

20 is double the parameter used in the ASE's neighborlist module) 

21 neighbor_list: function (optional). Optionally replace the built-in 

22 ASE neighbour list with an alternative with the same call 

23 signature, e.g. `matscipy.neighbours.neighbour_list`. 

24 

25 Returns: a tuple (i_list, j_list, d_list, fixed_atoms): 

26 i_list, j_list: i and j indices of each neighbour pair 

27 d_list: absolute distance between the corresponding pair 

28 fixed_atoms: indices of any fixed atoms 

29 """ 

30 

31 if isinstance(atoms, Filter): 

32 atoms = atoms.atoms 

33 

34 i_list, j_list, d_list = neighbor_list('ijd', atoms, r_cut) 

35 

36 # filter out self-interactions (across PBC) 

37 if not self_interaction: 

38 mask = i_list != j_list 

39 i_list = i_list[mask] 

40 j_list = j_list[mask] 

41 d_list = d_list[mask] 

42 

43 # filter out bonds where 1st atom (i) in pair is fixed 

44 fixed_atoms = [] 

45 for constraint in atoms.constraints: 

46 if isinstance(constraint, FixAtoms): 

47 fixed_atoms.extend(list(constraint.index)) 

48 

49 return i_list, j_list, d_list, fixed_atoms 

50 

51 

52def estimate_nearest_neighbour_distance(atoms, neighbor_list=neighbor_list): 

53 """ 

54 Estimate nearest neighbour distance r_NN 

55 

56 Args: 

57 atoms: Atoms object 

58 neighbor_list: function (optional). Optionally replace the built-in 

59 ASE neighbour list with an alternative with the same call 

60 signature, e.g. `matscipy.neighbours.neighbour_list`. 

61 

62 Returns 

63 ------- 

64 rNN: float 

65 Nearest neighbour distance 

66 """ 

67 

68 if isinstance(atoms, Filter): 

69 atoms = atoms.atoms 

70 

71 # start_time = time.time() 

72 # compute number of neighbours of each atom. If any atom doesn't 

73 # have a neighbour we increase the cutoff and try again, until our 

74 # cutoff exceeds the size of the system 

75 r_cut = 1.0 

76 phi = (1.0 + np.sqrt(5.0)) / 2.0 # Golden ratio 

77 

78 # cell lengths and angles 

79 a, b, c, _alpha, _beta, _gamma = cell_to_cellpar(atoms.cell) 

80 extent = [a, b, c] 

81 # print('estimate_nearest_neighbour_distance(): extent=%r' % extent) 

82 

83 while r_cut < 2.0 * max(extent): 

84 # print('estimate_nearest_neighbour_distance(): ' 

85 # 'calling neighbour_list with r_cut=%.2f A' % r_cut) 

86 i, _j, rij, _fixed_atoms = get_neighbours( 

87 atoms, r_cut, self_interaction=True, neighbor_list=neighbor_list 

88 ) 

89 if len(i) != 0: 

90 nn_i = np.bincount(i, minlength=len(atoms)) 

91 if (nn_i != 0).all(): 

92 break 

93 r_cut *= phi 

94 else: 

95 raise RuntimeError( 

96 'increased r_cut to twice system extent without ' 

97 'finding neighbours for all atoms. This can ' 

98 'happen if your system is too small; try ' 

99 'setting r_cut manually' 

100 ) 

101 

102 # maximum of nearest neighbour distances 

103 nn_distances = [np.min(rij[i == I]) for I in range(len(atoms))] 

104 r_NN = np.max(nn_distances) 

105 

106 # print('estimate_nearest_neighbour_distance(): got r_NN=%.3f in %s s' % 

107 # (r_NN, time.time() - start_time)) 

108 return r_NN