def test_socketio_espresso(factory): name = factory.name if name == 'abinit': factory.require_version('9.4') atoms = bulk('Si') exe = factory.factory.executable unixsocket = f'ase_test_socketio_{name}' espresso = factory.calc(kpts=[2, 2, 2], ) template = commands[name] command = template.format(exe=exe, unixsocket=unixsocket) espresso.command = command atoms.rattle(stdev=.2, seed=42) opt = BFGS(ExpCellFilter(atoms)) with pytest.warns(UserWarning, match='Subprocess exited'): with SocketIOCalculator(espresso, unixsocket=unixsocket) as calc: atoms.calc = calc for _ in opt.irun(fmax=0.05): e = atoms.get_potential_energy() fmax = max(np.linalg.norm(atoms.get_forces(), axis=0)) print(e, fmax)
def test_Pt_stress_cellopt(factory, pt_eam_potential_file): params = {} params['pair_style'] = 'eam' params['pair_coeff'] = ['1 1 {}'.format(pt_eam_potential_file)] # XXX Should it accept Path objects? Yes definitely for files. with factory.calc(specorder=['Pt'], files=[str(pt_eam_potential_file)], **params) as calc: rng = np.random.RandomState(17) atoms = bulk('Pt') * (2, 2, 2) atoms.rattle(stdev=0.1) atoms.cell += 2 * rng.rand(3, 3) atoms.calc = calc assert_allclose(atoms.get_stress(), calc.calculate_numerical_stress(atoms), atol=1e-4, rtol=1e-4) opt = BFGS(ExpCellFilter(atoms), trajectory='opt.traj') for i, _ in enumerate(opt.irun(fmax=0.001)): pass cell1_ref = np.array( [[0.16524, 3.8999, 3.92855], [4.211015, 0.634928, 5.047811], [4.429529, 3.293805, 0.447377]] ) assert_allclose(np.asarray(atoms.cell), cell1_ref, atol=3e-4, rtol=3e-4) assert_allclose(atoms.get_stress(), calc.calculate_numerical_stress(atoms), atol=1e-4, rtol=1e-4) assert i < 80, 'Expected 59 iterations, got many more: {}'.format(i)
def test_Pt_stress_cellopt(): import numpy as np from numpy.testing import assert_allclose from ase.calculators.lammpsrun import LAMMPS from ase.build import bulk from ase.test.eam_pot import Pt_u3 from ase.constraints import ExpCellFilter from ase.optimize import BFGS # (For now) reuse eam file stuff from other lammps test: pot_fn = 'Pt_u3.eam' f = open(pot_fn, 'w') f.write(Pt_u3) f.close() params = {} params['pair_style'] = 'eam' params['pair_coeff'] = ['1 1 {}'.format(pot_fn)] with LAMMPS(specorder=['Pt'], files=[pot_fn], **params) as calc: rng = np.random.RandomState(17) atoms = bulk('Pt') * (2, 2, 2) atoms.rattle(stdev=0.1) atoms.cell += 2 * rng.rand(3, 3) atoms.calc = calc assert_allclose(atoms.get_stress(), calc.calculate_numerical_stress(atoms), atol=1e-4, rtol=1e-4) opt = BFGS(ExpCellFilter(atoms), trajectory='opt.traj') for i, _ in enumerate(opt.irun(fmax=0.001)): pass cell1_ref = np.array([[0.16524, 3.8999, 3.92855], [4.211015, 0.634928, 5.047811], [4.429529, 3.293805, 0.447377]]) assert_allclose(np.asarray(atoms.cell), cell1_ref, atol=3e-4, rtol=3e-4) assert_allclose(atoms.get_stress(), calc.calculate_numerical_stress(atoms), atol=1e-4, rtol=1e-4) assert i < 80, 'Expected 59 iterations, got many more: {}'.format(i)
params['pair_coeff'] = ['1 1 {}'.format(pot_fn)] calc = LAMMPS(specorder=['Pt'], files=[pot_fn], **params) rng = np.random.RandomState(17) atoms = bulk('Pt') * (2, 2, 2) atoms.rattle(stdev=0.1) atoms.cell += 2 * rng.rand(3, 3) atoms.calc = calc assert_allclose(atoms.get_stress(), calc.calculate_numerical_stress(atoms), atol=1e-4, rtol=1e-4) opt = BFGS(ExpCellFilter(atoms), trajectory='opt.traj') for i, _ in enumerate(opt.irun(fmax=0.05)): pass cell1_ref = np.array([[0.16298762, 3.89912471, 3.92825365], [4.21007577, 0.63362427, 5.04668170], [4.42895706, 3.29171414, 0.44623618]]) assert_allclose(np.asarray(atoms.cell), cell1_ref, atol=1e-4, rtol=1e-4) assert_allclose(atoms.get_stress(), calc.calculate_numerical_stress(atoms), atol=1e-4, rtol=1e-4) assert i < 80, 'Expected 59 iterations, got many more: {}'.format(i)