Coverage for ase/utils/ff.py: 87.31%

599 statements  

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

1from collections.abc import Sequence 

2 

3import numpy as np 

4from numpy import linalg 

5from numpy.typing import NDArray 

6 

7from ase import Atoms, units 

8 

9# Three variables extracted from what used to be endless repetitions below. 

10Ax = np.array( 

11 [ 

12 [1, 0, 0, -1, 0, 0, 0, 0, 0], 

13 [0, 1, 0, 0, -1, 0, 0, 0, 0], 

14 [0, 0, 1, 0, 0, -1, 0, 0, 0], 

15 [0, 0, 0, -1, 0, 0, 1, 0, 0], 

16 [0, 0, 0, 0, -1, 0, 0, 1, 0], 

17 [0, 0, 0, 0, 0, -1, 0, 0, 1], 

18 ] 

19) 

20Bx = np.array([[1, 0, 0, -1, 0, 0], [0, 1, 0, 0, -1, 0], [0, 0, 1, 0, 0, -1]]) 

21Mx = Bx 

22 

23 

24class Morse: 

25 """Morse potential between two atoms. 

26 

27 Parameters 

28 ---------- 

29 atomi, atomj : int 

30 Indices of the two atoms. 

31 D : float 

32 Dissociation energy. 

33 alpha : float 

34 Parameter controlling the width of the potential. 

35 r0 : float 

36 Equilibrium bond length. 

37 """ 

38 

39 def __init__(self, atomi, atomj, D, alpha, r0): 

40 self.atomi = atomi 

41 self.atomj = atomj 

42 self.D = D 

43 self.alpha = alpha 

44 self.r0 = r0 

45 self.r = None 

46 

47 

48class Bond: 

49 """Harmonic bond-stretching potential between two atoms. 

50 

51 Parameters 

52 ---------- 

53 atomi, atomj : int 

54 Indices of the two atoms. 

55 k : float 

56 Bond force constant. 

57 b0 : float 

58 Equilibrium bond length. 

59 alpha : sequence of float or None 

60 Parameters for distance-dependent scaling of the Hessian. 

61 rref : sequence of float or None 

62 Reference bond lengths for distance-dependent scaling. 

63 """ 

64 

65 def __init__(self, atomi, atomj, k, b0, alpha=None, rref=None): 

66 self.atomi = atomi 

67 self.atomj = atomj 

68 self.k = k 

69 self.b0 = b0 

70 self.alpha = alpha 

71 self.rref = rref 

72 self.b = None 

73 

74 

75class Angle: 

76 """Harmonic angle-bending potential involving three atoms. 

77 

78 Parameters 

79 ---------- 

80 atomi, atomj, atomk : int 

81 Indices of the three atoms, with atomj as the central atom. 

82 k : float 

83 Angle force constant. 

84 a0 : float 

85 Equilibrium bond angle in radians. 

86 cos : bool 

87 If True, use the difference in cosines instead of angles. 

88 alpha : sequence of float or None 

89 Parameters for distance-dependent scaling of the Hessian. 

90 rref : sequence of float or None 

91 Reference bond lengths for distance-dependent scaling. 

92 """ 

93 

94 def __init__( 

95 self, atomi, atomj, atomk, k, a0, cos=False, alpha=None, rref=None 

96 ): 

97 self.atomi = atomi 

98 self.atomj = atomj 

99 self.atomk = atomk 

100 self.k = k 

101 self.a0 = a0 

102 self.cos = cos 

103 self.alpha = alpha 

104 self.rref = rref 

105 self.a = None 

106 

107 

108class Dihedral: 

109 """Dihedral potential involving four atoms. 

110 

111 The potential is harmonic if `n` is None and `d0` is specified, 

112 periodic if both are specified, and planar if `d0` is None. 

113 

114 Parameters 

115 ---------- 

116 atomi, atomj, atomk, atoml : int 

117 Indices of the four atoms defining the dihedral angle. 

118 k : float 

119 Force constant or potential amplitude. 

120 d0 : float or None 

121 Equilibrium dihedral angle or phase offset, in radians. 

122 n : int or None 

123 Periodicity of the torsional potential. 

124 """ 

125 

126 def __init__( 

127 self, 

128 atomi, 

129 atomj, 

130 atomk, 

131 atoml, 

132 k, 

133 d0=None, 

134 n=None, 

135 alpha=None, 

136 rref=None, 

137 ): 

138 self.atomi = atomi 

139 self.atomj = atomj 

140 self.atomk = atomk 

141 self.atoml = atoml 

142 self.k = k 

143 self.d0 = d0 

144 self.n = n 

145 self.alpha = alpha 

146 self.rref = rref 

147 self.d = None 

148 

149 

150class VdW: 

151 """Van der Waals potential between two atoms. 

152 

153 Parameters 

154 ---------- 

155 atomi, atomj : int 

156 Indices of the two atoms. 

157 Aij : float 

158 Coefficient of the repulsive r^-12 term. 

159 Bij : float 

160 Coefficient of the attractive r^-6 term. 

161 """ 

162 

163 def __init__( 

164 self, 

165 atomi, 

166 atomj, 

167 epsilonij=None, 

168 sigmaij=None, 

169 rminij=None, 

170 Aij=None, 

171 Bij=None, 

172 epsiloni=None, 

173 epsilonj=None, 

174 sigmai=None, 

175 sigmaj=None, 

176 rmini=None, 

177 rminj=None, 

178 scale=1.0, 

179 ): 

180 self.atomi = atomi 

181 self.atomj = atomj 

182 if epsilonij is not None: 

183 if sigmaij is not None: 

184 self.Aij = scale * 4.0 * epsilonij * sigmaij**12 

185 self.Bij = scale * 4.0 * epsilonij * sigmaij**6 

186 elif rminij is not None: 

187 self.Aij = scale * epsilonij * rminij**12 

188 self.Bij = scale * 2.0 * epsilonij * rminij**6 

