Fix wall_hit_cart/xend_cart inconsistency when from_cart fails#321
Fix wall_hit_cart/xend_cart inconsistency when from_cart fails#321krystophny wants to merge 2 commits intomainfrom
Conversation
When CGAL detects a wall intersection, wall_hit_cart correctly stores the intersection point in Cartesian coordinates. The code then calls from_cart() to convert this back to reference coordinates for zend. However, when from_cart() Newton iteration fails to converge (ierr != 0), the reference coordinates z(1:3) were not updated, causing zend and xend_cart to record the particle position PAST the wall rather than AT the wall intersection. This fix adds a linear interpolation fallback: when from_cart() fails, interpolate reference coordinates along the orbit segment using the fractional distance to the Cartesian hit point. Also update CGAL from 5.6.1 to 6.0.1 to fix GCC compilation error with Halfedge_around_source_iterator::base(). Test results show significant improvement: - Before: 32.1% of wall hits had >100cm discrepancy - After: 3.6% of wall hits have >100cm discrepancy
Visual EvidenceThe histogram compares position differences between Key improvements:
Test configuration: SQUID stellarator, 1ms simulation, 1024 particles starting at s=0.6, using CGAL-embedded STL chartmap. |
ⓘ Your approaching your monthly quota for Qodo. Upgrade your plan PR Compliance Guide 🔍Below is a summary of compliance checks for this PR:
Compliance status legend🟢 - Fully Compliant🟡 - Partial Compliant 🔴 - Not Compliant ⚪ - Requires Further Human Verification 🏷️ - Compliance label |
||||||||||||||||||||||||
|
Update: The visual evidence histogram is available locally at The fix significantly improves consistency between the CGAL-reported wall intersection ( |
ⓘ Your approaching your monthly quota for Qodo. Upgrade your plan PR Code Suggestions ✨Explore these optional code suggestions:
|
||||||||||||
For STL wall hits, xend_cart should equal wall_hit_cart (the true CGAL intersection point). Previously, xend_cart was computed from zend via forward chartmap transformation, which could produce incorrect positions when the earlier from_cart inverse transformation failed. This fix ensures xend_cart is always correct for STL hits by using wall_hit_cart directly, avoiding the round-trip coordinate conversion. Also set xend_cart to zero (not xstart_cart) for untraced particles. Test results: - PRE-FIX: 2/50 exact matches, max error 1595 cm - POST-FIX: 50/50 exact matches, zero error

Summary
Fix inconsistency between
xend_cartandwall_hit_cartfor STL wall hits.Root cause: For STL wall intersections detected by CGAL,
xend_cartwas computed fromzendvia forward chartmap transformation. When the earlierfrom_cart()inverse transformation failed (in ill-conditioned chartmap regions),zendcontained incorrect reference coordinates, causingxend_cartto be displaced from the true wall intersection.Fix (2 commits):
zendwhenfrom_cart()failswall_hit_cartdirectly forxend_carton STL hits (the clean fix)The second fix is the definitive solution -
xend_cartnow equalswall_hit_cartexactly for all STL wall hits, regardless of whetherfrom_cart()succeeds. The first fix improveszendfor angle-space analysis.Also: Update CGAL 5.6.1 -> 6.0.1 for GCC compatibility, and set
xend_cartto zero (notxstart_cart) for untraced particles.Related
wall_mesh_from_chartmap()to create STL exactly matching chartmap boundaryTest plan