Coverage for dynasor/modes/band.py: 100%
55 statements
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-09 14:30 +0000
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-09 14:30 +0000
1from __future__ import annotations
2from typing import TYPE_CHECKING
3from numpy.typing import NDArray
4from ..units import radians_per_fs_to_THz
5from .complex_coordinate import ComplexCoordinate, Q, P, F
7if TYPE_CHECKING: # pragma: no cover
8 from .qpoint import QPoint
9 from .mode_projector import ModeProjector
12class Band:
13 """Represents properties of a single band belonging to a q-point.
15 To modify the coordinates `Q`, `P` and `F` use `band.Q` and see doc for
16 :class:`~dynasor.modes.complex_coordinate.ComplexCoordinate`.
18 Parameters
19 ----------
20 q_index
21 q-point index.
22 band_index
23 Band index.
24 mp
25 Mode projector.
26 """
27 def __init__(self, q_index: int, band_index: int, mp: ModeProjector):
28 self._q = q_index
29 self._s = band_index
30 self._mp = mp
32 def __repr__(self):
33 return str(self)
35 def __str__(self):
36 s = ['### Band ###']
37 s += [f'Band index: {self.index}']
38 s += [f'q-point: [{self.qpoint.index}] {self.qpoint.q_reduced}']
39 s += [f'Frequency: {self.omega * radians_per_fs_to_THz:.2f} THz']
40 s += ['Polarization:']
41 string = '\n'.join(s)
43 string += '\n' + _make_table(self.polarization)
44 string += f"""
45 {'Q':<20}{'P':<20}{'F':<20}
46Value: {self.Q.complex:<20.2f}{self.P.complex:<20.2f}{self.F.complex:<20.2f}
47Amplitude: {self.Q.amplitude:<20.2f}{self.P.amplitude:<20.2f}{self.F.amplitude:<20.2f}
48Phase: {self.Q.phase:<20.2f}{self.P.phase:<20.2f}{self.F.phase:<20.2f}
49Energy: {self.potential_energy:<20.2f}{self.kinetic_energy:<20.2f}{self.virial_energy:<20.2f}
50"""
51 return string
53 @property
54 def index(self) -> int:
55 """The index of the band at the q-point."""
56 return self._s
58 @property
59 def qpoint(self) -> 'QPoint':
60 """The q-point to which the band belongs."""
61 return self._mp[self._q]
63 @property
64 def omega(self) -> float:
65 """Frequency of this band in rad/fs."""
66 return self.qpoint.omegas[self.index]
68 @property
69 def polarization(self) -> NDArray[float]:
70 """Polarization vector of this band, shape ``(Np, 3)``."""
71 return self.qpoint.polarizations[self.index]
73 @property
74 def eigenmode(self) -> NDArray[float]:
75 """Eigenmode in the supercell for this band, shape ``(N, 3)``."""
76 return self.qpoint.eigenmodes[self.index]
78 @property
79 def potential_energy(self) -> float:
80 """Potential energy of this band in eV."""
81 return self.qpoint.potential_energies[self.index]
83 @property
84 def kinetic_energy(self) -> float:
85 """Kinetic energy of this band in eV."""
86 return self.qpoint.kinetic_energies[self.index]
88 @property
89 def virial_energy(self) -> complex:
90 """Virial energy of this band in eV.
92 See :attr:`ModeProjector.virial_energies
93 <dynasor.ModeProjector.virial_energies>` for why this is complex.
94 """
95 return self.qpoint.virial_energies[self.index]
97 @property
98 def Q(self) -> ComplexCoordinate:
99 """The mode coordinate."""
100 return Q(self.qpoint.index, self.index, self._mp)
102 @property
103 def P(self) -> ComplexCoordinate:
104 """The mode momentum."""
105 return P(self.qpoint.index, self.index, self._mp)
107 @property
108 def F(self) -> ComplexCoordinate:
109 """The mode force."""
110 return F(self.qpoint.index, self.index, self._mp)
113def _make_table(M: NDArray[float]) -> str:
114 """Format a 2D array as fixed-width rows, one per line."""
115 return '\n'.join(''.join(f'{e:<20.2f}' for e in row) for row in M)