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:
co-authored by
Claude Opus 5
parent
4b48d19064
commit
0d07141b5c
@@ -192,7 +192,6 @@ bool XtalOptimizerInternal(XtalOptimizerData &data,
|
||||
// autodiff. Any other combination (stills also free the cell, the offline refiner frees distance
|
||||
// and detector angles) keeps the general form below.
|
||||
const bool beam_and_orientation_only = data.refine_beam_center
|
||||
&& !data.refine_distance_mm
|
||||
&& !data.refine_detector_angles
|
||||
&& !data.refine_rotation_axis
|
||||
&& !data.refine_unit_cell;
|
||||
@@ -272,11 +271,10 @@ bool XtalOptimizerInternal(XtalOptimizerData &data,
|
||||
);
|
||||
else
|
||||
problem.AddResidualBlock(
|
||||
new ceres::AutoDiffCostFunction<XtalResidual, 3, 2, 1, 2, 3, 3, 3, 3>(
|
||||
new XtalResidual(residual)),
|
||||
new ceres::AutoDiffCostFunction<XtalResidualFixedDistance, 3, 2, 2, 3, 3, 3, 3>(
|
||||
new XtalResidualFixedDistance(residual, distance_mm)),
|
||||
loss,
|
||||
beam,
|
||||
&distance_mm,
|
||||
detector_rot,
|
||||
rot_vec,
|
||||
latt_vec0,
|
||||
@@ -318,14 +316,6 @@ bool XtalOptimizerInternal(XtalOptimizerData &data,
|
||||
// Distance, detector angles, rotation axis and cell are parameter blocks only in the general
|
||||
// seven-block residual; the reduced one bakes them in, so there is nothing left to configure.
|
||||
if (!beam_and_orientation_only) {
|
||||
if (!data.refine_distance_mm)
|
||||
problem.SetParameterBlockConstant(&distance_mm);
|
||||
else {
|
||||
const double dist_range = 0.1;
|
||||
problem.SetParameterLowerBound(&distance_mm, 0, distance_mm * (1.0 - dist_range));
|
||||
problem.SetParameterUpperBound(&distance_mm, 0, distance_mm * (1.0 + dist_range));
|
||||
}
|
||||
|
||||
if (!data.refine_detector_angles) {
|
||||
problem.SetParameterBlockConstant(detector_rot);
|
||||
} else {
|
||||
@@ -374,7 +364,14 @@ bool XtalOptimizerInternal(XtalOptimizerData &data,
|
||||
|
||||
// Configure solver
|
||||
ceres::Solver::Options options;
|
||||
options.linear_solver_type = ceres::DENSE_QR;
|
||||
// Normal equations, not QR. The problem is very tall and thin - thousands of spots against at
|
||||
// most 17 parameters - and that is the shape DENSE_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 min(48, columns).
|
||||
// Accumulating J^T J reads the Jacobian once instead. Both solve the same damped system, so the
|
||||
// step is the same to round-off; the column scaling Ceres applies by default and the LM diagonal
|
||||
// keep the squared condition number in hand.
|
||||
options.linear_solver_type = ceres::DENSE_NORMAL_CHOLESKY;
|
||||
options.minimizer_progress_to_stdout = false;
|
||||
if (data.max_iterations > 0)
|
||||
options.max_num_iterations = data.max_iterations;
|
||||
@@ -400,8 +397,6 @@ bool XtalOptimizerInternal(XtalOptimizerData &data,
|
||||
data.geom.BeamX_pxl(beam[0]).BeamY_pxl(beam[1]);
|
||||
}
|
||||
|
||||
if (data.refine_distance_mm)
|
||||
data.geom.DetectorDistance_mm(distance_mm);
|
||||
|
||||
if (data.refine_detector_angles)
|
||||
data.geom.PoniRot1_rad(detector_rot[0]).PoniRot2_rad(detector_rot[1]);
|
||||
@@ -516,7 +511,7 @@ bool XtalOptimizerRotationOnly(XtalOptimizerData &data,
|
||||
}
|
||||
|
||||
ceres::Solver::Options options;
|
||||
options.linear_solver_type = ceres::DENSE_QR;
|
||||
options.linear_solver_type = ceres::DENSE_NORMAL_CHOLESKY; // tall and thin, as above
|
||||
options.minimizer_progress_to_stdout = false;
|
||||
if (data.max_iterations > 0)
|
||||
options.max_num_iterations = data.max_iterations;
|
||||
|
||||
Reference in New Issue
Block a user