Skip to content

Fix MPI polarization monitor statistics - #3057

Draft
mads-bertelsen-agentic wants to merge 6 commits into
mccode-dev:mainfrom
mads-bertelsen-agentic:fix/polarization-monitor-mpi
Draft

mads-bertelsen-agentic wants to merge 6 commits into
mccode-dev:mainfrom
mads-bertelsen-agentic:fix/polarization-monitor-mpi

Conversation

@mads-bertelsen-agentic

Copy link
Copy Markdown
Contributor

Free-form text area

Lukas Beddrich from the BIFROST team informed me of some odd findings on the polarization monitors when using mpi. The simulations and issues were discovered by Ewout Voorrips. I had an LLM look at a simple fix, and so the below is a suggestion that seems to fix the issue, but it requires components to be aware of MPI merging. That seems to be necessary because the current DETECTOR_ macros make assumptions about errors based on the recorded number being an intensity rather than a polarization, which have different properties.

Maybe instead we should have appropriate DETECTOR_ macros that handle polarization specific issues?

LLM explanation of the issue and fix:

Fix MPI polarization monitor statistics

Fix MPI aggregation and uncertainty calculations in Pol_monitor and
PSD_Pol_monitor.

Previously, PSD_Pol_monitor normalized polarization before passing data to
the generic detector output path, which performed a second MPI reduction.
This caused a fully polarized beam to report polarization equal to the number
of MPI nodes.

The monitors now:

  • Merge raw moments before calculating polarization.
  • Preserve correct polarization values across MPI ranks.
  • Calculate polarization uncertainties from the merged moments.
  • Compensate for the detector output path's subsequent MPI reduction.

Testing

Tested on macOS using the mcstas-dev-agent environment:

  • Serial polarization regression instrument.
  • Two-rank MPI polarization regression instrument.
  • Existing Test_pol_ideal instrument, serial and with two MPI ranks.
  • Generated C code compiled with clang and mpicc, including warning builds.

The fixed MPI regression reports polarization 1 per PSD pixel with
near-zero uncertainty, instead of polarization equal to the MPI node count.


Declaration of use of AI-tools

  • Please add a checkmark here if you used AI-tools during the work for this contribution
  • Furter, please describe how / where and for what the tools were used:

Development OS / boundary conditions

Please describe what OS you developed and tested your additions on, and if any special dependencies are required:
OS X 14.6.1 (23G93) in UTM virtual machine


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 mctest utility to test one or more instruments making use of the component (please attach mcviewtest report as screenshot in comments)
    • 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

@mads-bertelsen-agentic
mads-bertelsen-agentic marked this pull request as draft November 6, 2026 13:17
@willend

willend commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Good to have as an early blueprint, I think we need a centralised solution, so

Maybe instead we should have appropriate DETECTOR_ macros that handle polarization specific issues?

yes maybe something like that.

At least I would not like addition of this MPI-mangling stuff across the board "per monitor".

Longer / more complete review / thoughts later.

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