Files
AareCommon/tests/test_diffraction_geometry.py
2026-07-02 12:52:45 +02:00

67 lines
1.8 KiB
Python

import pytest
from aarecommon.math.diffraction_geometry import DiffractionGeometry
@pytest.fixture
def sample_dg():
return DiffractionGeometry(
energy_keV=12.4,
dtz_mm=100.0,
pixel_size_mm=0.172,
beam_center_pxl=(1000.0, 1000.0),
detector_size_pxl=(2000, 2000),
detector_description="Eiger 16M",
detector_serial_number="E-123",
poni_rot1_rad=0.0,
poni_rot2_rad=0.0,
)
def test_detector_max_radius_pxl(sample_dg):
# center (1000, 1000), size (2000, 2000)
# x0 = 2000-1000 = 1000
# x1 = 1000
# y0 = 2000-1000 = 1000
# y1 = 1000
# max = 1000
assert sample_dg.detector_max_radius_pxl == 1000.0
def test_detector_radius_mm(sample_dg):
# 1000 * 0.172 = 172.0
assert sample_dg.detector_radius_mm == 172.0
def test_wavelength_angstrom(sample_dg):
# 12.398 / 12.4 = 0.9998387...
assert sample_dg.wavelength_angstrom == pytest.approx(0.9998387)
def test_resolution_angstrom(sample_dg):
# dtz = 100
# radius = 172
# theta = atan(172/100) * 0.5 = atan(1.72) * 0.5 = 1.044 * 0.5 = 0.522 rad
# res = 0.9998 / (2 * sin(0.522)) = 0.9998 / (2 * 0.498) = 1.003
res = sample_dg.resolution_angstrom(100.0)
assert res > 0
assert res == pytest.approx(1.002469, abs=1e-5)
with pytest.raises(ValueError):
sample_dg.resolution_angstrom(0)
def test_max_resolution_angstrom(sample_dg):
assert sample_dg.max_resolution_angstrom == sample_dg.resolution_angstrom(sample_dg.dtz_mm)
def test_calc_dtz_mm(sample_dg):
res = sample_dg.resolution_angstrom(100.0)
dtz = sample_dg.calc_dtz_mm(res)
assert dtz == pytest.approx(100.0)
with pytest.raises(ValueError):
sample_dg.calc_dtz_mm(-1)
# test x >= 1.0 case: wavelength / (2*res) >= 1.0 -> res <= wavelength / 2
assert sample_dg.calc_dtz_mm(0.0001) == 0.0