Coverage for ase/calculators/siesta/parameters.py: 100.00%

36 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-11-27 15:21 +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 ---------- 

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

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

19 The initial label should be left out since it is 

20 determined programatically. 

21 Example1: 2 nodes 1.0 

22 n=2 0 2 E 50.0 2.5 

23 3.50 3.50 

24 0.95 1.00 

25 1 1 P 2 

26 3.50 

27 Example2: 1 

28 0 2 S 0.2 

29 5.00 0.00 

30 See siesta manual for details. 

31 """ 

32 assert isinstance(block, str) 

33 Parameters.__init__(self, block=block) 

34 

35 def script(self, label): 

36 """ 

37 Write the fdf script for the block. 

38 

39 Parameters 

40 ---------- 

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

42 """ 

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

44 

45 

46class Species(Parameters): 

47 """ 

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

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

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

51 

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

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

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

55 """ 

56 

57 def __init__( 

58 self, 

59 symbol, 

60 basis_set='DZP', 

61 pseudopotential=None, 

62 tag=None, 

63 ghost=False, 

64 excess_charge=None, 

65 ): 

66 kwargs = locals() 

67 kwargs.pop('self') 

68 Parameters.__init__(self, **kwargs) 

69 

70 

71def format_fdf(key, value): 

72 """ 

73 Write an fdf key-word value pair. 

74 

75 Parameters 

76 ---------- 

77 - key : The fdf-key 

78 - value : The fdf value. 

79 """ 

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

81 return '' 

82 

83 key = format_key(key) 

84 new_value = format_value(value) 

85 

86 if isinstance(value, list): 

87 string = ( 

88 '%block ' 

89 + key 

90 + '\n' 

91 + new_value 

92 + '\n' 

93 + '%endblock ' 

94 + key 

95 + '\n' 

96 ) 

97 else: 

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

99 

100 return string 

101 

102 

103def format_value(value): 

104 """ 

105 Format python values to fdf-format. 

106 

107 Parameters 

108 ---------- 

109 - value : The value to format. 

110 """ 

111 if isinstance(value, tuple): 

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

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

114 elif isinstance(value, list): 

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

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

117 else: 

118 value = str(value) 

119 

120 return value 

121 

122 

123def format_key(key): 

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

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

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

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

128 

129 return key