Ask the geometry refinement for the derivatives it uses, and solve the normal equations

Four changes to the same least-squares fit, which the rotation first pass runs on every
candidate lattice and the per-image path runs on every frame.

The linear solver was DENSE_QR on a problem that is very tall and thin - thousands of spots
against at most seventeen parameters. That is the shape QR handles worst: it copies the
Jacobian out of Ceres' row-major storage into a column-major buffer on every solve, and
Eigen's blocked Householder then degenerates to the unblocked path because its block size is
the column count. Accumulating J^T J reads the Jacobian once instead. Both solve the same
damped system, so the step is the same to round-off.

Ceres sizes its dual numbers from the declared parameter blocks, not from which of them the
caller then holds constant. Nothing outside a test set refine_distance_mm - the positional
residual leaves the distance degenerate with the cell scale, which is why the rotation
post-refinement fits it in a step of its own with the cell held fixed - so the block was
declared only to be frozen, and every residual differentiated seventeen parameters to use
sixteen. It is gone, along with the test that exercised distance recovery; that test seeded
the distance off truth, which the cell would now absorb, so its seed moves to the true value.
The post-refinement's own detector step held five of its seven blocks constant and now bakes
them into the residual, leaving beam and distance.

The predicted reciprocal vector was built by rotating all three direct columns and then
crossing them. A rotation commutes with the cross product and leaves the triple product
alone, so the same vector comes out of crossing the unrotated columns and turning the result
once - three rotations become one, for every crystal system.

The documentation described the arrangement before all this, and had drifted in a second way:
the first-pass rotation indexing has been refining the detector tilt and the rotation axis by
default, which the text said were held fixed.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
jungfrau
2026-08-24 05:31:59 -04:00
co-authored by Claude Opus 5
parent 4b48d19064
commit 0d07141b5c
8 changed files with 168 additions and 81 deletions
+4 -6
View File
@@ -543,15 +543,17 @@ TEST_CASE("XtalOptimizer_rotation") {
// direction that XtalOptimizer now deliberately restrains toward the header rather than refining
// freely (see the BeamComponentPrior in XtalOptimizer.cpp). So only the perpendicular component
// (Y) is seeded off-truth to exercise beam-centre recovery; X is seeded at its true value.
// The distance is seeded at its true value too: XtalOptimizer does not refine it - the rotation
// post-refinement fits it globally instead, where the cell is held fixed and the two are no longer
// degenerate - so a distance seeded off-truth here would simply be absorbed by the cell.
XtalOptimizerData xtal_opt{};
xtal_opt.latt = CrystalLattice(39.7f, 50.6f, 79.6f, 90.0f, 94.5f, 90.5f);
xtal_opt.geom.BeamX_pxl(1000).BeamY_pxl(997).DetectorDistance_mm(203.0)
xtal_opt.geom.BeamX_pxl(1000).BeamY_pxl(997).DetectorDistance_mm(200.0)
.PoniRot1_rad(0.01).PoniRot2_rad(0.02);
xtal_opt.crystal_system = gemmi::CrystalSystem::Monoclinic;
xtal_opt.axis = axis;
xtal_opt.min_spots = 200;
xtal_opt.refine_beam_center = true;
xtal_opt.refine_distance_mm = true;
xtal_opt.refine_detector_angles = false;
xtal_opt.max_time = 30.0;
@@ -568,9 +570,6 @@ TEST_CASE("XtalOptimizer_rotation") {
CHECK(fabsf(xtal_opt.geom.GetBeamX_pxl() - exp_i.GetBeamX_pxl()) < 0.2f);
CHECK(fabsf(xtal_opt.geom.GetBeamY_pxl() - exp_i.GetBeamY_pxl()) < 0.2f);
// 700 micron is a lot, but for now it is important to see that refinement improves in the right direction
CHECK(fabsf(xtal_opt.geom.GetDetectorDistance_mm() - exp_i.GetDetectorDistance_mm()) < 0.7f);
// Lattice checks
CHECK(fabsf(uc_ref.a - uc_out.a) < 0.2f);
CHECK(fabsf(uc_ref.b - uc_out.b) < 0.2f);
@@ -641,7 +640,6 @@ TEST_CASE("XtalOptimizer_refine_rotation_axis") {
std::nullopt);
xtal_opt.min_spots = 200;
xtal_opt.refine_beam_center = true;
xtal_opt.refine_distance_mm = false;
xtal_opt.refine_detector_angles = false;
xtal_opt.refine_rotation_axis = true;
xtal_opt.max_time = 30.0;