1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
# fmt: off
import os
import subprocess
import pytest
from ase import Atoms
from ase.calculators.calculator import CalculatorSetupError
"""
These tests monkeypatch Popen so as to abort execution and verify that
a particular command as executed.
They test several cases:
* command specified by environment
* command specified via keyword
* command not specified, with two possible behaviours:
- command defaults to particular value
- calculator raises CalculatorSetupError
(We do not bother to check e.g. conflicting combinations.)
"""
class InterceptedCommand(BaseException):
def __init__(self, command):
self.command = command
def mock_popen(command, shell=False, cwd=None, **kwargs):
assert isinstance(command, str) is shell
raise InterceptedCommand(command)
# Other calculators:
# * cp2k uses command but is not FileIOCalculator
# * turbomole hardcodes multiple commands but does not use command keyword
# Parameters for each calculator -- whatever it takes trigger a calculation
# without crashing first.
calculators = {
'ace': {},
'amber': {},
'castep': dict(keyword_tolerance=3),
'cp2k': {},
'crystal': {},
'demon': dict(basis_path='hello'),
'demonnano': dict(input_arguments={},
basis_path='hello'),
'dftb': {},
'dftd3': {},
'dmol': {},
'gamess_us': {},
'gaussian': {},
'gromacs': {},
'gulp': {},
'lammpsrun': {},
'mopac': {},
'nwchem': {},
'onetep': {},
'openmx': dict(data_path='.', dft_data_year='13'),
'psi4': {},
'qchem': {},
'siesta': dict(pseudo_path='.'),
'turbomole': {},
'vasp': {},
}
@pytest.fixture(autouse=True)
def miscellaneous_hacks(monkeypatch, tmp_path):
from ase.calculators.calculator import FileIOCalculator
from ase.calculators.crystal import CRYSTAL
from ase.calculators.demon import Demon
from ase.calculators.dftb import Dftb
from ase.calculators.gamess_us import GAMESSUS
from ase.calculators.gulp import GULP
from ase.calculators.openmx import OpenMX
from ase.calculators.siesta.siesta import FDFWriter
from ase.calculators.vasp import Vasp
def do_nothing(returnval=None):
def mock_function(*args, **kwargs):
return returnval
return mock_function
# Monkeypatches can be pretty dangerous because someone might obtain
# a reference to the monkeypatched value before the patch is undone.
#
# We should try to refactor so we can avoid all the monkeypatches.
monkeypatch.setattr(Demon, 'link_file', do_nothing())
monkeypatch.setattr(CRYSTAL, '_write_crystal_in', do_nothing())
monkeypatch.setattr(Dftb, 'write_dftb_in', do_nothing())
# It calls super, but we'd like to skip the userscr handling:
monkeypatch.setattr(GAMESSUS, 'calculate', FileIOCalculator.calculate)
monkeypatch.setattr(GULP, 'library_check', do_nothing())
# Attempts to read too many files.
monkeypatch.setattr(OpenMX, 'write_input', do_nothing())
monkeypatch.setattr(FDFWriter, 'link_pseudos_into_directory', do_nothing())
monkeypatch.setattr(Vasp, '_build_pp_list', do_nothing(returnval=[]))
def mkcalc(name, **kwargs):
from ase.calculators.calculator import get_calculator_class
cls = get_calculator_class(name)
kwargs = {**calculators[name], **kwargs}
return cls(**kwargs)
@pytest.fixture(autouse=True)
def mock_subprocess_popen(monkeypatch):
monkeypatch.setattr(subprocess, 'Popen', mock_popen)
def intercept_command(name, **kwargs):
atoms = Atoms('H', pbc=True)
atoms.center(vacuum=3.0)
try:
# cp2k runs cp2k_shell already in the constructor, and it has a right
# to choose to do so. Maybe other calculators do this as well.
# So we include both lines in "try".
atoms.calc = mkcalc(name, **kwargs)
atoms.get_potential_energy()
except InterceptedCommand as err:
print(err.command)
return err.command
envvars = {
'ace': 'ASE_ACE_COMMAND',
'amber': 'ASE_AMBER_COMMAND',
'castep': 'CASTEP_COMMAND',
'cp2k': 'ASE_CP2K_COMMAND',
'crystal': 'ASE_CRYSTAL_COMMAND',
'demon': 'ASE_DEMON_COMMAND',
'demonnano': 'ASE_DEMONNANO_COMMAND',
'dftb': 'DFTB_COMMAND',
'dftd3': 'ASE_DFTD3_COMMAND',
'dmol': 'DMOL_COMMAND', # XXX Crashes when it runs along other tests
'gamess_us': 'ASE_GAMESSUS_COMMAND',
'gaussian': 'ASE_GAUSSIAN_COMMAND',
'gromacs': 'ASE_GROMACS_COMMAND',
'gulp': 'ASE_GULP_COMMAND',
'lammpsrun': 'ASE_LAMMPSRUN_COMMAND',
'mopac': 'ASE_MOPAC_COMMAND',
'nwchem': 'ASE_NWCHEM_COMMAND',
# 'onetep': 'ASE_ONETEP_COMMAND', do we need to be compatible here?
'openmx': 'ASE_OPENMX_COMMAND', # fails in get_dft_data_year
# 'psi4', <-- has command but is Calculator
# 'qchem': 'ASE_QCHEM_COMMAND', # ignores environment
'siesta': 'ASE_SIESTA_COMMAND',
# 'turbomole': turbomole is not really a calculator
'vasp': 'ASE_VASP_COMMAND',
}
dftd3_boilerplate = (
'ase_dftd3.POSCAR -func pbe -grad -pbc -cnthr 40.0 -cutoff 95.0 -zero')
def get_expected_command(command, name, tmp_path, from_envvar):
if name == 'castep':
return f'{command} castep' # crazy
if name == 'dftb' and from_envvar:
# dftb modifies DFTB_COMMAND from envvar but not if given as keyword
return f'{command} > dftb.out'
if name == 'dftd3':
return f'{command} {dftd3_boilerplate}'.split()
if name == 'dmol' and from_envvar:
return f'{command} tmp > tmp.out'
if name == 'gromacs':
return (f'{command} mdrun -s gromacs.tpr -o gromacs.trr '
'-e gromacs.edr -g gromacs.log -c gromacs.g96 > MM.log 2>&1')
if name == 'lammpsrun':
# lammpsrun does not use a shell command
return [*command.split(), '-echo', 'log',
'-screen', 'none', '-log', '/dev/stdout']
if name == 'onetep':
return [*command.split(), 'onetep.dat']
if name == 'openmx':
# openmx converts the stream target to an abspath, so the command
# will vary depending on the tempdir we're running in.
return f'{command} openmx.dat > {os.path.join(tmp_path, "openmx.log")}'
return command
@pytest.mark.parametrize('name', list(envvars))
def test_envvar_command(monkeypatch, name, tmp_path):
command = 'dummy shell command from environment'
if name == 'cp2k':
command += 'cp2k_shell' # circumvent sanity check
expected_command = get_expected_command(command, name, tmp_path,
from_envvar=True)
monkeypatch.setenv(envvars[name], command)
actual_command = intercept_command(name)
assert actual_command == expected_command
def keyword_calculator_list():
skipped = {
'turbomole', # commands are hardcoded in turbomole
'qchem', # qchem does something entirely different. wth
'psi4', # needs external package
'onetep', # (takes profile)
}
return sorted(set(calculators) - skipped)
# castep uses another keyword than normal
command_keywords = {'castep': 'castep_command'}
@pytest.mark.parametrize('name', keyword_calculator_list())
def test_keyword_command(name, tmp_path):
command = 'dummy command via keyword'
if name == 'cp2k':
command += ' cp2k_shell' # circumvent sanity check
expected_command = get_expected_command(command, name, tmp_path,
from_envvar=False)
# normally {'command': command}
commandkwarg = {command_keywords.get(name, 'command'): command}
print(intercept_command(name, **commandkwarg))
assert intercept_command(name, **commandkwarg) == expected_command
# Calculators that (somewhat unwisely) have a hardcoded default command
default_commands = {
'amber': ('sander -O -i mm.in -o mm.out -p mm.top -c mm.crd '
'-r mm_dummy.crd'),
'castep': 'castep castep', # wth?
'cp2k': 'cp2k_shell',
'dftb': 'dftb+ > dftb.out',
'dftd3': f'dftd3 {dftd3_boilerplate}'.split(),
'gamess_us': 'rungms gamess_us.inp > gamess_us.log 2> gamess_us.err',
'gaussian': 'g16 < Gaussian.com > Gaussian.log',
'gulp': 'gulp < gulp.gin > gulp.got',
'lammpsrun': ['lammps', '-echo', 'log', '-screen', 'none',
'-log', '/dev/stdout'],
'mopac': 'mopac mopac.mop 2> /dev/null',
'nwchem': 'nwchem nwchem.nwi > nwchem.nwo',
# 'openmx': '', # command contains full path which is variable
'qchem': 'qchem qchem.inp qchem.out',
'siesta': 'siesta < siesta.fdf > siesta.out',
}
# Calculators that raise error if command not set
calculators_which_raise = [
'ace',
'demonnano',
'crystal',
'demon',
'dmol',
'gromacs',
# Fixme: onetep raises AttributError
# Should be handled universally by GenericFileIO system
'vasp',
]
@pytest.mark.parametrize('name', list(default_commands))
def test_nocommand_default(name, monkeypatch, override_config):
if name in envvars:
monkeypatch.delenv(envvars[name], raising=False)
assert intercept_command(name) == default_commands[name]
@pytest.mark.parametrize('name', calculators_which_raise)
def test_nocommand_raise(name, monkeypatch, override_config):
if name in envvars:
monkeypatch.delenv(envvars[name], raising=False)
with pytest.raises(CalculatorSetupError):
intercept_command(name)
|