File: test_cellfilter.py

package info (click to toggle)
python-ase 3.26.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,484 kB
  • sloc: python: 148,112; xml: 2,728; makefile: 110; javascript: 47
file content (172 lines) | stat: -rw-r--r-- 5,219 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# fmt: off
from itertools import product

import numpy as np
import pytest

import ase
from ase import Atoms
from ase.build import bulk
from ase.calculators.test import gradient_test
from ase.filters import ExpCellFilter, Filter, FrechetCellFilter, UnitCellFilter
from ase.io import Trajectory
from ase.optimize import LBFGS, MDMin
from ase.units import GPa


@pytest.fixture()
def atoms(asap3) -> ase.Atoms:
    rng = np.random.RandomState(0)
    atoms = bulk('Cu', cubic=True)
    atoms.positions[:, 0] *= 0.995
    atoms.cell += rng.uniform(-1e-2, 1e-2, size=9).reshape((3, 3))
    atoms.calc = asap3.EMT()
    return atoms


@pytest.mark.optimize()
@pytest.mark.filterwarnings("ignore:Use FrechetCellFilter")
@pytest.mark.parametrize(
    'cellfilter', [UnitCellFilter, FrechetCellFilter, ExpCellFilter]
)
def test_get_and_set_positions(atoms: Atoms, cellfilter: type[Filter]) -> None:
    filter = cellfilter(atoms)
    pos = filter.get_positions()
    filter.set_positions(pos)
    pos2 = filter.get_positions()
    assert np.allclose(pos, pos2)


@pytest.mark.filterwarnings("ignore:Use FrechetCellFilter")
@pytest.mark.parametrize(
    'cellfilter', [UnitCellFilter, FrechetCellFilter, ExpCellFilter]
)
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


@pytest.mark.filterwarnings("ignore:Use FrechetCellFilter")
@pytest.mark.parametrize(
    'cellfilter', [UnitCellFilter, FrechetCellFilter, ExpCellFilter]
)
def test_cellfilter_forces(atoms, cellfilter):
    xcellfilter = cellfilter(atoms)
    f, fn = gradient_test(xcellfilter)
    assert abs(f - fn).max() < 3e-6


@pytest.mark.parametrize('cellfilter,cell_factor_name, cell_factor_value', [
    (UnitCellFilter, 'cell_factor', 1),
    (UnitCellFilter, 'cell_factor', None),
    (FrechetCellFilter, 'exp_cell_factor', 1),
    (FrechetCellFilter, 'exp_cell_factor', None),
    # Never apply this test to ExpCellFilter because it is known to fail!
])
def test_cellfilter_stress(
    atoms: ase.Atoms,
    cellfilter,
    cell_factor_name: str,
    cell_factor_value
):
    filter: Filter = cellfilter(**{
        "atoms": atoms,
        cell_factor_name: cell_factor_value
    })

    # Check gradient at other than origin
    natoms = len(atoms)
    pos0 = filter.get_positions()
    rng = np.random.RandomState(0)
    pos0[natoms:, :] += 1e-2 * rng.randn(3, 3)
    filter.set_positions(pos0)
    grads_actual = -filter.get_forces()

    eps = 1e-4
    for alpha, beta in product(range(3), repeat=2):
        pos_p = pos0.copy()
        pos_p[natoms + alpha, beta] += eps
        filter.set_positions(pos_p)
        energy_p = filter.get_potential_energy()

        pos_m = pos0.copy()
        pos_m[natoms + alpha, beta] -= eps
        filter.set_positions(pos_m)
        energy_m = filter.get_potential_energy()

        expect = (energy_p - energy_m) / (2 * eps)
        actual = grads_actual[natoms + alpha, beta]
        assert np.isclose(actual, expect, atol=1e-4)


def test_intensive_cell_gradient(atoms: ase.Atoms):
    """Gradient w.r.t. cell variables for FrechetCellFilter should provide
    intensive values with an appropriate scaling factor.
    """
    filter = FrechetCellFilter(atoms, exp_cell_factor=float(len(atoms)))
    cell_grad = filter.get_forces()[-3:]

    atoms2 = atoms.copy()
    atoms2 *= (2, 2, 2)
    atoms2.calc = atoms.calc
    filter2 = FrechetCellFilter(atoms2, exp_cell_factor=float(len(atoms2)))
    cell_grad2 = filter2.get_forces()[-3:]

    assert np.allclose(cell_grad, cell_grad2)


@pytest.mark.filterwarnings("ignore:Use FrechetCellFilter")
@pytest.mark.parametrize(
    'cellfilter', [UnitCellFilter, FrechetCellFilter, ExpCellFilter]
)
def test_constant_volume(atoms: ase.Atoms, cellfilter):
    atoms_opt = atoms.copy()
    atoms_opt.calc = atoms.calc
    filter: Filter = cellfilter(atoms_opt, constant_volume=True)
    opt = LBFGS(filter)  # type: ignore[arg-type]
    opt.run()

    # Check if volume is conserved
    assert not np.allclose(atoms.cell.array, atoms_opt.cell.array)
    assert np.isclose(atoms.get_volume(), atoms_opt.get_volume())


# XXX This test should have some assertions!  --askhl
@pytest.mark.optimize()
def test_unitcellfilter(asap3, testdir):
    cu = bulk('Cu') * (6, 6, 6)
    cu.calc = asap3.EMT()
    f = UnitCellFilter(cu, [1, 1, 1, 0, 0, 0])
    opt = LBFGS(f)

    with Trajectory('Cu-fcc.traj', 'w', cu) as t:
        opt.attach(t)
        opt.run(5.0)
    # No assertions??


@pytest.mark.optimize()
def test_unitcellfilter_hcp(asap3, testdir):
    cu = bulk('Cu', 'hcp', a=3.6 / 2.0**0.5)
    cu.cell[1, 0] -= 0.05
    cu *= (6, 6, 3)
    cu.calc = asap3.EMT()
    print(cu.get_forces())
    print(cu.get_stress())
    f = UnitCellFilter(cu)
    opt = MDMin(f, dt=0.01)
    with Trajectory('Cu-hcp.traj', 'w', cu) as t:
        opt.attach(t)
        opt.run(0.2)
    # No assertions??