Skip to content

McXtrace: Mirror parabolic fixes - #3191

Merged
willend merged 6 commits into
mainfrom
2026-11-farhi-mirror-parabolic
Nov 25, 2026
Merged

willend merged 6 commits into
mainfrom
2026-11-farhi-mirror-parabolic

Conversation

@farhi

@farhi farhi commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

McXtrace optics Mirror parabolic fixes

This is rather limited code fix for the parabolic mirror optic component. No effect on actual test results.

A. Definite bugs

# Issue Where Proposed fix Impact
1 Edge-on tests compare the wrong coordinate. For a ray along z the surface height is zz = x²/a², but the code tests fabs(z - zz). The x-axis case does the same with fabs(x - xx). Both should compare y. L85, L92 Use y - zz and y - xx. Edge-on absorption is currently effectively dead, because z or x is unrelated to the surface height.
2 Edge-on tests have no extent check. They absorb any ray with the right height, whatever its x (or z) position, and whether or not it has already passed the mirror. L82-95 Add s ≤ xwidth/2 (or zdepth/2) and a "not yet past the mirror" test. Fewer wrongly absorbed rays.
3 Mirror limits are tested at the wrong point. sx and sz are computed at the ray position on the entry plane y = Y0, not at the actual hit point. The first hit is never checked afterwards. Only later reflections in the loop (L170-185) are checked correctly. The comment at L104 ("need a check for mirror limits here") flags this. L99-120 Check sx, sz at the first intersection after PROP_DL(ll), and restore the ray on a miss. Move the pre-check or drop it. Rays are accepted or rejected by the entry-plane position. The error is at most Y0·tanα, so it matters for deep (short-focus) mirrors and oblique rays. For shallow mirrors it is small.
4 Rays starting below the entry plane are misjudged. If the plane is behind the ray (l0 ≤ 0), the limit check uses the ray's raw position, which can be far from the mirror. The ray is then rejected even if it would hit it downstream. L100-120 Fixed by fix 3 (check at the hit, not before). Same as fix 3.

B. Minor issues

  • Ignored return value: the return value of plane_intersect is ignored (L100), and l0 is uninitialised. A ray parallel to the plane (ky = 0) could leave l0 unset before the if (l0 > 0) test. Fix: check the return value.
  • Re-hit loop: the while (status) loop (L135) has no iteration cap. It relies on the helper snapping the self-intersection to exactly 0 (the comment at L125 assumes this). A tiny positive l0 would re-reflect at the same point. A cap would make it safe.

C. Consistency and cleanup

  • Miss handling: the limit-miss branch calls RESTORE_XRAY (L119), but the "no intersection" path (L133) leaves the ray moved to the Y0 plane. This is physically harmless but inconsistent.
  • Unused or shadowed variables: first is set but never read. xx and zz are declared at L81 and shadowed inside the loop (L170, L176). ll could trigger a maybe-uninitialised warning.
  • Repeated arc-length formula: it appears six times across TRACE and MCDISPLAY. The formula itself is correct, and I rederived it: s = (a²/4)(u√(1+u²) + asinh u) with u = 2x/a². Folding it into one helper Mirror_parabolic_s in SHARE.
  • yheight is symmetric: the test is |y − zz| ≤ yheight, but a substrate is one-sided (below the surface, since the paraboloid opens toward +y). Rays above the surface within yheight now reflect, not be absorbed.

D. Documentation errors

  • Focus position: "focal point is (0,0,f(a,b))" is wrong. The paraboloid axis is y, so the focus is at (0, f, 0) with f = a²/4 for the x-curve and b²/4 for the z-curve. For a ≠ b these are two different focal lengths (astigmatic), not one focus.
  • Output parameters: xmax and zmax are listed but don't exist outside MCDISPLAY.
  • radius: it sets the focal length, so the name is misleading. Renaming would break compatibility, so I'd only document it.
  • Extent definition: xwidth and zdepth are measured as arc length along the surface, not as projected extents. The doc says only "Width of mirror".

E. MCDISPLAY

  • Outline mismatch: removed the rectangle outline drawn in the plane y = 0 at ±xwidth/2 and ±zdepth/2 (L203-206). The polygons are on the curved surface and span ±xmax (projected, which is smaller). The outline therefore didn't sit on the mirror.
  • Bisection tolerance: it is an absolute 1e-4 m, which is too coarse for mirrors a few mm wide. A relative tolerance has been applied to allow 'small' mirrors.

