Contain band indices in spectral function hdf5 file

This commit is contained in:
Atsushi Togo 2022-05-07 23:05:51 +09:00
parent 831fb9799c
commit 5e733eb629
8 changed files with 197 additions and 152 deletions

View File

@ -2051,6 +2051,7 @@ class Phono3py:
band_indices=self._band_indices,
write_txt=write_txt,
write_hdf5=write_hdf5,
output_filename=output_filename,
log_level=self._log_level,
)

View File

@ -46,7 +46,6 @@ from phonopy.units import Kb, THzToEv
from phono3py.conductivity.base import ConductivityBase, ConductivityMixIn
from phono3py.conductivity.utils import (
ConductivityLBTEWriter,
all_bands_exist,
select_colmat_solver,
set_collision_from_file,
)
@ -57,7 +56,7 @@ from phono3py.conductivity.wigner import (
)
from phono3py.file_IO import read_pp_from_hdf5
from phono3py.phonon3.collision_matrix import CollisionMatrix
from phono3py.phonon3.interaction import Interaction
from phono3py.phonon3.interaction import Interaction, all_bands_exist
from phono3py.phonon.grid import get_grid_points_by_rotations

View File

@ -45,7 +45,6 @@ from phono3py.conductivity.kubo import ConductivityKuboMixIn
from phono3py.conductivity.utils import (
ConductivityRTAWriter,
ShowCalcProgress,
all_bands_exist,
set_gamma_from_file,
)
from phono3py.conductivity.utils import write_pp as write_phph
@ -55,7 +54,7 @@ from phono3py.conductivity.wigner import (
)
from phono3py.file_IO import read_pp_from_hdf5
from phono3py.phonon3.imag_self_energy import ImagSelfEnergy, average_by_degeneracy
from phono3py.phonon3.interaction import Interaction
from phono3py.phonon3.interaction import Interaction, all_bands_exist
from phono3py.phonon.grid import get_grid_points_by_rotations

View File

