Build Packages / Build documentation (push) Successful in 46s
Build Packages / Create release (push) Successful in 8s
Build Packages / build:portable:macos-arm64:nocuda (push) Successful in 5m25s
Build Packages / build:rugnux:linux-aarch64:cuda (push) Successful in 9m46s
Build Packages / build:portable:linux-x86_64:nocuda (push) Successful in 13m25s
Build Packages / build:portable:linux-x86_64:cuda (push) Successful in 16m41s
Build Packages / build:portable:windows-x86_64:nocuda (push) Successful in 23m46s
Build Packages / HDF5 consumer tests (DIALS, XDS) (push) Successful in 25m37s
Build Packages / build:jfjoch:rocky8:nocuda (push) Successful in 24m31s
Build Packages / build:portable:windows-x86_64:cuda (push) Successful in 28m9s
Build Packages / build:jfjoch:ubuntu2204:nocuda (push) Successful in 20m47s
Build Packages / build:jfjoch:rocky9:nocuda (push) Successful in 26m21s
Build Packages / build:jfjoch:ubuntu2404:nocuda (push) Successful in 27m36s
Build Packages / Generate python client (push) Successful in 31s
Build Packages / build:jfjoch:rocky8:cuda-sls9 (push) Successful in 29m27s
Build Packages / build:jfjoch:rocky9:cuda-sls9 (push) Successful in 31m46s
Build Packages / build:jfjoch:ubuntu2204:cuda (push) Successful in 25m22s
Build Packages / build:jfjoch:rocky8:cuda (push) Successful in 30m36s
Build Packages / build:jfjoch:rocky9:cuda (push) Successful in 31m49s
Build Packages / build:jfjoch:ubuntu2404:cuda (push) Successful in 23m57s
Build Packages / Unit tests (push) Successful in 1h47m52s
Build Packages / Validation Battery (push) Successful in 44m55s
* Rugnux: Rotation processing recovers most of the speed lost since 1.0.0-rc.180; live processing in jfjoch_broker finishes sooner. * Rugnux: More reliable rotation processing and indexing, including small molecules and supercells. * Rugnux: More robust serial data and model refinement; molecular replacement handles multi-chain models. * jfjoch_broker (experimental): Live serial processing keeps the detector geometry fixed for the whole run. * jfjoch_broker: Pixel sum without an FPGA, a mask bit for inactive pixels, and several stability fixes. * jfjoch_broker: Rotation indexing and quick integration moved to rugnux live mode; their API settings are deprecated and ignored. * jfjoch_broker, Rugnux: Azimuthal integration can use logarithmic (log10) Q bins or a given number of bins, set from the API, the web interface and the Rugnux command line. * jfjoch_viewer: Build and display fixes; the README is rewritten. Reviewed-on: #95 Co-authored-by: Filip Leonarski <filip.leonarski@psi.ch>
103 lines
5.6 KiB
C++
103 lines
5.6 KiB
C++
// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
#pragma once
|
|
|
|
#include <array>
|
|
#include <cstdint>
|
|
#include <optional>
|
|
#include <span>
|
|
#include <vector>
|
|
|
|
#include <Eigen/Dense>
|
|
|
|
#include "../../common/UnitCell.h"
|
|
#include "../IntegrationOutcome.h"
|
|
#include "gemmi/symmetry.hpp"
|
|
|
|
// The e.s.d.s of the unit cell, as SHELXL's ZERR and a CIF's _cell.*_esd ask for them.
|
|
//
|
|
// The cell is fitted to d-spacings: each strong integrated observation gives |q| = 1/d at its
|
|
// observed centroid, which depends on the detector geometry alone (not on the orientation, nor on the
|
|
// rotation angle), and q^2 = h^T G* h is LINEAR in the reciprocal metric tensor G*. So the fit is a
|
|
// plain linear least squares in the components of G* the point group leaves free, and its covariance,
|
|
// scaled by the goodness of fit, maps onto a, b, c, alpha, beta, gamma through the derivatives of the
|
|
// cell with respect to G*. A length the group ties to another gets the same e.s.d., an angle it fixes
|
|
// gets zero.
|
|
//
|
|
// The distance the observations were taken at is uncertain too, and a cell measured at a wrong distance is
|
|
// scaled with it - the two are correlated near 1 at low angle. So the least-squares fit frees the distance
|
|
// alongside the metric (see fit_distance below) and the cell's e.s.d.s are those of the metric with the
|
|
// distance free: the distance's uncertainty, as far as the spacings themselves can measure it, propagated
|
|
// through its correlation with the cell.
|
|
//
|
|
// Least-squares cell e.s.d.s are known to be too small (Herbstein (2000) Acta Cryst. B56, 547-557):
|
|
// they see only the scatter of the centroids, not what changes over the sweep. The same fit is
|
|
// therefore also made on blocks of consecutive frames, and the scatter of the block cells, over the
|
|
// square root of their number, is the second estimate. The e.s.d. written is the larger of the two.
|
|
|
|
// One observation: the Miller index in the setting of the cell, and |q| = 1/d at the observed
|
|
// centroid, in 1/A.
|
|
struct CellObservation {
|
|
int32_t h = 0, k = 0, l = 0;
|
|
float q = 0.0f;
|
|
int32_t frame = 0;
|
|
// How |q| at the observed centroid moves with the detector distance: d|q| / d(ln distance), in 1/A.
|
|
float dq_dlnd = 0.0f;
|
|
};
|
|
|
|
struct CellEsd {
|
|
std::array<double, 6> esd{}; // written: per parameter the larger of the two below
|
|
std::array<double, 6> esd_lsq{}; // least squares, scaled by the goodness of fit
|
|
std::optional<std::array<double, 6>> esd_blocks; // block scatter / sqrt(blocks)
|
|
int blocks = 0;
|
|
int64_t observations = 0; // observations in the fit, after rejection
|
|
int64_t reflections = 0; // distinct h k l among them (_cell_measurement.reflns_used)
|
|
double theta_min_deg = 0.0;
|
|
double theta_max_deg = 0.0;
|
|
UnitCell fitted{}; // the d-spacing fit's own cell, for comparison with the reported one
|
|
// The detector distance as the spacings measure it, relative to the observations' own (FitReciprocalMetric,
|
|
// fit_distance), and its e.s.d.: the distance uncertainty esd_lsq carries.
|
|
double distance_rel = 0.0, distance_rel_esd = 0.0;
|
|
};
|
|
|
|
// Symmetric 3x3 matrices spanning the reciprocal metrics G* the point group of `sg` allows
|
|
// (G* = R G* R^T for every rotation part R): 6 for triclinic down to 1 for cubic. Orthonormal as
|
|
// 6-vectors (g11, g22, g33, g12, g13, g23).
|
|
std::vector<Eigen::Matrix3d> MetricBasis(const gemmi::SpaceGroup &sg);
|
|
|
|
// a, b, c (A) and alpha, beta, gamma (deg) of the reciprocal metric G*.
|
|
std::array<double, 6> CellFromReciprocalMetric(const Eigen::Matrix3d &gstar);
|
|
|
|
// The e.s.d.s of a, b, c, alpha, beta, gamma for G* = sum_k p_k basis_k with covariance cov(p).
|
|
std::array<double, 6> CellEsdFromCovariance(const std::vector<Eigen::Matrix3d> &basis, const Eigen::VectorXd &p,
|
|
const Eigen::MatrixXd &cov);
|
|
|
|
struct MetricFit {
|
|
bool ok = false;
|
|
Eigen::VectorXd p; // the metric's components, then (fit_distance) the relative distance change
|
|
Eigen::MatrixXd cov; // scaled by the goodness of fit
|
|
std::vector<bool> used;
|
|
};
|
|
|
|
// Weighted linear least squares of q^2 = h^T G* h, |q| taken at one constant e.s.d. (weight 1/q^2 on
|
|
// q^2), with the observations further than 4 sigma off the fit rejected and the fit repeated.
|
|
// fit_distance: the detector distance is a parameter too, as e = (distance / the observations'
|
|
// distance) - 1, the last element of p: each |q| is then read as q + e dq_dlnd. A distance error and
|
|
// a cell scale move a spot the same way at low angle; they part with 2theta (a cell scale shifts
|
|
// |q| in proportion, a distance by cos^2(theta) cos(2theta) of that), so e is measured by how the
|
|
// spacings' residuals curve with 2theta, and its covariance with the metric is what a cell measured
|
|
// at an uncertain distance inherits.
|
|
MetricFit FitReciprocalMetric(std::span<const CellObservation> obs, const std::vector<Eigen::Matrix3d> &basis,
|
|
bool fit_distance = false);
|
|
|
|
// Both estimates for observations in the setting of `sg`; blocks are cut by frame. Empty with too
|
|
// few observations to fit.
|
|
std::optional<CellEsd> EstimateCellEsd(std::span<const CellObservation> obs, const gemmi::SpaceGroup &sg,
|
|
double wavelength_A, int nblocks = 8);
|
|
|
|
// The observations of a rotation run's integrated frames: observed, unclipped, unsaturated, off the
|
|
// ice rings and I > 3 sigma.
|
|
std::vector<CellObservation> GatherCellObservations(const std::vector<IntegrationOutcome> &outcomes,
|
|
size_t nthreads);
|