Coverage for ase/optimize/mdmin.py: 97.06%

34 statements  

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

1from typing import IO 

2 

3import numpy as np 

4 

5from ase import Atoms 

6from ase.optimize.optimize import Optimizer 

7from ase.utils import deprecate_positionals 

8 

9 

10class MDMin(Optimizer): 

11 # default parameters 

12 defaults = {**Optimizer.defaults, 'dt': 0.2} 

13 

14 @deprecate_positionals(2) 

15 def __init__( 

16 self, 

17 atoms: Atoms, 

18 restart: str | None = None, 

19 logfile: IO | str = '-', 

20 trajectory: str | None = None, 

21 dt: float | None = None, 

22 maxstep: float | None = None, 

23 **kwargs, 

24 ): 

25 """ 

26 

27 Parameters 

28 ---------- 

29 atoms: :class:`~ase.Atoms` 

30 The Atoms object to relax. 

31 

32 restart: str 

33 JSON file used to store hessian matrix. If set, file with 

34 such a name will be searched and hessian matrix stored will 

35 be used, if the file exists. 

36 

37 trajectory: str 

38 Trajectory file used to store optimisation path. 

39 

40 logfile: str 

41 Text file used to write summary information. 

42 

43 dt: float 

44 Time step for integrating the equation of motion. 

45 

46 maxstep: float 

47 Spatial step limit in Angstrom. This allows larger values of dt 

48 while being more robust to instabilities in the optimization. 

49 

50 kwargs : dict, optional 

51 Extra arguments passed to 

52 :class:`~ase.optimize.optimize.Optimizer`. 

53 

54 Examples 

55 -------- 

56 

57 >>> from ase import Atoms 

58 >>> from ase.optimize import MDMin 

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

60 ... 

61 >>> system = Atoms( 

62 ... 'N2', 

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

64 ... calculator=EMT() 

65 ... ) 

66 >>> dyn = MDMin(system) 

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

68 Step Time Energy fmax 

69 MDMin: 0 ... 0.440344 3.251800 

70 MDMin: 1 ... 0.278778 1.160079 

71 MDMin: 2 ... 0.269160 0.689054 

72 MDMin: 3 ... 0.264472 0.368254 

73 MDMin: 4 ... 0.263357 0.211246 

74 MDMin: 5 ... 0.262951 0.117040 

75 MDMin: 6 ... 0.262833 0.066207 

76 MDMin: 7 ... 0.262795 0.037033 

77 ... 

78 """ 

79 super().__init__(atoms, restart, logfile, trajectory, **kwargs) 

80 

81 self.dt = dt or self.defaults['dt'] 

82 self.maxstep = maxstep or self.defaults['maxstep'] 

83 

84 def initialize(self): 

85 self.v = None 

86 

87 def read(self): 

88 self.v, self.dt = self.load() 

89 

90 def step(self, forces=None): 

91 forces = -self._get_gradient(forces) 

92 

93 optimizable = self.optimizable 

94 

95 if self.v is None: 

96 self.v = np.zeros(optimizable.ndofs()) 

97 else: 

98 self.v += 0.5 * self.dt * forces 

99 # Correct velocities: 

100 vf = np.vdot(self.v, forces) 

101 if vf < 0.0: 

102 self.v[:] = 0.0 

103 else: 

104 self.v[:] = forces * vf / np.vdot(forces, forces) 

105 

106 self.v += 0.5 * self.dt * forces 

107 pos = optimizable.get_x() 

108 dpos = self.dt * self.v 

109 

110 # For any dpos magnitude larger than maxstep, scaling 

111 # is <1. We add a small float to prevent overflows/zero-div errors. 

112 # All displacement vectors (rows) of dpos which have a norm larger 

113 # than self.maxstep are scaled to it. 

114 

115 # XXX Here we are using gradient_norm() to get the norm of positions 

116 maxstep = self.optimizable.gradient_norm(dpos) 

117 scaling = self.maxstep / (1e-6 + maxstep) 

118 dpos *= np.clip(scaling, 0.0, 1.0) 

119 optimizable.set_x(pos + dpos) 

120 self.dump((self.v, self.dt))