Coverage for ase/build/attach.py: 86.05%

43 statements  

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

1import numpy as np 

2 

3from ase.geometry import get_distances 

4from ase.parallel import broadcast, world 

5 

6 

7def random_unit_vector(rng): 

8 """Random unit vector equally distributed on the sphere 

9 

10 Parameter 

11 --------- 

12 rng: random number generator object 

13 """ 

14 ct = -1 + 2 * rng.random() 

15 phi = 2 * np.pi * rng.random() 

16 st = np.sqrt(1 - ct**2) 

17 return np.array([st * np.cos(phi), st * np.sin(phi), ct]) 

18 

19 

20def nearest(atoms1, atoms2, cell=None, pbc=None): 

21 """Return indices of nearest atoms""" 

22 p1 = atoms1.get_positions() 

23 p2 = atoms2.get_positions() 

24 vd_aac, d2_aa = get_distances(p1, p2, cell, pbc) 

25 i1, i2 = np.argwhere(d2_aa == d2_aa.min())[0] 

26 return i1, i2, vd_aac[i1, i2] 

27 

28 

29def attach( 

30 atoms1, atoms2, distance, direction=(1, 0, 0), maxiter=50, accuracy=1e-5 

31): 

32 """Attach two structures 

33 

34 Parameters 

35 ---------- 

36 atoms1: Atoms 

37 cell and pbc of this object are used 

38 atoms2: Atoms 

39 distance: float 

40 minimal distance (Angstrom) 

41 direction: unit vector (3 floats) 

42 relative direction between center of masses 

43 maxiter: int 

44 maximal number of iterations to get required distance, default 100 

45 accuracy: float 

46 required accuracy for minimal distance (Angstrom), default 1e-5 

47 

48 Returns 

49 ------- 

50 Joined structure as an atoms object. 

51 """ 

52 atoms = atoms1.copy() 

53 atoms2 = atoms2.copy() 

54 

55 direction = np.array(direction, dtype=float) 

56 direction /= np.linalg.norm(direction) 

57 assert len(direction) == 3 

58 dist2 = distance**2 

59 

60 _i1, _i2, dv_c = nearest(atoms, atoms2, atoms.cell, atoms.pbc) 

61 

62 for _ in range(maxiter): 

63 dv2 = (dv_c**2).sum() 

64 

65 vcost = np.dot(dv_c, direction) 

66 a = np.sqrt(max(0, dist2 - dv2 + vcost**2)) 

67 move = a - vcost 

68 if abs(move) < accuracy: 

69 atoms += atoms2 

70 return atoms 

71 

72 # we need to move 

73 atoms2.translate(direction * move) 

74 _i1, _i2, dv_c = nearest(atoms, atoms2, atoms.cell, atoms.pbc) 

75 

76 raise RuntimeError('attach did not converge') 

77 

78 

79def attach_randomly(atoms1, atoms2, distance, rng=np.random): 

80 """Randomly attach two structures with a given minimal distance 

81 

82 Parameters 

83 ---------- 

84 atoms1: Atoms object 

85 atoms2: Atoms object 

86 distance: float 

87 Required distance 

88 rng: random number generator object 

89 defaults to np.random.RandomState() 

90 

91 Returns 

92 ------- 

93 Joined structure as an atoms object. 

94 """ 

95 atoms2 = atoms2.copy() 

96 atoms2.rotate( 

97 'x', random_unit_vector(rng), center=atoms2.get_center_of_mass() 

98 ) 

99 return attach(atoms1, atoms2, distance, direction=random_unit_vector(rng)) 

100 

101 

102def attach_randomly_and_broadcast( 

103 atoms1, atoms2, distance, rng=np.random, comm=world 

104): 

105 """Randomly attach two structures with a given minimal distance 

106 and ensure that these are distributed. 

107 

108 Parameters 

109 ---------- 

110 atoms1: Atoms object 

111 atoms2: Atoms object 

112 distance: float 

113 Required distance 

114 rng: random number generator object 

115 defaults to np.random.RandomState() 

116 comm: communicator to distribute 

117 Communicator to distribute the structure, default: world 

118 

119 Returns 

120 ------- 

121 Joined structure as an atoms object. 

122 """ 

123 if comm.rank == 0: 

124 joined = attach_randomly(atoms1, atoms2, distance, rng) 

125 broadcast(joined, 0, comm=comm) 

126 else: 

127 joined = broadcast(None, 0, comm) 

128 return joined