Skip to content

Fix two bugs in Targeter finite-difference corrector#526

Open
KonradStanski wants to merge 1 commit intonyx-space:masterfrom
KonradStanski:fix/targeter-vnc-dcm-transpose
Open

Fix two bugs in Targeter finite-difference corrector#526
KonradStanski wants to merge 1 commit intonyx-space:masterfrom
KonradStanski:fix/targeter-vnc-dcm-transpose

Conversation

@KonradStanski
Copy link
Copy Markdown

Summary

  • Remove spurious .transpose() on VNC→inertial DCM in the convergence path of raphson_finite_diff.rs, which caused apply_with_traj() to fail verification on any VNC-frame targeting
  • Move init_guess application outside the per-variable loop to prevent cumulative over-application (first variable's guess applied 3×, second 2×, third 1×), which caused verification failures on sensitive arcs even in inertial frame

Details

Bug 1 — DCM transpose: The convergence branch (building corrected_state from xi_start + total_correction) transposed the dcm_to_inertial rotation matrix at line ~549, effectively rotating inertial→VNC instead of VNC→inertial. All three other call sites in the same file (init guess application, Jacobian perturbation, iteration step) use the DCM without transpose.

Bug 2 — Init guess double-counting: state_correction was applied to xi inside the for (i, var) loop. With 3 velocity variables [vx, vy, vz]:

  • After variable 0: xi.vel += [vx, 0, 0]
  • After variable 1: xi.vel += [vx, vy, 0] (accumulated) → total [2·vx, vy, 0]
  • After variable 2: xi.vel += [vx, vy, vz] → total [3·vx, 2·vy, vz]

But total_correction correctly stores [vx, vy, vz], so the convergence path's corrected_state disagrees with the iterated xi. The fix moves application outside the loop, matching the pattern already used in the iteration loop (~lines 608-729).

Neither bug manifests when init_guess = 0 (the default from Vary::into()), which is why existing tests pass. Both bugs trigger on sensitive, long-arc targeting with nonzero initial guesses (e.g., trans-lunar injection).

Test plan

  • All 83 existing cargo test --lib tests pass
  • Verified with a trans-lunar injection scenario (HEO → lunar flyby): Nyx Targeter now converges in 4 iterations with apply_with_traj verification passing

Closes #525

🤖 Generated with Claude Code

1. Remove spurious .transpose() on VNC→inertial DCM in convergence path.
   The convergence branch (building corrected_state from xi_start +
   total_correction) transposed the dcm_to_inertial rotation matrix,
   effectively applying the Δv in the wrong direction. All three other
   call sites in the same file use the DCM without transpose. This caused
   apply_with_traj() to fail verification on any VNC-frame targeting.

2. Move init_guess application outside the per-variable loop. Previously,
   state_correction was applied to xi inside the loop over variables,
   causing cumulative over-application: with 3 velocity variables
   [vx, vy, vz], the first variable's guess was applied 3 times, the
   second twice, and the third once. The corrected_state reconstruction
   (which applies total_correction once) would then disagree with the
   iterated xi, causing verification failures on sensitive arcs even
   in inertial frame. Now matches the pattern used in the iteration
   loop, which correctly accumulates then applies once.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the initial guess application in the Raphson finite difference targeter by accumulating state corrections and applying them once after the loop, and corrects a matrix calculation by removing an unnecessary transposition. Feedback indicates that the new !finite_burn_target check should be removed to ensure consistency with the main iteration loop, allowing impulsive state corrections to be applied even when finite burn variables are present.

Comment thread src/md/opti/raphson_finite_diff.rs

// Apply the initial guess
// Apply the initial guess: first accumulate, then apply once (matching
// the pattern used in the iteration loop at lines ~608-729).
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// the pattern used in the iteration loop at lines ~608-729).
// the pattern used in the iteration loop).

I prefer not referring specific line numbers in comments.

Comment thread src/md/opti/raphson_finite_diff.rs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Spurious .transpose() on DCM in Targeter convergence path causes verification failure

2 participants