Coverage for /builds/kinetik161/ase/ase/calculators/autodetect.py: 19.35%
62 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-12-10 11:04 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-12-10 11:04 +0000
1import importlib.util
2import shutil
4from ase.calculators.calculator import names
5from ase.config import cfg
7builtins = {'eam', 'emt', 'ff', 'lj', 'morse', 'tip3p', 'tip4p'}
9required_envvars = {'abinit': ['ABINIT_PP_PATH'],
10 'elk': ['ELK_SPECIES_PATH'],
11 'openmx': ['OPENMX_DFT_DATA_PATH']}
13default_executables = {'abinit': ['abinit'],
14 'cp2k': ['cp2k_shell', 'cp2k_shell.psmp',
15 'cp2k_shell.popt', 'cp2k_shell.ssmp',
16 'cp2k_shell.sopt'],
17 'dftb': ['dftb+'],
18 'elk': ['elk', 'elk-lapw'],
19 'espresso': ['pw.x'],
20 'gamess_us': ['rungms'],
21 'gromacs': ['gmx', 'gmx_d', 'gmx_mpi', 'gmx_mpi_d'],
22 'lammpsrun': ['lammps', 'lmp', 'lmp_mpi', 'lmp_serial'],
23 'mopac': ['mopac', 'run_mopac7'], # run_mopac7: debian
24 'nwchem': ['nwchem'],
25 'octopus': ['octopus'],
26 'openmx': ['openmx'],
27 'psi4': ['psi4'],
28 'siesta': ['siesta'],
29 }
31python_modules = {'gpaw': 'gpaw',
32 'asap': 'asap3',
33 'lammpslib': 'lammps'}
36def get_executable_env_var(name):
37 return f'ASE_{name.upper()}_COMMAND'
40def detect(name):
41 assert name in names
42 d = {'name': name}
44 if name in builtins:
45 d['type'] = 'builtin'
46 return d
48 if name in python_modules:
49 spec = importlib.util.find_spec(python_modules[name])
50 if spec is not None:
51 d['type'] = 'python'
52 d['module'] = python_modules[name]
53 d['path'] = spec.loader.get_filename()
54 return d
56 envvar = get_executable_env_var(name)
57 if envvar in cfg:
58 d['command'] = cfg[envvar]
59 d['envvar'] = envvar
60 d['type'] = 'environment'
61 return d
63 if name in default_executables:
64 commands = default_executables[name]
65 for command in commands:
66 fullpath = shutil.which(command)
67 if fullpath:
68 d['command'] = command
69 d['fullpath'] = fullpath
70 d['type'] = 'which'
71 return d
74def detect_calculators():
75 configs = {}
76 for name in names:
77 result = detect(name)
78 if result:
79 configs[name] = result
80 return configs
83def format_configs(configs):
84 messages = []
85 for name in names:
86 config = configs.get(name)
88 if config is None:
89 state = 'no'
90 else:
91 type = config['type']
92 if type == 'builtin':
93 state = 'yes, builtin: module ase.calculators.{name}'
94 elif type == 'python':
95 state = 'yes, python: {module} ▶ {path}'
96 elif type == 'which':
97 state = 'yes, shell command: {command} ▶ {fullpath}'
98 else:
99 state = 'yes, environment: ${envvar} ▶ {command}'
101 state = state.format(**config)
103 messages.append(f'{name:<10s} {state}')
104 return messages