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

1import importlib.util 

2import shutil 

3 

4from ase.calculators.calculator import names 

5from ase.config import cfg 

6 

7builtins = {'eam', 'emt', 'ff', 'lj', 'morse', 'tip3p', 'tip4p'} 

8 

9required_envvars = {'abinit': ['ABINIT_PP_PATH'], 

10 'elk': ['ELK_SPECIES_PATH'], 

11 'openmx': ['OPENMX_DFT_DATA_PATH']} 

12 

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 } 

30 

31python_modules = {'gpaw': 'gpaw', 

32 'asap': 'asap3', 

33 'lammpslib': 'lammps'} 

34 

35 

36def get_executable_env_var(name): 

37 return f'ASE_{name.upper()}_COMMAND' 

38 

39 

40def detect(name): 

41 assert name in names 

42 d = {'name': name} 

43 

44 if name in builtins: 

45 d['type'] = 'builtin' 

46 return d 

47 

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 

55 

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 

62 

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 

72 

73 

74def detect_calculators(): 

75 configs = {} 

76 for name in names: 

77 result = detect(name) 

78 if result: 

79 configs[name] = result 

80 return configs 

81 

82 

83def format_configs(configs): 

84 messages = [] 

85 for name in names: 

86 config = configs.get(name) 

87 

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}' 

100 

101 state = state.format(**config) 

102 

103 messages.append(f'{name:<10s} {state}') 

104 return messages