File: test_basis_sets_O3.py

package info (click to toggle)
python-symfc 1.5.4-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,980 kB
  • sloc: python: 10,485; makefile: 12
file content (203 lines) | stat: -rw-r--r-- 6,988 bytes parent folder | download | duplicates (2)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
"""Tests of FCBasisSetO3."""

from pathlib import Path

import numpy as np
import pytest

from symfc.basis_sets import FCBasisSetO2, FCBasisSetO3
from symfc.solvers import FCSolverO2O3
from symfc.utils.utils import SymfcAtoms
from symfc.utils.utils_O3 import get_lat_trans_compr_matrix_O3

cwd = Path(__file__).parent


def test_fc_basis_set_o3():
    """Test symmetry adapted basis sets of FCBasisSetO3."""
    a = 5.4335600299999998
    lattice = np.array([[a, 0, 0], [0, a, 0], [0, 0, a]])
    positions = np.array(
        [
            [0.875, 0.875, 0.875],
            [0.875, 0.375, 0.375],
            [0.375, 0.875, 0.375],
            [0.375, 0.375, 0.875],
            [0.125, 0.125, 0.125],
            [0.125, 0.625, 0.625],
            [0.625, 0.125, 0.625],
            [0.625, 0.625, 0.125],
        ]
    )
    numbers = [1, 1, 1, 1, 1, 1, 1, 1]
    supercell = SymfcAtoms(cell=lattice, scaled_positions=positions, numbers=numbers)
    sbs = FCBasisSetO3(supercell, log_level=1).run()

    basis_ref = [
        0.671875,
        0.656250,
        0.765625,
        1.000000,
        0.750000,
        0.875000,
        0.562500,
        0.765625,
        0.718750,
        0.671875,
        1.000000,
        0.875000,
        0.875000,
        0.625000,
        0.750000,
        0.562500,
        0.875000,
    ]
    np.testing.assert_allclose(
        np.sort([v @ v for v in sbs.basis_set]), np.sort(basis_ref)
    )

    assert np.linalg.norm(sbs.basis_set) ** 2 == pytest.approx(13.0)

    np.testing.assert_array_equal(
        sbs.compact_compression_matrix.tocoo().row[[0, 6, 9]], [5, 32, 42]
    )
    norm = sbs.compression_matrix.data[[0, 6, 9]] @ [
        -0.1443375672974064,
        0.058925565098878946,
        0.0833333333333333,
    ]
    norm /= np.linalg.norm(sbs.compression_matrix.data[[0, 6, 9]])
    norm /= np.linalg.norm(
        [-0.1443375672974064, 0.058925565098878946, 0.0833333333333333]
    )
    assert norm == pytest.approx(1.0) or norm == pytest.approx(-1.0)

    np.testing.assert_array_equal(
        sbs.compression_matrix.tocoo().row[[0, 6, 9]], [5, 32, 42]
    )
    norm = sbs.compression_matrix.data[[0, 6, 9]] @ [
        -0.1443375672974064,
        0.058925565098878946,
        0.0833333333333333,
    ]
    norm /= np.linalg.norm(sbs.compression_matrix.data[[0, 6, 9]])
    norm /= np.linalg.norm(
        [-0.1443375672974064, 0.058925565098878946, 0.0833333333333333]
    )
    assert norm == pytest.approx(1.0) or norm == pytest.approx(-1.0)

    lat_trans_compr_matrix_O3 = get_lat_trans_compr_matrix_O3(
        sbs.translation_permutations
    )
    np.testing.assert_allclose(lat_trans_compr_matrix_O3.data, [0.5] * 13824)
    assert lat_trans_compr_matrix_O3.indices[-1] == 2726


def test_si_111_fc3(ph3_si_111_fc3: tuple[SymfcAtoms, np.ndarray, np.ndarray]):
    """Test fc2 and fc3 by Si-111-222 supercells and compared with ALM.

    This test with ALM is skipped when ALM is not installed.

    """
    supercell, displacements, forces = ph3_si_111_fc3
    basis_set_o2 = FCBasisSetO2(supercell, log_level=1).run()
    basis_set_o3 = FCBasisSetO3(supercell, log_level=1).run()
    fc_solver = FCSolverO2O3([basis_set_o2, basis_set_o3], log_level=1).solve(
        displacements, forces
    )
    fc2, fc3 = fc_solver.compact_fc
    fc2_ref = np.loadtxt(cwd / ".." / "compact_fc_Si_111_fc3_2.xz").reshape(fc2.shape)
    fc3_ref = np.loadtxt(cwd / ".." / "compact_fc_Si_111_fc3_3.xz").reshape(fc3.shape)
    np.testing.assert_allclose(fc2_ref, fc2, atol=1e-6)
    np.testing.assert_allclose(fc3_ref, fc3, atol=1e-6)


