def test_stress(self): a = FaceCenteredCubic('Au', size=[2,2,2]) calc = EAM('Au-Grochola-JCP05.eam.alloy') a.set_calculator(calc) self.assertArrayAlmostEqual(a.get_stress(), calc.calculate_numerical_stress(a), tol=self.tol) sx, sy, sz = a.cell.diagonal() a.set_cell([sx, 0.9*sy, 1.2*sz], scale_atoms=True) self.assertArrayAlmostEqual(a.get_stress(), calc.calculate_numerical_stress(a), tol=self.tol) a.set_cell([[sx, 0.1*sx, 0], [0, 0.9*sy, 0], [0, -0.1*sy, 1.2*sz]], scale_atoms=True) self.assertArrayAlmostEqual(a.get_stress(), calc.calculate_numerical_stress(a), tol=self.tol)
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()
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()
1, 0) ReportTest("Forces required", pot.calculation_required(atoms, ["forces"]), 1, 0) ReportTest("Stress required", pot.calculation_required(atoms, ["stress"]), 1, 0) ReportTest("Magmom required", pot.calculation_required(atoms, ["magmoms"]), 1, 0) e = atoms.get_potential_energy() ReportTest("Energy not required", pot.calculation_required(atoms, ["energy"]), 0, 0) ReportTest("Forces required (II)", pot.calculation_required(atoms, ["forces"]), 1, 0) f = atoms.get_forces() ReportTest("Energy not required (II)", pot.calculation_required(atoms, ["energy"]), 0, 0) ReportTest("Forces not required", pot.calculation_required(atoms, ["forces"]), 0, 0) ReportTest("Energy or forces not required", pot.calculation_required(atoms, ["energy", "forces"]), 0, 0) ReportTest("Energy or stress required", pot.calculation_required(atoms, ["energy", "stress"]), 1, 0) s = atoms.get_stress() ReportTest("Stress not required", pot.calculation_required(atoms, ["stress"]), 0, 0) r = atoms.get_positions() r[0, 0] += 0.1 atoms.set_positions(r) ReportTest.Summary()
print "Number of atoms:", atoms.get_number_of_atoms() print "Heating to %d K using Langevin" % T_goal lgv = Langevin(atoms, 5 * units.fs, temperature=2*T_goal*units.kB, friction=0.05) while atoms.get_kinetic_energy() < 1.5 * atoms.get_number_of_atoms() * T_goal * units.kB: lgv.run(5) T = atoms.get_kinetic_energy() / (1.5 * atoms.get_number_of_atoms() * units.kB) print "Temperature is now %.2f K" % (T,) print "Desired temperature reached!" lgv.set_temperature(T_goal*units.kB) for i in range(2): lgv.run(20) s = atoms.get_stress() p = -(s[0] + s[1] + s[2])/3.0 / units.GPa T = atoms.get_kinetic_energy() / (1.5 * atoms.get_number_of_atoms() * units.kB) print "Pressure is %f GPa, desired pressure is %f GPa (T = %.2f K)" % (p, p_goal, T) dv = (p - p_goal) / bulk print "Adjusting volume by", dv cell = atoms.get_cell() atoms.set_cell(cell * (1.0 + dv/3.0)) T = atoms.get_kinetic_energy() / (1.5 * atoms.get_number_of_atoms() * units.kB) print "Temperature is now %.2f K" % (T,) stressstate = array([-2, -1, 0, 0, 0, 0])*p_goal*units.GPa dyn = NPT(atoms, 5 * units.fs, T_goal*units.kB, stressstate, ttime*units.fs, (ptime*units.fs)**2 * bulk * units.GPa) traj = PickleTrajectory("NPT-atoms.traj", "w", atoms)
#!/usr/bin/env python from jasp import * from ase.lattice.cubic import FaceCenteredCubic atoms = FaceCenteredCubic(symbol='Al') with jasp('bulk/Al-bulk', xc='PBE', kpts=(12, 12, 12), encut=350, prec='High', isif=3, nsw=30, ibrion=1, atoms=atoms) as calc: print atoms.get_potential_energy() print atoms.get_stress()
temperature=2 * T_goal * units.kB, friction=0.05) while atoms.get_kinetic_energy( ) < 1.5 * atoms.get_number_of_atoms() * T_goal * units.kB: lgv.run(5) T = atoms.get_kinetic_energy() / (1.5 * atoms.get_number_of_atoms() * units.kB) print "Temperature is now %.2f K" % (T, ) print "Desired temperature reached!" lgv.set_temperature(T_goal * units.kB) for i in range(2): lgv.run(20) s = atoms.get_stress() p = -(s[0] + s[1] + s[2]) / 3.0 / units.GPa T = atoms.get_kinetic_energy() / (1.5 * atoms.get_number_of_atoms() * units.kB) print "Pressure is %f GPa, desired pressure is %f GPa (T = %.2f K)" % ( p, p_goal, T) dv = (p - p_goal) / bulk print "Adjusting volume by", dv cell = atoms.get_cell() atoms.set_cell(cell * (1.0 + dv / 3.0)) T = atoms.get_kinetic_energy() / (1.5 * atoms.get_number_of_atoms() * units.kB) print "Temperature is now %.2f K" % (T, ) stressstate = array([-2, -1, 0, 0, 0, 0]) * p_goal * units.GPa
#set_verbose(1) atoms = FaceCenteredCubic(directions=((1,0,0), (0,1,0), (0,0,1)), size=(15,15,15), symbol="Cu", pbc=True) atoms.set_calculator(EMT()) atoms.get_forces() atoms.get_forces() MaxwellBoltzmannDistribution(atoms, 300 * units.kB) atoms.get_forces() atoms.get_forces() atoms = FaceCenteredCubic(directions=((1,0,0), (0,1,0), (0,0,1)), size=(15,15,15), symbol="Cu", pbc=True) atoms.set_calculator(EMT()) s = atoms.get_stress() print print "Stress:", s s = atoms.get_stress() print print "Stress:", s MaxwellBoltzmannDistribution(atoms, 300 * units.kB) s = atoms.get_stress() print print "Stress:", s s = atoms.get_stress() print print "Stress:", s MaxwellBoltzmannDistribution(atoms, 300 * units.kB) s = atoms.get_stress() print
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) ReportTest.Summary()
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) ReportTest.Summary()