File: test_force_constants.py

package info (click to toggle)
phonopy 2.44.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 29,136 kB
  • sloc: python: 42,934; xml: 12,080; ansic: 3,227; cpp: 525; sh: 213; makefile: 20
file content (159 lines) | stat: -rw-r--r-- 4,757 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
"""Tests for routines in force_constants.py."""

import numpy as np
import pytest

from phonopy import Phonopy
from phonopy.harmonic.force_constants import (
    cutoff_force_constants,
    rearrange_force_constants_array,
)
from phonopy.structure.atoms import PhonopyAtoms
from phonopy.structure.cells import get_primitive, isclose


def test_fc(ph_nacl: Phonopy):
    """Test of force constants calculation with fcsym by NaCl."""
    fc_1_10_ref = [
        -0.037549,
        0.000000,
        0.000000,
        0.000000,
        0.002415,
        -0.001746,
        0.000000,
        -0.001746,
        0.002415,
    ]
    fc_1_10 = ph_nacl.force_constants[1, 10]
    # print("".join(["%f, " % v for v in fc_1_10.ravel()]))
    np.testing.assert_allclose(fc_1_10.ravel(), fc_1_10_ref, atol=1e-5)


def test_fc_nofcsym(ph_nacl_nofcsym: Phonopy):
    """Test of force constants calculation without fcsym by NaCl."""
    fc_1_10_nofcsym_ref = [
        -0.005051,
        0.000000,
        0.000000,
        0.000000,
        0.094457,
        0.000000,
        0.000000,
        0.000000,
        -0.020424,
    ]

    fc_1_10 = ph_nacl_nofcsym.force_constants[1, 10]
    # print("".join(["%f, " % v for v in fc_1_10.ravel()]))
    np.testing.assert_allclose(fc_1_10.ravel(), fc_1_10_nofcsym_ref, atol=1e-5)


def test_fc_compact_fcsym(ph_nacl_compact_fcsym: Phonopy):
    """Test of force constants calculation in compact format with fcsym by NaCl."""
    fc_1_10_compact_fcsym_ref = [
        -0.004481,
        0.000000,
        0.000000,
        0.000000,
        0.095230,
        0.000000,
        0.000000,
        0.000000,
        -0.019893,
    ]

    fc_1_10 = ph_nacl_compact_fcsym.force_constants[1, 10]
    # print("".join(["%f, " % v for v in fc_1_10.ravel()]))
    np.testing.assert_allclose(fc_1_10.ravel(), fc_1_10_compact_fcsym_ref, atol=1e-5)


def test_fc_compact_symfc(ph_nacl_rd_symfc: Phonopy):
    """Test of force constants calculation in compact format using symfc by NaCl."""
    fc_1_10_compact_symfc_ref = [
        -6.777705e-03,
        0,
        0,
        0,
        9.591639e-02,
        0,
        0,
        0,
        -2.087195e-02,
    ]
    fc_1_10 = ph_nacl_rd_symfc.force_constants[1, 10]
    # print("".join(["%f, " % v for v in fc_1_10.ravel()]))
    np.testing.assert_allclose(fc_1_10.ravel(), fc_1_10_compact_symfc_ref, atol=1e-5)


@pytest.mark.parametrize("is_compact", [False, True])
def test_fc_cutoff_radius(ph_nacl: Phonopy, ph_nacl_compact_fcsym: Phonopy, is_compact):
    """Test of cutoff radius of force constants calculation by NaCl."""
    if is_compact:
        ph = ph_nacl_compact_fcsym
    else:
        ph = ph_nacl

    # Need restore fc because fc are overwritten.
    fc_orig = ph.force_constants.copy()
    ph.set_force_constants_zero_with_radius(4.0)
    changed = np.abs(fc_orig - ph.force_constants) > 1e-8
    ph.force_constants = fc_orig

    if is_compact:
        assert np.sum(changed) == 534
    else:
        assert np.sum(changed) == 17088


@pytest.mark.parametrize(
    "is_compact,store_dense_svecs",
    [(False, False), (False, True), (True, False), (True, True)],
)
def test_fc_cutoff_radius_svecs(
    ph_nacl: Phonopy, ph_nacl_compact_fcsym: Phonopy, is_compact, store_dense_svecs
):
    """Test of cutoff radius with dense-svecs format by NaCl."""
    if is_compact:
        ph = ph_nacl_compact_fcsym
    else:
        ph = ph_nacl

    fc = ph.force_constants.copy()
    primitive_matrix = np.dot(np.linalg.inv(ph.supercell_matrix), ph.primitive_matrix)
    primitive = get_primitive(
        ph.supercell, primitive_matrix, store_dense_svecs=store_dense_svecs
    )

    cutoff_force_constants(fc, ph.supercell, primitive, 4.0)
    changed = np.abs(ph.force_constants - fc) > 1e-8

    if is_compact:
        assert np.sum(changed) == 534
    else:
        assert np.sum(changed) == 17088


def test_rearrange_force_constants_array(
    nacl_unitcell_order1: PhonopyAtoms,
    nacl_unitcell_order2: PhonopyAtoms,
    ph_nacl: Phonopy,
):
    """Test of rearrange_force_constants_array."""
    ph1 = ph_nacl
    assert isclose(nacl_unitcell_order1, ph1.unitcell)
    fc = ph1.force_constants
    assert fc.shape[0] == fc.shape[1]
    ph2 = Phonopy(
        nacl_unitcell_order2,
        supercell_matrix=[2, 2, 2],
        primitive_matrix="F",
    )
    re_fc, indices = rearrange_force_constants_array(fc, ph1.supercell, ph2.supercell)
    ph1.run_qpoints([[0.5, 0.5, 0.5]])
    freq1 = ph1.get_qpoints_dict()["frequencies"]
    ph2.force_constants = re_fc
    ph2.run_qpoints([[0.5, 0.5, 0.5]])
    freq2 = ph2.get_qpoints_dict()["frequencies"]
    np.testing.assert_allclose(fc[indices[3], indices[3]], re_fc[3, 3])
    np.testing.assert_allclose(freq1, freq2)