Component test (on test Instruments)

mxtest --comp Mirror_parabolic --mpi auto --noplots reports SUCCESS

loading system configuration
Output of test will be placed in: .
ncount is: 1e6
mpi count is: auto
No plots of the test output will be generated
Testing: 3.10.3

Finding instruments in: /usr/share/mcxtrace/resources/examples
(Instrument list includes those using component(s) Mirror_parabolic )
Copying instruments to: ./mcxtrace-3.10.3_mpi_x_auto_Mirror_parabolic_1e6_Linux_20261124_2204_27

Compiling instruments [seconds]...
PBD_BT                :   4.15
Test_Mirrors          :   3.32
Test_mirror_parabolic :   3.59

Running tests / getting status...
PBD_BT                 :   Display OK (0s)
PBD_BT                 :   0.71    [val: 2828490000000000.0 / 2831700000000000.0 = 100 %]
Test_Mirrors           :   Display OK (0s)
Test_Mirrors           :   0.66    [val: 1.98944e-08 / 2e-08 = 99 %]
Test_Mirrors_2         :   0.74    [val: 1.98944e-08 / 2e-08 = 99 %]
Test_Mirrors_3         :   0.74    [val: 1.98944e-08 / 2e-08 = 99 %]
Test_Mirrors_4         :   0.70    [val: 1.98944e-08 / 2e-08 = 99 %]
Test_Mirrors_5         :   0.73    [val: 1.98944e-08 / 2e-08 = 99 %]
Test_Mirrors_6         :   0.73    [val: 1.98944e-08 / 2e-08 = 99 %]
Test_Mirrors_7         :   0.74    [val: 1.98944e-08 / 2e-08 = 99 %]
Test_Mirrors_8         :   0.85    [val: 1.94367e-08 / 2e-08 = 97 %]
Test_Mirrors_9         :   0.87    [val: 1.98944e-08 / 2e-08 = 99 %]
Test_Mirrors_10        :   0.87    [val: 1.98944e-08 / 2e-08 = 99 %]
Test_Mirrors_11        :   0.86    [val: 1.94332e-08 / 2e-08 = 97 %]
Test_Mirrors_12        :   0.89    [val: 1.98944e-08 / 2e-08 = 99 %]
Test_Mirrors_13        :   0.87    [val: 1.98591e-08 / 2e-08 = 99 %]
Test_Mirrors_14        :   0.69    [val: 1.9453e-08 / 2e-08 = 97 %]
Test_Mirrors_15        :   0.73    [val: 1.98944e-08 / 2e-08 = 99 %]
Test_Mirrors_16        :   0.75    [val: 1.98944e-08 / 2e-08 = 99 %]
Test_Mirrors_17        :   0.73    [val: 1.94493e-08 / 2e-08 = 97 %]
Test_Mirrors_18        :   0.78    [val: 1.98944e-08 / 2e-08 = 99 %]
Test_Mirrors_19        :   0.72    [val: 1.98604e-08 / 2e-08 = 99 %]
Test_mirror_parabolic  :   Display OK (0s)
Test_mirror_parabolic  :   0.61    [val: 1.0 / 1.0 = 100 %]
Test_mirror_parabolic_2:   0.61    [val: 0.105671 / 0.104959 = 101 %]
======================================
Compilation time: 0:00:13
Simulation time: 0:00:17
Total runtime: 0:00:30
Overall test result:
SUCCESS

Declaration of use of AI-tools

  • Please add a checkmark here if you used AI-tools during the work for this contribution
  • Further, please describe how / where and for what the tools were used: Claude + Mistral
  • AI-assisted commits must carry a Co-Authored-By line naming the tool, see CONTRIBUTING.md

Development OS / boundary conditions

  • debian/arm64
  • McXtrace 3.10.3

PR Checklist for contributing to McStas/McXtrace

For a coherent and useful contribution to McStas/McXtrace, please fill in relevant parts of the checklist:

  • My contribution includes patches to an existing component file

    • I have used the mcdoc utility and rendered a reasonable documentation page for the component (please attach as screenshot in comments!)
    • I have ensured that basic use of the component is OK (e.g. an instrument using it compiles?)
    • I have used the mccode-clangformat tool to apply the standard McCode component indentation scheme
    • I have used the mcrun --c-lint "linter" and followed advice to remove most / all warnings that are raised