def test_fc_basis_set_o3_wurtzite():
    """Test symmetry adapted basis sets of FCBasisSetO3."""
    lattice = np.array(
        [
            [3.786186160293827, 0, 0],
            [-1.893093080146913, 3.278933398271515, 0],
            [0, 0, 6.212678269409001],
        ]
    )
    positions = np.array(
        [
            [0.333333333333333, 0.666666666666667, 0.002126465711614],
            [0.666666666666667, 0.333333333333333, 0.502126465711614],
            [0.333333333333333, 0.666666666666667, 0.376316514288389],
            [0.666666666666667, 0.333333333333333, 0.876316514288389],
        ]
    )
    numbers = [1, 1, 2, 2]
    supercell = SymfcAtoms(cell=lattice, scaled_positions=positions, numbers=numbers)
    sbs = FCBasisSetO3(supercell, log_level=1).run()

    assert sbs.basis_set.shape[0] == 40
    assert sbs.basis_set.shape[1] == 18
    compact_basis = sbs.compact_compression_matrix @ sbs.basis_set
    assert np.linalg.norm(compact_basis) ** 2 == pytest.approx(18.0)

    sbs = FCBasisSetO3(supercell, cutoff=3.0, log_level=1).run()
    assert sbs.basis_set.shape[0] == 22
    assert sbs.basis_set.shape[1] == 6
    compact_basis = sbs.compact_compression_matrix @ sbs.basis_set
    assert np.linalg.norm(compact_basis) ** 2 == pytest.approx(6.0)


def test_fc_basis_set_o3_diamond():
    """Test symmetry adapted basis sets of FCBasisSetO4."""
    a = 5.4335600299999998
    lattice = np.array([[a, 0, 0], [0, a, 0], [0, 0, a]])
    positions = np.array(
        [
            [0.875, 0.875, 0.875],
            [0.875, 0.375, 0.375],
            [0.375, 0.875, 0.375],
            [0.375, 0.375, 0.875],
            [0.125, 0.125, 0.125],
            [0.125, 0.625, 0.625],
            [0.625, 0.125, 0.625],
            [0.625, 0.625, 0.125],
        ]
    )
    numbers = [1, 1, 1, 1, 1, 1, 1, 1]
    supercell = SymfcAtoms(cell=lattice, scaled_positions=positions, numbers=numbers)
    sbs = FCBasisSetO3(supercell, log_level=1).run()

    assert sbs.basis_set.shape[0] == 17
    assert sbs.basis_set.shape[1] == 13
    compact_basis = sbs.compact_compression_matrix @ sbs.basis_set
    assert np.linalg.norm(compact_basis) ** 2 == pytest.approx(3.25)

    sbs = FCBasisSetO3(supercell, cutoff=3.5, log_level=1).run()
    assert sbs.basis_set.shape[0] == 5
    assert sbs.basis_set.shape[1] == 3
    compact_basis = sbs.compact_compression_matrix @ sbs.basis_set
    assert np.linalg.norm(compact_basis) ** 2 == pytest.approx(0.75)


def test_fc_basis_set_o3_wurtzite_332(cell_wurtzite_332: SymfcAtoms):
    """Test symmetry adapted basis sets of FCBasisSetO3."""
    supercell = cell_wurtzite_332

    """
    sbs = FCBasisSetO3(supercell, cutoff=6.0, log_level=1).run()
    assert sbs.basis_set.shape[0] == 2162
    assert sbs.basis_set.shape[1] == 1950
    """
    sbs = FCBasisSetO3(supercell, cutoff=5.0, log_level=1).run()
    assert sbs.basis_set.shape[0] == 698
    assert sbs.basis_set.shape[1] == 569

    sbs = FCBasisSetO3(supercell, cutoff=4.0, log_level=1).run()
    assert sbs.basis_set.shape[0] == 306
    assert sbs.basis_set.shape[1] == 218

    sbs = FCBasisSetO3(supercell, cutoff=3.8, log_level=1).run()
    assert sbs.basis_set.shape[0] == 270
    assert sbs.basis_set.shape[1] == 187

    sbs = FCBasisSetO3(supercell, cutoff=3.0, log_level=1).run()
    assert sbs.basis_set.shape[0] == 34
    assert sbs.basis_set.shape[1] == 9