def test_energy_forces_stress(): """ To test that the calculator can produce correct energy and forces. This is done by comparing the energy for an FCC argon lattice with an example model to the known value; the forces/stress returned by the model are compared to numerical estimates via finite difference. """ import numpy as np from pytest import importorskip importorskip('kimpy') from ase.calculators.kim import KIM from ase.lattice.cubic import FaceCenteredCubic # Create an FCC atoms crystal atoms = FaceCenteredCubic( directions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]], size=(1, 1, 1), symbol="Ar", pbc=(1, 0, 0), latticeconstant=3.0, ) # Perturb the x coordinate of the first atom by less than the cutoff distance atoms.positions[0, 0] += 0.01 calc = KIM("ex_model_Ar_P_Morse_07C") atoms.set_calculator(calc) # Get energy and analytical forces/stress from KIM model energy = atoms.get_potential_energy() forces = atoms.get_forces() stress = atoms.get_stress() # Previously computed energy for this configuration for this model energy_ref = 19.7196709065 # eV # Compute forces and virial stress numerically forces_numer = calc.calculate_numerical_forces(atoms, d=0.0001) stress_numer = calc.calculate_numerical_stress(atoms, d=0.0001, voigt=True) tol = 1e-6 assert np.isclose(energy, energy_ref, tol) assert np.allclose(forces, forces_numer, tol) assert np.allclose(stress, stress_numer, tol) # This has been known to segfault atoms.set_pbc(True) atoms.get_potential_energy()
def test_lammpslib_change_cell_bcs(factory, lattice_params, calc_params_NiH): """Test that a change in unit cell boundary conditions is handled correctly by lammpslib""" atoms = FaceCenteredCubic(**lattice_params) calc = factory.calc(**calc_params_NiH) atoms.calc = calc energy_ppp_ref = -142.400000403 energy_ppp = atoms.get_potential_energy() print("Computed energy with boundary ppp = {}".format(energy_ppp)) assert energy_ppp == pytest.approx(energy_ppp_ref, rel=1e-4) atoms.set_pbc((False, False, True)) energy_ssp_ref = -114.524625705 energy_ssp = atoms.get_potential_energy() print("Computed energy with boundary ssp = {}".format(energy_ssp)) assert energy_ssp == pytest.approx(energy_ssp_ref, rel=1e-4)
def test_lammpslib_change_cell_bcs(): # test that a change in unit cell boundary conditions is # handled correctly by lammpslib import numpy as np from ase.calculators.lammpslib import LAMMPSlib from ase.lattice.cubic import FaceCenteredCubic cmds = ["pair_style eam/alloy", "pair_coeff * * NiAlH_jea.eam.alloy Ni H"] lammps = LAMMPSlib(lmpcmds=cmds, atom_types={ 'Ni': 1, 'H': 2 }, log_file='test.log', keep_alive=True) atoms = FaceCenteredCubic(size=(2, 2, 2), latticeconstant=3.52, symbol="Ni", pbc=True) atoms.calc = lammps energy_ppp_ref = -142.400000403 energy_ppp = atoms.get_potential_energy() print("Computed energy with boundary ppp = {}".format(energy_ppp)) np.testing.assert_allclose(energy_ppp, energy_ppp_ref, atol=1e-4, rtol=1e-4) atoms.set_pbc((False, False, True)) energy_ssp_ref = -114.524625705 energy_ssp = atoms.get_potential_energy() print("Computed energy with boundary ssp = {}".format(energy_ssp)) np.testing.assert_allclose(energy_ssp, energy_ssp_ref, atol=1e-4, rtol=1e-4)
def get_structure(self, name, elements, a=None, c=None, l=None): # Check number of elements if name[:3] in ['fcc', 'hcp']: if len(elements) != 1: raise ValueError("Tuple of elements must be of length one") if name[:3] in ['l12', 'l10'] or name[:2] == 'B2': if len(elements) != 2: raise ValueError("Tuple of elements must be of length two") # Get lattice constants if a is None: if name[:2] == 'B2': a = self.get_lattice_constant_a(name[:2], elements) elif name[:3] in ['fcc', 'hcp', 'bcc', 'l12', 'l10']: a = self.get_lattice_constant_a(name[:3], elements) if c is None: if name[:3] in ['hcp', 'l10']: c = self.get_lattice_constant_c(name[:3], elements) # Get size if name in ['fcc', 'hcp', 'bcc', 'l12', 'l10', 'B2']: size = self.properties[name + '_size'] elif name in ['fcc100', 'fcc111', 'hcp0001']: size = self.properties[name + '_size'][:2] + (l, ) # Make structure if name == 'fcc': atoms = FaceCenteredCubic(symbol=elements[0], size=size, latticeconstant=a) elif name == 'hcp': atoms = HexagonalClosedPacked(symbol=elements[0], size=size, directions=[[2, -1, -1, 0], [0, 1, -1, 0], [0, 0, 0, 1]], latticeconstant=(a, c)) elif name == 'bcc': atoms = BodyCenteredCubic(symbol=elements[0], size=size, latticeconstant=a) elif name == 'B2': atoms = B2(symbol=elements, size=size, latticeconstant=a) elif name == 'l12': atoms = L1_2(symbol=elements, size=size, latticeconstant=a) elif name == 'l10': atoms = L1_0(symbol=elements, size=size, latticeconstant=(a, c)) elif name == 'fcc100': atoms = fcc100(symbol=elements[0], size=size, a=a, vacuum=10.0) elif name == 'fcc111': atoms = fcc111(symbol=elements[0], size=size, a=a, vacuum=10.0, orthogonal=True) elif name == 'hcp0001': atoms = hcp0001(symbol=elements[0], size=size, a=a, c=c, vacuum=10.0, orthogonal=True) elif name == 'hcp1010A': raise ValueError("Structure '%s' not supported" % (name, )) atoms = None elif name == 'hcp1010B': raise ValueError("Structure '%s' not supported" % (name, )) atoms = None elif name == 'l12100': n = (l + 1) / 2 atoms = L1_2(symbol=elements, size=(8, 8, n), latticeconstant=a) atoms.set_pbc([True, True, False]) # Remove layers atoms = atoms[atoms.get_positions()[:, 2] > 0.1 * a] # Set vacuum atoms.center(axis=2, vacuum=10.0) elif name == 'l12111': if l % 3 == 0: n = l / 3 c = 0 else: n = l / 3 + 1 c = 3 - l % 3 atoms = L1_2( symbol=elements, size=(8, 4, n), #directions=[[1,-1,0],[1,0,-1],[1,1,1]], latticeconstant=a) directions=[[1, -1, 0], [1, 1, -2], [1, 1, 1]], latticeconstant=a) atoms.set_pbc([True, True, False]) # Wrap positions scpos = atoms.get_scaled_positions() scpos[scpos > (1.0 - 1e-12)] = 0.0 atoms.set_scaled_positions(scpos) # Remove layers if c > 0: atoms = atoms[atoms.get_positions()[:, 2] > (c - 0.5) * a / np.sqrt(3.0)] # Set vacuum atoms.center(axis=2, vacuum=10.0) else: raise ValueError("Structure '%s' not supported" % (name, )) return atoms
from numpy import * symbol = 'Ar' cells = 15 ar = FaceCenteredCubic(symbol, pbc=[(1, 1, 1)], directions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]], size=[cells, cells, 1]) for m in listmodels(): if symbol in m: try: print "Try model: ", m calc1 = KIMCalculator(m) ar.set_calculator(calc1) N = ar.get_number_of_atoms() ar.set_pbc([(1, 1, 1)]) bulk_energy = ar.get_potential_energy() / N ar.set_pbc([(1, 1, 0)]) surf_energy = ar.get_potential_energy() / N print "\tsurface = ", surf_energy - bulk_energy #virial = ar.get_stresses() #print "\tvirial = ", virial print except SupportError, e: print "\tskipping ", m, "\n\t", e, " ...\n" continue
from asap3 import * from ase.lattice.cubic import FaceCenteredCubic from numpy import * from asap3.testtools import ReportTest print_version(1) #set_verbose(1) atoms = FaceCenteredCubic(directions=[[1,0,0],[0,1,0],[0,0,1]], size=(6,6,6), symbol="Cu", pbc=(1,1,0)) atoms.set_calculator(EMT()) ecorrect = atoms.get_potential_energy() atoms2 = FaceCenteredCubic(directions=[[1,0,0],[0,1,0],[0,0,1]], size=(6,6,6), symbol="Cu", pbc=(0,0,0)) atoms2.set_pbc((1,1,0)) atoms2.set_calculator(EMT()) e1= atoms2.get_potential_energy() ReportTest("e1 correct", e1, ecorrect, 0.001) atoms.set_pbc((0,1,0)) atoms.set_pbc((1,1,0)) e2 = atoms.get_potential_energy() ReportTest("e2 correct", e2, ecorrect, 0.001) print "Setting pbc" atoms.set_pbc((0,1,0)) print "Calculating energy" dummy = atoms.get_potential_energy() assert(fabs(dummy - ecorrect) > 1.0) atoms.set_pbc((1,1,0))
radius = 15 size = [14, 10, 100] # radius = 30 # size = [28, 20, 100] # 51500 atoms a = FaceCenteredCubic('Au', directions=[[1, 0, -1], [0, 1, 0], [1, 0, 1]], size=size) c = a.cell.diagonal() / 2 dir1 = [sqrt(2), 1, 0] dir2 = [sqrt(2), -1, 0] dir3 = [0, 1, 0] dir1 = np.array(dir1) / np.linalg.norm(dir1) dir2 = np.array(dir2) / np.linalg.norm(dir2) dir3 = np.array(dir3) / np.linalg.norm(dir3) r = a.get_positions() - c m = np.abs(r.dot(dir1)) > radius m = np.logical_or(m, np.abs(r.dot(dir2)) > radius) m = np.logical_or(m, np.abs(r.dot(dir3)) > radius) del a[m] a.center() a.set_pbc([False, False, True]) io.write('whisker.xyz', a) io.write('whisker.data', a, format='lammps-data')
from kimcalculator import * from numpy import * symbol = 'Ar' cells = 15 ar = FaceCenteredCubic(symbol, pbc=[(1,1,1)], directions=[[1,0,0],[0,1,0],[0,0,1]], size=[cells,cells,1]) for m in listmodels(): if symbol in m: try: print "Try model: ", m calc1 = KIMCalculator(m) ar.set_calculator(calc1) N = ar.get_number_of_atoms() ar.set_pbc([(1,1,1)]) bulk_energy = ar.get_potential_energy() / N ar.set_pbc([(1,1,0)]) surf_energy = ar.get_potential_energy() / N print "\tsurface = ", surf_energy - bulk_energy #virial = ar.get_stresses() #print "\tvirial = ", virial print except SupportError, e: print "\tskipping ", m, "\n\t", e, " ...\n" continue
pbc=(1, 0, 0), latticeconstant=3.0, ) # Perturb the x coordinate of the first atom by less than the cutoff distance atoms.positions[0, 0] += 0.01 calc = KIM("ex_model_Ar_P_Morse_07C") atoms.set_calculator(calc) # Get energy and analytical forces/stress from KIM model energy = atoms.get_potential_energy() forces = atoms.get_forces() stress = atoms.get_stress() # Previously computed energy for this configuration for this model energy_ref = 19.7196709065 # eV # Compute forces and virial stress numerically forces_numer = calc.calculate_numerical_forces(atoms, d=0.0001) stress_numer = calc.calculate_numerical_stress(atoms, d=0.0001, voigt=True) tol = 1e-6 assert np.isclose(energy, energy_ref, tol) assert np.allclose(forces, forces_numer, tol) assert np.allclose(stress, stress_numer, tol) # This has been known to segfault atoms.set_pbc(True) atoms.get_potential_energy()
# test that a change in unit cell boundary conditions is # handled correctly by lammpslib import numpy as np from ase.calculators.lammpslib import LAMMPSlib from ase.lattice.cubic import FaceCenteredCubic cmds = ["pair_style eam/alloy", "pair_coeff * * NiAlH_jea.eam.alloy Ni H"] lammps = LAMMPSlib(lmpcmds=cmds, atom_types={ 'Ni': 1, 'H': 2 }, log_file='test.log', keep_alive=True) atoms = FaceCenteredCubic(size=(2, 2, 2), latticeconstant=3.52, symbol="Ni", pbc=True) atoms.set_calculator(lammps) energy_ppp_ref = -142.400000403 energy_ppp = atoms.get_potential_energy() print("Computed energy with boundary ppp = {}".format(energy_ppp)) np.testing.assert_allclose(energy_ppp, energy_ppp_ref, atol=1e-4, rtol=1e-4) atoms.set_pbc((False, False, True)) energy_ssp_ref = -114.524625705 energy_ssp = atoms.get_potential_energy() print("Computed energy with boundary ssp = {}".format(energy_ssp)) np.testing.assert_allclose(energy_ssp, energy_ssp_ref, atol=1e-4, rtol=1e-4)
if nbltype.startswith("MI_OPBC"): boundaries = ((1, 1, 1), ) elif nbltype == "CLUSTER": boundaries = ((0, 0, 0), ) else: boundaries = ((1, 1, 1), (0, 0, 0), (0, 0, 1)) for pbc in boundaries: txt = nbltype + (" PBC=%1i%1i%1i " % pbc) # Test that EMT reimported through OpenKIM gives the right results. atoms_kim = FaceCenteredCubic(size=(10, 10, 10), symbol='Cu') #atoms_kim = FaceCenteredCubic(directions=[[1,0,0],[0,1,0],[0,0,1]], # size=(30, 30, 30), # symbol="Cu") natoms = len(atoms_kim) atoms_kim.set_pbc(pbc) r = atoms_kim.get_positions() r.flat[:] += 0.1 * np.sin(np.arange(3 * natoms)) atoms_kim.set_positions(r) atoms_emt = atoms_kim.copy() kim = OpenKIMcalculator(openkimmodel, allowed=nbltype) emt = EMT() emt.set_subtractE0(False) atoms_kim.set_calculator(kim) atoms_emt.set_calculator(emt) ek = atoms_kim.get_potential_energy() ee = atoms_emt.get_potential_energy() ReportTest(txt + "Total energy", ek, ee, 1e-8) ek = atoms_kim.get_potential_energies() ee = atoms_emt.get_potential_energies() for i in range(0, natoms, step):
if nbltype.startswith("MI_OPBC"): boundaries = ((1,1,1),) elif nbltype == "CLUSTER": boundaries = ((0,0,0),) else: boundaries = ((1,1,1), (0,0,0), (0,0,1)) for pbc in boundaries: txt = nbltype + (" PBC=%1i%1i%1i " % pbc) # Test that EMT reimported through OpenKIM gives the right results. atoms_kim = FaceCenteredCubic(size=(10,10,10), symbol='Cu') #atoms_kim = FaceCenteredCubic(directions=[[1,0,0],[0,1,0],[0,0,1]], # size=(30, 30, 30), # symbol="Cu") natoms = len(atoms_kim) atoms_kim.set_pbc(pbc) r = atoms_kim.get_positions() r.flat[:] += 0.1 * np.sin(np.arange(3*natoms)) atoms_kim.set_positions(r) atoms_emt = atoms_kim.copy() kim = OpenKIMcalculator(openkimmodel, allowed=nbltype) emt = EMT() emt.set_subtractE0(False) atoms_kim.set_calculator(kim) atoms_emt.set_calculator(emt) ek = atoms_kim.get_potential_energy() ee = atoms_emt.get_potential_energy() ReportTest(txt+"Total energy", ek, ee, 1e-8) ek = atoms_kim.get_potential_energies() ee = atoms_emt.get_potential_energies() for i in range(0, natoms, step):