def test_main(): # create calculator #modelname = 'ex_model_Ar_P_MLJ_C' modelname = 'ex_model_Ar_P_Morse_07C' calc = KIMCalculator(modelname, debug=True) # create an FCC crystal argon = 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 coords of the first atoms argon.positions[0,0] += 0.01 # attach the calculator to the atoms object argon.set_calculator(calc) for i in range(4): print ('step', i) # get energy and forces energy = argon.get_potential_energy() forces = argon.get_forces() # rigidly move the atoms argon.positions[:,0] += 1.629/2. # the cutoff skin is 1.63 # create an FCC crystal with no periodic BC argon = FaceCenteredCubic(directions=[[1,0,0], [0,1,0], [0,0,1]], size=(2,1,1), symbol='Ar', pbc=(0,0,0), latticeconstant=3.0) # attach the SAME calculator to the new atoms object argon.set_calculator(calc) for i in range(4): print('step', i) # get energy and forces energy = argon.get_potential_energy() forces = argon.get_forces() # rigidly move the atoms argon.positions[:,0] += 1.631/2. # the cutoff skin is 1.63
def test_forces_random_structure(self): atoms = FaceCenteredCubic('H', size=[2,2,2], latticeconstant=2.37126) calc = Polydisperse(InversePowerLawPotential(1.0, 1.4, 0.1, 3, 1, 2.22)) atoms.set_masses(masses=np.repeat(1.0, len(atoms))) atoms.set_array("size", np.random.uniform(1.0, 2.22, size=len(atoms)), dtype=float) atoms.set_calculator(calc) f = atoms.get_forces() fn = calc.calculate_numerical_forces(atoms, d=0.0001) self.assertArrayAlmostEqual(f, fn, tol=self.tol)
def test_direct_evaluation(self): a = FaceCenteredCubic('Au', size=[2,2,2]) a.rattle(0.1) calc = EAM('Au-Grochola-JCP05.eam.alloy') a.set_calculator(calc) f = a.get_forces() calc2 = EAM('Au-Grochola-JCP05.eam.alloy') i_n, j_n, dr_nc, abs_dr_n = neighbour_list('ijDd', a, cutoff=calc2.cutoff) epot, virial, f2 = calc2.energy_virial_and_forces(a.numbers, i_n, j_n, dr_nc, abs_dr_n) self.assertArrayAlmostEqual(f, f2) a = FaceCenteredCubic('Cu', size=[2,2,2]) calc = EAM('CuAg.eam.alloy') a.set_calculator(calc) FIRE(StrainFilter(a, mask=[1,1,1,0,0,0]), logfile=None).run(fmax=0.001) e_Cu = a.get_potential_energy()/len(a) a = FaceCenteredCubic('Ag', size=[2,2,2]) a.set_calculator(calc) FIRE(StrainFilter(a, mask=[1,1,1,0,0,0]), logfile=None).run(fmax=0.001) e_Ag = a.get_potential_energy()/len(a) self.assertTrue(abs(e_Ag+2.85)<1e-6) a = L1_2(['Ag', 'Cu'], size=[2,2,2], latticeconstant=4.0) a.set_calculator(calc) FIRE(UnitCellFilter(a, mask=[1,1,1,0,0,0]), logfile=None).run(fmax=0.001) e = a.get_potential_energy() syms = np.array(a.get_chemical_symbols()) self.assertTrue(abs((e-(syms=='Cu').sum()*e_Cu- (syms=='Ag').sum()*e_Ag)/len(a)-0.096)<0.0005) a = B1(['Ag', 'Cu'], size=[2,2,2], latticeconstant=4.0) a.set_calculator(calc) FIRE(UnitCellFilter(a, mask=[1,1,1,0,0,0]), logfile=None).run(fmax=0.001) e = a.get_potential_energy() syms = np.array(a.get_chemical_symbols()) self.assertTrue(abs((e-(syms=='Cu').sum()*e_Cu- (syms=='Ag').sum()*e_Ag)/len(a)-0.516)<0.0005) a = B2(['Ag', 'Cu'], size=[2,2,2], latticeconstant=4.0) a.set_calculator(calc) FIRE(UnitCellFilter(a, mask=[1,1,1,0,0,0]), logfile=None).run(fmax=0.001) e = a.get_potential_energy() syms = np.array(a.get_chemical_symbols()) self.assertTrue(abs((e-(syms=='Cu').sum()*e_Cu- (syms=='Ag').sum()*e_Ag)/len(a)-0.177)<0.0003) a = L1_2(['Cu', 'Ag'], size=[2,2,2], latticeconstant=4.0) a.set_calculator(calc) FIRE(UnitCellFilter(a, mask=[1,1,1,0,0,0]), logfile=None).run(fmax=0.001) e = a.get_potential_energy() syms = np.array(a.get_chemical_symbols()) self.assertTrue(abs((e-(syms=='Cu').sum()*e_Cu- (syms=='Ag').sum()*e_Ag)/len(a)-0.083)<0.0005)
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()
norbs = [] sccs = ['non-SCC', 'SCC'][SCC] for nx in [1, 2, 3, 4, 5]: for ny in [1, 2, 3, 4, 5]: print('nx', nx, ny) atoms = FaceCenteredCubic(directions=[[1,-1,0], [1,1,-2], [1,1,1]],\ size=(nx,ny,1), symbol='Au', pbc=(0,0,0)) calc = Hotbit(verbose=True, SCC=SCC, gamma_cut=3, txt='size2.cal', **default_param) atoms.set_calculator(calc) try: atoms.get_forces() except: continue calc.timer.summary() timings.append(calc.timer.get_timings()) norbs.append(calc.el.get_nr_orbitals()) calc.__del__() order = argsort(norbs) norbs = sort(norbs) timings2 = [] for i in order: timings2.append(timings[i]) times = {} maxtime = 0.0
from asap3 import * from asap3.Timing import report_timing from ase.lattice.cubic import FaceCenteredCubic import time nsteps = 10 start = time.time() if len(sys.argv) >= 2 and sys.argv[1] == '-t': AsapThreads() if len(sys.argv) >= 2 and sys.argv[1] == '-T': AsapThreads(6) atoms = FaceCenteredCubic(size=(100,100,50), symbol='Cu') atoms.set_calculator(EMT()) print "Number of atoms:", len(atoms) d = 0.1 pos = atoms.arrays['positions'] # Nasty! for i in range(nsteps): pos[50][0] += d d = -d f = atoms.get_forces() report_timing() print "Wall time elapsed:", time.time() - start
for j in range(3): if i != j and np.abs(uc[i, j]) > 1e-15: diagonal = False if not (self.allow_mi_opbc and atoms.get_pbc().all() and diagonal): # Minimum Image Orthogonal Periodic Boundary Conditions # are not allowed remove.extend(["MI_OPBC_H", "MI_OPBC_F"]) if atoms.get_pbc().any(): # Cluster method is not allowed remove.append("CLUSTER") for rem in remove: if rem in allowed: allowed.remove(rem) if self.verbose: print "Allowed PBC:", allowed return allowed if __name__ == '__main__': from ase.lattice.cubic import FaceCenteredCubic atoms = FaceCenteredCubic(size=(10, 10, 10), symbol='Cu') print "Creating calculator" pot = OpenKIMcalculator( 'EMT_Asap_Standard_AlAgAuCuNiPdPt__MO_118428466217_000') print "Setting atoms" atoms.set_calculator(pot) print "Calculating energy" print atoms.get_potential_energy() print atoms.get_forces()[10:] print atoms.get_stress()
for j in range(3): if i != j and np.abs(uc[i,j]) > 1e-15: diagonal = False if not (self.allow_mi_opbc and atoms.get_pbc().all() and diagonal): # Minimum Image Orthogonal Periodic Boundary Conditions # are not allowed remove.extend(["MI_OPBC_H", "MI_OPBC_F"]) if atoms.get_pbc().any(): # Cluster method is not allowed remove.append("CLUSTER") for rem in remove: if rem in allowed: allowed.remove(rem) if self.verbose: print "Allowed PBC:", allowed return allowed if __name__ == '__main__': from ase.lattice.cubic import FaceCenteredCubic atoms = FaceCenteredCubic(size=(10,10,10), symbol='Cu') print "Creating calculator" pot = OpenKIMcalculator('EMT_Asap_Standard_AlAgAuCuNiPdPt__MO_118428466217_000') print "Setting atoms" atoms.set_calculator(pot) print "Calculating energy" print atoms.get_potential_energy() print atoms.get_forces()[10:] print atoms.get_stress()
"""Check that energy is correct even after wrapping through periodic boundary conditions. """ from ase.lattice.cubic import FaceCenteredCubic from asap3 import * from asap3.testtools import * import random ref_atoms = FaceCenteredCubic(size=(7, 7, 7), symbol="Cu", pbc=(True, False, True)) ref_atoms.set_calculator(EMT()) ref_energy = ref_atoms.get_potential_energy() ref_energies = ref_atoms.get_potential_energies() ref_forces = ref_atoms.get_forces() passes = 5 for ps in range(passes): print "Pass", ps, "of", passes atoms = ref_atoms.copy() atoms.set_calculator(EMT()) nat = random.randint(0, len(atoms)) assert nat < len(atoms) pos0 = atoms[nat].position cell = atoms.get_cell() for d in range(1, 4): for dx in (-d, 0, d): #for dy in (-d, 0, d): for dy in (0, ):
from asap3 import * from ase.lattice.cubic import FaceCenteredCubic from asap3.testtools import ReportTest atoms = FaceCenteredCubic(directions=[[1,0,0],[0,1,0],[0,0,1]], size=(6,6,6), symbol="Cu") atoms.set_calculator(EMT()) f1 = atoms.get_forces() atoms.set_calculator(EMT()) f2 = atoms.get_forces() maxdev = abs(f2 - f1).max() print maxdev ReportTest("Max error 1:", maxdev, 0.0, 1e-6) atoms2 = Atoms(atoms) if atoms2.get_calculator() is None: # Slightly old ase atoms2.set_calculator(atoms.get_calculator()) f2 = atoms2.get_forces() maxdev = abs(f2 - f1).max() print maxdev ReportTest("Max error 2:", maxdev, 0.0, 1e-6) f2 = atoms.get_forces() maxdev = abs(f2 - f1).max() print maxdev ReportTest("Max error 1:", maxdev, 0.0, 1e-6)
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
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): ReportTest(txt + "Energy of atom %i" % (i, ), ek[i], ee[i], 1e-8) fk = atoms_kim.get_forces() fe = atoms_emt.get_forces() n = 0 for i in range(0, natoms, step): n = (n + 1) % 3 ReportTest(txt + "Force(%i) of atom %i" % (n, i), fk[i, n], fe[i, n], 1e-8) sk = atoms_kim.get_stress() se = atoms_emt.get_stress() for i in range(6): ReportTest(txt + "Stress(%i)" % (i, ), sk[i], se[i], 1e-8) sk = atoms_kim.get_stresses() se = atoms_emt.get_stresses() for i in range(0, natoms, step): n = (n + 1) % 6 # Volume per atom is not defined the same way: greater tolerance needed
#set_verbose(1) master = world.rank == 0 if master: atoms0 = FaceCenteredCubic(symbol='Pt', size=(15, 15, 30)) else: atoms0 = None atoms0 = MakeParallelAtoms(atoms0, (1, 1, 2)) atoms0.set_calculator(pot()) print >> sys.stderr, "*********** FIRST FORCE CALCULATION ************" print >> sys.stderr, "len(atoms) =", len( atoms0), " no. atoms =", atoms0.get_number_of_atoms() f0 = atoms0.get_forces() perturbation = 0.01 * np.random.standard_normal(atoms0.get_positions().shape) r = atoms0.get_positions() + perturbation atoms0.set_positions(r) print >> sys.stderr, "*********** SECOND FORCE CALCULATION ************" f1 = atoms0.get_forces() print >> sys.stderr, "*********** COPYING ATOMS **************" atoms2 = ParallelAtoms((1, 1, 2), atoms0.comm, atoms0, distribute=False) atoms2.set_calculator(pot()) print >> sys.stderr, "*********** THIRD FORCE CALCULATION ************" f2 = atoms2.get_forces() #maxdev = abs(f2 - f1).max() #print maxdev
"""Check that energy is correct even after wrapping through periodic boundary conditions. """ from ase.lattice.cubic import FaceCenteredCubic from asap3 import * from asap3.testtools import * import random ref_atoms = FaceCenteredCubic(size=(7,7,7), symbol="Cu", pbc=(True, False, True)) ref_atoms.set_calculator(EMT()) ref_energy = ref_atoms.get_potential_energy() ref_energies = ref_atoms.get_potential_energies() ref_forces = ref_atoms.get_forces() passes = 5 for ps in range(passes): print "Pass", ps, "of", passes atoms = ref_atoms.copy() atoms.set_calculator(EMT()) nat = random.randint(0, len(atoms)) assert nat < len(atoms) pos0 = atoms[nat].position cell = atoms.get_cell() for d in range(1,4): for dx in (-d, 0, d): #for dy in (-d, 0, d): for dy in (0,): for dz in (-d, 0 ,d): deltar = dx * cell[0] + dy * cell[1] + dz * cell[2]
def MakeEMT2013(): "EMT2013 potential." return EMT2013(PtY_parameters) for pot in (EMT, MakeLJ, MakeEMT2013): print "Testing:", pot.__doc__.split("\n")[0] atoms1 = FaceCenteredCubic(directions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]], size=(6, 6, 6), symbol="Pt") emt = pot() atoms1.set_calculator(emt) f1 = atoms1.get_forces() atoms2 = FaceCenteredCubic(directions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]], size=(5, 5, 5), symbol="Pt") atoms2.set_calculator(emt) f3 = atoms1.get_forces() maxdev = abs(f3 - f1).max() print maxdev ReportTest("Max error 1:", maxdev, 0.0, 1e-6) atoms1 = FaceCenteredCubic(directions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]], size=(6, 6, 6), symbol="Pt") emt = pot() atoms1.set_calculator(emt)
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): ReportTest(txt+"Energy of atom %i" % (i,), ek[i], ee[i], 1e-8) fk = atoms_kim.get_forces() fe = atoms_emt.get_forces() n = 0 for i in range(0, natoms, step): n = (n + 1) % 3 ReportTest(txt+"Force(%i) of atom %i" % (n, i), fk[i, n], fe[i, n], 1e-8) sk = atoms_kim.get_stress() se = atoms_emt.get_stress() for i in range(6): ReportTest(txt+"Stress(%i)" % (i,), sk[i], se[i], 1e-8) sk = atoms_kim.get_stresses() se = atoms_emt.get_stresses() for i in range(0, natoms, step): n = (n + 1) % 6 # Volume per atom is not defined the same way: greater tolerance needed ReportTest(txt+"Stress(%i) of atom %i" % (n, i), sk[i, n], se[i, n], 1e-3)