Skip to content

Commit

Permalink
nest get_transformation_matrix inside method
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielYang59 committed Jun 18, 2024
1 parent 6e70e2b commit 2843854
Showing 1 changed file with 43 additions and 44 deletions.
87 changes: 43 additions & 44 deletions pymatgen/electronic_structure/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,48 +228,6 @@ def from_global_moment_and_saxis(
magmom = cls(global_moment)
return cls(magmom.get_moment(saxis=saxis), saxis=saxis)

@classmethod
def _get_transformation_matrix(
cls,
saxis: Vector3D,
) -> tuple[Vector3D, Vector3D, Vector3D]:
saxis = saxis / np.linalg.norm(saxis)

alpha = np.arctan2(saxis[1], saxis[0])
beta = np.arctan2(np.sqrt(saxis[0] ** 2 + saxis[1] ** 2), saxis[2])

cos_a = np.cos(alpha)
cos_b = np.cos(beta)
sin_a = np.sin(alpha)
sin_b = np.sin(beta)

return (
(cos_b * cos_a, -sin_a, sin_b * cos_a),
(cos_b * sin_a, cos_a, sin_b * sin_a),
(-sin_b, 0, cos_b),
)

@classmethod
def _get_transformation_matrix_inv(
cls,
saxis: Vector3D,
) -> tuple[Vector3D, Vector3D, Vector3D]:
saxis = saxis / np.linalg.norm(saxis)

alpha = np.arctan2(saxis[1], saxis[0])
beta = np.arctan2(np.sqrt(saxis[0] ** 2 + saxis[1] ** 2), saxis[2])

cos_a = np.cos(alpha)
cos_b = np.cos(beta)
sin_a = np.sin(alpha)
sin_b = np.sin(beta)

return (
(cos_b * cos_a, cos_b * sin_a, -sin_b),
(-sin_a, cos_a, 0),
(sin_b * cos_a, sin_b * sin_a, cos_b),
)

def get_moment(self, saxis: Vector3D = (0, 0, 1)) -> NDArray:
"""Get magnetic moment relative to a given spin quantization axis.
If no axis is provided, moment will be given relative to the
Expand All @@ -282,12 +240,53 @@ def get_moment(self, saxis: Vector3D = (0, 0, 1)) -> NDArray:
Returns:
NDArray of length 3.
"""

def get_transformation_matrix(
saxis: Vector3D,
) -> tuple[Vector3D, Vector3D, Vector3D]:
"""Get the matrix to transform spin axis to z-axis."""
saxis = saxis / np.linalg.norm(saxis)

alpha = np.arctan2(saxis[1], saxis[0])
beta = np.arctan2(np.sqrt(saxis[0] ** 2 + saxis[1] ** 2), saxis[2])

cos_a = np.cos(alpha)
cos_b = np.cos(beta)
sin_a = np.sin(alpha)
sin_b = np.sin(beta)

return (
(cos_b * cos_a, -sin_a, sin_b * cos_a),
(cos_b * sin_a, cos_a, sin_b * sin_a),
(-sin_b, 0, cos_b),
)

def get_transformation_matrix_inv(
saxis: Vector3D,
) -> tuple[Vector3D, Vector3D, Vector3D]:
"""Get the inverse of matrix to transform spin axis to z-axis."""
saxis = saxis / np.linalg.norm(saxis)

alpha = np.arctan2(saxis[1], saxis[0])
beta = np.arctan2(np.sqrt(saxis[0] ** 2 + saxis[1] ** 2), saxis[2])

cos_a = np.cos(alpha)
cos_b = np.cos(beta)
sin_a = np.sin(alpha)
sin_b = np.sin(beta)

return (
(cos_b * cos_a, cos_b * sin_a, -sin_b),
(-sin_a, cos_a, 0),
(sin_b * cos_a, sin_b * sin_a, cos_b),
)

# Transform to moment with spin axis (0, 0, 1)
trafo_mat_inv = self._get_transformation_matrix_inv(self.saxis)
trafo_mat_inv = get_transformation_matrix_inv(self.saxis)
moment = np.matmul(self.moment, trafo_mat_inv)

# Transform to new saxis
trafo_mat = self._get_transformation_matrix(saxis)
trafo_mat = get_transformation_matrix(saxis)
moment = np.matmul(moment, trafo_mat)

# Round small values to zero
Expand Down

0 comments on commit 2843854

Please sign in to comment.