Coverage for ase/calculators/kim/kim.py: 36.96%

46 statements  

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

1""" 

2Knowledgebase of Interatomic Models (KIM) Calculator for ASE written by: 

3 

4Ellad B. Tadmor 

5Mingjian Wen 

6Daniel S. Karls 

7University of Minnesota 

8 

9This calculator functions as a wrapper that selects an appropriate 

10calculator for a given KIM model depending on whether it supports the 

11KIM application programming interface (API) or not. For more information 

12on KIM, visit https://openkim.org. 

13""" 

14 

15from . import kimpy_wrappers 

16from .calculators import ( 

17 ASAPCalculator, 

18 KIMCalculator, 

19 LAMMPSLibCalculator, 

20 LAMMPSRunCalculator, 

21) 

22from .exceptions import KIMCalculatorError 

23 

24 

25def KIM(model_name, simulator=None, options=None, debug=False): 

26 """Calculator wrapper for OpenKIM models 

27 

28 Returns a suitable calculator that can be used with any model 

29 archived in the Open Knowledgebase of Interatomic Models (OpenKIM) 

30 at https://openkim.org. There are two kinds of models in KIM: 

31 Portable Models (PMs), which can be used with any KIM API-compliant 

32 simulator, and Simulator Models (SMs), which are essentially just 

33 wrappers around native commands in a specific simulator (often 

34 combined with values for the model parameters). PMs published on 

35 openkim.org contain the string '__MO_' in their name, while SMs 

36 published on openkim.org contain the string '__SM_' in their name. 

37 

38 Parameters 

39 ---------- 

40 model_name : str 

41 The name of the KIM model installed on your system. KIM models 

42 published on openkim.org follow a specific naming scheme (see 

43 https://openkim.org/doc/schema/kim-ids). 

44 

45 simulator : str, optional 

46 Used to identify the ASE calculator that will be used. 

47 Currently supported values include 'kimmodel', 'lammpslib', 

48 'lammpsrun' and 'asap', and correspond to different calculators 

49 as follows: 

50 

51 - kimmodel (default for PMs) 

52 : :py:mod:`ase.calculators.kim.kimmodel.KIMModelCalculator` 

53 

54 - lammpsrun (PMs or LAMMPS SMs) 

55 : :py:mod:`ase.calculators.lammpsrun.LAMMPS` 

56 

57 - lammpslib (default for LAMMPS SMs) 

58 : :py:mod:`ase.calculators.lammpslib.LAMMPSlib` 

59 

60 - asap (PMs) 

61 : :py:mod:`asap3.Internal.OpenKIMcalculator.OpenKIMcalculator` 

62 

63 - asap (ASAP SMs) 

64 : :py:mod:`asap3.Internal.BuiltinPotentials.EMT` 

65 

66 In general, this argument should be omitted, in which case a 

67 calculator compatible with the specified model will 

68 automatically be determined. 

69 

70 options : dict, optional 

71 Additional options passed to the initializer of the selected 

72 calculator. If ``simulator`` == 'kimmodel', possible options are: 

73 

74 - ase_neigh (bool) 

75 : Whether to use the kimpy neighbor list library (False) or 

76 use ASE's internal neighbor list mechanism (True). Usually 

77 kimpy's neighbor list library will be faster. (Default: 

78 False) 

79 

80 - neigh_skin_ratio (float) 

81 : The skin distance used for neighbor list construction, 

82 expressed as a fraction of the model cutoff (Default: 0.2) 

83 

84 - release_GIL (bool) 

85 : Whether to release python GIL. Releasing the GIL allows a KIM 

86 model to run with multiple concurrent threads. (Default: False) 

87 

88 See the ASE LAMMPS calculators doc page 

89 (https://docs.ase-lib.org/ase/calculators/lammps.html) for 

90 available options for the lammpslib and lammpsrun calculators. 

91 

92 debug : bool, optional 

93 If True, detailed information is printed to stdout. If the 

94 lammpsrun calculator is being used, this also serves as the 

95 value of the ``keep_tmp_files`` option. (Default: False) 

96 

97 Returns 

98 ------- 

99 ase.calculators.calculator.Calculator 

100 An ASE-compatible calculator. Currently, this will be an instance of 

101 KIMModelCalculator, LAMMPS (the lammpsrun calculator), or LAMMPSlib, 

102 which are all defined in the ASE codebase, or an instance of either 

103 OpenKIMcalculator or EMT defined in the asap3 codebase. 

104 

105 Raises 

106 ------ 

107 KIMCalculatorError 

108 Indicates an error occurred in initializing the calculator, 

109 e.g. due to incompatible combinations of argument values 

110 """ 