189 else: 

190 raise NotImplementedError( 

191 'not implemented combinationof vdW parameters.' 

192 ) 

193 elif Aij is not None and Bij is not None: 

194 self.Aij = scale * Aij 

195 self.Bij = scale * Bij 

196 elif epsiloni is not None and epsilonj is not None: 

197 if sigmai is not None and sigmaj is not None: 

198 self.Aij = ( 

199 scale 

200 * 4.0 

201 * np.sqrt(epsiloni * epsilonj) 

202 * ((sigmai + sigmaj) / 2.0) ** 12 

203 ) 

204 self.Bij = ( 

205 scale 

206 * 4.0 

207 * np.sqrt(epsiloni * epsilonj) 

208 * ((sigmai + sigmaj) / 2.0) ** 6 

209 ) 

210 elif rmini is not None and rminj is not None: 

211 self.Aij = ( 

212 scale 

213 * np.sqrt(epsiloni * epsilonj) 

214 * ((rmini + rminj) / 2.0) ** 12 

215 ) 

216 self.Bij = ( 

217 scale 

218 * 2.0 

219 * np.sqrt(epsiloni * epsilonj) 

220 * ((rmini + rminj) / 2.0) ** 6 

221 ) 

222 else: 

223 raise NotImplementedError( 

224 'not implemented combinationof vdW parameters.' 

225 ) 

226 self.r = None 

227 

228 

229class Coulomb: 

230 """Coulomb potential between two point charges. 

231 

232 Parameters 

233 ---------- 

234 atomi, atomj : int 

235 Indices of the two atoms. 

236 chargeij : float or None 

237 Product of the two charges. 

238 chargei, chargej : float or None 

239 Individual atomic charges, used if `chargeij` is None. 

240 scale : float 

241 Scaling factor for the Coulomb interaction. 

242 """ 

243 

244 def __init__( 

245 self, atomi, atomj, chargeij=None, chargei=None, chargej=None, scale=1.0 

246 ): 

247 self.atomi = atomi 

248 self.atomj = atomj 

249 if chargeij is not None: 

250 self.chargeij = ( 

251 scale 

252 * chargeij 

253 * 8.9875517873681764e9 

254 * units.m 

255 * units.J 

256 / units.C 

257 / units.C 

258 ) 

259 elif chargei is not None and chargej is not None: 

260 self.chargeij = ( 

261 scale 

262 * chargei 

263 * chargej 

264 * 8.9875517873681764e9 

265 * units.m 

266 * units.J 

267 / units.C 

268 / units.C 

269 ) 

270 else: 

271 raise NotImplementedError( 

272 'not implemented combinationof Coulomb parameters.' 

273 ) 

274 self.r = None 

275 

276 

277def get_morse_potential_eta( 

278 atoms: Atoms, 

279 morse: Morse, 

280) -> float: 

281 i = morse.atomi 

282 j = morse.atomj 

283 

284 rij = rel_pos_pbc(atoms, i, j) 

285 dij = linalg.norm(rij) 

286 

287 if dij > morse.r0: 

288 exp = np.exp(-morse.alpha * (dij - morse.r0)) 

289 eta = 1.0 - (1.0 - exp) ** 2 

290 else: 

291 eta = 1.0 

292 

293 return eta 

294 

295 

296def get_morse_potential_value( 

297 atoms: Atoms, 

298 morse: Morse, 

299) -> tuple[int, int, float]: 

300 i = morse.atomi 

301 j = morse.atomj 

302 

303 rij = rel_pos_pbc(atoms, i, j) 

304 dij = linalg.norm(rij) 

305 

306 exp = np.exp(-morse.alpha * (dij - morse.r0)) 

307 

308 v = morse.D * (1.0 - exp) ** 2 

309 

310 morse.r = dij 

311 

312 return i, j, v 

313 

314 

315def get_morse_potential_gradient( 

316 atoms: Atoms, 

317 morse: Morse, 

318) -> tuple[int, int, NDArray[np.floating]]: 

319 i = morse.atomi 

320 j = morse.atomj 

321 

322 rij = rel_pos_pbc(atoms, i, j) 

323 dij = linalg.norm(rij) 

324 eij = rij / dij 

325 

326 exp = np.exp(-morse.alpha * (dij - morse.r0)) 

327 

328 gr = 2.0 * morse.D * morse.alpha * exp * (1.0 - exp) * eij 

329 

330 gx = np.dot(Mx.T, gr) 

331 

332 morse.r = dij 

333 

334 return i, j, gx 

335 

336 

337def get_morse_potential_hessian( 

338 atoms: Atoms, 

339 morse: Morse, 

340 spectral: bool = False, 

341) -> tuple[int, int, NDArray[np.floating]]: 

342 i = morse.atomi 

343 j = morse.atomj 

344 

345 rij = rel_pos_pbc(atoms, i, j) 

346 dij = linalg.norm(rij) 

347 eij = rij / dij 

348 

349 Pij = np.tensordot(eij, eij, axes=0) 

350 Qij = np.eye(3) - Pij 

351 

352 exp = np.exp(-morse.alpha * (dij - morse.r0)) 

353 

354 Hr = ( 

355 2.0 

356 * morse.D 

357 * morse.alpha 

358 * exp 

359 * (morse.alpha * (2.0 * exp - 1.0) * Pij + (1.0 - exp) / dij * Qij) 

360 ) 

361 

362 Hx = np.dot(Mx.T, np.dot(Hr, Mx)) 

363 

364 if spectral: 

365 eigvals, eigvecs = linalg.eigh(Hx) 

366 D = np.diag(np.abs(eigvals)) 

367 U = eigvecs 

368 Hx = np.dot(U, np.dot(D, np.transpose(U))) 

369 

370 morse.r = dij 

371 

372 return i, j, Hx 

373 

374 

375def get_morse_potential_reduced_hessian( 

376 atoms: Atoms, 

377 morse: Morse, 

378) -> tuple[int, int, NDArray[np.floating]]: 

