Files
Jungfraujoch/tests/XtalRefineCeres.h
T
leonarski_f a395f358ef
Build Packages / Create release (push) Successful in 17s
Build Packages / build:viewer:macos-arm64:nocuda (push) Successful in 3m22s
Build Packages / build:rugnux:macos-arm64:nocuda (push) Successful in 2m37s
Build Packages / build:rugnux:linux-aarch64:cuda (push) Successful in 9m33s
Build Packages / build:rugnux:linux-x86_64:cuda (push) Successful in 10m39s
Build Packages / build:viewer:linux-x86_64:nocuda (push) Successful in 11m4s
Build Packages / build:viewer:linux-x86_64:cuda (push) Successful in 13m19s
Build Packages / build:jfjoch:rocky8:nocuda (push) Successful in 17m37s
Build Packages / build:jfjoch:rocky9:nocuda (push) Successful in 18m49s
Build Packages / build:viewer:windows-x86_64:nocuda (push) Successful in 19m10s
Build Packages / build:viewer:windows-x86_64:cuda (push) Successful in 24m26s
Build Packages / HDF5 consumer tests (DIALS, XDS) (push) Successful in 25m31s
Build Packages / build:jfjoch:ubuntu2404:nocuda (push) Successful in 18m54s
Build Packages / build:jfjoch:ubuntu2204:nocuda (push) Successful in 20m45s
Build Packages / Generate python client (push) Successful in 37s
Build Packages / build:jfjoch:rocky8:cuda-sls9 (push) Successful in 20m20s
Build Packages / Build documentation (push) Successful in 1m32s
Build Packages / build:rugnux:windows-x86_64:cuda (push) Successful in 14m37s
Build Packages / build:jfjoch:rocky9:cuda-sls9 (push) Successful in 21m6s
Build Packages / build:jfjoch:rocky8:cuda (push) Successful in 19m49s
Build Packages / build:jfjoch:rocky9:cuda (push) Successful in 20m29s
Build Packages / build:jfjoch:ubuntu2204:cuda (push) Successful in 17m2s
Build Packages / build:jfjoch:ubuntu2404:cuda (push) Successful in 14m27s
Build Packages / Unit tests (push) Successful in 1h18m12s
1.0.0-rc.174 (#84)
* Rugnux: Performance improvements on GPU and CPU (more of the pre-scan and of scaling on the GPU, faster CPU spot finding and crystal refinement), with unchanged results.
* Rugnux: More robust processing - patches of persistently hot pixels are masked, an inconsistent merge triggers a retry at the measured beam centre, and builds targeting different CPU levels give the same results.
* Rugnux: Improved scaling and merging - reflections with an overloaded pixel are dropped, as in XDS, sparse rotation sweeps are scaled more reliably, and French-Wilson amplitudes use an anisotropic Wilson prior.
* Rugnux: Improved space-group determination - glide planes in groups without a centre of symmetry, screw axes from short or weak axial rows kept when a higher group is adopted, and more reliable decisions on twinned and pseudo-symmetric crystals.
* Rugnux: Improved small-molecule processing - spots that grow wider than the integration disk and split spots are integrated over their measured footprint, sparse lattices are integrated on every frame, and the `.hkl` file holds unmerged scaled reflections (SHELX HKLF 4).
* Rugnux: Reads Rigaku d*TREK SMV images (Saturn CCD), including detector 2theta and encoded pixel overflows; home-source (rotating-anode) datasets were added to the validation battery.
* jfjoch_viewer: Fixed processing failing at the end with "Wrong JPEG library version" on Linux; the merge window shows the space group with proper subscripts and a checklist of crystal pathologies.

Reviewed-on: #84
Co-authored-by: Filip Leonarski <filip.leonarski@psi.ch>
2026-10-06 14:03:18 +02:00

93 lines
4.1 KiB
C++

// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#pragma once
// The reference the LM solver of XtalRefine is checked against: the same XtalRefineProblem handed to
// Ceres exactly as XtalOptimizer used to build it - the same residual functors, losses, priors, bounds,
// manifold and options.
#include "ceres/ceres.h"
#include "../image_analysis/geom_refinement/XtalRefine.h"
struct XtalRefineCeresPrior {
XtalRefineCeresPrior(double gx, double gy, double p0, double weight)
: gx(gx), gy(gy), p0(p0), weight(weight) {}
template<typename T>
bool operator()(const T *const p, T *residual) const {
residual[0] = T(weight) * (T(gx) * p[0] + T(gy) * p[1] - T(p0));
return true;
}
double gx, gy, p0, weight;
};
inline ceres::Solver::Summary SolveXtalRefineCeres(XtalRefineProblem &p, int num_threads) {
std::vector<XtalFrameConstants> frame_const;
frame_const.reserve(p.frame_angle_rad.size());
if (p.beam_and_orientation_only)
for (const double angle: p.frame_angle_rad)
frame_const.emplace_back(p.detector_rot, p.rot_vec, angle, p.latt_vec1, p.latt_vec2, p.crystal_system);
ceres::Problem problem;
for (size_t i = 0; i < p.residuals.size(); i++) {
ceres::LossFunction *loss = p.weight_sq.empty()
? nullptr
: new ceres::ScaledLoss(nullptr, p.weight_sq[i], ceres::TAKE_OWNERSHIP);
if (p.beam_and_orientation_only)
problem.AddResidualBlock(
new ceres::AutoDiffCostFunction<XtalResidualBeamOrientation, 3, 2, 3>(
new XtalResidualBeamOrientation(p.residuals[i], p.distance_mm, frame_const[p.frame[i]])),
loss, p.beam, p.latt_vec0);
else
problem.AddResidualBlock(
new ceres::AutoDiffCostFunction<XtalResidualFixedDistance, 3, 2, 2, 3, 3, 3, 3>(
new XtalResidualFixedDistance(p.residuals[i], p.distance_mm)),
loss, p.beam, p.detector_rot, p.rot_vec, p.latt_vec0, p.latt_vec1, p.latt_vec2);
}
for (const auto &prior: p.priors)
problem.AddResidualBlock(
new ceres::AutoDiffCostFunction<XtalRefineCeresPrior, 1, 2>(
new XtalRefineCeresPrior(prior.gx, prior.gy, prior.p0, prior.weight)),
nullptr, prior.block == XtalRefinePrior::Block::Beam ? p.beam : p.detector_rot);
const auto bounds = [&](double *block, const double *lo, const double *hi, int n) {
for (int i = 0; i < n; i++) {
if (lo[i] > -XtalRefineProblem::kNoBound)
problem.SetParameterLowerBound(block, i, lo[i]);
if (hi[i] < XtalRefineProblem::kNoBound)
problem.SetParameterUpperBound(block, i, hi[i]);
}
};
if (p.beam_constant)
problem.SetParameterBlockConstant(p.beam);
if (!p.beam_and_orientation_only) {
if (p.detector_rot_constant)
problem.SetParameterBlockConstant(p.detector_rot);
else
bounds(p.detector_rot, p.detector_rot_lower, p.detector_rot_upper, 2);
if (p.rot_vec_constant)
problem.SetParameterBlockConstant(p.rot_vec);
else
problem.SetManifold(p.rot_vec, new ceres::SphereManifold<3>);
if (p.latt_vec1_constant)
problem.SetParameterBlockConstant(p.latt_vec1);
else
bounds(p.latt_vec1, p.latt_vec1_lower, p.latt_vec1_upper, 3);
if (p.latt_vec2_constant)
problem.SetParameterBlockConstant(p.latt_vec2);
else
bounds(p.latt_vec2, p.latt_vec2_lower, p.latt_vec2_upper, 3);
}
ceres::Solver::Options options;
options.linear_solver_type = ceres::DENSE_NORMAL_CHOLESKY;
options.minimizer_progress_to_stdout = false;
options.max_num_iterations = p.options.max_iterations;
options.max_solver_time_in_seconds = p.options.max_time_s;
options.logging_type = ceres::LoggingType::SILENT;
options.num_threads = num_threads;
ceres::Solver::Summary summary;
ceres::Solve(options, &problem, &summary);
return summary;
}