File: test_api.py

package info (click to toggle)
python-symfc 1.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,972 kB
  • sloc: python: 10,795; makefile: 12
file content (198 lines) | stat: -rw-r--r-- 7,688 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
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
"""Tests of Symfc."""

from __future__ import annotations

from pathlib import Path

import numpy as np
import pytest

from symfc import Symfc
from symfc.utils.cutoff_tools import FCCutoff
from symfc.utils.utils import SymfcAtoms

cwd = Path(__file__).parent


def test_api_NaCl_222(ph_nacl_222: tuple[SymfcAtoms, np.ndarray, np.ndarray]):
    """Test Symfc class."""
    supercell, displacements, forces = ph_nacl_222
    symfc = Symfc(supercell)
    symfc.compute_basis_set(2)
    symfc.displacements = displacements
    assert symfc.displacements is not None
    np.testing.assert_array_almost_equal(symfc.displacements, displacements)  # type: ignore
    symfc.forces = forces
    assert symfc.forces is not None
    np.testing.assert_array_almost_equal(symfc.forces, forces)  # type: ignore
    symfc.solve(2)
    fc = symfc.force_constants[2]
    fc_ref = np.loadtxt(cwd / "compact_fc_NaCl_222.xz").reshape(fc.shape)
    np.testing.assert_allclose(fc, fc_ref)


def test_api_NaCl_222_with_dataset(
    ph_nacl_222: tuple[SymfcAtoms, np.ndarray, np.ndarray],
):
    """Test Symfc class with displacements and forces as input.

    1. symfc.run()
    2. basis_set = symfc.basis_set.
    3. new_symfc.basis_set
    4. new_symfc.solve()

    """
    supercell, displacements, forces = ph_nacl_222
    symfc = Symfc(supercell)
    symfc.displacements = displacements
    symfc.forces = forces
    symfc.run(max_order=2)
    fc = symfc.force_constants[2]
    fc_ref = np.loadtxt(cwd / "compact_fc_NaCl_222.xz").reshape(fc.shape)
    np.testing.assert_allclose(fc, fc_ref)

    new_symfc = Symfc(
        supercell,
    )
    new_symfc.displacements = displacements
    new_symfc.forces = forces
    new_symfc.basis_set = symfc.basis_set
    new_symfc.solve(max_order=2)
    np.testing.assert_allclose(new_symfc.force_constants[2], fc_ref)


def test_api_NaCl_222_exception(ph_nacl_222: tuple[SymfcAtoms, np.ndarray, np.ndarray]):
    """Test Symfc class with displacements and forces as input."""
    supercell, _, _ = ph_nacl_222
    symfc = Symfc(supercell)
    symfc.compute_basis_set(2)
    with pytest.raises(RuntimeError):
        symfc.solve(2)