379 """Calculate the reduced Hessian of a Morse potential. 

380 

381 Only the bond-stretching contribution is retained, and the absolute value 

382 of the curvature is used to obtain a positive-semidefinite Hessian. 

383 """ 

384 i = morse.atomi 

385 j = morse.atomj 

386 

387 rij = rel_pos_pbc(atoms, i, j) 

388 dij = linalg.norm(rij) 

389 eij = rij / dij 

390 

391 Pij = np.tensordot(eij, eij, axes=0) 

392 

393 exp = np.exp(-morse.alpha * (dij - morse.r0)) 

394 

395 Hr = np.abs(2.0 * morse.D * morse.alpha**2 * exp * (2.0 * exp - 1.0)) * Pij 

396 

397 Hx = np.dot(Mx.T, np.dot(Hr, Mx)) 

398 

399 morse.r = dij 

400 

401 return i, j, Hx 

402 

403 

404def get_bond_potential_value( 

405 atoms: Atoms, 

406 bond: Bond, 

407) -> tuple[int, int, float]: 

408 i = bond.atomi 

409 j = bond.atomj 

410 

411 rij = rel_pos_pbc(atoms, i, j) 

412 dij = linalg.norm(rij) 

413 

414 v = 0.5 * bond.k * (dij - bond.b0) ** 2 

415 

416 bond.b = dij 

417 

418 return i, j, v 

419 

420 

421def get_bond_potential_gradient( 

422 atoms: Atoms, 

423 bond: Bond, 

424) -> tuple[int, int, NDArray[np.floating]]: 

425 i = bond.atomi 

426 j = bond.atomj 

427 

428 rij = rel_pos_pbc(atoms, i, j) 

429 dij = linalg.norm(rij) 

430 eij = rij / dij 

431 

432 gr = bond.k * (dij - bond.b0) * eij 

433 

434 gx = np.dot(Bx.T, gr) 

435 

436 bond.b = dij 

437 

438 return i, j, gx 

439 

440 

441def get_bond_potential_hessian( 

442 atoms: Atoms, 

443 bond: Bond, 

444 morses: Sequence[Morse] | None = None, 

445 spectral: bool = False, 

446) -> tuple[int, int, NDArray[np.floating]]: 

447 i = bond.atomi 

448 j = bond.atomj 

449 

450 rij = rel_pos_pbc(atoms, i, j) 

451 dij = linalg.norm(rij) 

452 eij = rij / dij 

453 

454 Pij = np.tensordot(eij, eij, axes=0) 

455 Qij = np.eye(3) - Pij 

456 

457 Hr = bond.k * Pij + bond.k * (dij - bond.b0) / dij * Qij 

458 

459 if bond.alpha is not None: 

460 Hr *= np.exp(bond.alpha[0] * (bond.rref[0] ** 2 - dij**2)) 

461 

462 if morses is not None: 

463 for m in range(len(morses)): 

464 if morses[m].atomi == i or morses[m].atomi == j: 

465 Hr *= get_morse_potential_eta(atoms, morses[m]) 

466 elif morses[m].atomj == i or morses[m].atomj == j: 

467 Hr *= get_morse_potential_eta(atoms, morses[m]) 

468 

469 Hx = np.dot(Bx.T, np.dot(Hr, Bx)) 

470 

471 if spectral: 

472 eigvals, eigvecs = linalg.eigh(Hx) 

473 D = np.diag(np.abs(eigvals)) 

474 U = eigvecs 

475 Hx = np.dot(U, np.dot(D, np.transpose(U))) 

476 

477 bond.b = dij 

478 

479 return i, j, Hx 

480 

481 

482def get_bond_potential_reduced_hessian( 

483 atoms: Atoms, 

484 bond: Bond, 

485 morses: Sequence[Morse] | None = None, 

486) -> tuple[int, int, NDArray[np.floating]]: 

487 """Calculate the reduced Hessian of a harmonic bond potential. 

488 

489 Only the bond-stretching contribution is retained, neglecting 

490 the transverse contributions to the Hessian. 

491 

492 The Hessian is optionally scaled by distance-dependent factors 

493 and Morse-potential-based bond-breaking factors. 

494 """ 

495 i = bond.atomi 

496 j = bond.atomj 

497 

498 rij = rel_pos_pbc(atoms, i, j) 

499 dij = linalg.norm(rij) 

500 eij = rij / dij 

501 

502 Pij = np.tensordot(eij, eij, axes=0) 

503 

504 Hr = bond.k * Pij 

505 

506 if bond.alpha is not None: 

507 Hr *= np.exp(bond.alpha[0] * (bond.rref[0] ** 2 - dij**2)) 

508 

509 if morses is not None: 

510 for m in range(len(morses)): 

511 if morses[m].atomi == i or morses[m].atomi == j: 

512 Hr *= get_morse_potential_eta(atoms, morses[m]) 

513 elif morses[m].atomj == i or morses[m].atomj == j: 

514 Hr *= get_morse_potential_eta(atoms, morses[m]) 

515 

516 Hx = np.dot(Bx.T, np.dot(Hr, Bx)) 

517 

518 bond.b = dij 

519 

520 return i, j, Hx 

521 

522 

523def get_angle_potential_value( 

524 atoms: Atoms, 

525 angle: Angle, 

526) -> tuple[int, int, int, float]: 

527 

528 i = angle.atomi 

529 j = angle.atomj 

530 k = angle.atomk 

531 

532 rij = rel_pos_pbc(atoms, i, j) 

533 dij = linalg.norm(rij) 

534 eij = rij / dij 

535 rkj = rel_pos_pbc(atoms, k, j) 

536 dkj = linalg.norm(rkj) 

537 ekj = rkj / dkj 

538 eijekj = np.dot(eij, ekj) 

539 if np.abs(eijekj) > 1.0: 

540 eijekj = np.sign(eijekj) 

