Example #1
0
def _test_disp_corr_matrix(ph3):
    supercell_phonon = get_supercell_phonon(ph3)
    uu = DispCorrMatrix(supercell_phonon)
    uu.run(300.0)

    sqrt_masses = np.repeat(np.sqrt(ph3.supercell.masses), 3)
    uu_inv = mass_inv(uu.psi_matrix, sqrt_masses)
    np.testing.assert_allclose(uu.upsilon_matrix, uu_inv, atol=1e-8, rtol=0)

    rd = RandomDisplacements(ph3.supercell, ph3.primitive, ph3.fc2)
    rd.run_correlation_matrix(300)
    rd_uu_inv = np.transpose(rd.uu_inv, axes=[0, 2, 1,
                                              3]).reshape(uu_inv.shape)
    np.testing.assert_allclose(uu.upsilon_matrix, rd_uu_inv, atol=1e-8, rtol=0)
    def test_NaCl(self):
        """Test by fixed random numbers of np.random.normal

        randn_ii and randn_ij were created by

            np.random.seed(seed=100)
            randn_ii = np.random.normal(size=(N_ii, 1, num_band))
            randn_ij = np.random.normal(size=(N_ij, 2, 1, num_band)).

        numpy v1.16.4 (py37h6b0580a_0) on macOS installed from conda-forge
        was used.

        """

        phonon = self._get_phonon_NaCl()
        rd = RandomDisplacements(phonon.supercell,
                                 phonon.primitive,
                                 phonon.force_constants,
                                 cutoff_frequency=0.01)
        num_band = len(phonon.primitive) * 3
        N = int(np.rint(phonon.supercell.volume / phonon.primitive.volume))
        N_ij = N - len(rd.qpoints)
        N_ii = N - N_ij * 2
        shape_ii = (N_ii, 1, num_band)
        randn_ii = np.fromstring(randn_ii_str.replace('\n', ' '),
                                 dtype=float,
                                 sep=' ').reshape(shape_ii)
        shape_ij = (N_ij, 2, 1, num_band)
        randn_ij = np.zeros(shape_ij, dtype=float)
        randn_ij[:, 0, 0, :] = np.fromstring(randn_ij_str_1.replace('\n', ' '),
                                             dtype=float,
                                             sep=' ').reshape(N_ij, num_band)
        randn_ij[:, 1, 0, :] = np.fromstring(randn_ij_str_2.replace('\n', ' '),
                                             dtype=float,
                                             sep=' ').reshape(N_ij, num_band)

        rd.run(500, randn=(randn_ii, randn_ij))

        data = np.fromstring(disp_str.replace('\n', ' '), dtype=float, sep=' ')
        np.testing.assert_allclose(data, rd.u.ravel(), atol=1e-5)
def test_random_displacements(ph_nacl):
    """Test by fixed random numbers of np.random.normal

    randn_ii and randn_ij were created by

        np.random.seed(seed=100)
        randn_ii = np.random.normal(size=(N_ii, 1, num_band))
        randn_ij = np.random.normal(size=(N_ij, 2, 1, num_band)).

    numpy v1.16.4 (py37h6b0580a_0) on macOS installed from conda-forge
    was used.

    """

    ph = ph_nacl
    rd = RandomDisplacements(ph.supercell,
                             ph.primitive,
                             ph.force_constants,
                             cutoff_frequency=0.01)
    num_band = len(ph.primitive) * 3
    N = len(ph.supercell) // len(ph.primitive)
    # N = N_ii + N_ij * 2
    # len(rd.qpoints) = N_ii + N_ij
    N_ij = N - len(rd.qpoints)
    N_ii = N - N_ij * 2
    shape_ii = (N_ii, 1, num_band)
    randn_ii = np.fromstring(randn_ii_str.replace('\n', ' '),
                             dtype=float, sep=' ').reshape(shape_ii)
    shape_ij = (N_ij, 2, 1, num_band)
    randn_ij = np.zeros(shape_ij, dtype=float)
    randn_ij[:, 0, 0, :] = np.fromstring(
        randn_ij_str_1.replace('\n', ' '),
        dtype=float, sep=' ').reshape(N_ij, num_band)
    randn_ij[:, 1, 0, :] = np.fromstring(
        randn_ij_str_2.replace('\n', ' '),
        dtype=float, sep=' ').reshape(N_ij, num_band)

    rd.run(500, randn=(randn_ii, randn_ij))

    # for line in rd.u.ravel().reshape(-1, 6):
    #     print(("%.7f, " * 6) % tuple(line))

    data = np.array(disp_ref)
    np.testing.assert_allclose(data, rd.u.ravel(), atol=1e-5)

    rd.run_d2f()
    np.testing.assert_allclose(rd.force_constants, ph.force_constants,
                               atol=1e-5, rtol=1e-5)

    rd = RandomDisplacements(ph.supercell,
                             ph.primitive,
                             ph.force_constants)
    rd.run_correlation_matrix(500)
    shape = (len(ph.supercell) * 3, len(ph.supercell) * 3)
    uu = np.transpose(rd.uu, axes=[0, 2, 1, 3]).reshape(shape)
    uu_inv = np.transpose(rd.uu_inv, axes=[0, 2, 1, 3]).reshape(shape)

    sqrt_masses = np.repeat(np.sqrt(ph.supercell.masses), 3)
    uu_bare = mass_sand(uu, sqrt_masses)
    uu_inv_bare = np.linalg.pinv(uu_bare)
    _uu_inv = mass_sand(uu_inv_bare, sqrt_masses)

    np.testing.assert_allclose(_uu_inv, uu_inv,
                               atol=1e-5, rtol=1e-5)
 def test_NaCl(self):
     phonon = self._get_phonon_NaCl()
     np.random.seed(19680801)
     rd = RandomDisplacements(phonon.dynamical_matrix,
                              cutoff_frequency=0.01)
