diff --git a/image_analysis/bragg_prediction/BraggPredictionRot.cpp b/image_analysis/bragg_prediction/BraggPredictionRot.cpp index 181b7077b..022d9e596 100644 --- a/image_analysis/bragg_prediction/BraggPredictionRot.cpp +++ b/image_analysis/bragg_prediction/BraggPredictionRot.cpp @@ -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 diff --git a/image_analysis/bragg_prediction/BraggPredictionRotGPU.cu b/image_analysis/bragg_prediction/BraggPredictionRotGPU.cu index 31353f61f..1e5d9a39f 100644 --- a/image_analysis/bragg_prediction/BraggPredictionRotGPU.cu +++ b/image_analysis/bragg_prediction/BraggPredictionRotGPU.cu @@ -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 diff --git a/image_analysis/bragg_prediction/RockingSlice.h b/image_analysis/bragg_prediction/RockingSlice.h index 6178f6bac..adc514e9b 100644 --- a/image_analysis/bragg_prediction/RockingSlice.h +++ b/image_analysis/bragg_prediction/RockingSlice.h @@ -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) {