graintrace.orientation_helper#
Orientation math helpers: Euler/matrix/MRP/quaternion conversions and misorientation.
Convention (normative, shared with MIDAS and neml2):
Euler angles are the standard Bunge triplet
(phi1, Phi, phi2).The orientation matrix is the ACTIVE rotation
R = Rz(phi1) Rx(Phi) Rz(phi2)(crystal -> sample:v_sample = R v_crystal). It equals MIDASEuler2OrientMat/ Grains.csvO11..O33(row-major) and NEPER’srotmat:active; the classical Bunge passive matrix isg = R^T.MRP is the neml2 v3 MRP of
R(tan(theta/4) * axis); a neml2 model’sorientationis exactly this, soeuler_to_mrpfeeds neml2 directly.Crystal symmetry acts on the RIGHT (equivalents
R S); a sample-frame rotation acts on the LEFT (R' = Q R). Misorientation ismin_k angle(R1^T R2 S_k).
In graintrace <= 0.2 the matrix was g and the MRP was mrp(g) == -mrp(R);
legacy_mrp_to_mrp() converts MRP files written by those versions.
- graintrace.orientation_helper.ROTATION_CONVENTION = 'active'#
active (crystal -> sample),
R = Rz(phi1) Rx(Phi) Rz(phi2)for Bunge Euler angles.- Type:
The rotation convention of every matrix/MRP/quaternion in graintrace
- graintrace.orientation_helper.euler_to_matrix(euler, convention='bunge', angle_type='degrees')[source]#
Convert Euler angles to active rotation matrices
R(crystal -> sample).Bunge angles give
R = Rz(phi1) Rx(Phi) Rz(phi2)(MIDASEuler2OrientMat, NEPERrotmat:active), built as a Z-X-Z composition ofMRP.from_axis_anglerotations througheuler_rodrigues. The classical Bunge (passive, sample -> crystal) matrix isR^T.
- graintrace.orientation_helper.matrix_to_euler(M, convention='bunge', angle_type='degrees')[source]#
Convert rotation matrices to Euler angles (inverse of
euler_to_matrix()).First and third angles are wrapped to
[0, 360)deg ([0, 2*pi)rad). At gimbal lockPhisnaps to 0 or 180 and the third Bunge angle is 0.
- graintrace.orientation_helper.matrix_to_mrp(M)[source]#
Convert rotation matrices (…, 3, 3) to neml2 v3 MRP (…, 3) (no transpose).
- graintrace.orientation_helper.euler_to_mrp(euler, convention='bunge', angle_type='degrees')[source]#
Convert Euler angles to neml2 v3 MRP (…, 3) of the active
R.This is the orientation a neml2 crystal-plasticity model expects (its
orientationstate / the CPFEori_rodriguesoutput).
- graintrace.orientation_helper.legacy_mrp_to_mrp(p)[source]#
Convert an MRP written by graintrace <= 0.2 to the current convention.
Older graintrace versions stored the MRP of the passive Bunge matrix
g(e.g.orientations_MRP.datfromeuler_to_mrp, NForientations.csv,tess_to_gnnfeatures). That is the inverse rotation of today’s MRP ofR = g^T, i.e.mrp(g) == -mrp(R), so the conversion is a negation.Note: CPFE results computed from such inverted orientations are physically different (every grain was loaded in the inverse orientation); rerun them rather than negating their outputs.
- graintrace.orientation_helper.quat_to_matrix(q)[source]#
Convert a scalar-first unit quaternion (…, 4)
(w, x, y, z)to a rotation matrix (…, 3, 3), via neml2quaternion_rotation_matrix.
- graintrace.orientation_helper.mrp_to_matrix(p)[source]#
Convert neml2 v3 MRP orientations (…, 3) to rotation matrices (…, 3, 3).
- graintrace.orientation_helper.mrp_to_euler(p, convention='bunge', angle_type='degrees')[source]#
Convert neml2 v3 MRP orientations (…, 3) to Euler angles (…, 3).
- graintrace.orientation_helper.perturb_orientation(euler, sigma_deg, convention='bunge', angle_type='degrees', rng=None)[source]#
Apply an independent random misorientation to each orientation.
Each orientation is composed with a random rotation about a uniformly distributed axis, with rotation angle drawn from
N(0, sigma_deg)(degrees).sigma_degis therefore the standard deviation of the applied rotation angle; the resulting fundamental-zone misorientation magnitude is|angle|. This is a proper (gimbal-free) perturbation, unlike scaling each Euler angle independently.sigma_deg <= 0returns the input orientations unchanged.- Parameters:
- Returns:
(…, 3) perturbed Euler angles in the same convention/units.
- Return type:
- graintrace.orientation_helper.symmetry_operators(symmetry)[source]#
Crystal symmetry operators as rotation matrices (nops, 3, 3).
- graintrace.orientation_helper.misorientation_matrix(R1, R2, symmetry='1', angle_type='degrees')[source]#
Misorientation angle between two sets of rotation matrices under symmetry.
min_k angle(R1^T R2 S_k)for active matrices with crystal symmetryS_kon the right (both-sided reductionS_i R1^T R2 S_j; equivalent result).- Parameters:
- Returns:
(N,) misorientation angles.
- Return type:
- graintrace.orientation_helper.move_to_fundamental_zone(R, symmetry='1')[source]#
Reduce rotation matrices to the fundamental zone under crystal symmetry.
Picks, per orientation, the symmetry-equivalent with the smallest rotation angle (largest trace of
R @ S; crystal symmetry acts on the right).
- graintrace.orientation_helper.average_orientation(euler, weights=None, convention='bunge', angle_type='degrees', symmetry='1')[source]#
Symmetry-aware weighted mean of a set of Euler orientations.
Every orientation is brought into the symmetry-equivalent closest to the highest-weight orientation, matrix-averaged (weighted), and re-projected onto SO(3) via SVD (the polar/chordal mean); the result is converted back to Euler angles in the given convention/units.
- Parameters:
euler (ndarray | list | Tensor) – (N, 3) Euler angles, or (3,) for a single orientation.
weights (ndarray | list | Tensor | None) – (N,) non-negative, finite weights (default: uniform). If they sum to zero (e.g. two zero-radius grains), the mean falls back to uniform weights instead of dividing by zero.
convention (str) – ‘bunge’, ‘kocks’, or ‘roe’.
angle_type (str) – ‘degrees’ or ‘radians’.
symmetry (str) – crystal symmetry in orbifold notation.
- Returns:
(3,) averaged Euler angles in the given convention/units.
- Raises:
ValueError – if
weightshas the wrong length, or holds a negative or non-finite value.- Return type:
- graintrace.orientation_helper.misorientation(e1, e2, angle_convention='bunge', angle_type='degrees', symmetry='1')[source]#
Compute misorientation between two sets of Euler angles
- Parameters:
- Keyword Arguments:
- Returns:
Nx1 array of misorientation angles
- Return type:
- graintrace.orientation_helper.load_orientation_matrices(df, field='O')[source]#
Load per-row active rotation matrices
R(…, 3, 3) from a dataframe.The columns must hold the ACTIVE matrix
R(crystal -> sample): MIDAS Grains.csvO11..O33or NEPERrotmat:active. A passive (NEPER defaultrotmat/rotmat:passive) matrix must be transposed first.Storage is assumed row-major:
{field}{i}{j}maps toM[i-1, j-1](and thefield=Nonepositional path reshapes the first 9 columns row-major). A column-major producer would silently yield the transpose.- Parameters:
df (pd.DataFrame) – Dataframe containing orientation data.
field (str, optional) – Column prefix for the 3x3 orientation matrix (columns
{field}{i}{j}, row-major). IfNone, the first 9 columns are used positionally (row-major). Defaults to ‘O’.
- Returns:
(n, 3, 3) rotation matrices.
- Return type:
- graintrace.orientation_helper.load_orientations(df, field='O')[source]#
Load orientations as neml2 v3 modified Rodrigues parameters (n, 3).
The matrix columns must hold the active
R(seeload_orientation_matrices()).- Parameters:
df (pd.DataFrame) – Dataframe containing orientation data.
field (str, optional) – Column name for orientation matrix. Defaults to ‘O’.
- Returns:
(n, 3) neml2 MRP orientations.
- Return type:
- graintrace.orientation_helper.load_orientations_mrp(df, field='O')#
Load orientations as neml2 v3 modified Rodrigues parameters (n, 3).
The matrix columns must hold the active
R(seeload_orientation_matrices()).- Parameters:
df (pd.DataFrame) – Dataframe containing orientation data.
field (str, optional) – Column name for orientation matrix. Defaults to ‘O’.
- Returns:
(n, 3) neml2 MRP orientations.
- Return type:
- graintrace.orientation_helper.matrix_to_quat(M)[source]#
Convert rotation matrices (…, 3, 3) to scalar-first unit quaternions (…, 4)
(w, x, y, z), via neml2 (MRP.from_matrix->to_quaternion).
- graintrace.orientation_helper.load_strains(df, field='eKen', factor=1e-06)[source]#
Load strains from dataframe and convert to torch tensor.
- Parameters:
- Returns:
Tensor containing strain data.
- Return type:
- graintrace.orientation_helper.load_weights(df, field='GrainRadius')[source]#
Load the grain volumes and convert to a torch tensor of weights.
- Parameters:
df (pd.DataFrame) – Dataframe containing grain volume data.
field (str, optional) – Column name for grain volume data. Defaults to ‘GrainRadius’.
- Returns:
Normalized weight tensor.
- Return type: