// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute // SPDX-License-Identifier: GPL-3.0-only #include #include #include "SweepCoverage.h" #include "../../common/JFJochMath.h" namespace { // The proper rotations of the point group, identity included, as gemmi row-convention matrices // on Miller indices (h' = h W). Friedel is added at the orbit, not here. std::vector, 3>> ProperRotations(const gemmi::SpaceGroup &sg) { std::vector, 3>> rot; for (const auto &op : sg.operations().derive_symmorphic().sym_ops) { const auto &w = op.rot; const double det = static_cast(w[0][0]) * (w[1][1] * w[2][2] - w[1][2] * w[2][1]) - static_cast(w[0][1]) * (w[1][0] * w[2][2] - w[1][2] * w[2][0]) + static_cast(w[0][2]) * (w[1][0] * w[2][1] - w[1][1] * w[2][0]); if (det <= 0) continue; std::array, 3> m{}; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) m[i][j] = static_cast(w[i][j]) / gemmi::Op::DEN; rot.push_back(m); } return rot; } double Mod360(double a) { a = std::fmod(a, 360.0); return a < 0 ? a + 360.0 : a; } } std::vector EwaldCrossingAngles_deg(const Coord &s, const Coord &u, const Coord &s0) { // The crystal turned by -phi: s(phi) = s_par + s_perp cos phi - (u x s_perp) sin phi, in reflecting // position when 2 s(phi).s0 + |s|^2 = 0, i.e. A cos phi - B sin phi = C = rho cos(phi + delta). const Coord s_par = u * (u * s), s_perp = s - s_par; const double A = 2.0 * (s_perp * s0), B = 2.0 * ((u % s_perp) * s0); const double C = -static_cast(s * s) - 2.0 * (s_par * s0); const double rho = std::sqrt(A * A + B * B); if (rho < 1e-12 || std::fabs(C) > rho) return {}; const double delta = std::atan2(B, A) * 180.0 / PI; const double half = std::acos(C / rho) * 180.0 / PI; return {Mod360(-delta + half), Mod360(-delta - half)}; } std::optional SweepCoverageForecast::DegreesTo(double target_fraction) const { for (size_t k = 0; k < fraction_at.size(); k++) if (fraction_at[k] >= target_fraction) return static_cast(k); return {}; } std::optional ForecastSweepCoverage(const gemmi::SpaceGroup &sg, const CrystalLattice &lattice, const GoniometerAxis &gonio, const Coord &s0, double d_min_A, double start_deg, double end_deg) { const Coord spindle = gonio.GetAxis(); if (spindle.Length() < 1e-9f || d_min_A <= 0 || !(end_deg > start_deg) || s0.Length() < 1e-9f) return {}; const Coord u = spindle.Normalize(); const double sweep = end_deg - start_deg; const auto rot = ProperRotations(sg); const Coord a = lattice.Vec0(), b = lattice.Vec1(), c = lattice.Vec2(); const Coord as = lattice.Astar(), bs = lattice.Bstar(), cs = lattice.Cstar(); // Uniform over reciprocal space to d_min: a spiral of directions, radii at the volume quantiles. constexpr int N_DIRECTIONS = 1536, N_RADII = 8; const double golden_angle = PI * (3.0 - std::sqrt(5.0)); const double q_max = 1.0 / d_min_A; std::vector reached(361, 0); // reached[k]: measured by [start, end + k] int64_t measurable = 0, total = 0; for (int i = 0; i < N_DIRECTIONS; i++) { const double z = 1.0 - 2.0 * (static_cast(i) + 0.5) / N_DIRECTIONS; const double r = std::sqrt(std::max(0.0, 1.0 - z * z)); const double phi = golden_angle * i; const Coord p(static_cast(r * std::cos(phi)), static_cast(r * std::sin(phi)), static_cast(z)); const double h0 = a * p, h1 = b * p, h2 = c * p; for (int k = 0; k < N_RADII; k++) { const double q = q_max * std::cbrt((static_cast(k) + 0.5) / N_RADII); ++total; bool measured = false, crosses = false; double extension = 360.0; for (const auto &m : rot) { const double g0 = h0 * m[0][0] + h1 * m[1][0] + h2 * m[2][0]; const double g1 = h0 * m[0][1] + h1 * m[1][1] + h2 * m[2][1]; const double g2 = h0 * m[0][2] + h1 * m[1][2] + h2 * m[2][2]; const Coord dir = as * static_cast(g0) + bs * static_cast(g1) + cs * static_cast(g2); for (const float sign : {1.0f, -1.0f}) { // Friedel const Coord s = dir.Normalize() * static_cast(sign * q); for (const double root : EwaldCrossingAngles_deg(s, u, s0)) { crosses = true; const double from_start = Mod360(root - start_deg); if (from_start <= sweep) measured = true; else extension = std::min(extension, Mod360(root - end_deg)); } } } if (!crosses) continue; ++measurable; const int first = measured ? 0 : static_cast(std::ceil(extension)); for (int e = std::min(first, 360); e <= 360; e++) ++reached[e]; } } if (total == 0) return {}; SweepCoverageForecast out; out.fraction_at.resize(361); for (int e = 0; e <= 360; e++) out.fraction_at[e] = static_cast(reached[e]) / static_cast(total); out.measured_fraction = out.fraction_at[0]; out.asymptote_fraction = static_cast(measurable) / static_cast(total); return out; } double SecondSweepTilt_deg(double wavelength_A, double d_min_A, double max_lost_fraction) { // A reciprocal point at angle a from a spindle is blind to a sweep about it past q = 2 sin(a) / lambda. // Count, uniformly over reciprocal space to d_min, the points blind to the spindle u and to u tilted // by `tilt` alike; by the cones' symmetry about their axes only the angle between the two matters. constexpr int N_DIRECTIONS = 4096, N_RADII = 16; const double golden_angle = PI * (3.0 - std::sqrt(5.0)); for (int tilt = 1; tilt <= 90; tilt++) { const double t = tilt * PI / 180.0; int64_t lost = 0; for (int i = 0; i < N_DIRECTIONS; i++) { const double z = 1.0 - 2.0 * (static_cast(i) + 0.5) / N_DIRECTIONS; const double r = std::sqrt(std::max(0.0, 1.0 - z * z)); const double x = r * std::cos(golden_angle * i); const double sin_a1 = r; // from u = (0, 0, 1) const double c2 = x * std::sin(t) + z * std::cos(t); // from u tilted towards x const double sin_a2 = std::sqrt(std::max(0.0, 1.0 - c2 * c2)); for (int k = 0; k < N_RADII; k++) { const double q = std::cbrt((static_cast(k) + 0.5) / N_RADII) / d_min_A; const double half = q * wavelength_A / 2.0; if (sin_a1 < half && sin_a2 < half) ++lost; } } if (static_cast(lost) / (static_cast(N_DIRECTIONS) * N_RADII) <= max_lost_fraction) return tilt; } return 90.0; }