@pytest.mark.parametrize("is_compact_fc", [True, False])
def test_api_si_111_fc3(
    ph3_si_111_fc3: tuple[SymfcAtoms, np.ndarray, np.ndarray], is_compact_fc: bool
):
    """Test Symfc class with displacements and forces as input."""
    supercell, displacements, forces = ph3_si_111_fc3
    symfc = Symfc(supercell)
    symfc.displacements = displacements
    symfc.forces = forces
    symfc.run(max_order=3, is_compact_fc=is_compact_fc)
    fc2 = symfc.force_constants[2]
    fc3 = symfc.force_constants[3]

    if is_compact_fc:
        # np.savetxt(cwd / "compact_fc_Si_111_fc3_2.xz", fc2.ravel())
        # np.savetxt(cwd / "compact_fc_Si_111_fc3_3.xz", fc3.ravel())
        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)
    else:
        # np.savetxt(cwd / "full_fc_Si_111_fc3_2.xz", fc2.ravel())
        # np.savetxt(cwd / "full_fc_Si_111_fc3_3.xz", fc3.ravel())
        fc2_ref = np.loadtxt(cwd / "full_fc_Si_111_fc3_2.xz").reshape(fc2.shape)
        fc3_ref = np.loadtxt(cwd / "full_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)


@pytest.mark.parametrize("is_compact_fc", [True, False])
def test_api_si_111_fc4(
    ph3_si_111_fc3: tuple[SymfcAtoms, np.ndarray, np.ndarray], is_compact_fc: bool
):
    """Test Symfc class with displacements and forces as input."""
    supercell, displacements, forces = ph3_si_111_fc3
    symfc = Symfc(supercell)
    symfc.displacements = displacements
    symfc.forces = forces
    symfc.run(orders=[2, 3, 4], is_compact_fc=is_compact_fc)
    fc2 = symfc.force_constants[2]
    fc3 = symfc.force_constants[3]
    fc4 = symfc.force_constants[4]

    if is_compact_fc:
        # np.savetxt(cwd / "compact_fc_Si_111_fc4_2.xz", fc2.ravel())
        # np.savetxt(cwd / "compact_fc_Si_111_fc4_3.xz", fc3.ravel())
        # np.savetxt(cwd / "compact_fc_Si_111_fc4_4.xz", fc4.ravel())
        fc2_ref = np.loadtxt(cwd / "compact_fc_Si_111_fc4_2.xz").reshape(fc2.shape)
        fc3_ref = np.loadtxt(cwd / "compact_fc_Si_111_fc4_3.xz").reshape(fc3.shape)
        fc4_ref = np.loadtxt(cwd / "compact_fc_Si_111_fc4_4.xz").reshape(fc4.shape)
    else:
        # np.savetxt(cwd / "full_fc_Si_111_fc4_2.xz", fc2.ravel())
        # np.savetxt(cwd / "full_fc_Si_111_fc4_3.xz", fc3.ravel())
        # np.savetxt(cwd / "full_fc_Si_111_fc4_4.xz", fc4.ravel())
        fc2_ref = np.loadtxt(cwd / "full_fc_Si_111_fc4_2.xz").reshape(fc2.shape)
        fc3_ref = np.loadtxt(cwd / "full_fc_Si_111_fc4_3.xz").reshape(fc3.shape)
        fc4_ref = np.loadtxt(cwd / "full_fc_Si_111_fc4_4.xz").reshape(fc4.shape)
    np.testing.assert_allclose(fc2_ref, fc2, atol=1e-6)
    np.testing.assert_allclose(fc3_ref, fc3, atol=1e-6)
    np.testing.assert_allclose(fc4_ref, fc4, atol=1e-6)


@pytest.mark.parametrize("is_compact_fc", [False])
def test_api_si_111_fc4_step(
    ph3_si_111_fc3: tuple[SymfcAtoms, np.ndarray, np.ndarray], is_compact_fc: bool
):
    """Test Symfc class with displacements and forces as input."""
    supercell, displacements, forces = ph3_si_111_fc3
    symfc = Symfc(supercell)
    symfc.displacements = displacements
    symfc.forces = forces
    symfc.run(orders=[2], is_compact_fc=is_compact_fc)
    fc2 = symfc.force_constants[2]

    natom = fc2.shape[0]
    N3 = natom * 3
    fc2_mat = fc2.transpose((0, 2, 1, 3)).reshape((N3, N3))
    displacements_mat = displacements.reshape((-1, N3))
    forces_mat = forces.reshape((-1, N3))

    forces_mat -= -displacements_mat @ fc2_mat
    forces = forces_mat.reshape((-1, natom, 3))

    symfc = Symfc(supercell)
    symfc.displacements = displacements
    symfc.forces = forces
    symfc.run(orders=[3, 4], is_compact_fc=is_compact_fc)
    fc3 = symfc.force_constants[3]
    fc4 = symfc.force_constants[4]

    fc2_ref = np.loadtxt(cwd / "full_fc_Si_111_fc4_2.xz").reshape(fc2.shape)
    fc3_ref = np.loadtxt(cwd / "full_fc_Si_111_fc4_3.xz").reshape(fc3.shape)
    np.testing.assert_allclose(fc2_ref, fc2, atol=1e-1)
    np.testing.assert_allclose(fc3_ref, fc3, atol=1e-1)

    # np.savetxt(cwd / "full_fc_Si_111_fc4_step_2.xz", fc2.ravel())
    # np.savetxt(cwd / "full_fc_Si_111_fc4_step_3.xz", fc3.ravel())
    # np.savetxt(cwd / "full_fc_Si_111_fc4_step_4.xz", fc4.ravel())
    fc2_ref = np.loadtxt(cwd / "full_fc_Si_111_fc4_step_2.xz").reshape(fc2.shape)
    fc3_ref = np.loadtxt(cwd / "full_fc_Si_111_fc4_step_3.xz").reshape(fc3.shape)
    fc4_ref = np.loadtxt(cwd / "full_fc_Si_111_fc4_step_4.xz").reshape(fc4.shape)
    np.testing.assert_allclose(fc2_ref, fc2, atol=1e-6)
    np.testing.assert_allclose(fc3_ref, fc3, atol=1e-6)
    np.testing.assert_allclose(fc4_ref, fc4, atol=1e-6)


def test_api_estimate_basis_size_NaCl_222(
    cell_nacl_222: SymfcAtoms,
):
    """Test Symfc class."""
    symfc = Symfc(cell_nacl_222)
    ref_estimates = {
        2: 12,
        3: 768,
        4: 36864,
    }

    assert symfc.estimate_basis_size(orders=[2, 3, 4]) == ref_estimates
    assert symfc.estimate_basis_size(max_order=4) == ref_estimates


def test_api_NaCl_222_cutoff_attribute(cell_nacl_222: SymfcAtoms):
    """Test accessing FCCutoff via Symfc class."""
    symfc = Symfc(cell_nacl_222, cutoff={3: 4.0})
    symfc.compute_basis_set(3)
    fc_cutoff = symfc.basis_set[3].fc_cutoff
    assert isinstance(fc_cutoff, FCCutoff)
    assert fc_cutoff.nonzero_atomic_indices_fc3().shape[0] == len(cell_nacl_222) ** 3