File: test_sparse_solver.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 (55 lines) | stat: -rw-r--r-- 1,458 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
"""Tests of Symfc."""

from __future__ import annotations

from pathlib import Path

import numpy as np
from scipy.sparse import csr_array

from symfc import Symfc
from symfc.solvers.solver_O2 import run_solver_O2
from symfc.utils.utils import SymfcAtoms

cwd = Path(__file__).parent


def test_api_NaCl_222_with_dataset_fd(
    ph_nacl_222: tuple[SymfcAtoms, np.ndarray, np.ndarray],
):
    """Test solver with sparse displacements and forces as input."""
    supercell, displacements, forces = ph_nacl_222
    symfc = Symfc(
        supercell,
    )
    symfc.displacements = displacements
    symfc.forces = forces
    symfc.compute_basis_set(orders=(2,))

    n_data, n_atom, _ = forces.shape
    f = forces.reshape(n_data, -1)
    d = csr_array(displacements.reshape(n_data, -1))

    fc2_basis = symfc.basis_set[2]
    compress_mat_fc2 = fc2_basis.compact_compression_matrix
    basis_set_fc2 = fc2_basis.blocked_basis_set

    atomic_decompr_idx_fc2 = fc2_basis.atomic_decompr_idx

    coefs = run_solver_O2(
        d,
        f,
        compress_mat_fc2,
        basis_set_fc2,
        atomic_decompr_idx_fc2,
        use_sparse_disps=True,
        use_mkl=False,
    )

    fc = fc2_basis.blocked_basis_set.dot(coefs)
    fc = np.array(
        (compress_mat_fc2 @ fc).reshape((-1, n_atom, 3, 3)), dtype="double", order="C"
    )

    fc_ref = np.loadtxt(cwd / "compact_fc_NaCl_222.xz").reshape(fc.shape)
    np.testing.assert_allclose(fc, fc_ref)