Coverage for gpaw/directmin/__init__.py: 88%

41 statements  

« prev     ^ index     » next       coverage.py v7.7.1, created at 2025-07-08 00:17 +0000

1""" 

2Exponential Transformation Direct Minimization 

3""" 

4 

5from gpaw.xc import xc_string_to_dict 

6from gpaw.directmin.sd_etdm import SteepestDescent, FRcg, LBFGS, \ 

7 LBFGS_P, LSR1P, ModeFollowing 

8from gpaw.directmin.ls_etdm import MaxStep, StrongWolfeConditions 

9 

10 

11def search_direction(method, etdm=None, pd=None): 

12 if isinstance(method, str): 

13 method = xc_string_to_dict(method) 

14 

15 if isinstance(method, dict): 

16 kwargs = method.copy() 

17 names = kwargs.pop('name').replace('-', '').lower().split('_') 

18 concave_step_length = 0.1 

19 if 'concave_step_length' in kwargs.keys(): 

20 concave_step_length = kwargs.pop('concave_step_length') 

21 

22 searchdir = {'sd': SteepestDescent, 

23 'frcg': FRcg, 

24 'lbfgs': LBFGS, 

25 'lbfgsp': LBFGS_P, 

26 'lsr1p': LSR1P 

27 }[names[0]](**kwargs) 

28 

29 if len(names) == 2: 

30 if names[1] == 'gmf': 

31 pd['gmf'] = True 

32 searchdir = ModeFollowing(partial_diagonalizer(pd, etdm), 

33 searchdir, concave_step_length) 

34 

35 return searchdir 

36 else: 

37 raise ValueError('Check keyword for search direction!') 

38 

39 

40def line_search_algorithm(method, objective_function, searchdir_algo): 

41 if isinstance(method, str): 

42 method = xc_string_to_dict(method) 

43 

44 if isinstance(method, dict): 

45 kwargs = method.copy() 

46 name = kwargs.pop('name').replace('-', '').lower() 

47 if name == 'swcawc': 

48 # for swc-awc we need to know 

49 # what search. dir. algo is used 

50 if 'searchdirtype' not in kwargs: 

51 kwargs['searchdirtype'] = searchdir_algo.type 

52 

53 ls_algo = {'maxstep': MaxStep, 

54 'swcawc': StrongWolfeConditions 

55 }[name](objective_function, **kwargs) 

56 

57 return ls_algo 

58 else: 

59 raise ValueError('Check keyword for line search!') 

60 

61 

62def partial_diagonalizer(method, domom): 

63 from gpaw.directmin.derivatives import Davidson 

64 if isinstance(method, str): 

65 method = xc_string_to_dict(method) 

66 

67 if isinstance(method, dict): 

68 kwargs = method.copy() 

69 name = kwargs.pop('name') 

70 if name == 'Davidson': 

71 return Davidson(domom, **kwargs) 

72 else: 

73 raise ValueError('Check keyword for partial diagonalizer!')