Coverage for ase/codes.py: 79.53%
171 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-27 15:21 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-27 15:21 +0000
1from dataclasses import dataclass
3# Note: There could be more than one "calculator" for any given code;
4# for example Espresso can work both as GenericFileIOCalculator and
5# SocketIOCalculator, or as part of some DFTD3 combination.
6#
7# Also, DFTD3 is one external code but can be invoked alone (as PureDFTD3)
8# as well as together with a DFT code (the main DFTD3 calculator).
9#
10# The current CodeMetadata object only specifies a single calculator class.
11# We should be wary of these invisible "one-to-one" restrictions.
14@dataclass
15class CodeMetadata:
16 name: str
17 longname: str
18 modulename: str
19 classname: str
20 configurable: bool = True
21 is_wrapper: bool = False
23 def calculator_class(self):
24 from importlib import import_module
26 module = import_module(self.modulename)
27 cls = getattr(module, self.classname)
28 return cls
30 @classmethod
31 def define_code(
32 cls,
33 name: str,
34 longname: str,
35 importpath: str,
36 configurable: bool = True,
37 is_wrapper: bool = False,
38 ):
39 modulename, classname = importpath.rsplit('.', 1)
40 return cls(
41 name,
42 longname,
43 modulename,
44 classname,
45 configurable=configurable,
46 is_wrapper=is_wrapper,
47 )
49 def _description(self):
50 yield f'Name: {self.longname}'
51 yield f'Import: {self.modulename}.{self.classname}'
52 yield f'Type: {self.calculator_type()}'
53 yield from self._wrapper_description()
54 yield from self._config_description()
56 def description(self, indent=''):
57 return '\n'.join(indent + line for line in self._description())
59 def is_legacy_fileio(self):
60 from ase.calculators.calculator import FileIOCalculator
62 return issubclass(self.calculator_class(), FileIOCalculator)
64 def is_generic_fileio(self):
65 from ase.calculators.genericfileio import CalculatorTemplate
67 # It is nicer to check for the template class, since it has the name,
68 # but then calculator_class() should be renamed.
69 return issubclass(self.calculator_class(), CalculatorTemplate)
71 def is_calculator_oldbase(self):
72 from ase.calculators.calculator import Calculator
74 return issubclass(self.calculator_class(), Calculator)
76 def is_base_calculator(self):
77 from ase.calculators.calculator import BaseCalculator
79 return issubclass(self.calculator_class(), BaseCalculator)
81 def calculator_type(self):
82 cls = self.calculator_class()
84 if self.is_generic_fileio():
85 return 'GenericFileIOCalculator'
87 if self.is_legacy_fileio():
88 return 'FileIOCalculator (legacy)'
90 if self.is_calculator_oldbase():
91 return 'Calculator (legacy base class)'
93 if self.is_base_calculator():
94 return 'Base calculator'
96 return f'BAD: Not a proper calculator (superclasses: {cls.__mro__})'
98 def profile(self):
99 from ase.calculators.calculator import FileIOCalculator
100 from ase.calculators.genericfileio import CalculatorTemplate
101 from ase.config import cfg
103 cls = self.calculator_class()
104 if issubclass(cls, CalculatorTemplate):
105 return cls().load_profile(cfg)
106 elif hasattr(cls, 'fileio_rules'):
107 assert issubclass(cls, FileIOCalculator)
108 return cls.load_argv_profile(cfg, self.name)
109 else:
110 raise NotImplementedError('profile() not implemented')
112 def _wrapper_description(self):
113 if self.is_wrapper:
114 yield ('')
115 yield ('Wraps other Calculator(s)')
117 def _config_description(self):
118 from ase.calculators.genericfileio import BadConfiguration
119 from ase.config import cfg
121 if not self.configurable:
122 return
124 yield ''
126 parser = cfg.parser
127 if self.name not in parser:
128 yield f'Not configured: No [{self.name}] section in configuration'
129 return
131 try:
132 profile = self.profile()
133 except BadConfiguration as ex:
134 yield f'Error in configuration section [{self.name}]'
135 yield 'Missing or bad parameters:'
136 yield f' {ex}'
137 return
138 except NotImplementedError as ex:
139 yield f'N/A: {ex}'
140 return
142 yield f'Configured by section [{self.name}]:'
143 configvars = vars(profile)
144 for name in sorted(configvars):
145 yield f' {name} = {configvars[name]}'
147 return
150def register_codes():
152 codes = {}
154 def reg(name, *args, configurable=False, is_wrapper=False):
155 code = CodeMetadata.define_code(
156 name, *args, configurable=configurable, is_wrapper=is_wrapper
157 )
158 codes[name] = code
160 reg(
161 'abinit',
162 'Abinit',
163 'ase.calculators.abinit.AbinitTemplate',
164 configurable=True,
165 )
166 reg(
167 'ace',
168 'ACE molecule',
169 'ase.calculators.acemolecule.ACE',
170 configurable=True,
171 )
172 reg('acn', 'ACN force field', 'ase.calculators.acn.ACN')
173 reg(
174 'aims',
175 'FHI-Aims',
176 'ase.calculators.aims.AimsTemplate',
177 configurable=True,
178 )
179 reg('amber', 'Amber', 'ase.calculators.amber.Amber', configurable=True)
180 reg(
181 'average',
182 'AverageCalculator',
183 'ase.calculators.mixing.AverageCalculator',
184 is_wrapper=True,
185 )
186 reg('castep', 'Castep', 'ase.calculators.castep.Castep', configurable=True)
187 reg(
188 'checkpoint',
189 'CheckpointCalculator',
190 'ase.calculators.checkpoint.CheckpointCalculator',
191 is_wrapper=True,
192 )
193 reg(
194 'combine_mm',
195 'CombineMM',
196 'ase.calculators.combine_mm.CombineMM',
197 is_wrapper=True,
198 )
199 reg(
200 'counterion',
201 'AtomicCounterIon',
202 'ase.calculators.counterions.AtomicCounterIon',
203 )
204 reg('cp2k', 'CP2K', 'ase.calculators.cp2k.CP2K', configurable=True)
205 reg(
206 'crystal',
207 'CRYSTAL',
208 'ase.calculators.crystal.CRYSTAL',
209 configurable=True,
210 )
211 reg('demon', 'deMon', 'ase.calculators.demon.Demon', configurable=True)
212 reg(
213 'demonnano',
214 'deMon-nano',
215 'ase.calculators.demonnano.DemonNano',
216 configurable=True,
217 )
218 reg('dftb', 'DFTB+', 'ase.calculators.dftb.Dftb', configurable=True)
219 reg('dftd3', 'DFT-D3', 'ase.calculators.dftd3.DFTD3', configurable=True)
220 reg('dmol', 'DMol3', 'ase.calculators.dmol.DMol3', configurable=True)
221 reg('eam', 'EAM', 'ase.calculators.eam.EAM')
222 reg(
223 'eiqmmmm',
224 'Explicit interaction QMMM',
225 'ase.calculators.qmmm.EIQMMM',
226 is_wrapper=True,
227 )
228 reg('elk', 'ELK', 'ase.calculators.elk.ELK', configurable=True)
229 reg('emt', 'EMT potential', 'ase.calculators.emt.EMT')
230 reg(
231 'espresso',
232 'Quantum Espresso',
233 'ase.calculators.espresso.EspressoTemplate',
234 configurable=True,
235 )
236 reg(
237 'exciting',
238 'Exciting',
239 'ase.calculators.exciting.exciting.ExcitingGroundStateTemplate',
240 configurable=True,
241 )
242 reg('ff', 'FF', 'ase.calculators.ff.ForceField')
243 reg(
244 'fd',
245 'Finite Difference',
246 'ase.calculators.fd.FiniteDifferenceCalculator',
247 is_wrapper=True,
248 )
249 reg(
250 'forceconstant',
251 'ForceConstantCalculator',
252 'ase.calculators.qmmm.ForceConstantCalculator',
253 )
254 reg(
255 'force-qmmmm',
256 'Force-based QM/MM Calculator',
257 'ase.calculators.qmmm.ForceQMMM',
258 is_wrapper=True,
259 )
260 reg(
261 'gamess_us',
262 'GAMESS-US',
263 'ase.calculators.gamess_us.GAMESSUS',
264 configurable=True,
265 )
266 reg(
267 'gaussian',
268 'Gaussian',
269 'ase.calculators.gaussian.Gaussian',
270 configurable=True,
271 )
272 reg(
273 'gromacs',
274 'Gromacs',
275 'ase.calculators.gromacs.Gromacs',
276 configurable=True,
277 )
278 reg('gulp', 'GULP', 'ase.calculators.gulp.GULP', configurable=True)
279 reg(
280 'harmonic',
281 'Harmonic potential',
282 'ase.calculators.harmonic.HarmonicCalculator',
283 )
284 reg('idealgas', 'Ideal gas', 'ase.calculators.idealgas.IdealGas')
285 reg(
286 'lammpslib',
287 'Lammps (python library)',
288 'ase.calculators.lammpslib.LAMMPSlib',
289 configurable=True,
290 )
291 reg(
292 'lammpsrun',
293 'Lammps (external)',
294 'ase.calculators.lammpsrun.LAMMPS',
295 configurable=True,
296 )
297 reg(
298 'linearcombination',
299 'LinearCombinationCalculator',
300 'ase.calculators.mixing.LinearCombinationCalculator',
301 is_wrapper=True,
302 )
303 reg(
304 'lj',
305 'Lennard–Jones potential',
306 'ase.calculators.lj.LennardJones',
307 is_wrapper=True,
308 )
309 reg(
310 'logging',
311 'LoggingCalculator',
312 'ase.calculators.loggingcalc.LoggingCalculator',
313 is_wrapper=True,
314 )
315 reg(
316 'mixed',
317 'MixedCalculator',
318 'ase.calculators.mixing.MixedCalculator',
319 is_wrapper=True,
320 )
321 reg('mopac', 'MOPAC', 'ase.calculators.mopac.MOPAC', configurable=True)
322 reg('morse', 'Morse potential', 'ase.calculators.morse.MorsePotential')
323 reg('nwchem', 'NWChem', 'ase.calculators.nwchem.NWChem', configurable=True)
324 reg(
325 'octopus',
326 'Octopus',
327 'ase.calculators.octopus.OctopusTemplate',
328 configurable=True,
329 )
330 reg(
331 'onetep',
332 'Onetep',
333 'ase.calculators.onetep.OnetepTemplate',
334 configurable=True,
335 )
336 reg('openmx', 'OpenMX', 'ase.calculators.openmx.OpenMX', configurable=True)
337 reg('orca', 'ORCA', 'ase.calculators.orca.OrcaTemplate', configurable=True)
338 reg('plumed', 'Plumed', 'ase.calculators.plumed.Plumed', configurable=True)
339 reg('psi4', 'Psi4', 'ase.calculators.psi4.Psi4', configurable=True)
340 reg(
341 'python-subprocess',
342 'PythonSubProcessCalculator',
343 'ase.calculators.subprocesscalculator.PythonSubProcessCalculator',
344 is_wrapper=True,
345 )
346 reg('qchem', 'QChem', 'ase.calculators.qchem.QChem', configurable=True)
347 reg(
348 'rescaled',
349 'Rescaled Calculator',
350 'ase.calculators.qmmm.RescaledCalculator',
351 is_wrapper=True,
352 )
353 reg('siesta', 'SIESTA', 'ase.calculators.siesta.Siesta', configurable=True)
354 reg(
355 'simple-qmmmm',
356 'Simple QMMM',
357 'ase.calculators.qmmm.SimpleQMMM',
358 is_wrapper=True,
359 )
360 reg(
361 'singlepoint',
362 'SinglePoint',
363 'ase.calculators.singlepoint.SinglePointCalculator',
364 )
365 reg(
366 'singlepoint-dft',
367 'SinglePointDFTCalculator',
368 'ase.calculators.singlepoint.SinglePointDFTCalculator',
369 )
370 reg(
371 'socketio',
372 'SocketIOCalculator',
373 'ase.calculators.socketio.SocketIOCalculator',
374 is_wrapper=True,
375 )
376 reg(
377 'spring',
378 'Spring Calculator',
379 'ase.calculators.harmonic.SpringCalculator',
380 )
381 reg(
382 'sum',
383 'SumCalculator',
384 'ase.calculators.mixing.SumCalculator',
385 is_wrapper=True,
386 )
387 reg('tersoff', 'Tersoff potential', 'ase.calculators.tersoff.Tersoff')
388 reg('tip3p', 'TIP3P', 'ase.calculators.tip3p.TIP3P')
389 reg('tip4p', 'TIP4P', 'ase.calculators.tip4p.TIP4P')
390 reg(
391 'turbomole',
392 'Turbomole',
393 'ase.calculators.turbomole.Turbomole',
394 configurable=True,
395 )
396 reg('vasp', 'VASP', 'ase.calculators.vasp.Vasp', configurable=True)
397 # internal: vdwcorrection # This only really works with GPAW, could move?
399 return codes
402codes = register_codes()
405def list_codes(names: list[str]):
406 from ase.config import cfg
408 cfg.print_header()
409 print()
411 for name in names:
412 code = codes[name]
413 print(code.name)
414 try:
415 print(code.description(indent=' '))
416 except Exception as ex:
417 print(f'Bad configuration of {name}: {ex!r}')
418 print()
421if __name__ == '__main__':
422 import sys
424 names = sys.argv[1:]
425 if not names:
426 names = [*codes]
427 list_codes(names)