|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | + |
| 4 | +from openmc_mcnp_adapter import rotation_matrix |
| 5 | + |
| 6 | + |
| 7 | +def is_orthogonal(R: np.ndarray, atol: float = 1e-12) -> bool: |
| 8 | + """Check if the matrix R is orthogonal""" |
| 9 | + I = np.identity(3) |
| 10 | + return np.allclose(R.T @ R, I, atol=atol) and np.allclose(R @ R.T, I, atol=atol) |
| 11 | + |
| 12 | + |
| 13 | +@pytest.mark.parametrize( |
| 14 | + "v1, v2", |
| 15 | + [ |
| 16 | + # Same direction, different magnitudes |
| 17 | + (np.array([1.0, 2.0, 3.0]), np.array([2.0, 4.0, 6.0])), |
| 18 | + (np.array([0.0, 0.0, 1.0]), np.array([0.0, 0.0, 10.0])), |
| 19 | + ], |
| 20 | +) |
| 21 | +def test_rotation_parallel(v1, v2): |
| 22 | + """Test rotation_matrix for parallel vectors""" |
| 23 | + R = rotation_matrix(v1, v2) |
| 24 | + # Should be exactly identity from the parallel branch |
| 25 | + assert np.allclose(R, np.identity(3)) |
| 26 | + assert is_orthogonal(R) |
| 27 | + assert np.isclose(np.linalg.det(R), 1.0) |
| 28 | + |
| 29 | + |
| 30 | +@pytest.mark.parametrize( |
| 31 | + "v1, v2", |
| 32 | + [ |
| 33 | + (np.array([0.0, 0.0, 1.0]), np.array([0.0, 0.0, -5.0])), # z -> -z |
| 34 | + (np.array([1.0, 0.0, 0.0]), np.array([-2.0, 0.0, 0.0])), # x -> -x |
| 35 | + ], |
| 36 | +) |
| 37 | +def test_rotation_antiparallel(v1, v2): |
| 38 | + """Test rotation_matrix for anti-parallel vectors""" |
| 39 | + R = rotation_matrix(v1, v2) |
| 40 | + |
| 41 | + # Maps v1 direction to v2 direction |
| 42 | + v2_hat = v2 / np.linalg.norm(v2) |
| 43 | + mapped = R @ (v1 / np.linalg.norm(v1)) |
| 44 | + assert np.allclose(mapped, v2_hat, atol=1e-12) |
| 45 | + |
| 46 | + # No NaNs, orthogonal and det +1 |
| 47 | + assert not np.isnan(R).any() |
| 48 | + assert is_orthogonal(R) |
| 49 | + assert np.isclose(np.linalg.det(R), 1.0, atol=1e-12) |
| 50 | + # 180-degree rotation has trace -1 |
| 51 | + assert np.isclose(np.trace(R), -1.0, atol=1e-12) |
| 52 | + |
| 53 | + |
| 54 | +@pytest.mark.parametrize( |
| 55 | + "v1, v2", |
| 56 | + [ |
| 57 | + (np.array([0.0, 0.0, 1.0]), np.array([1.0, 2.0, 3.0])), |
| 58 | + (np.array([0.0, 1.0, 0.0]), np.array([3.0, -1.0, 2.0])), |
| 59 | + ], |
| 60 | +) |
| 61 | +def test_rotation_general(v1, v2): |
| 62 | + """Test rotation_matrix for general vectors""" |
| 63 | + R = rotation_matrix(v1, v2) |
| 64 | + |
| 65 | + # Maps v1 to v2 direction |
| 66 | + v2_hat = v2 / np.linalg.norm(v2) |
| 67 | + mapped = R @ (v1 / np.linalg.norm(v1)) |
| 68 | + assert np.allclose(mapped, v2_hat, atol=1e-12) |
| 69 | + |
| 70 | + # Orthogonal and proper rotation |
| 71 | + assert is_orthogonal(R) |
| 72 | + assert np.isclose(np.linalg.det(R), 1.0, atol=1e-12) |
| 73 | + |
| 74 | + # A vector perpendicular to v1 remains perpendicular to R v1 (i.e., to v2_hat) |
| 75 | + # Use a simple perpendicular vector: pick the least-aligned basis axis and project out |
| 76 | + basis = [ |
| 77 | + np.array([1.0, 0.0, 0.0]), |
| 78 | + np.array([0.0, 1.0, 0.0]), |
| 79 | + np.array([0.0, 0.0, 1.0]) |
| 80 | + ] |
| 81 | + u1_hat = v1 / np.linalg.norm(v1) |
| 82 | + e = min(basis, key=lambda b: abs(np.dot(b, u1_hat))) |
| 83 | + x = e - np.dot(e, u1_hat) * u1_hat |
| 84 | + x /= np.linalg.norm(x) |
| 85 | + |
| 86 | + # Should be perpendicular to v2_hat |
| 87 | + assert np.isclose(np.dot(R @ x, v2_hat), 0.0, atol=1e-12) |
0 commit comments