The per-image geometry refinement is the largest stage of the image loop, and a third of it was arithmetic on numbers that never change. The residual derives the detector angles' sines and cosines, the goniometer's back-rotation - a three-argument hypot, a sine, a cosine and a division - and the reciprocal basis of the cell on every evaluation. On the rotation path the detector angles and the axis are held fixed and stored as plain doubles, so all of it is constant, not merely constant per block: there is one frame per image and one cell. Three solves an image, fifty iterations a solve and a thousand spots make it tens of thousands of repetitions of the same result. The frame's constants are now built once and handed in. The body they feed is the same body, split out rather than copied, so no expression is reassociated - in particular the reciprocal vector is still formed as the basis times the inverse volume, with the volume not folded into the basis. The spot confidence weights depend only on each spot's resolution and intensity, which no solver touches, and were recomputed identically for each of the three passes. They are computed once. The sort behind them ordered indices through a projection that chased a random eighty-byte-strided element per comparison; it now sorts a packed resolution and index, which makes the same comparisons in the same sequence and therefore the same permutation. The spot list itself was copied per image through an initializer list whose elements are const; it is passed as a view. The integration engine was the last one in the loop copying through pageable host memory - three transfers in and eight out per image, twenty-six bytes a reflection, while every other engine already page-locks its staging. A driver copy from pageable memory stages through its own pinned buffer on the calling thread, which is why an asynchronous copy was averaging a hundred and thirteen microseconds. Page-locked, the same seventeen thousand calls cost four hundred and thirty-two milliseconds instead of one and a half seconds, and the wait moves to the synchronisation point where it belongs. Two smaller ones: the reflections were copied into the per-image message for a process file that a merging run does not write, so the copy is made where a writer exists; and the intensity statistics and the Wilson estimate walked the same eighty-byte array twice to read twelve bytes, which is now one pass with each accumulation in its own order. Every reflection file is byte-identical on four crystals; the process file's reflections match dataset for dataset, and its azimuthal arrays differ no more between this build and the last than the last differs from itself. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EGpGdgmJ8MyY9pCGWjktyi
67 lines
2.8 KiB
C++
67 lines
2.8 KiB
C++
// SPDX-FileCopyrightText: 2025 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
#pragma once
|
|
|
|
#include <optional>
|
|
#include <span>
|
|
|
|
#include "../common/GoniometerAxis.h"
|
|
#include "../common/CrystalLattice.h"
|
|
#include "../common/DiffractionGeometry.h"
|
|
#include "../common/SpotToSave.h"
|
|
#include "gemmi/symmetry.hpp"
|
|
|
|
struct XtalOptimizerData {
|
|
DiffractionGeometry geom;
|
|
CrystalLattice latt;
|
|
gemmi::CrystalSystem crystal_system = gemmi::CrystalSystem::Triclinic;
|
|
int64_t min_spots = 8;
|
|
|
|
float min_length_A = 5.0;
|
|
float max_length_A = 500.0;
|
|
float min_angle_deg = 60.0f;
|
|
float max_angle_deg = 120.0f;
|
|
|
|
bool refine_beam_center = true;
|
|
bool refine_detector_angles = false;
|
|
bool refine_unit_cell = true; // This refines unit cell size + angles - orientation is always refined
|
|
bool refine_rotation_axis = false;
|
|
|
|
bool index_ice_rings = true;
|
|
|
|
// Weight each spot by how strong it is for its resolution, so that low-confidence spots contribute
|
|
// without driving the fit (see SpotConfidenceWeights). Off by default: the indexers call this with a
|
|
// spot list they have already selected, it is the per-image refinement that gets the raw list.
|
|
bool weight_spots_by_confidence = false;
|
|
|
|
// Stopping rule. max_iterations > 0 bounds the solver by ITERATIONS, which is reproducible;
|
|
// otherwise it is bounded by max_time, wall-clock seconds, which is not - the same image refines
|
|
// to a different answer on a busier machine. Online acquisition needs the wall-clock bound because
|
|
// its budget is real; offline reprocessing wants the reproducible one.
|
|
float max_time = 1.0;
|
|
int max_iterations = 0;
|
|
|
|
std::optional<GoniometerAxis> axis;
|
|
|
|
// output
|
|
std::optional<double> beam_corr_x;
|
|
std::optional<double> beam_corr_y;
|
|
|
|
// For rotation only optimizer
|
|
std::optional<double> angle_corr;
|
|
std::optional<Coord> angle_axis;
|
|
};
|
|
|
|
// num_threads sets the Ceres solver thread count for the internal least-squares refine. It defaults
|
|
// to 1 because XtalOptimizer is usually called from many threads at once; raise it only when a caller
|
|
// runs a small number of refinements concurrently and wants each to use several cores.
|
|
bool XtalOptimizer(XtalOptimizerData &data, std::span<const std::vector<SpotToSave>> spots,
|
|
int num_threads = 1);
|
|
// Single frame. Not the same as passing {spots} to the overload above: a braced list copies the spot
|
|
// list, its elements being const, which on the per-image path is the whole list once per image.
|
|
bool XtalOptimizer(XtalOptimizerData &data, const std::vector<SpotToSave> &spots, int num_threads = 1);
|
|
bool XtalOptimizerRotationOnly(XtalOptimizerData &data, const std::vector<SpotToSave> &spots, float tolerance);
|
|
|
|
|