@ -50,6 +50,7 @@ from phono3py.file_IO import (
write_pp_to_hdf5,
write_unitary_matrix_to_hdf5,
)
from phono3py.phonon3.interaction import all_bands_exist
from phono3py.phonon3.triplets import get_all_triplets
if TYPE_CHECKING:
@ -909,16 +910,6 @@ class ShowCalcProgress:
print("")
def all_bands_exist(interaction: Interaction):
"""Return if all bands are selected or not."""
band_indices = interaction.band_indices
num_band = len(interaction.primitive) * 3
if len(band_indices) == num_band:
if (band_indices - np.arange(num_band) == 0).all():
return True
return False
def write_pp(
conductivity: "ConductivityBase",
pp: Interaction,

View File

@ -718,6 +718,7 @@ def write_spectral_function_to_hdf5(
sigma=None,
frequency_points=None,
frequencies=None,
all_band_exist=False,
filename=None,
):
"""Wirte spectral functions (currently only bubble) in hdf5.
@ -729,10 +730,18 @@ def write_spectral_function_to_hdf5(
"""
full_filename = "spectral"
suffix = _get_filename_suffix(mesh, grid_point=grid_point, sigma=sigma)
if all_band_exist:
_band_indices = None
else:
_band_indices = np.hstack(band_indices).astype("int_")
suffix = _get_filename_suffix(
mesh, grid_point=grid_point, band_indices=_band_indices, sigma=sigma
)
_band_indices = np.array(band_indices, dtype="intc")
full_filename += suffix
if filename is not None:
full_filename += f".{filename}"
full_filename += ".hdf5"
with h5py.File(full_filename, "w") as w:

View File

@ -905,3 +905,13 @@ class Interaction:
self._eigenvectors = np.zeros(
(num_grid, num_band, num_band), dtype=("c%d" % (itemsize * 2)), order="C"
)
def all_bands_exist(interaction: Interaction):
"""Return if all bands are selected or not."""
band_indices = interaction.band_indices
num_band = len(interaction.primitive) * 3
if len(band_indices) == num_band:
if (band_indices - np.arange(num_band) == 0).all():
return True
return False

View File

@ -46,7 +46,7 @@ from phono3py.phonon3.imag_self_energy import (
get_frequency_points,
run_ise_at_frequency_points_batch,
)
from phono3py.phonon3.interaction import Interaction
from phono3py.phonon3.interaction import Interaction, all_bands_exist
from phono3py.phonon3.real_self_energy import imag_to_real
@ -135,38 +135,40 @@ def run_spectral_function(
pos = 0
for k in range(j):
pos += len(band_indices[k])
filename = write_spectral_function_at_grid_point(
gp,
bi,
spf.frequency_points,
spf_at_t[pos : (pos + len(bi))].sum(axis=0) / len(bi),
interaction.mesh_numbers,
t,
sigma=sigma,
filename=output_filename,
is_mesh_symmetry=interaction.is_mesh_symmetry,
)
if write_txt:
filename = write_spectral_function_at_grid_point(
gp,
bi,
spf.frequency_points,
spf_at_t[pos : (pos + len(bi))].sum(axis=0) / len(bi),
interaction.mesh_numbers,
t,
sigma=sigma,
filename=output_filename,
is_mesh_symmetry=interaction.is_mesh_symmetry,
)
if log_level:
print("Spectral functions were written to")
print('"%s".' % filename)
print(f'Spectral functions were written to "{filename}".')
filename = write_spectral_function_to_hdf5(
gp,
band_indices,
temperatures,
spf.spectral_functions[sigma_i, :, i],
spf.shifts[sigma_i, :, i],
spf.half_linewidths[sigma_i, :, i],
interaction.mesh_numbers,
interaction.bz_grid,
sigma=sigma,
frequency_points=spf.frequency_points,
frequencies=frequencies[gp],
filename=output_filename,
)
if write_hdf5:
filename = write_spectral_function_to_hdf5(
gp,
bi,
temperatures,
spf.spectral_functions[sigma_i, :, i],
spf.shifts[sigma_i, :, i],
spf.half_linewidths[sigma_i, :, i],
interaction.mesh_numbers,
interaction.bz_grid,
sigma=sigma,
frequency_points=spf.frequency_points,
frequencies=frequencies[gp],
all_band_exist=all_bands_exist(interaction),
filename=output_filename,
)
if log_level:
print('Spectral functions were stored in "%s".' % filename)
print(f'Spectral functions were stored in "{filename}".')
sys.stdout.flush()
return spf

View File

@ -1,6 +1,7 @@
"""Test spectral_function.py."""
import numpy as np
from phono3py import Phono3py
from phono3py.phonon3.spectral_function import SpectralFunction
shifts = [
@ -127,120 +128,120 @@ shifts = [
]
spec_funcs = [
-0.0000000,
-0.0000000,
0.0000000,
-0.0000000,
-0.0000000,
-0.0000000,
0.0000165,
0.0000165,
0.0022357,
0.0001249,
0.0001318,
0.0001318,
0.0000027,
0.0000027,
0.0000592,
0.0005056,
0.0004722,
0.0004722,
0.0000016,
0.0000016,
0.0000118,
0.0008887,
0.0008281,
0.0008281,
0.0000001,
0.0000001,
0.0000007,
0.0062806,
0.0053809,
0.0053809,
0.0000003,
0.0000003,
0.0000026,
0.0025466,
0.0035400,
0.0035400,
0.0000001,
0.0000001,
0.0000010,
0.0004469,
0.0002613,
0.0002613,
0.0000001,
0.0000001,
0.0000011,
0.0011874,
0.0012518,
0.0012518,
0.0000000,
0.0000000,
0.0000003,
0.0002964,
0.0003119,
0.0003119,
0.0000000,
0.0000000,
0.0000000,
0.0000000,
0.0000000,
0.0000000,
0.0000520,
0.0000520,
0.0070211,
0.0003925,
0.0004139,
0.0004139,
0.0000085,
0.0000085,
0.0001872,
0.0016104,
0.0014443,
0.0014443,
0.0000051,
0.0000051,
0.0000370,
0.0027822,
0.0025951,
0.0025951,
0.0000004,
0.0000004,
0.0000000,
-0.0000000,
0.0000000,
0.0000000,
0.0000000,
0.0000000,
0.0032185,
0.0005560,
0.0001434,
0.0001868,
0.0001602,
0.0001670,
0.0000726,
0.0002220,
0.0016122,
0.0004059,
0.0003461,
0.0003024,
0.0000878,
0.0001280,
0.0033720,
0.0027162,
0.0013271,
0.0011427,
0.0000049,
0.0000094,
0.0000892,
0.0137571,
0.0405108,
0.0173674,
0.0000052,
0.0000084,
0.0000567,
0.0005807,
0.0013438,
0.0014025,
0.0000021,
0.0197933,
0.0168956,
0.0168956,
0.0000034,
0.0000405,
0.0003176,
0.0003022,
0.0003523,
0.0000019,
0.0000034,
0.0000235,
0.0003419,
0.0008701,
0.0008436,
0.0000004,
0.0000010,
0.0000010,
0.0000082,
0.0080833,
0.0110838,
0.0110838,
0.0000002,
0.0000002,
0.0000031,
0.0014052,
0.0008202,
0.0008202,
0.0000002,
0.0000002,
0.0000035,
0.0037304,
0.0039325,
0.0039325,
0.0000000,
0.0000000,
0.0000009,
0.0009279,
0.0009800,
0.0009800,
0.0000000,
0.0000000,
0.0000000,
0.0000000,
0.0000000,
0.0000000,
0.0000000,
0.0000000,
0.0000000,
0.0000000,
0.0000000,
0.0000000,
0.0101136,
0.0017460,
0.0004489,
0.0005850,
0.0005048,
0.0005229,
0.0002283,
0.0006942,
0.0050470,
0.0012772,
0.0010867,
0.0009498,
0.0002702,
0.0004036,
0.0106017,
0.0086169,
0.0041489,
0.0035906,
0.0000154,
0.0000295,
0.0002803,
0.0434066,
0.1278558,
0.0549209,
0.0000166,
0.0000264,
0.0001776,
0.0018060,
0.0042557,
0.0043927,
0.0000066,
0.0000108,
0.0001284,
0.0010011,
0.0009471,
0.0011088,
0.0000059,
0.0000105,
0.0000738,
0.0010751,
0.0027300,
0.0026490,
0.0000012,
0.0000033,
0.0000504,
0.0005539,
0.0009128,
0.0009358,
0.0000162,
0.0001759,
0.0002910,
0.0002978,
0.0000000,
0.0000000,
0.0000000,
@ -250,7 +251,7 @@ spec_funcs = [
]
def test_SpectralFunction(si_pbesol):
def test_SpectralFunction(si_pbesol: Phono3py):
"""Spectral function of Si."""
si_pbesol.mesh_numbers = [9, 9, 9]
si_pbesol.init_phph_interaction()
@ -274,7 +275,40 @@ def test_SpectralFunction(si_pbesol):
)
np.testing.assert_allclose(
spec_funcs,
np.swapaxes(sf.spectral_functions * np.pi, -2, -1).ravel(),
np.swapaxes(sf.spectral_functions, -2, -1).ravel(),
atol=1e-2,
rtol=1e-2,
)
def test_SpectralFunction_band_indices(si_pbesol: Phono3py):
"""Spectral function of Si."""
si_pbesol.mesh_numbers = [9, 9, 9]
si_pbesol.band_indices = [[4, 5]]
si_pbesol.init_phph_interaction()
sf = SpectralFunction(
si_pbesol.phph_interaction,
si_pbesol.grid.grg2bzg[[1, 103]],
temperatures=[
300,
],
num_frequency_points=10,
log_level=1,
)
sf.run()
# for line in np.swapaxes(sf.spectral_functions, -2, -1).reshape(-1, 6):
# print(("%.7f, " * 6) % tuple(line))
# raise
np.testing.assert_allclose(
np.reshape(shifts, (-1, 6))[:, [4, 5]],
np.swapaxes(sf.shifts, -2, -1).reshape(-1, 2),
atol=1e-2,
)
np.testing.assert_allclose(
np.reshape(spec_funcs, (-1, 6))[:, [4, 5]],
np.swapaxes(sf.spectral_functions, -2, -1).reshape(-1, 2),
atol=1e-2,
rtol=1e-2,
)