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
|
"""Test module for explicitly unittesting parts of the VASP calculator"""
import pytest
from ase.build import molecule
from ase.calculators.calculator import CalculatorSetupError, get_calculator_class
from ase.calculators.vasp import Vasp
from ase.calculators.vasp.vasp import check_atoms, check_pbc, check_cell, check_atoms_type
@pytest.fixture
def atoms():
return molecule('H2', vacuum=5, pbc=True)
def test_check_atoms(atoms, mock_vasp_calculate):
"""Test checking atoms passes for a good atoms object"""
check_atoms(atoms)
check_pbc(atoms)
check_cell(atoms)
@pytest.mark.parametrize(
'bad_atoms',
[
None,
'a_string',
# We cannot handle lists of atoms either
[molecule('H2', vacuum=5)],
])
def test_not_atoms(bad_atoms, mock_vasp_calculate):
"""Check that passing in objects which are not
actually Atoms objects raises a setup error """
with pytest.raises(CalculatorSetupError):
check_atoms_type(bad_atoms)
with pytest.raises(CalculatorSetupError):
check_atoms(bad_atoms)
# Test that error is also raised properly when launching
# from calculator
calc = Vasp()
with pytest.raises(CalculatorSetupError):
calc.calculate(atoms=bad_atoms)
@pytest.mark.parametrize('pbc', [
3 * [False],
[True, False, True],
[False, True, False],
])
def test_bad_pbc(atoms, pbc, mock_vasp_calculate):
"""Test handling of PBC"""
atoms.pbc = pbc
check_cell(atoms) # We have a cell, so this should not raise
# Check that our helper functions raises the expected error
with pytest.raises(CalculatorSetupError):
check_pbc(atoms)
with pytest.raises(CalculatorSetupError):
check_atoms(atoms)
# Check we also raise in the calculator when launching
# a calculation, but before VASP is actually executed
calc = Vasp()
atoms.calc = calc
with pytest.raises(CalculatorSetupError):
atoms.get_potential_energy()
def test_vasp_no_cell(mock_vasp_calculate):
"""Check missing cell handling."""
# Molecules come with no unit cell
atoms = molecule('CH4')
# We should not have a cell
assert atoms.cell.rank == 0
with pytest.raises(CalculatorSetupError):
check_cell(atoms)
with pytest.raises(CalculatorSetupError):
check_atoms(atoms)
with pytest.raises(RuntimeError):
atoms.write('POSCAR')
calc = Vasp()
atoms.calc = calc
with pytest.raises(CalculatorSetupError):
atoms.get_total_energy()
def test_vasp_name(mock_vasp_calculate):
"""Test the calculator class has the expected name"""
expected = 'vasp'
assert Vasp.name == expected # Test class attribute
assert Vasp().name == expected # Ensure instance attribute hasn't changed
def test_vasp_get_calculator(mock_vasp_calculate):
cls_ = get_calculator_class('vasp')
assert cls_ == Vasp
# Test we get the correct calculator when loading from name
assert get_calculator_class(Vasp.name) == cls_
|