def vasp_vol_relax(): Al = bulk('Al', 'fcc', a=4.5, cubic=True) calc = Vasp(xc='LDA', isif=7, nsw=5, ibrion=1, ediffg=-1e-3, lwave=False, lcharg=False) calc.calculate(Al) # Explicitly parse atomic position output file from Vasp CONTCAR_Al = io.read('CONTCAR', format='vasp') print('Stress after relaxation:\n', calc.read_stress()) print('Al cell post relaxation from calc:\n', calc.get_atoms().get_cell()) print('Al cell post relaxation from atoms:\n', Al.get_cell()) print('Al cell post relaxation from CONTCAR:\n', CONTCAR_Al.get_cell()) # All the cells should be the same. assert (calc.get_atoms().get_cell() == CONTCAR_Al.get_cell()).all() assert (Al.get_cell() == CONTCAR_Al.get_cell()).all() return Al
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()
def ase_vol_relax(): Al = bulk('Al', 'fcc', a=4.5, cubic=True) calc = Vasp(xc='LDA') Al.set_calculator(calc) from ase.constraints import StrainFilter sf = StrainFilter(Al) qn = QuasiNewton(sf, logfile='relaxation.log') qn.run(fmax=0.1, steps=5) print('Stress:\n', calc.read_stress()) print('Al post ASE volume relaxation\n', calc.get_atoms().get_cell()) return Al
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 atoms2 = 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
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()
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()