541 

542 a = np.arccos(eijekj) 

543 

544 if angle.cos: 

545 da = np.cos(a) - np.cos(angle.a0) 

546 else: 

547 da = a - angle.a0 

548 da = da - np.around(da / np.pi) * np.pi 

549 

550 v = 0.5 * angle.k * da**2 

551 

552 angle.a = a 

553 

554 return i, j, k, v 

555 

556 

557def get_angle_potential_gradient( 

558 atoms: Atoms, 

559 angle: Angle, 

560) -> tuple[int, int, int, NDArray[np.floating]]: 

561 i = angle.atomi 

562 j = angle.atomj 

563 k = angle.atomk 

564 

565 rij = rel_pos_pbc(atoms, i, j) 

566 dij = linalg.norm(rij) 

567 eij = rij / dij 

568 rkj = rel_pos_pbc(atoms, k, j) 

569 dkj = linalg.norm(rkj) 

570 ekj = rkj / dkj 

571 eijekj = np.dot(eij, ekj) 

572 if np.abs(eijekj) > 1.0: 

573 eijekj = np.sign(eijekj) 

574 

575 a = np.arccos(eijekj) 

576 if angle.cos: 

577 da = np.cos(a) - np.cos(angle.a0) 

578 else: 

579 da = a - angle.a0 

580 da = da - np.around(da / np.pi) * np.pi 

581 sina = np.sin(a) 

582 

583 Pij = np.tensordot(eij, eij, axes=0) 

584 Qij = np.eye(3) - Pij 

585 Pkj = np.tensordot(ekj, ekj, axes=0) 

586 Qkj = np.eye(3) - Pkj 

587 

588 gr = np.zeros(6) 

589 if angle.cos: 

590 gr[0:3] = angle.k * da / dij * np.dot(Qij, ekj) 

591 gr[3:6] = angle.k * da / dkj * np.dot(Qkj, eij) 

592 elif np.abs(sina) > 0.001: 

593 gr[0:3] = -angle.k * da / sina / dij * np.dot(Qij, ekj) 

594 gr[3:6] = -angle.k * da / sina / dkj * np.dot(Qkj, eij) 

595 

596 gx = np.dot(Ax.T, gr) 

597 

598 angle.a = a 

599 

600 return i, j, k, gx 

601 

602 

603def get_angle_potential_hessian( 

604 atoms: Atoms, 

605 angle: Angle, 

606 morses: Sequence[Morse] | None = None, 

607 spectral: bool = False, 

608) -> tuple[int, int, int, NDArray[np.floating]]: 

609 i = angle.atomi 

610 j = angle.atomj 

611 k = angle.atomk 

612 

613 rij = rel_pos_pbc(atoms, i, j) 

614 dij = linalg.norm(rij) 

615 dij2 = dij * dij 

616 eij = rij / dij 

617 rkj = rel_pos_pbc(atoms, k, j) 

618 dkj = linalg.norm(rkj) 

619 dkj2 = dkj * dkj 

620 ekj = rkj / dkj 

621 dijdkj = dij * dkj 

622 eijekj = np.dot(eij, ekj) 

623 if np.abs(eijekj) > 1.0: 

624 eijekj = np.sign(eijekj) 

625 

626 a = np.arccos(eijekj) 

627 if angle.cos: 

628 da = np.cos(a) - np.cos(angle.a0) 

629 cosa0 = np.cos(angle.a0) 

630 else: 

631 da = a - angle.a0 

632 da = da - np.around(da / np.pi) * np.pi 

633 sina = np.sin(a) 

634 cosa = np.cos(a) 

635 ctga = cosa / sina 

636 

637 Pij = np.tensordot(eij, eij, axes=0) 

638 Qij = np.eye(3) - Pij 

639 Pkj = np.tensordot(ekj, ekj, axes=0) 

640 Qkj = np.eye(3) - Pkj 

641 Pik = np.tensordot(eij, ekj, axes=0) 

642 Pki = np.tensordot(ekj, eij, axes=0) 

643 P = np.eye(3) * eijekj 

644 

645 QijPkjQij = np.dot(Qij, np.dot(Pkj, Qij)) 

646 QijPkiQkj = np.dot(Qij, np.dot(Pki, Qkj)) 

647 QkjPijQkj = np.dot(Qkj, np.dot(Pij, Qkj)) 

648 

649 Hr = np.zeros((6, 6)) 

650 if angle.cos and np.abs(sina) > 0.001: 

651 factor = 1.0 - 2.0 * cosa * cosa + cosa * cosa0 

652 Hr[0:3, 0:3] = ( 

653 angle.k 

654 * ( 

655 factor * QijPkjQij / sina 

656 - sina 

657 * da 

658 * ( 

659 -ctga * QijPkjQij / sina 

660 + np.dot(Qij, Pki) 

661 - np.dot(Pij, Pki) * 2.0 

662 + (Pik + P) 

663 ) 

664 ) 

665 / sina 

666 / dij2 

667 ) 

668 Hr[0:3, 3:6] = ( 

669 angle.k 

670 * ( 

671 factor * QijPkiQkj / sina 

672 - sina * da * (-ctga * QijPkiQkj / sina - np.dot(Qij, Qkj)) 

673 ) 

674 / sina 

675 / dijdkj 

676 ) 

677 Hr[3:6, 0:3] = Hr[0:3, 3:6].T 

678 Hr[3:6, 3:6] = ( 

679 angle.k 

680 * ( 

681 factor * QkjPijQkj / sina 

682 - sina 

683 * da 

684 * ( 

685 -ctga * QkjPijQkj / sina 

686 + np.dot(Qkj, Pik) 

687 - np.dot(Pkj, Pik) * 2.0 

688 + (Pki + P) 

689 ) 

690 ) 

691 / sina 

692 / dkj2 

693 ) 

694 elif np.abs(sina) > 0.001: 

