Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" An object which can be associated with a local relaxation in order 

2to make the relaxations run more smoothly.""" 

3from math import sqrt 

4import numpy as np 

5 

6 

7class VariansBreak: 

8 

9 """ Helper class which can be attached to a structure optimization, 

10 in order to terminale stalling calculations. 

11 

12 Parameters: 

13 

14 atoms: Atoms object being optimized 

15 dyn: The relaxation object being used 

16 min_stdev: The limiting std. deviation in forces to terminate at 

17 N: The number of steps used to calculate the st. dev. 

18 """ 

19 def __init__(self, atoms, dyn, min_stdev=0.005, N=15): 

20 self.atoms = atoms 

21 self.dyn = dyn 

22 self.N = N 

23 self.forces = [] 

24 self.min_stdev = min_stdev 

25 

26 def write(self): 

27 """ The method called by the optimizer in each step. """ 

28 if len(self.forces) >= self.N: 

29 self.forces.pop(0) 

30 fmax = (self.atoms.get_forces()**2).sum(axis=1).max()**0.5 

31 self.forces.append(fmax) 

32 

33 m = sum(self.forces) / float(len(self.forces)) 

34 

35 stdev = sqrt(sum([(c - m)**2 for c in self.forces]) / 

36 float(len(self.forces))) 

37 

38 if len(self.forces) >= self.N and stdev < self.min_stdev: 

39 self.dyn.converged = lambda x: True 

40 

41 

42class DivergenceBreak: 

43 

44 """ Helper class which can be attached to a structure optimization, 

45 in order to terminate diverging calculations. 

46 

47 Parameters: 

48 

49 atoms: Atoms object being optimized 

50 dyn: The relaxation object being used 

51 N: The maximum number of recent steps to be included in the 

52 evaluation of the slope  

53 Nmin: The minimal amount of steps required before evaluating 

54 the slope 

55 """ 

56 def __init__(self, atoms, dyn, N=15, Nmin=5): 

57 self.atoms = atoms 

58 self.dyn = dyn 

59 self.N = N 

60 self.Nmin = 5 

61 self.energies = [] 

62 

63 def write(self): 

64 """ The method called by the optimizer in each step. """ 

65 

66 if len(self.energies) >= self.N: 

67 self.energies.pop(0) 

68 self.energies.append(self.atoms.get_potential_energy()) 

69 

70 if len(self.energies) > self.Nmin: 

71 x = np.array(range(len(self.energies))) 

72 y = np.array(self.energies) 

73 A = np.vstack([x, np.ones(len(x))]).T 

74 slope, intersect = np.linalg.lstsq(A, y)[0] 

75 

76 if len(self.energies) >= self.N and slope > 0: 

77 self.dyn.converged = lambda x: True