Coverage for ase/optimize/rfo.py: 84.21%

38 statements  

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

1import numpy as np 

2import scipy 

3 

4from ase.optimize.bfgs import BFGS 

5 

6 

7class RFO(BFGS): 

8 """RFO (Rational Function Optimizer) combined with BFGS-based Hessian 

9 updates. 

10 

11 RFO will take quasi-Newton-like steps in quadratic regime and rational 

12 function damped steps outside of it. The ``damping`` factor determines 

13 the transition threshold between the regimes. 

14 

15 Read about this algorithm here: 

16 

17 | A. Banerjee, N. Adams, J. Simons, R. Shepard, 

18 | :doi:`Search for Stationary Points on Surfaces <10.1021/j100247a015>` 

19 | J. Phys. Chem. 1985, 89, 52-57. 

20 

21 Note that ``damping`` is the reciprocal of coordinate scale :math:`a` from 

22 the reference. 

23 """ 

24 

25 # default parameters 

26 defaults = {**BFGS.defaults, 'damping': 1.0, 'eigsh_maxiter': 50} 

27 

28 def __init__( 

29 self, 

30 *args, 

31 damping: float | None = None, 

32 eigsh_maxiter: int | None = None, 

33 **kwargs, 

34 ): 

35 """ 

36 Parameters 

37 ---------- 

38 *args 

39 Positional arguments passed to :class:`~ase.optimize.BFGS`. 

40 

41 damping: float 

42 Determines transition threshold between quasi-Newton-like and 

43 rational function damped steps. The larger the value, the larger 

44 and stronger the damped regime. (default is 1.0 Å^-1). 

45 

46 eigsh_maxiter: int 

47 Maximum number of eigsh iterations done before falling back to eigh 

48 for solving the RFO step. 

49 

50 **kwargs 

51 Keyword arguments passed to :class:`~ase.optimize.BFGS`. 

52 

53 Examples 

54 -------- 

55 

56 >>> from ase import Atoms 

57 >>> from ase.optimize import RFO 

58 >>> from ase.calculators.emt import EMT 

59 ... 

60 >>> system = Atoms( 

61 ... 'N2', 

62 ... positions = [(0.0, 0.0, 0.0), (0.0, 0.0, 1.1)], 

63 ... calculator=EMT() 

64 ... ) 

65 >>> dyn = RFO(system) 

66 >>> dyn.run(fmax=0.05) # doctest:+ELLIPSIS 

67 Step Time Energy fmax 

68 RFO: 0 ... 0.440344 3.251800 

69 RFO: 1 ... 0.264502 0.362456 

70 RFO: 2 ... 0.262867 0.084092 

71 RFO: 3 ... 0.262777 0.001583 

72 ... 

73 """ 

74 if damping is None: 

75 self.damping = self.defaults['damping'] 

76 else: 

77 self.damping = damping 

78 

79 if eigsh_maxiter is None: 

80 self.eigsh_maxiter = self.defaults['eigsh_maxiter'] 

81 else: 

82 self.eigsh_maxiter = eigsh_maxiter 

83 

84 super().__init__(*args, **kwargs) 

85 

86 def initialize(self): 

87 # Initialize Hessian 

88 super().initialize() 

89 n = len(self.H0) 

90 self.aug_H = np.zeros((n + 1, n + 1)) 

91 self.v0 = None 

92 

93 def read(self): 

94 # Read Hessian 

95 super().read() 

96 n = len(self.H) 

97 self.aug_H = np.zeros((n + 1, n + 1)) 

98 self.v0 = None 

99 

100 def prepare_step(self, pos, gradient): 

101 """Compute step from first eigenvector of gradient-augmented Hessian""" 

102 # Update Hessian BFGS-style 

103 self.update(pos, -gradient, self.pos0, self.forces0) 

104 self.aug_H[:-1, :-1] = self.H / self.damping**2 

105 self.aug_H[-1, :-1] = gradient / self.damping 

106 self.aug_H[:-1, -1] = gradient / self.damping 

107 

108 # Try Lanczos and fall back to dense solver in case of convergence 

109 # issues 

110 try: 

111 V = scipy.sparse.linalg.eigsh( 

112 self.aug_H, 

113 k=1, 

114 which='SA', 

115 v0=self.v0, 

116 maxiter=self.eigsh_maxiter, 

117 )[1] 

118 except scipy.sparse.linalg.ArpackNoConvergence: 

119 V = scipy.linalg.eigh(self.aug_H, subset_by_index=(0, 0))[1] 

120 

121 self.v0 = V[:, 0] 

122 dpos = self.v0[:-1] / self.v0[-1] / self.damping 

123 steplengths = self.optimizable.gradient_norm(dpos) 

124 self.pos0 = pos 

125 self.forces0 = -gradient.copy() 

126 return dpos, steplengths