695 Hr[0:3, 0:3] = ( 

696 angle.k 

697 * ( 

698 QijPkjQij / sina 

699 + da 

700 * ( 

701 -ctga * QijPkjQij / sina 

702 + np.dot(Qij, Pki) 

703 - np.dot(Pij, Pki) * 2.0 

704 + (Pik + P) 

705 ) 

706 ) 

707 / sina 

708 / dij2 

709 ) 

710 Hr[0:3, 3:6] = ( 

711 angle.k 

712 * ( 

713 QijPkiQkj / sina 

714 + da * (-ctga * QijPkiQkj / sina - np.dot(Qij, Qkj)) 

715 ) 

716 / sina 

717 / dijdkj 

718 ) 

719 Hr[3:6, 0:3] = Hr[0:3, 3:6].T 

720 Hr[3:6, 3:6] = ( 

721 angle.k 

722 * ( 

723 QkjPijQkj / sina 

724 + da 

725 * ( 

726 -ctga * QkjPijQkj / sina 

727 + np.dot(Qkj, Pik) 

728 - np.dot(Pkj, Pik) * 2.0 

729 + (Pki + P) 

730 ) 

731 ) 

732 / sina 

733 / dkj2 

734 ) 

735 

736 if angle.alpha is not None: 

737 Hr *= np.exp(angle.alpha[0] * (angle.rref[0] ** 2 - dij**2)) * np.exp( 

738 angle.alpha[1] * (angle.rref[1] ** 2 - dkj**2) 

739 ) 

740 

741 if morses is not None: 

742 for m in range(len(morses)): 

743 if ( 

744 morses[m].atomi == i 

745 or morses[m].atomi == j 

746 or morses[m].atomi == k 

747 ): 

748 Hr *= get_morse_potential_eta(atoms, morses[m]) 

749 elif ( 

750 morses[m].atomj == i 

751 or morses[m].atomj == j 

752 or morses[m].atomj == k 

753 ): 

754 Hr *= get_morse_potential_eta(atoms, morses[m]) 

755 

756 Hx = np.dot(Ax.T, np.dot(Hr, Ax)) 

757 

758 if spectral: 

759 eigvals, eigvecs = linalg.eigh(Hx) 

760 D = np.diag(np.abs(eigvals)) 

761 U = eigvecs 

762 Hx = np.dot(U, np.dot(D, np.transpose(U))) 

763 

764 angle.a = a 

765 

766 return i, j, k, Hx 

767 

768 

769def get_angle_potential_reduced_hessian( 

770 atoms: Atoms, 

771 angle: Angle, 

772 morses: Sequence[Morse] | None = None, 

773) -> tuple[int, int, int, NDArray[np.floating]]: 

774 """Calculate the reduced Hessian of an angle potential. 

775 

776 Only the angle-bending contribution is retained. 

777 

778 The absolute value of the angular curvature is used to obtain 

779 a positive-semidefinite Hessian. 

780 

781 The Hessian is optionally scaled by distance-dependent factors 

782 and Morse-potential-based bond-breaking factors. 

783 """ 

784 i = angle.atomi 

785 j = angle.atomj 

786 k = angle.atomk 

787 

788 rij = rel_pos_pbc(atoms, i, j) 

789 dij = linalg.norm(rij) 

790 dij2 = dij * dij 

791 eij = rij / dij 

792 rkj = rel_pos_pbc(atoms, k, j) 

793 dkj = linalg.norm(rkj) 

794 dkj2 = dkj * dkj 

795 ekj = rkj / dkj 

796 dijdkj = dij * dkj 

797 eijekj = np.dot(eij, ekj) 

798 if np.abs(eijekj) > 1.0: 

799 eijekj = np.sign(eijekj) 

800 

801 a = np.arccos(eijekj) 

802 sina = np.sin(a) 

803 sina2 = sina * sina 

804 

805 Pij = np.tensordot(eij, eij, axes=0) 

806 Qij = np.eye(3) - Pij 

807 Pkj = np.tensordot(ekj, ekj, axes=0) 

808 Qkj = np.eye(3) - Pkj 

809 Pki = np.tensordot(ekj, eij, axes=0) 

810 

811 Hr = np.zeros((6, 6)) 

812 if np.abs(sina) > 0.001: 

813 Hr[0:3, 0:3] = np.dot(Qij, np.dot(Pkj, Qij)) / dij2 

814 Hr[0:3, 3:6] = np.dot(Qij, np.dot(Pki, Qkj)) / dijdkj 

815 Hr[3:6, 0:3] = Hr[0:3, 3:6].T 

816 Hr[3:6, 3:6] = np.dot(Qkj, np.dot(Pij, Qkj)) / dkj2 

817 

818 if angle.cos and np.abs(sina) > 0.001: 

819 cosa = np.cos(a) 

820 cosa0 = np.cos(angle.a0) 

821 factor = np.abs(1.0 - 2.0 * cosa * cosa + cosa * cosa0) 

822 Hr = Hr * factor * angle.k / sina2 

823 elif np.abs(sina) > 0.001: 

824 Hr = Hr * angle.k / sina2 

825 

826 if angle.alpha is not None: 

827 Hr *= np.exp(angle.alpha[0] * (angle.rref[0] ** 2 - dij**2)) * np.exp( 

828 angle.alpha[1] * (angle.rref[1] ** 2 - dkj**2) 

829 ) 

830 

831 if morses is not None: 

832 for m in range(len(morses)): 

833 if ( 

834 morses[m].atomi == i 

835 or morses[m].atomi == j 

836 or morses[m].atomi == k 

837 ): 

838 Hr *= get_morse_potential_eta(atoms, morses[m]) 

839 elif ( 

840 morses[m].atomj == i 

841 or morses[m].atomj == j 

842 or morses[m].atomj == k 

843 ): 

844 Hr *= get_morse_potential_eta(atoms, morses[m]) 

845 

846 Hx = np.dot(Ax.T, np.dot(Hr, Ax)) 

