Coverage for /builds/kinetik161/ase/ase/calculators/siesta/parameters.py: 91.67%

36 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-12-10 11:04 +0000

1from ase.calculators.calculator import Parameters 

2 

3""" 

42017.04 - Pedro Brandimarte: changes for python 2-3 compatible 

5""" 

6 

7 

8class PAOBasisBlock(Parameters): 

9 """ 

10 Representing a block in PAO.Basis for one species. 

11 """ 

12 

13 def __init__(self, block): 

14 """ 

15 Parameters: 

16 -block : String. A block defining the basis set of a single 

17 species using the format of a PAO.Basis block. 

18 The initial label should be left out since it is 

19 determined programatically. 

20 Example1: 2 nodes 1.0 

21 n=2 0 2 E 50.0 2.5 

22 3.50 3.50 

23 0.95 1.00 

24 1 1 P 2 

25 3.50 

26 Example2: 1 

27 0 2 S 0.2 

28 5.00 0.00 

29 See siesta manual for details. 

30 """ 

31 assert isinstance(block, str) 

32 Parameters.__init__(self, block=block) 

33 

34 def script(self, label): 

35 """ 

36 Write the fdf script for the block. 

37 

38 Parameters: 

39 -label : The label to insert in front of the block. 

40 """ 

41 return label + ' ' + self['block'] 

42 

43 

44class Species(Parameters): 

45 """ 

46 Parameters for specifying the behaviour for a single species in the 

47 calculation. If the tag argument is set to an integer then atoms with 

48 the specified element and tag will be a separate species. 

49 

50 Pseudopotential and basis set can be specified. Additionally the species 

51 can be set be a ghost species, meaning that they will not be considered 

52 atoms, but the corresponding basis set will be used. 

53 """ 

54 

55 def __init__(self, 

56 symbol, 

57 basis_set='DZP', 

58 pseudopotential=None, 

59 tag=None, 

60 ghost=False, 

61 excess_charge=None): 

62 kwargs = locals() 

63 kwargs.pop('self') 

64 Parameters.__init__(self, **kwargs) 

65 

66 

67def format_fdf(key, value): 

68 """ 

69 Write an fdf key-word value pair. 

70 

71 Parameters: 

72 - key : The fdf-key 

73 - value : The fdf value. 

74 """ 

75 if isinstance(value, (list, tuple)) and len(value) == 0: 

76 return '' 

77 

78 key = format_key(key) 

79 new_value = format_value(value) 

80 

81 if isinstance(value, list): 

82 string = '%block ' + key + '\n' +\ 

83 new_value + '\n' + \ 

84 '%endblock ' + key + '\n' 

85 else: 

86 string = f'{key}\t{new_value}\n' 

87 

88 return string 

89 

90 

91def format_value(value): 

92 """ 

93 Format python values to fdf-format. 

94 

95 Parameters: 

96 - value : The value to format. 

97 """ 

98 if isinstance(value, tuple): 

99 sub_values = [format_value(v) for v in value] 

100 value = '\t'.join(sub_values) 

101 elif isinstance(value, list): 

102 sub_values = [format_value(v) for v in value] 

103 value = '\n'.join(sub_values) 

104 else: 

105 value = str(value) 

106 

107 return value 

108 

109 

110def format_key(key): 

111 """ Fix the fdf-key replacing '_' with '.' and '__' with '_' """ 

112 key = key.replace('__', '#') 

113 key = key.replace('_', '.') 

114 key = key.replace('#', '_') 

115 

116 return key