Skip to content

Commit

Permalink
fix mypy in SupercellTransformation.from_boundary_distance
Browse files Browse the repository at this point in the history
  • Loading branch information
janosh committed Aug 14, 2023
1 parent d094726 commit fd33f04
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
14 changes: 7 additions & 7 deletions pymatgen/analysis/elasticity/elastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,24 +164,24 @@ def compliance_tensor(self):
return ComplianceTensor.from_voigt(s_voigt)

@property
def k_voigt(self):
def k_voigt(self) -> float:
"""Returns the K_v bulk modulus."""
return self.voigt[:3, :3].mean()

@property
def g_voigt(self):
def g_voigt(self) -> float:
"""Returns the G_v shear modulus."""
return (
2 * self.voigt[:3, :3].trace() - np.triu(self.voigt[:3, :3]).sum() + 3 * self.voigt[3:, 3:].trace()
) / 15.0

@property
def k_reuss(self):
def k_reuss(self) -> float:
"""Returns the K_r bulk modulus."""
return 1 / self.compliance_tensor.voigt[:3, :3].sum()

@property
def g_reuss(self):
def g_reuss(self) -> float:
"""Returns the G_r shear modulus."""
return 15 / (
8 * self.compliance_tensor.voigt[:3, :3].trace()
Expand All @@ -190,17 +190,17 @@ def g_reuss(self):
)

@property
def k_vrh(self):
def k_vrh(self) -> float:
"""Returns the K_vrh (Voigt-Reuss-Hill) average bulk modulus."""
return 0.5 * (self.k_voigt + self.k_reuss)

@property
def g_vrh(self):
def g_vrh(self) -> float:
"""Returns the G_vrh (Voigt-Reuss-Hill) average shear modulus."""
return 0.5 * (self.g_voigt + self.g_reuss)

@property
def y_mod(self):
def y_mod(self) -> float:
"""
Calculates Young's modulus (in SI units) using the
Voigt-Reuss-Hill averages of bulk and shear moduli.
Expand Down
10 changes: 5 additions & 5 deletions pymatgen/transformations/standard_transformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def from_scaling_factors(scale_a=1, scale_b=1, scale_c=1):

@staticmethod
def from_boundary_distance(
structure: Structure, min_boundary_dist: float = 6, allow_rotation: bool = False, max_atoms: int | None = None
structure: Structure, min_boundary_dist: float = 6, allow_rotation: bool = False, max_atoms: float = -1
) -> SupercellTransformation:
"""
Get a SupercellTransformation according to the desired minimum distance between periodic
Expand All @@ -264,11 +264,11 @@ def from_boundary_distance(
SupercellTransformation.
"""
min_expand = np.int8(min_boundary_dist / np.array([structure.lattice.d_hkl(plane) for plane in np.eye(3)]))
max_atoms = max_atoms or np.Inf
max_atoms = max_atoms if max_atoms > 0 else float("inf")

# Try to find a scaling_matrix satisfying the required boundary distance with smaller cell.
if allow_rotation and sum(min_expand != 0) > 1:
min1, min2, min3 = map(int, min_expand) # map(int) just for mypy's sake
min1, min2, min3 = map(int, min_expand) # type: ignore # map(int) just for mypy's sake
scaling_matrix = [
[min1 if min1 else 1, 1 if min1 and min2 else 0, 1 if min1 and min3 else 0],
[-1 if min2 and min1 else 0, min2 if min2 else 1, 1 if min2 and min3 else 0],
Expand All @@ -278,10 +278,10 @@ def from_boundary_distance(
min_expand_scaled = np.int8(
min_boundary_dist / np.array([struct_scaled.lattice.d_hkl(plane) for plane in np.eye(3)])
)
if np.count_nonzero(min_expand_scaled) == 0 and len(struct_scaled) <= max_atoms:
if sum(min_expand_scaled != 0) == 0 and len(struct_scaled) <= max_atoms:
return SupercellTransformation(scaling_matrix)

scaling_matrix = np.eye(3) + np.diag(min_expand)
scaling_matrix = np.eye(3) + np.diag(min_expand) # type: ignore[assignment]
struct_scaled = structure.make_supercell(scaling_matrix, in_place=False)
if len(struct_scaled) <= max_atoms:
return SupercellTransformation(scaling_matrix)
Expand Down

0 comments on commit fd33f04

Please sign in to comment.