847 

848 angle.a = a 

849 

850 return i, j, k, Hx 

851 

852 

853def get_dihedral_potential_value( 

854 atoms: Atoms, 

855 dihedral: Dihedral, 

856) -> tuple[int, int, int, int, float]: 

857 i = dihedral.atomi 

858 j = dihedral.atomj 

859 k = dihedral.atomk 

860 l = dihedral.atoml 

861 

862 rij = rel_pos_pbc(atoms, i, j) 

863 rkj = rel_pos_pbc(atoms, k, j) 

864 rkl = rel_pos_pbc(atoms, k, l) 

865 

866 rmj = np.cross(rij, rkj) 

867 dmj = linalg.norm(rmj) 

868 emj = rmj / dmj 

869 rnk = np.cross(rkj, rkl) 

870 dnk = linalg.norm(rnk) 

871 enk = rnk / dnk 

872 emjenk = np.dot(emj, enk) 

873 if np.abs(emjenk) > 1.0: 

874 emjenk = np.sign(emjenk) 

875 

876 d = np.sign(np.dot(rkj, np.cross(rmj, rnk))) * np.arccos(emjenk) 

877 

878 if dihedral.d0 is None: 

879 v = 0.5 * dihedral.k * (1.0 - np.cos(2.0 * d)) 

880 else: 

881 dd = d - dihedral.d0 

882 dd = dd - np.around(dd / np.pi / 2.0) * np.pi * 2.0 

883 if dihedral.n is None: 

884 v = 0.5 * dihedral.k * dd**2 

885 else: 

886 v = dihedral.k * (1.0 + np.cos(dihedral.n * d - dihedral.d0)) 

887 

888 dihedral.d = d 

889 

890 return i, j, k, l, v 

891 

892 

893def get_dihedral_potential_gradient( 

894 atoms: Atoms, 

895 dihedral: Dihedral, 

896) -> tuple[int, int, int, int, NDArray[np.floating]]: 

897 i = dihedral.atomi 

898 j = dihedral.atomj 

899 k = dihedral.atomk 

900 l = dihedral.atoml 

901 

902 rij = rel_pos_pbc(atoms, i, j) 

903 rkj = rel_pos_pbc(atoms, k, j) 

904 dkj = linalg.norm(rkj) 

905 dkj2 = dkj * dkj 

906 rkl = rel_pos_pbc(atoms, k, l) 

907 

908 rijrkj = np.dot(rij, rkj) 

909 rkjrkl = np.dot(rkj, rkl) 

910 

911 rmj = np.cross(rij, rkj) 

912 dmj = linalg.norm(rmj) 

913 dmj2 = dmj * dmj 

914 emj = rmj / dmj 

915 rnk = np.cross(rkj, rkl) 

916 dnk = linalg.norm(rnk) 

917 dnk2 = dnk * dnk 

918 enk = rnk / dnk 

919 emjenk = np.dot(emj, enk) 

920 if np.abs(emjenk) > 1.0: 

921 emjenk = np.sign(emjenk) 

922 

923 dddri = dkj / dmj2 * rmj 

924 dddrl = -dkj / dnk2 * rnk 

925 

926 gx = np.zeros(12) 

927 

928 gx[0:3] = dddri 

929 gx[3:6] = (rijrkj / dkj2 - 1.0) * dddri - rkjrkl / dkj2 * dddrl 

930 gx[6:9] = (rkjrkl / dkj2 - 1.0) * dddrl - rijrkj / dkj2 * dddri 

931 gx[9:12] = dddrl 

932 

933 d = np.sign(np.dot(rkj, np.cross(rmj, rnk))) * np.arccos(emjenk) 

934 

935 if dihedral.d0 is None: 

936 gx *= dihedral.k * np.sin(2.0 * d) 

937 else: 

938 dd = d - dihedral.d0 

939 dd = dd - np.around(dd / np.pi / 2.0) * np.pi * 2.0 

940 if dihedral.n is None: 

941 gx *= dihedral.k * dd 

942 else: 

943 gx *= ( 

944 -dihedral.k * dihedral.n * np.sin(dihedral.n * d - dihedral.d0) 

945 ) 

946 

947 dihedral.d = d 

948 

949 return i, j, k, l, gx 

950 

951 

952def get_dihedral_potential_hessian( 

953 atoms: Atoms, 

954 dihedral: Dihedral, 

955 morses: Sequence[Morse] | None = None, 

956 spectral: bool = False, 

957) -> tuple[int, int, int, int, NDArray[np.floating]]: 

958 eps = 0.000001 

959 

960 i, j, k, l, g = get_dihedral_potential_gradient(atoms, dihedral) 

961 

962 Hx = np.zeros((12, 12)) 

963 

964 dihedral_eps = Dihedral( 

965 dihedral.atomi, 

966 dihedral.atomj, 

967 dihedral.atomk, 

968 dihedral.atoml, 

969 dihedral.k, 

970 dihedral.d0, 

971 dihedral.n, 

972 ) 

973 indx = [ 

974 3 * i, 

975 3 * i + 1, 

976 3 * i + 2, 

977 3 * j, 

978 3 * j + 1, 

979 3 * j + 2, 

980 3 * k, 

981 3 * k + 1, 

982 3 * k + 2, 

983 3 * l, 

984 3 * l + 1, 

985 3 * l + 2, 

986 ] 

987 for x in range(12): 

988 a = atoms.copy() 

989 positions = np.reshape(a.get_positions(), -1) 

990 positions[indx[x]] += eps 

991 a.set_positions(np.reshape(positions, (len(a), 3))) 

992 i, j, k, l, geps = get_dihedral_potential_gradient(a, dihedral_eps) 

993 for y in range(12): 

994 Hx[x, y] += 0.5 * (geps[y] - g[y]) / eps 

995 Hx[y, x] += 0.5 * (geps[y] - g[y]) / eps 

996 

997 if dihedral.alpha is not None: 