- fix Edge-on tests compare the wrong coordinate.
- fix Edge-on tests have no extent check.
- fix Mirror limits are tested at the wrong point.
- fix Rays starting below the entry plane are misjudged.
- test plane_intersect Ignored return value
- fix while (status) Re-hit loop cap, added
- Miss handling "no intersection" -> RESTORE_XRAY
- clear Unused/shadowed variables
- avoid code duplication (Arc-length helper) -> Mirror_parabolic_s
- fix One-sided yheight: edge-on fast paths now absorb only substrate-side rays
- header update
- mcdisplay Outline mismatch
- mcdisplay: Bisection tolerance tightening to allow small mirrors
@willend

willend commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Thanks Emmanuel, good clean-up. The tests give the same intensities before and after, and the limit check at the hit already helps clearly at grazing incidence. Looking at it I found some issues, the main one mine. I hope it is OK by you that I am adding these fixes directly on this branch:

# Fix Origin
1 paraboloid_intersect called with sign=1 again. With sign=0 rays hit the plane y=0, not the paraboloid mine, f1c14e0
2 mcxtrace-r.c: missing factor 2, sign*ky*y*b2i -> sign*2*ky*y*b2i. Very likely the real cause of #1633, hidden by sign=0 old
3 TRACE: check limits before PROP_DL, and RESTORE_XRAY on the first hit only. Otherwise the restore undoes earlier reflections (does not show with sign=0) this PR
4 MCDISPLAY: outline at z=-zmax needs + zmax*zmax*b2inv this PR
5 #pragma acc routine on Mirror_parabolic_s this PR

1-3 belong together: 1 without 2 loses rays, 1 without 3 loses reflections.

Note: 1 and 2 change numerical results. No %Test values need updating.

Check (same seed) main / PR sign=1 only, on main PR + all fixes
Spot half-width at focus, Test_mirror_parabolic mirror, 5 mm parallel beam 835 µm 5 µm (one bin) 5 µm (one bin)
#1633 mirror, source at f: rays detected 100 % 28 % 100 %
#1633 mirror, source at f: divergence ±0.03° ±0.01° (one bin) ±0.01° (one bin)
Test_Mirrors index=2 gamma=5, psd4 1.989e-08 1.000e-08 1.989e-08
Rays from above vs. exact reference tracer, wrong position or direction 88-100 % not run 0

Left for later

  • Some grazing front-face hits are still missed, since rays are first moved to the Y0 plane and only the nearest root is tried. With all fixes: 0 % for a=1, b=0, 11 % for a=0.2, b=63.2, but 98 % for a=0.2, b=3 (84 % with sign=0). Dropping the Y0 step and taking the nearest root within the limits solves this with less code, but makes the back face reflect for all rays. Is that wanted?
  • PBD_BT does not test the mirror: 0 of 200k rays hit it, before and after. Without the Y0 step, hits are nearly all on the back face, so please check the orientation.
  • Test_Mirrors index=2 gamma=0.1: only 35 % of rays hit, and the detector also sees the direct beam.
  • Minor: a2inv / b2inv are in [m^-1], and the commits lack the Co-Authored-By line for the AI tools (the squash merge can add it).

Tested on macOS arm64, CPU, no MPI, with Test_mirror_parabolic, Test_Mirrors index=2 and PBD_BT. Not tested: GPU, other platforms, full mxtest. AI-assisted (Claude).

- paraboloid_intersect: add missing factor 2 in B (likely cause of #1633)
- call paraboloid_intersect with sign=1 again (was 0 since f1c14e0)
- check limits before propagating, restore on the first hit only
- MCDISPLAY: fix sign of outline at z=-zmax
- Mirror_parabolic_s: add acc routine

Changes numerical results.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@farhi

farhi commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Hi @willend , no pb to directly contribute for further fixes.
I know that many tests (most ?) do not properly test the parabolic mirror because it is not easy to position/configure. This should be improved later, after main bug fixes.
Also, I wish to add coating handling, polarisation E and phase angling as in Mirror_curved.

@willend

willend commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

I read your reply as "good to go" - or do you want to include more here?

@farhi

farhi commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Yes, go ahead

@willend
willend merged commit 47b4cc6 into main Nov 25, 2026
27 checks passed
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.

2 participants

⌂ Proxy