Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor refactoring of direct solution code #317

Merged
merged 3 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 32 additions & 15 deletions phono3py/conductivity/direct_solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,21 @@
)
self.collision_matrix = collision_matrix

def get_collision_eigenvalues(self):
@property
def collision_eigenvalues(self):
"""Return eigenvalues of collision matrix."""
return self._collision_eigenvalues

def get_collision_eigenvalues(self):
"""Return eigenvalues of collision matrix."""
warnings.warn(

Check warning on line 187 in phono3py/conductivity/direct_solution.py

View check run for this annotation

Codecov / codecov/patch

phono3py/conductivity/direct_solution.py#L187

Added line #L187 was not covered by tests
"Use attribute, Conductivity_LBTE.collision_eigenvalues "
"instead of Conductivity_LBTE.get_collision_eigenvalues().",
DeprecationWarning,
stacklevel=2,
)
return self.collision_eigenvalues

Check warning on line 193 in phono3py/conductivity/direct_solution.py

View check run for this annotation

Codecov / codecov/patch

phono3py/conductivity/direct_solution.py#L193

Added line #L193 was not covered by tests

def get_frequencies_all(self):
"""Return phonon frequencies on GR-grid."""
return self._frequencies[self._pp.bz_grid.grg2bzg]
Expand Down Expand Up @@ -690,12 +701,12 @@
import phono3py._phono3py as phono3c

if self._log_level:
sys.stdout.write("- Making collision matrix symmetric " "(built-in) ")
sys.stdout.write("- Making collision matrix symmetric (built-in) ")
sys.stdout.flush()
phono3c.symmetrize_collision_matrix(self._collision_matrix)
except ImportError:
if self._log_level:
sys.stdout.write("- Making collision matrix symmetric " "(numpy) ")
sys.stdout.write("- Making collision matrix symmetric (numpy) ")

Check warning on line 709 in phono3py/conductivity/direct_solution.py

View check run for this annotation

Codecov / codecov/patch

phono3py/conductivity/direct_solution.py#L709

Added line #L709 was not covered by tests
sys.stdout.flush()

if self._is_reducible_collision_matrix:
Expand Down Expand Up @@ -819,14 +830,18 @@

start = time.time()

if self._pinv_method == 0:
eig_str = "abs(eig)"
else:
eig_str = "eig"
print(
f"Calculating pseudo-inv by ignoring {eig_str}<{self._pinv_cutoff:<.1e}",
end="",
)
if self._log_level:
if self._pinv_method == 0:
eig_str = "abs(eig)"
else:
eig_str = "eig"

Check warning on line 837 in phono3py/conductivity/direct_solution.py

View check run for this annotation

Codecov / codecov/patch

phono3py/conductivity/direct_solution.py#L837

Added line #L837 was not covered by tests
w = self._collision_eigenvalues[i_sigma, i_temp]
null_space = (np.abs(w) < self._pinv_cutoff).sum()
print(
f"Pinv by ignoring {null_space}/{len(w)} dims "
f"under {eig_str}<{self._pinv_cutoff:<.1e}",
end="",
)
if solver in [0, 1, 2, 3, 4, 5]:
if self._log_level:
print(" (np.dot) ", end="")
Expand Down Expand Up @@ -1866,13 +1881,14 @@
assert size == shape[1]

solver = select_colmat_solver(pinv_solver)
trace = np.trace(collision_matrices[i_sigma, i_temp].reshape(size, size))

# [1] dsyev: safer and slower than dsyevd and smallest memory usage
# [2] dsyevd: faster than dsyev and largest memory usage
if solver in [1, 2]:
if log_level:
routine = ["dsyev", "dsyevd"][solver - 1]
sys.stdout.write("Diagonalizing by lapacke %s... " % routine)
sys.stdout.write("Diagonalizing by lapacke %s ... " % routine)

Check warning on line 1891 in phono3py/conductivity/direct_solution.py

View check run for this annotation

Codecov / codecov/patch

phono3py/conductivity/direct_solution.py#L1891

Added line #L1891 was not covered by tests
sys.stdout.flush()
import phono3py._phono3py as phono3c

Expand All @@ -1890,29 +1906,30 @@
) # only diagonalization
elif solver == 3: # np.linalg.eigh depends on dsyevd.
if log_level:
sys.stdout.write("Diagonalizing by np.linalg.eigh... ")
sys.stdout.write("Diagonalize by np.linalg.eigh ")
sys.stdout.flush()
col_mat = collision_matrices[i_sigma, i_temp].reshape(size, size)
w, col_mat[:] = np.linalg.eigh(col_mat)

elif solver == 4: # fully scipy dsyev
if log_level:
sys.stdout.write("Diagonalizing by " "scipy.linalg.lapack.dsyev... ")
sys.stdout.write("Diagonalize by scipy.linalg.lapack.dsyev ")
sys.stdout.flush()
import scipy.linalg

col_mat = collision_matrices[i_sigma, i_temp].reshape(size, size)
w, _, info = scipy.linalg.lapack.dsyev(col_mat.T, overwrite_a=1)
elif solver == 5: # fully scipy dsyevd
if log_level:
sys.stdout.write("Diagonalizing by " "scipy.linalg.lapack.dsyevd... ")
sys.stdout.write("Diagnalize by scipy.linalg.lapack.dsyevd ")
sys.stdout.flush()
import scipy.linalg

col_mat = collision_matrices[i_sigma, i_temp].reshape(size, size)
w, _, info = scipy.linalg.lapack.dsyevd(col_mat.T, overwrite_a=1)

if log_level:
print(f"sum={w.sum():<.1e} d={trace - w.sum():<.1e} ", end="")
print("[%.3fs]" % (time.time() - start))
sys.stdout.flush()

Expand Down
2 changes: 1 addition & 1 deletion phono3py/conductivity/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@
mfp = lbte.get_mean_free_path()
boundary_mfp = lbte.boundary_mfp

coleigs = lbte.get_collision_eigenvalues()
coleigs = lbte.collision_eigenvalues

Check warning on line 553 in phono3py/conductivity/utils.py

View check run for this annotation

Codecov / codecov/patch

phono3py/conductivity/utils.py#L553

Added line #L553 was not covered by tests
# After kappa calculation, the variable is overwritten by unitary matrix
unitary_matrix = lbte.collision_matrix

Expand Down
1 change: 1 addition & 0 deletions phono3py/phonon/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,7 @@ def _set_mesh_numbers(
self._D_diag = length2mesh(
length, self._lattice, rotations=symmetry_dataset.rotations
)
self._D_diag = np.array(self._D_diag, dtype="long")
if num_values == 9:
self._run_grg(
symmetry_dataset,
Expand Down
Loading