Bragg prediction: rotation partiality in the tail from erfc, the same on CPU and GPU

Last-bit change, not bit-exact: every partial whose frame does not contain its exact diffracting
condition gets a partiality - and so a slice-centroid walk along its Debye ring - that differs in
the last bits.

The partiality was (erf(b) - erf(a)) / 2 in float. Where the frame's slice lies in one tail of the
rocking curve both erf are near +-1, and their difference keeps only a few ulps of 1: glibc erf and
CUDA erff, one ulp apart, gave partialities ~1e-3 apart (relative) at p ~ 3e-5. The slice centroid
(RockingSliceCentroid_rad) divides by that partiality, and at zeta ~ 0.05 the walk it scales is
hundreds of pixels, so CPU and GPU placed such a reflection up to 0.1 px apart -
BraggPrediction_CPU_GPU_consistency_large_cell failed on one of them once --fmad=false (7ed970503)
moved the ulps. A slice in one tail now takes the same difference from erfcf, which keeps its relative
precision there; one helper (RockingSlicePartiality) serves both predictors.

Reflections placed more than 0.01 px apart by CPU and GPU in that test: 65 -> 0. BraggPrediction*,
BraggPredictionRot*, BraggPredictionRotGPU*, RockingSlice* pass. 7qij image passes 5.68/7.90 s ->
5.26/7.50 s (one lock hold; no cost).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EizBKhTqrAYago9KJG3dA3
This commit is contained in:
leonarski_fandClaude Opus 5.5 committed 2026-10-13 16:10:38 +02:00
1 parent 7d64a62872
commit 0b86ef5dab
3 files changed
+14 -4

No files matched your search

@@ -249,8 +249,7 @@ int BraggPredictionRot::Calc(const DiffractionExperiment &experiment, const Crys
const float lorentz_reciprocal = std::fabs(m2 * (S % S0)) / (S.Length() * S0.Length());
const float c1 = zeta_abs / (std::sqrt(2.0f) * mos_eff_rad);
const float partiality = (std::erf((phi + half_wedge_angle_rad) * c1)
- std::erf((phi - half_wedge_angle_rad) * c1)) / 2.0f;
const float partiality = RockingSlicePartiality(phi, half_wedge_angle_rad, c1);
// Place the partial where this frame recorded it: at the centre of the frame's slice of
// the rocking curve, which is the exact-condition position walked along its Debye ring by
// the rotation between the two (RockingSlice.h). Walking along the ring is turning S
@@ -154,8 +154,7 @@ namespace {
// c1 = sqrt(2) * sigma / zeta, where sigma = mosaicity
float c1 = zeta_abs / (sqrtf(2.0f) * mos_eff_rad);
float half_wedge = C.wedge_angle_rad / 2.0f;
float partiality = (erff((phi + half_wedge) * c1)
- erff((phi - half_wedge) * c1)) / 2.0f;
float partiality = RockingSlicePartiality(phi, half_wedge, c1);
// The partial sits at the centre of the frame's slice of its rocking curve, walked along
@@ -32,6 +32,18 @@
#define ROCKING_SLICE_HD inline
#endif
// The frame's slice of the rocking curve, (erf(b) - erf(a)) / 2 with a = (phi - w/2) c1, b = (phi + w/2) c1.
// Where the slice lies in one tail of the curve both erf are near +-1 and their difference cancels to a
// few float ulps of 1, so the same difference is taken from erfc, which keeps its relative precision there.
ROCKING_SLICE_HD float RockingSlicePartiality(float phi, float half_wedge, float c1) {
const float a = (phi - half_wedge) * c1, b = (phi + half_wedge) * c1;
if (a > 0.0f)
return (erfcf(a) - erfcf(b)) / 2.0f;
if (b < 0.0f)
return (erfcf(-b) - erfcf(-a)) / 2.0f;
return (erff(b) - erff(a)) / 2.0f;
}
// Flux-weighted centre of the frame's slice of the rocking curve, relative to the exact diffracting
// condition, in radians of rotation. partiality is the slice's own mass (the caller has it already).
ROCKING_SLICE_HD float RockingSliceCentroid_rad(float phi, float half_wedge, float c1, float partiality) {