From 439a77b8308deb6219054f311d78a5d067c032b5 Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Wed, 21 May 2025 13:35:45 +0200 Subject: [PATCH] Add diffraction conversion routines --- common/src/aaredaqlib/diffraction.py | 47 ++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 common/src/aaredaqlib/diffraction.py diff --git a/common/src/aaredaqlib/diffraction.py b/common/src/aaredaqlib/diffraction.py new file mode 100644 index 00000000..e5e2c733 --- /dev/null +++ b/common/src/aaredaqlib/diffraction.py @@ -0,0 +1,47 @@ +import math + + +def cal_res_A(detector_distance_mm: float, + wavelength_angstrom: float, + radius_mm: float) -> float: + """ + Calculate maximum achievable resolution in Angstroms. + + Args: + detector_distance_mm: Distance from crystal to detector in mm + wavelength_angstrom: X-ray wavelength in Angstroms + radius_mm: Detector radius in mm + + Returns: + Resolution in Angstroms + """ + # Calculate theta using arctangent of opposite/adjacent + theta = math.atan(radius_mm / detector_distance_mm) / 2.0 + + # Using Bragg's law: n𝜆 = 2d*sin(θ) + # Solving for d: d = 𝜆/(2*sin(θ)) + resolution = wavelength_angstrom / (2 * math.sin(theta)) + return resolution + + +def calc_dtz_mm(desired_resolution_angstrom: float, + wavelength_angstrom: float, + radius_mm: float) -> float: + """ + Calculate required detector distance for desired resolution. + + Args: + desired_resolution_angstrom: Desired resolution in Angstroms + wavelength_angstrom: X-ray wavelength in Angstroms + radius_mm: Detector radius in mm + + Returns: + Required detector distance in mm + """ + # From Bragg's law: sin(θ) = 𝜆/(2d) + theta = math.asin(wavelength_angstrom / (2 * desired_resolution_angstrom)) + + # Using tangent: tan(2θ) = opposite/adjacent + # Therefore: distance = radius/tan(2θ) + distance = radius_mm / math.tan(2 * theta) + return distance