998 rij = rel_pos_pbc(atoms, i, j) 

999 dij = linalg.norm(rij) 

1000 rkj = rel_pos_pbc(atoms, k, j) 

1001 dkj = linalg.norm(rkj) 

1002 rkl = rel_pos_pbc(atoms, k, l) 

1003 dkl = linalg.norm(rkl) 

1004 Hx *= ( 

1005 np.exp(dihedral.alpha[0] * (dihedral.rref[0] ** 2 - dij**2)) 

1006 * np.exp(dihedral.alpha[1] * (dihedral.rref[1] ** 2 - dkj**2)) 

1007 * np.exp(dihedral.alpha[2] * (dihedral.rref[2] ** 2 - dkl**2)) 

1008 ) 

1009 

1010 if morses is not None: 

1011 for m in range(len(morses)): 

1012 if ( 

1013 morses[m].atomi == i 

1014 or morses[m].atomi == j 

1015 or morses[m].atomi == k 

1016 or morses[m].atomi == l 

1017 ): 

1018 Hx *= get_morse_potential_eta(atoms, morses[m]) 

1019 elif ( 

1020 morses[m].atomj == i 

1021 or morses[m].atomj == j 

1022 or morses[m].atomj == k 

1023 or morses[m].atomj == l 

1024 ): 

1025 Hx *= get_morse_potential_eta(atoms, morses[m]) 

1026 

1027 if spectral: 

1028 eigvals, eigvecs = linalg.eigh(Hx) 

1029 D = np.diag(np.abs(eigvals)) 

1030 U = eigvecs 

1031 Hx = np.dot(U, np.dot(D, np.transpose(U))) 

1032 

1033 return i, j, k, l, Hx 

1034 

1035 

1036def get_dihedral_potential_reduced_hessian( 

1037 atoms: Atoms, 

1038 dihedral: Dihedral, 

1039 morses: Sequence[Morse] | None = None, 

1040) -> tuple[int, int, int, int, NDArray[np.floating]]: 

1041 """Calculate the reduced Hessian of a dihedral potential. 

1042 

1043 Only the torsional contribution is retained. 

1044 

1045 The absolute value of the torsional curvature is used to obtain 

1046 a positive-semidefinite Hessian. 

1047 

1048 The Hessian is optionally scaled by distance-dependent factors 

1049 and Morse-potential-based bond-breaking factors. 

1050 """ 

1051 i = dihedral.atomi 

1052 j = dihedral.atomj 

1053 k = dihedral.atomk 

1054 l = dihedral.atoml 

1055 

1056 rij = rel_pos_pbc(atoms, i, j) 

1057 rkj = rel_pos_pbc(atoms, k, j) 

1058 dkj = linalg.norm(rkj) 

1059 dkj2 = dkj * dkj 

1060 rkl = rel_pos_pbc(atoms, k, l) 

1061 

1062 rijrkj = np.dot(rij, rkj) 

1063 rkjrkl = np.dot(rkj, rkl) 

1064 

1065 rmj = np.cross(rij, rkj) 

1066 dmj = linalg.norm(rmj) 

1067 dmj2 = dmj * dmj 

1068 emj = rmj / dmj 

1069 rnk = np.cross(rkj, rkl) 

1070 dnk = linalg.norm(rnk) 

1071 dnk2 = dnk * dnk 

1072 enk = rnk / dnk 

1073 emjenk = np.dot(emj, enk) 

1074 if np.abs(emjenk) > 1.0: 

1075 emjenk = np.sign(emjenk) 

1076 

1077 d = np.sign(np.dot(rkj, np.cross(rmj, rnk))) * np.arccos(emjenk) 

1078 

1079 dddri = dkj / dmj2 * rmj 

1080 dddrl = -dkj / dnk2 * rnk 

1081 

1082 gx = np.zeros(12) 

1083 

1084 gx[0:3] = dddri 

1085 gx[3:6] = (rijrkj / dkj2 - 1.0) * dddri - rkjrkl / dkj2 * dddrl 

1086 gx[6:9] = (rkjrkl / dkj2 - 1.0) * dddrl - rijrkj / dkj2 * dddri 

1087 gx[9:12] = dddrl 

1088 

1089 if dihedral.d0 is None: 

1090 Hx = np.abs(2.0 * dihedral.k * np.cos(2.0 * d)) * np.tensordot( 

1091 gx, gx, axes=0 

1092 ) 

1093 if dihedral.n is None: 

1094 Hx = dihedral.k * np.tensordot(gx, gx, axes=0) 

1095 else: 

1096 Hx = np.abs( 

1097 -dihedral.k * dihedral.n**2 * np.cos(dihedral.n * d - dihedral.d0) 

1098 ) * np.tensordot(gx, gx, axes=0) 

1099 

1100 if dihedral.alpha is not None: 

1101 rij = rel_pos_pbc(atoms, i, j) 

1102 dij = linalg.norm(rij) 

1103 rkj = rel_pos_pbc(atoms, k, j) 

1104 dkj = linalg.norm(rkj) 

1105 rkl = rel_pos_pbc(atoms, k, l) 

1106 dkl = linalg.norm(rkl) 

1107 Hx *= ( 

1108 np.exp(dihedral.alpha[0] * (dihedral.rref[0] ** 2 - dij**2)) 

1109 * np.exp(dihedral.alpha[1] * (dihedral.rref[1] ** 2 - dkj**2)) 

1110 * np.exp(dihedral.alpha[2] * (dihedral.rref[2] ** 2 - dkl**2)) 

1111 ) 

1112 

1113 if morses is not None: 

1114 for m in range(len(morses)): 

1115 if ( 

1116 morses[m].atomi == i 

1117 or morses[m].atomi == j 

1118 or morses[m].atomi == k 

1119 or morses[m].atomi == l 

1120 ): 

1121 Hx *= get_morse_potential_eta(atoms, morses[m]) 

