def test_springcalc(): # setup k = 3.0 atoms_ideal = bulk('Al').repeat(3) calc = SpringCalculator(atoms_ideal.get_positions(), k) displacements = np.array([(d, 2 * d, 3 * d) for d in np.linspace(0, 1, len(atoms_ideal))]) # calc forces and energy atoms = atoms_ideal.copy() atoms.positions += displacements atoms.calc = calc forces = atoms.get_forces() Epot = atoms.get_potential_energy() # reference forces and energy Epot_target = np.sum(k / 2.0 * displacements**2) forces_target = -k * displacements assert np.allclose(forces, forces_target) assert np.isclose(Epot, Epot_target) # numeric forces test atoms_ideal.calc = calc f, fn = gradient_test(atoms_ideal) assert abs(f - fn).max() < 1e-10
def test_unitcellfilterpressure(): a0 = bulk('Cu', cubic=True) # perturb the atoms s = a0.get_scaled_positions() s[:, 0] *= 0.995 a0.set_scaled_positions(s) # perturb the cell a0.cell[...] += np.random.uniform(-1e-2, 1e-2, size=9).reshape((3, 3)) atoms = a0.copy() atoms.calc = LennardJones() ucf = UnitCellFilter(atoms, scalar_pressure=10.0 * GPa) # test all derivatives f, fn = gradient_test(ucf) assert abs(f - fn).max() < 1e-6 opt = FIRE(ucf) opt.run(1e-3) # check pressure is within 0.1 GPa of target sigma = atoms.get_stress() / GPa pressure = -(sigma[0] + sigma[1] + sigma[2]) / 3.0 assert abs(pressure - 10.0) < 0.1 atoms = a0.copy() atoms.calc = LennardJones() ecf = ExpCellFilter(atoms, scalar_pressure=10.0 * GPa) # test all deritatives f, fn = gradient_test(ecf) assert abs(f - fn).max() < 1e-6 opt = LBFGSLineSearch(ecf) opt.run(1e-3) # check pressure is within 0.1 GPa of target sigma = atoms.get_stress() / GPa pressure = -(sigma[0] + sigma[1] + sigma[2]) / 3.0 assert abs(pressure - 10.0) < 0.1
def test_pressure(atoms, cellfilter): xcellfilter = cellfilter(atoms, scalar_pressure=10.0 * GPa) # test all derivatives f, fn = gradient_test(xcellfilter) assert abs(f - fn).max() < 5e-6 opt = LBFGS(xcellfilter) opt.run(1e-3) # check pressure is within 0.1 GPa of target sigma = atoms.get_stress() / GPa pressure = -(sigma[0] + sigma[1] + sigma[2]) / 3.0 assert abs(pressure - 10.0) < 0.1
def test_cellfilter(atoms, cellfilter): xcellfilter = cellfilter(atoms) f, fn = gradient_test(xcellfilter) assert abs(f - fn).max() < 3e-6
import numpy as np from ase.build import bulk from ase.calculators.test import gradient_test from ase.calculators.lj import LennardJones from ase.constraints import UnitCellFilter a0 = bulk('Cu', cubic=True) # perturb the atoms s = a0.get_scaled_positions() s[:, 0] *= 0.995 a0.set_scaled_positions(s) # perturb the cell a0.cell[...] += np.random.uniform(-1e-2, 1e-2, size=9).reshape((3, 3)) atoms = a0.copy() atoms.set_calculator(LennardJones()) ucf = UnitCellFilter(atoms) # test all deritatives f, fn = gradient_test(ucf) assert abs(f - fn).max() < 1e-6
def test_expcellfilter(setup_atoms): ecf = ExpCellFilter(setup_atoms) # test all derivatives f, fn = gradient_test(ecf) assert abs(f - fn).max() < 3e-6
def test_unitcellfilter(setup_atoms): ucf = UnitCellFilter(setup_atoms) f, fn = gradient_test(ucf) assert abs(f - fn).max() < 3e-6
import numpy as np from ase.build import bulk from ase.calculators.harmonic import SpringCalculator from ase.calculators.test import gradient_test # setup k = 3.0 atoms_ideal = bulk('Al').repeat(3) calc = SpringCalculator(atoms_ideal.get_positions(), k) displacements = np.array([(d, 2 * d, 3 * d) for d in np.linspace(0, 1, len(atoms_ideal))]) # calc forces and energy atoms = atoms_ideal.copy() atoms.positions += displacements atoms.set_calculator(calc) forces = atoms.get_forces() Epot = atoms.get_potential_energy() # reference forces and energy Epot_target = np.sum(k / 2.0 * displacements**2) forces_target = -k * displacements assert np.allclose(forces, forces_target) assert np.isclose(Epot, Epot_target) # numeric forces test atoms_ideal.set_calculator(calc) f, fn = gradient_test(atoms_ideal) assert abs(f - fn).max() < 1e-10
import numpy as np from ase.build import bulk from ase.calculators.test import gradient_test from ase.calculators.lj import LennardJones from ase.constraints import UnitCellFilter a0 = bulk('Cu', cubic=True) # perturb the atoms s = a0.get_scaled_positions() s[:, 0] *= 0.995 a0.set_scaled_positions(s) # perturb the cell a0.cell[...] += np.random.uniform(-1e-2, 1e-2, size=9).reshape((3,3)) atoms = a0.copy() atoms.set_calculator(LennardJones()) ucf = UnitCellFilter(atoms) # test all deritatives f, fn = gradient_test(ucf) assert abs(f - fn).max() < 1e-6