forked from orest-d/p4vasp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix for issue orest-d#18 (orest-d#26)
We create new Kpoints classes to read and refine the data from the HDF5 file. This removes some logic from the Band class that more logically belongs to the Kpoints class. In the process, we change the logic such that different input modes for different KPOINTS files do not crash the Band class.
- Loading branch information
1 parent
e5789dc
commit b5647af
Showing
11 changed files
with
342 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from py4vasp.data import _util | ||
from py4vasp.exceptions import RefinementException | ||
import functools | ||
import numpy as np | ||
|
||
|
||
class Kpoints: | ||
def __init__(self, raw_kpoints): | ||
self._raw = raw_kpoints | ||
self._distances = None | ||
|
||
def read(self): | ||
return { | ||
"mode": self.mode(), | ||
"line_length": self.line_length(), | ||
"coordinates": self._raw.coordinates[:], | ||
"weights": self._raw.weights[:], | ||
"labels": self.labels(), | ||
} | ||
|
||
def line_length(self): | ||
if self.mode() == "line": | ||
return self._raw.number | ||
return len(self._raw.coordinates) | ||
|
||
def number_lines(self): | ||
return len(self._raw.coordinates) // self.line_length() | ||
|
||
def distances(self): | ||
if self._distances is not None: | ||
return self._distances | ||
cell = self._raw.cell.lattice_vectors * self._raw.cell.scale | ||
cartesian_kpoints = np.linalg.solve(cell, self._raw.coordinates[:].T).T | ||
kpoint_lines = np.split(cartesian_kpoints, self.number_lines()) | ||
kpoint_norms = [np.linalg.norm(line - line[0], axis=1) for line in kpoint_lines] | ||
concatenate_distances = lambda current, addition: ( | ||
np.concatenate((current, addition + current[-1])) | ||
) | ||
self._distances = functools.reduce(concatenate_distances, kpoint_norms) | ||
return self._distances | ||
|
||
def mode(self): | ||
mode = _util.decode_if_possible(self._raw.mode).strip() or "# empty string" | ||
first_char = mode[0].lower() | ||
if first_char == "a": | ||
return "automatic" | ||
elif first_char == "e": | ||
return "explicit" | ||
elif first_char == "g": | ||
return "gamma" | ||
elif first_char == "l": | ||
return "line" | ||
elif first_char == "m": | ||
return "monkhorst" | ||
else: | ||
raise RefinementException( | ||
"Could not understand the mode '{}' ".format(mode) | ||
+ "when refining the raw kpoints data." | ||
) | ||
|
||
def labels(self): | ||
if self._raw.labels is None or self._raw.label_indices is None: | ||
return None | ||
labels = [""] * len(self._raw.coordinates) | ||
use_line_mode = self.mode() == "line" | ||
for label, index in zip(self._raw.labels, self._raw.label_indices): | ||
label = _util.decode_if_possible(label.strip()) | ||
if use_line_mode: | ||
index = self.line_length() * (index // 2) + index % 2 | ||
index -= 1 # convert from Fortran to Python | ||
labels[index] = label | ||
return labels |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from .exceptions import * | ||
|
||
|
||
class Py4VaspException(Exception): | ||
"""Base class for all exceptions raised by py4vasp""" | ||
|
||
|
||
class RefinementException(Py4VaspException): | ||
"""When refining the raw dataclass into the class handling e.g. reading and | ||
plotting of the data an error occured""" |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.