1122 elif ( 

1123 morses[m].atomj == i 

1124 or morses[m].atomj == j 

1125 or morses[m].atomj == k 

1126 or morses[m].atomj == l 

1127 ): 

1128 Hx *= get_morse_potential_eta(atoms, morses[m]) 

1129 

1130 dihedral.d = d 

1131 

1132 return i, j, k, l, Hx 

1133 

1134 

1135def get_vdw_potential_value( 

1136 atoms: Atoms, 

1137 vdw: VdW, 

1138) -> tuple[int, int, float]: 

1139 i = vdw.atomi 

1140 j = vdw.atomj 

1141 

1142 rij = rel_pos_pbc(atoms, i, j) 

1143 dij = linalg.norm(rij) 

1144 

1145 v = vdw.Aij / dij**12 - vdw.Bij / dij**6 

1146 

1147 vdw.r = dij 

1148 

1149 return i, j, v 

1150 

1151 

1152def get_vdw_potential_gradient( 

1153 atoms: Atoms, 

1154 vdw: VdW, 

1155) -> tuple[int, int, NDArray[np.floating]]: 

1156 i = vdw.atomi 

1157 j = vdw.atomj 

1158 

1159 rij = rel_pos_pbc(atoms, i, j) 

1160 dij = linalg.norm(rij) 

1161 eij = rij / dij 

1162 

1163 gr = (-12.0 * vdw.Aij / dij**13 + 6.0 * vdw.Bij / dij**7) * eij 

1164 

1165 gx = np.dot(Bx.T, gr) 

1166 

1167 vdw.r = dij 

1168 

1169 return i, j, gx 

1170 

1171 

1172def get_vdw_potential_hessian( 

1173 atoms: Atoms, 

1174 vdw: VdW, 

1175 spectral: bool = False, 

1176) -> tuple[int, int, NDArray[np.floating]]: 

1177 i = vdw.atomi 

1178 j = vdw.atomj 

1179 

1180 rij = rel_pos_pbc(atoms, i, j) 

1181 dij = linalg.norm(rij) 

1182 eij = rij / dij 

1183 

1184 Pij = np.tensordot(eij, eij, axes=0) 

1185 Qij = np.eye(3) - Pij 

1186 

1187 Hr = (156.0 * vdw.Aij / dij**14 - 42.0 * vdw.Bij / dij**8) * Pij + ( 

1188 -12.0 * vdw.Aij / dij**13 + 6.0 * vdw.Bij / dij**7 

1189 ) / dij * Qij 

1190 

1191 Hx = np.dot(Bx.T, np.dot(Hr, Bx)) 

1192 

1193 if spectral: 

1194 eigvals, eigvecs = linalg.eigh(Hx) 

1195 D = np.diag(np.abs(eigvals)) 

1196 U = eigvecs 

1197 Hx = np.dot(U, np.dot(D, np.transpose(U))) 

1198 

1199 vdw.r = dij 

1200 

1201 return i, j, Hx 

1202 

1203 

1204def get_coulomb_potential_value( 

1205 atoms: Atoms, 

1206 coulomb: Coulomb, 

1207) -> tuple[int, int, float]: 

1208 i = coulomb.atomi 

1209 j = coulomb.atomj 

1210 

1211 rij = rel_pos_pbc(atoms, i, j) 

1212 dij = linalg.norm(rij) 

1213 

1214 v = coulomb.chargeij / dij 

1215 

1216 coulomb.r = dij 

1217 

1218 return i, j, v 

1219 

1220 

1221def get_coulomb_potential_gradient( 

1222 atoms: Atoms, 

1223 coulomb: Coulomb, 

1224) -> tuple[int, int, NDArray[np.floating]]: 

1225 i = coulomb.atomi 

1226 j = coulomb.atomj 

1227 

1228 rij = rel_pos_pbc(atoms, i, j) 

1229 dij = linalg.norm(rij) 

1230 eij = rij / dij 

1231 

1232 gr = -coulomb.chargeij / dij / dij * eij 

1233 

1234 gx = np.dot(Bx.T, gr) 

1235 

1236 coulomb.r = dij 

1237 

1238 return i, j, gx 

1239 

1240 

1241def get_coulomb_potential_hessian( 

1242 atoms: Atoms, 

1243 coulomb: Coulomb, 

1244 spectral: bool = False, 

1245) -> tuple[int, int, NDArray[np.floating]]: 

1246 i = coulomb.atomi 

1247 j = coulomb.atomj 

1248 

1249 rij = rel_pos_pbc(atoms, i, j) 

1250 dij = linalg.norm(rij) 

1251 eij = rij / dij 

1252 

1253 Pij = np.tensordot(eij, eij, axes=0) 

1254 Qij = np.eye(3) - Pij 

1255 

1256 Hr = (2.0 * coulomb.chargeij / dij**3) * Pij + ( 

1257 -coulomb.chargeij / dij / dij 

1258 ) / dij * Qij 

1259 

1260 Hx = np.dot(Bx.T, np.dot(Hr, Bx)) 

1261 

1262 if spectral: 

1263 eigvals, eigvecs = linalg.eigh(Hx) 

1264 D = np.diag(np.abs(eigvals)) 

1265 U = eigvecs 

1266 Hx = np.dot(U, np.dot(D, np.transpose(U))) 

1267 

1268 coulomb.r = dij 

1269 

1270 return i, j, Hx 

1271 

1272 

1273def rel_pos_pbc(atoms: Atoms, i: int, j: int) -> NDArray[np.floating]: 

1274 """ 

1275 Return difference between two atomic positions, 

1276 correcting for jumps across PBC 

1277 """ 

1278 d = atoms.get_positions()[i, :] - atoms.get_positions()[j, :] 

1279 g = linalg.inv(atoms.get_cell().T) 

1280 f = np.floor(np.dot(g, d.T) + 0.5) 

1281 d -= np.dot(atoms.get_cell().T, f).T 

1282 return d