111 

112 if options is None: 

113 options = {} 

114 

115 # If this is a KIM Portable Model (supports KIM API), return 

116 # support through a KIM-compliant simulator 

117 model_type = 'pm' if _is_portable_model(model_name) else 'sm' 

118 

119 if model_type == 'pm': 

120 if simulator is None: # Default 

121 simulator = 'kimmodel' 

122 

123 if simulator == 'kimmodel': 

124 return KIMCalculator(model_name, options, debug) 

125 

126 elif simulator == 'asap': 

127 return ASAPCalculator( 

128 model_name, model_type, options=options, verbose=debug 

129 ) 

130 

131 elif simulator == 'lammpsrun': 

132 supported_species = get_model_supported_species(model_name) 

133 

134 # Return LAMMPS calculator 

135 return LAMMPSRunCalculator( 

136 model_name, model_type, supported_species, options, debug 

137 ) 

138 

139 elif simulator == 'lammpslib': 

140 raise KIMCalculatorError( 

141 '"lammpslib" calculator does not support KIM Portable Models. ' 

142 'Try using the "lammpsrun" calculator.' 

143 ) 

144 else: 

145 raise KIMCalculatorError( 

146 'Unsupported simulator "{}" requested to run KIM ' 

147 'Portable Model.'.format(simulator) 

148 ) 

149 

150 ####################################################### 

151 # If we get to here, the model is a KIM Simulator Model 

152 ####################################################### 

153 with kimpy_wrappers.SimulatorModel(model_name) as sm: 

154 # Handle default behavior for 'simulator' 

155 if simulator is None: 

156 if sm.simulator_name == 'ASAP': 

157 simulator = 'asap' 

158 elif sm.simulator_name == 'LAMMPS': 

159 simulator = 'lammpslib' 

160 

161 if sm.simulator_name == 'ASAP': 

162 return ASAPCalculator( 

163 model_name, 

164 model_type, 

165 options=options, 

166 model_defn=sm.model_defn, 

167 verbose=debug, 

168 supported_units=sm.supported_units, 

169 ) 

170 

171 elif sm.simulator_name == 'LAMMPS': 

172 if simulator == 'lammpsrun': 

173 return LAMMPSRunCalculator( 

174 model_name, 

175 model_type, 

176 sm.supported_species, 

177 options, 

178 debug, 

179 atom_style=sm.atom_style, 

180 supported_units=sm.supported_units, 

181 ) 

182 

183 elif simulator == 'lammpslib': 

184 return LAMMPSLibCalculator( 

185 model_name, 

186 sm.supported_species, 

187 sm.supported_units, 

188 options, 

189 ) 

190 

191 else: 

192 raise KIMCalculatorError( 

193 f'Unknown LAMMPS calculator: "{simulator}".' 

194 ) 

195 

196 else: 

197 raise KIMCalculatorError( 

198 f'Unsupported simulator: "{sm.simulator_name}".' 

199 ) 

200 

201 

202def _is_portable_model(model_name): 

203 """ 

204 Returns True if the model specified is a KIM Portable Model (if it 

205 is not, then it must be a KIM Simulator Model -- there are no other 

206 types of models in KIM) 

207 """ 

208 with kimpy_wrappers.ModelCollections() as col: 

209 model_type = col.get_item_type(model_name) 

210 

211 return ( 

212 model_type == kimpy_wrappers.wrappers.collection_item_type_portableModel 

213 ) 

214 

215 

216def get_model_supported_species(model_name): 

217 if _is_portable_model(model_name): 

218 with kimpy_wrappers.PortableModel(model_name, debug=False) as pm: 

219 supported_species, _ = pm.get_model_supported_species_and_codes() 

220 else: 

221 with kimpy_wrappers.SimulatorModel(model_name) as sm: 

222 supported_species = sm.supported_species 

223 

224 return supported_species