Coverage for ase/calculators/abinit.py: 94.23%

52 statements  

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

1"""This module defines an ASE interface to ABINIT. 

2 

3https://www.abinit.org/ 

4""" 

5 

6from pathlib import Path 

7from subprocess import check_output 

8 

9import ase.io.abinit as io 

10from ase.calculators.genericfileio import ( 

11 BaseProfile, 

12 CalculatorTemplate, 

13 GenericFileIOCalculator, 

14) 

15 

16 

17class AbinitProfile(BaseProfile): 

18 configvars = {'pp_paths'} 

19 

20 def __init__(self, command, *, pp_paths=None, **kwargs): 

21 super().__init__(command, **kwargs) 

22 # XXX pp_paths is a raw configstring when it gets here. 

23 # All the config stuff should have been loaded somehow by now, 

24 # so this should be refactored. 

25 if isinstance(pp_paths, str): 

26 pp_paths = [path for path in pp_paths.splitlines() if path] 

27 if pp_paths is None: 

28 pp_paths = [] 

29 self.pp_paths = pp_paths 

30 

31 def version(self): 

32 argv = [*self._split_command, '--version'] 

33 return check_output(argv, encoding='ascii').strip() 

34 

35 def get_calculator_command(self, inputfile): 

36 return [str(inputfile)] 

37 

38 def socketio_argv_unix(self, socket): 

39 # XXX clean up the passing of the inputfile 

40 inputfile = AbinitTemplate().input_file 

41 return [inputfile, '--ipi', f'{socket}:UNIX'] 

42 

43 

44class AbinitTemplate(CalculatorTemplate): 

45 _label = 'abinit' # Controls naming of files within calculation directory 

46 

47 def __init__(self): 

48 super().__init__( 

49 name='abinit', 

50 implemented_properties=[ 

51 'energy', 

52 'free_energy', 

53 'forces', 

54 'stress', 

55 'magmom', 

56 ], 

57 ) 

58 

59 # XXX superclass should require inputname and outputname 

60 

61 self.inputname = f'{self._label}.in' 

62 self.outputname = f'{self._label}.log' 

63 self.errorname = f'{self._label}.err' 

64 

65 def execute(self, directory, profile) -> None: 

66 profile.run( 

67 directory, self.inputname, self.outputname, errorfile=self.errorname 

68 ) 

69 

70 def write_input(self, profile, directory, atoms, parameters, properties): 

71 directory = Path(directory) 

72 parameters = dict(parameters) 

73 pp_paths = parameters.pop('pp_paths', profile.pp_paths) 

74 assert pp_paths is not None 

75 

76 kw = dict(xc='LDA', smearing=None, kpts=None, raw=None, pps='fhi') 

77 kw.update(parameters) 

78 

79 io.prepare_abinit_input( 

80 directory=directory, 

81 atoms=atoms, 

82 properties=properties, 

83 parameters=kw, 

84 pp_paths=pp_paths, 

85 ) 

86 

87 def read_results(self, directory): 

88 return io.read_abinit_outputs(directory, self._label) 

89 

90 def load_profile(self, cfg, **kwargs): 

91 return AbinitProfile.from_config(cfg, self.name, **kwargs) 

92 

93 def socketio_argv(self, profile, unixsocket, port): 

94 # XXX This handling of --ipi argument is used by at least two 

95 # calculators, should refactor if needed yet again 

96 if unixsocket: 

97 ipi_arg = f'{unixsocket}:UNIX' 

98 else: 

99 ipi_arg = f'localhost:{port:d}' 

100 

101 return profile.get_calculator_command(self.inputname) + [ 

102 '--ipi', 

103 ipi_arg, 

104 ] 

105 

106 def socketio_parameters(self, unixsocket, port): 

107 return dict(ionmov=28, expert_user=1, optcell=2) 

108 

109 

110class Abinit(GenericFileIOCalculator): 

111 """Class for doing ABINIT calculations. 

112 

113 The default parameters are very close to those that the ABINIT 

114 Fortran code would use. These are the exceptions:: 

115 

116 calc = Abinit(xc='LDA', ecut=400, toldfe=1e-5) 

117 """ 

118 

119 def __init__( 

120 self, 

121 *, 

122 profile=None, 

123 directory='.', 

124 **kwargs, 

125 ): 

126 """Construct ABINIT-calculator object. 

127 

128 Examples 

129 -------- 

130 Use default values: 

131 

132 >>> h = Atoms('H', calculator=Abinit(ecut=200, toldfe=0.001)) 

133 >>> h.center(vacuum=3.0) 

134 >>> e = h.get_potential_energy() 

135 

136 """ 

137 

138 super().__init__( 

139 template=AbinitTemplate(), 

140 profile=profile, 

141 directory=directory, 

142 parameters=kwargs, 

143 )