1import numpy as np
2import pytest
3
4from ase.build import bulk
5from ase.calculators.lj import LennardJones
6from ase.optimize.precon import PreconLBFGS, Exp
7from ase.constraints import UnitCellFilter, ExpCellFilter
8
9
10#@pytest.mark.skip('FAILS WITH PYAMG')
11@pytest.mark.slow
12def test_precon():
13    cu0 = bulk("Cu") * (2, 2, 2)
14    lj = LennardJones(sigma=cu0.get_distance(0, 1))
15
16    cu = cu0.copy()
17    cu.set_cell(1.2*cu.get_cell())
18    cu.calc = lj
19    ucf = UnitCellFilter(cu, constant_volume=True)
20    opt = PreconLBFGS(ucf, precon=Exp(mu=1.0, mu_c=1.0))
21    opt.run(fmax=1e-3)
22    assert abs(np.linalg.det(cu.cell)/np.linalg.det(cu0.cell) - 1.2**3) < 1e-3
23
24    # EcpCellFilter allows relaxing to lower tolerance
25    cu = cu0.copy()
26    cu.set_cell(1.2 * cu.get_cell())
27    cu.calc = lj
28    ecf = ExpCellFilter(cu, constant_volume=True)
29    opt = PreconLBFGS(ecf, precon=Exp(mu=1.0, mu_c=1.0))
30    opt.run(fmax=1e-3)
31    assert abs(np.linalg.det(cu.cell) / np.linalg.det(cu0.cell) - 1.2**3) < 1e-7
32