Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2Knowledgebase of Interatomic Models (KIM) Calculator for ASE written by:
4Ellad B. Tadmor
5Mingjian Wen
6Daniel S. Karls
7University of Minnesota
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"""
15from . import kimpy_wrappers
16from .exceptions import KIMCalculatorError
17from .calculators import (
18 KIMCalculator,
19 ASAPCalculator,
20 LAMMPSRunCalculator,
21 LAMMPSLibCalculator,
22)
25def KIM(model_name, simulator=None, options=None, debug=False):
26 """Calculator wrapper for OpenKIM models
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.
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).
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:
51 - kimmodel (default for PMs)
52 : :py:mod:`ase.calculators.kim.kimmodel.KIMModelCalculator`
54 - lammpsrun (PMs or LAMMPS SMs)
55 : :py:mod:`ase.calculators.lammpsrun.LAMMPS`
57 - lammpslib (default for LAMMPS SMs)
58 : :py:mod:`ase.calculators.lammpslib.LAMMPSlib`
60 - asap (PMs)
61 : :py:mod:`asap3.Internal.OpenKIMcalculator.OpenKIMcalculator`
63 - asap (ASAP SMs)
64 : :py:mod:`asap3.Internal.BuiltinPotentials.EMT`
66 In general, this argument should be omitted, in which case a
67 calculator compatible with the specified model will
68 automatically be determined.
70 options : dict, optional
71 Additional options passed to the initializer of the selected
72 calculator. If ``simulator`` == 'kimmodel', possible options are:
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)
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)
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)
88 See the ASE LAMMPS calculators doc page
89 (https://wiki.fysik.dtu.dk/ase/ase/calculators/lammps.html) for
90 available options for the lammpslib and lammpsrun calculators.
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)
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.
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 """
112 if options is None:
113 options = dict()
115 # If this is a KIM Portable Model (supports KIM API), return support through
116 # a KIM-compliant simulator
117 model_type = "pm" if _is_portable_model(model_name) else "sm"
119 if model_type == "pm":
120 if simulator is None: # Default
121 simulator = "kimmodel"
123 if simulator == "kimmodel":
124 return KIMCalculator(model_name, options, debug)
126 elif simulator == "asap":
127 return ASAPCalculator(
128 model_name, model_type, options=options, verbose=debug
129 )
131 elif simulator == "lammpsrun":
132 supported_species = get_model_supported_species(model_name)
134 # Return LAMMPS calculator
135 return LAMMPSRunCalculator(
136 model_name, model_type, supported_species, options, debug
137 )
139 elif simulator == "lammpslib":
140 raise KIMCalculatorError(
141 '"lammpslib" calculator does not support KIM Portable Models. Try '
142 'using the "lammpsrun" calculator.'
143 )
144 else:
145 raise KIMCalculatorError(
146 'Unsupported simulator "{}" requested to run KIM Portable Model.'.format(
147 simulator
148 )
149 )
151 #######################################################
152 # If we get to here, the model is a KIM Simulator Model
153 #######################################################
154 with kimpy_wrappers.SimulatorModel(model_name) as sm:
156 # Handle default behavior for 'simulator'
157 if simulator is None:
158 if sm.simulator_name == "ASAP":
159 simulator = "asap"
160 elif sm.simulator_name == "LAMMPS":
161 simulator = "lammpslib"
163 if sm.simulator_name == "ASAP":
165 return ASAPCalculator(
166 model_name,
167 model_type,
168 options=options,
169 model_defn=sm.model_defn,
170 verbose=debug,
171 supported_units=sm.supported_units,
172 )
174 elif sm.simulator_name == "LAMMPS":
176 if simulator == "lammpsrun":
178 return LAMMPSRunCalculator(
179 model_name,
180 model_type,
181 sm.supported_species,
182 options,
183 debug,
184 atom_style=sm.atom_style,
185 supported_units=sm.supported_units,
186 )
188 elif simulator == "lammpslib":
189 return LAMMPSLibCalculator(
190 model_name, sm.supported_species, sm.supported_units, options
191 )
193 else:
194 raise KIMCalculatorError(
195 'Unknown LAMMPS calculator: "{}".'.format(simulator)
196 )
198 else:
199 raise KIMCalculatorError(
200 'Unsupported simulator: "{}".'.format(sm.simulator_name)
201 )
204def _is_portable_model(model_name):
205 """
206 Returns True if the model specified is a KIM Portable Model (if it
207 is not, then it must be a KIM Simulator Model -- there are no other
208 types of models in KIM)
209 """
210 with kimpy_wrappers.ModelCollections() as col:
211 model_type = col.get_item_type(model_name)
213 return model_type == kimpy_wrappers.collection_item_type_portableModel
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
224 return supported_species