File: test_filter.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 (67 lines) | stat: -rw-r--r-- 2,059 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
# fmt: off
import numpy as np
import pytest

from ase.build import bulk, molecule
from ase.calculators.emt import EMT
from ase.filters import (
    ExpCellFilter,
    Filter,
    FrechetCellFilter,
    StrainFilter,
    UnitCellFilter,
)
from ase.optimize import QuasiNewton
from ase.stress import full_3x3_to_voigt_6_strain, voigt_6_to_full_3x3_strain


@pytest.mark.optimize()
def test_filter(testdir):
    """Test that the filter and trajectories are playing well together."""

    atoms = molecule('CO2')
    atoms.calc = EMT()
    filter = Filter(atoms, indices=[1, 2])

    with QuasiNewton(filter, trajectory='filter-test.traj',
                     logfile='filter-test.log') as opt:
        opt.run()
    # No assertions=??


@pytest.mark.optimize()
@pytest.mark.filterwarnings("ignore:Use FrechetCellFilter")
@pytest.mark.parametrize(
    'filterclass', [StrainFilter,
                    UnitCellFilter,
                    FrechetCellFilter,
                    ExpCellFilter])
@pytest.mark.parametrize(
    'mask', [[1, 1, 0, 0, 0, 1],
             [1, 0, 0, 0, 0, 0],
             [1, 0, 1, 1, 0, 0],
             [0, 1, 0, 0, 1, 1]]
)
def test_apply_strain_to_mask(filterclass, mask):
    cu = bulk('Cu', a=3.14) * (6, 3, 1)
    orig_cell = cu.cell.copy()
    rng = np.random.RandomState(69)

    # Create extreme deformation
    deformation_vv = np.eye(3) + 1e2 * rng.randn(3, 3)
    filter = filterclass(cu, mask=mask)
    if filterclass is not StrainFilter:
        pos_and_deform = \
            np.concatenate((cu.get_positions(),
                            deformation_vv), axis=0)
    else:
        pos_and_deform = full_3x3_to_voigt_6_strain(deformation_vv)

    # Apply the deformation to the filter, which should then apply it
    # with a mask.
    filter.set_positions(pos_and_deform)
    full_mask = voigt_6_to_full_3x3_strain(mask) != 0

    # Ensure the mask is respected to a very tight numerical tolerance
    assert np.linalg.solve(orig_cell, cu.cell)[~full_mask] == \
        pytest.approx(np.eye(3)[~full_mask], abs=1e-12)