def main(): assert installed() # simple test calculation of CO molecule d = 1.14 co = Atoms('CO', positions=[(0, 0, 0), (0, 0, d)], pbc=True) co.center(vacuum=5.) calc = Vasp(xc='PBE', prec='Low', algo='Fast', ismear=0, sigma=1., istart=0, lwave=False, lcharg=False, ldipol=True) co.set_calculator(calc) energy = co.get_potential_energy() forces = co.get_forces() dipole_moment = co.get_dipole_moment() # check that parsing of vasprun.xml file works conf = read('vasprun.xml') assert conf.calc.parameters['kpoints_generation'] assert conf.calc.parameters['sigma'] == 1.0 assert conf.calc.parameters['ialgo'] == 68 assert energy - conf.get_potential_energy() == 0.0 assert np.allclose(conf.get_forces(), forces) assert np.allclose(conf.get_dipole_moment(), dipole_moment, atol=1e-6) # Cleanup calc.clean()
def test_vasp2_co(): """ Run some VASP tests to ensure that the VASP calculator works. This is conditional on the existence of the VASP_COMMAND or VASP_SCRIPT environment variables """ from ase.test.vasp import installed2 as installed assert installed() from ase import Atoms from ase.io import write from ase.calculators.vasp import Vasp2 as Vasp import numpy as np def array_almost_equal(a1, a2, tol=np.finfo(type(1.0)).eps): """Replacement for old numpy.testing.utils.array_almost_equal.""" return (np.abs(a1 - a2) < tol).all() d = 1.14 co = Atoms('CO', positions=[(0, 0, 0), (0, 0, d)], pbc=True) co.center(vacuum=5.) calc = Vasp(xc='PBE', prec='Low', algo='Fast', ismear=0, sigma=1., istart=0, lwave=False, lcharg=False) co.set_calculator(calc) en = co.get_potential_energy() write('vasp_co.traj', co) assert abs(en + 14.918933) < 5e-3 # Secondly, check that restart from the previously created VASP output works calc2 = Vasp(restart=True) co2 = calc2.get_atoms() # Need tolerance of 1e-14 because VASP itself changes coordinates # slightly between reading POSCAR and writing CONTCAR even if no ionic # steps are made. assert array_almost_equal(co.positions, co2.positions, 1e-14) assert en - co2.get_potential_energy() == 0. assert array_almost_equal(calc.get_stress(co), calc2.get_stress(co2)) assert array_almost_equal(calc.get_forces(co), calc2.get_forces(co2)) assert array_almost_equal(calc.get_eigenvalues(), calc2.get_eigenvalues()) assert calc.get_number_of_bands() == calc2.get_number_of_bands() assert calc.get_xc_functional() == calc2.get_xc_functional() # Cleanup calc.clean()
lwave=False, lcharg=False) calc = Vasp(**settings) atoms.set_calculator(calc) en1 = atoms.get_potential_energy() # Make a parameter change to the calculator calc.set(sigma=0.5) # Check that we capture a change for float params assert calc.check_state(atoms) == ['float_params'] assert calc.calculation_required(atoms, ['energy', 'forces']) en2 = atoms.get_potential_energy() # The change in sigma should result in a small change in energy assert (en1 - en2) > 1e-7 # Now we make a change in input_params instead calc.kpts = 2 # Check that this requires a new calculation assert calc.check_state(atoms) == ['input_params'] assert calc.calculation_required(atoms, ['energy', 'forces']) # Clean up calc.clean()
def test_vasp2_kpoints(require_vasp): """ Check the many ways of specifying KPOINTS """ import os from ase.calculators.vasp import Vasp2 as Vasp from ase.build import bulk from ase.test.calculator.vasp import installed2 as installed assert installed() Al = bulk('Al', 'fcc', a=4.5, cubic=True) def check_kpoints_line(n, contents): """Assert the contents of a line""" with open('KPOINTS', 'r') as f: lines = f.readlines() assert lines[n] == contents # Default to (1 1 1) calc = Vasp(gamma=True) calc.write_kpoints() check_kpoints_line(2, 'Gamma\n') check_kpoints_line(3, '1 1 1 \n') calc.clean() # 3-tuple prints mesh calc = Vasp(gamma=False, kpts=(4, 4, 4)) calc.write_kpoints() check_kpoints_line(2, 'Monkhorst-Pack\n') check_kpoints_line(3, '4 4 4 \n') calc.clean() # Auto mode calc = Vasp(kpts=20) calc.write_kpoints() check_kpoints_line(1, '0\n') check_kpoints_line(2, 'Auto\n') check_kpoints_line(3, '20 \n') calc.clean() # 1-element list ok, Gamma ok calc = Vasp(kpts=[20], gamma=True) calc.write_kpoints() check_kpoints_line(1, '0\n') check_kpoints_line(2, 'Auto\n') check_kpoints_line(3, '20 \n') calc.clean() # KSPACING suppresses KPOINTS file calc = Vasp(kspacing=0.23) calc.initialize(Al) calc.write_kpoints() calc.write_incar(Al) assert not os.path.isfile('KPOINTS') with open('INCAR', 'r') as f: assert ' KSPACING = 0.230000\n' in f.readlines() calc.clean() # Negative KSPACING raises an error calc = Vasp(kspacing=-0.5) try: calc.write_kpoints() except ValueError: pass else: raise AssertionError("Negative KSPACING did not raise ValueError") calc.clean() # Explicit weighted points with nested lists, Cartesian if not specified calc = Vasp( kpts=[[0.1, 0.2, 0.3, 2], [0.0, 0.0, 0.0, 1], [0.0, 0.5, 0.5, 2]]) calc.write_kpoints() with open('KPOINTS.ref', 'w') as f: f.write("""KPOINTS created by Atomic Simulation Environment 3 Cartesian 0.100000 0.200000 0.300000 2.000000 0.000000 0.000000 0.000000 1.000000 0.000000 0.500000 0.500000 2.000000 """) assert filecmp_ignore_whitespace('KPOINTS', 'KPOINTS.ref') os.remove('KPOINTS.ref') # Explicit points as list of tuples, automatic weighting = 1. calc = Vasp( kpts=[(0.1, 0.2, 0.3), (0.0, 0.0, 0.0), (0.0, 0.5, 0.5)], reciprocal=True) calc.write_kpoints() with open('KPOINTS.ref', 'w') as f: f.write("""KPOINTS created by Atomic Simulation Environment 3 Reciprocal 0.100000 0.200000 0.300000 1.0 0.000000 0.000000 0.000000 1.0 0.000000 0.500000 0.500000 1.0 """) assert filecmp_ignore_whitespace('KPOINTS', 'KPOINTS.ref') os.remove('KPOINTS.ref')
def test_vasp2_check_state(): """ Run tests to ensure that the VASP check_state() function call works correctly, i.e. correctly sets the working directories and works in that directory. This is conditional on the existence of the VASP_COMMAND or VASP_SCRIPT environment variables """ from ase.test.vasp import installed2 as installed import os from ase import Atoms from ase.calculators.vasp import Vasp2 as Vasp assert installed() # Test setup system, borrowed from vasp_co.py d = 1.14 atoms = Atoms('CO', positions=[(0, 0, 0), (0, 0, d)], pbc=True) atoms.extend(Atoms('CO', positions=[(0, 2, 0), (0, 2, d)])) atoms.center(vacuum=5.) # Test settings = dict(xc='LDA', prec='Low', algo='Fast', ismear=0, sigma=1., istart=0, lwave=False, lcharg=False) s1 = atoms.get_chemical_symbols() calc = Vasp(**settings) atoms.set_calculator(calc) en1 = atoms.get_potential_energy() # Test JSON dumping and restarting works fi = 'json_test.json' calc.write_json(filename=fi) assert os.path.isfile(fi) calc2 = Vasp() calc2.read_json(fi) assert not calc2.calculation_required(atoms, ['energy', 'forces']) en2 = calc2.get_potential_energy() assert abs(en1 - en2) < 1e-8 os.remove(fi) # Clean up the JSON file # Check that the symbols remain in order (non-sorted) s2 = calc.atoms.get_chemical_symbols() assert s1 == s2 s3 = sorted(s2) assert s2 != s3 # Check that get_atoms() doesn't reset results r1 = dict(calc.results) # Force a copy calc.get_atoms() r2 = dict(calc.results) assert r1 == r2 # Make a parameter change to the calculator calc.set(sigma=0.5) # Check that we capture a change for float params assert calc.check_state(atoms) == ['float_params'] assert calc.calculation_required(atoms, ['energy', 'forces']) en2 = atoms.get_potential_energy() # The change in sigma should result in a small change in energy assert (en1 - en2) > 1e-7 # Now we make a change in input_params instead calc.kpts = 2 # Check that this requires a new calculation assert calc.check_state(atoms) == ['input_params'] assert calc.calculation_required(atoms, ['energy', 'forces']) # Clean up calc.clean()