def test_random_displacements_all_atoms_TiPN3(ph_tipn3):
    """Test by fixed random numbers of np.random.normal

    randn_ii and randn_ij were created by

        np.random.seed(seed=100)
        randn_ii = np.random.normal(size=(N_ii, 1, num_band))
        randn_ij = np.random.normal(size=(N_ij, 2, 1, num_band)).

    Precalculated eigenvectors are used because those depend on
    eigensolvers such as openblas, mkl, blis, or netlib. Eigenvalues
    are expected to be equivalent over the eigensolvers. So the
    eigenvalues calculated on the fly are used.

    """

    ph = ph_tipn3
    rd = RandomDisplacements(ph.supercell,
                             ph.primitive,
                             ph.force_constants)

    num_band = len(ph.primitive) * 3
    N = len(ph.supercell) // len(ph.primitive)
    # N = N_ii + N_ij * 2
    # len(rd.qpoints) = N_ii + N_ij
    N_ij = N - len(rd.qpoints)
    N_ii = N - N_ij * 2
    shape_ii = (N_ii, 1, num_band)
    shape_ij = (N_ij, 2, 1, num_band)
    randn_ii = np.reshape(randn_ii_TiPN3, shape_ii)
    randn_ij = np.reshape(randn_ij_TiPN3, shape_ij)

    eigvecs_ii = np.reshape(
        np.loadtxt(os.path.join(current_dir, "eigvecs_ii_TiPN3.txt")),
        (N_ii, num_band, num_band))
    eigvecs_ij = np.reshape(
        np.loadtxt(os.path.join(current_dir, "eigvecs_ij_TiPN3.txt"),
                   dtype=complex),
        (N_ij, num_band, num_band))
    rd._eigvecs_ii = eigvecs_ii
    rd._eigvecs_ij = eigvecs_ij
    rd.run(500, randn=(randn_ii, randn_ij))

    # for line in rd.u.ravel().reshape(-1, 6):
    #     print(("%.7f, " * 6) % tuple(line))

    data = np.array(disp_ref_TiPN3)
    np.testing.assert_allclose(data, rd.u.ravel(), atol=1e-5)

    rd.run_d2f()
    np.testing.assert_allclose(rd.force_constants, ph.force_constants,
                               atol=1e-5, rtol=1e-5)

    rd.run_correlation_matrix(500)
    shape = (len(ph.supercell) * 3, len(ph.supercell) * 3)
    uu = np.transpose(rd.uu, axes=[0, 2, 1, 3]).reshape(shape)
    uu_inv = np.transpose(rd.uu_inv, axes=[0, 2, 1, 3]).reshape(shape)

    sqrt_masses = np.repeat(np.sqrt(ph.supercell.masses), 3)
    uu_bare = _mass_sand(uu, sqrt_masses)
    uu_inv_bare = np.linalg.pinv(uu_bare)
    _uu_inv = _mass_sand(uu_inv_bare, sqrt_masses)

    np.testing.assert_allclose(_uu_inv, uu_inv,
                               atol=1e-5, rtol=1e-5)