Skip to content

Union polyhedron display: only check volumes that overlap - #3192

Open
lomholy-agentic wants to merge 1 commit into
mccode-dev:mainfrom
lomholy-agentic:union-display-overlap-lists
Open

lomholy-agentic wants to merge 1 commit into
mccode-dev:mainfrom
lomholy-agentic:union-display-overlap-lists

Conversation

@lomholy-agentic

@lomholy-agentic lomholy-agentic commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Free-form text area

--trace=2 draws each Union volume as a polyhedron. For every triangle edge it intersects all other volumes of the master, so the cost grows as n². In the ESS facility model, MAGIC_master (778 boxes, 188 mesh masks) took 70 of the 77 s needed to write the geometry.

generate_overlap_lists already finds, in INITIALIZE, which volumes overlap each volume, but generate_lists freed that result. This PR keeps it for the display:

  • generate_lists takes an optional out-pointer. When it is set, the raw overlap lists (geometric overlap, masks included) are handed back instead of freed. Union_master_GPU passes NULL, so it frees them as before.
  • Union_master stores the lists, passes N's list to union_mcdisplay_polyhedron, and frees them in FINALLY.
  • union_mcdisplay_polyhedron leaves out (NULL) every volume that does not overlap N. The loops in draw_line_with_highest_priority and r_has_highest_priority skip NULL entries, and a NULL mask counts as "point not inside". This is exact: such a mask does not overlap N, so it contains no point of N's surface.

Display only: TRACE does not call these functions, and the overlap lists are computed as before, so simulation results are unaffected. The wireframe display (--trace=1, display_mode=1) is unchanged.

Same change in McStas and McXtrace. This is an alternative to #3185: it reuses the existing overlap lists instead of adding bounding boxes per geometry, as suggested in the discussion there. It also leaves out the far masks, which #3185 keeps.

Timing, ESS facility, --trace=2, -O0, macOS arm64:

total MAGIC_master
main 77.2 s 70.0 s
#3185 36.7 s
this PR 9.7 s 3.2 s

Tested (MCDISPLAY output with --trace=2, main against this branch):

  • ESS facility: polyhedra byte-identical (70 502 polyhedra)
  • McStas Union examples: polyhedra identical for Radiography_Sword, Geometry_test, Many_meshes, Conditional_test, Test_logger_3D_space and Test_mesh_boxes. Test_mask draws no polyhedra; its full MCDISPLAY output is identical
  • McXtrace: polyhedra identical for Test_mesh and Geometry_test
  • Seeded runs (-n 1e4 --seed=1234) of Test_mask, Test_mesh_boxes and Geometry_test: identical detector output
  • Test_lib_union_lib in both flavours: PSD_I=10 as in %Test
  • Not tested: PowderCompton_union and Test_KN_Comp_Rayl_union do not link here on main either (xraylib missing)

The display now relies on the overlap tests never missing a real overlap. Some of them are approximate (e.g. mesh_overlaps_mesh samples points), but tracing already depends on the same lists.


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 Code (Opus 5.5) profiled, implemented and tested the change, following the approach proposed by Lomholy
  • AI-assisted commits must carry a Co-Authored-By line naming the tool, see CONTRIBUTING.md

Development OS / boundary conditions

macOS arm64, clang. Not tested on Linux, Windows or GPU. No new dependencies.


PR Checklist for contributing to McStas/McXtrace

  • My contribution includes patches to an existing component file

    • 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
  • My work touches / adds to the runtime lib code (.c,.h etc in multiple locations

    • I have added reasoning and documentation for the change below
    • Unit test: Test_lib_union_lib exists in both flavours and passes

🤖 Generated with Claude Code


Tip for reviewers: Visit https://blueblazin.github.io/blockviz/#github=https://github.com/mccode-dev/McCode/pull/3192 for a visual look at code changes

--trace=2 drew each Union volume by intersecting every triangle edge with
all other volumes of the master. The raw overlap lists made in
generate_lists are now kept by Union_master (freed in FINALLY) and
union_mcdisplay_polyhedron only checks the volumes that overlap the drawn
one. Display only: TRACE does not use these functions.

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

@willend willend left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks @Lomholy, I prefer this over #3185: it reuses what generate_overlap_lists already knows, has nothing to update when new geometries are added, and is faster. I will close #3185.

One simplification to consider: geometry_struct already holds lists like this (intersect_check_list, next_volume_list, ...), freed in FINALLY. Storing the raw overlap list there avoids the out-pointer:

// union-lib.h, geometry_struct
// List of volumes overlapping this one (only used in polyhedron display)
struct pointer_to_1d_int_list overlap_list;

// union-lib.c, generate_lists: instead of freeing raw_overlap_lists[iterate]->elements
Volumes[iterate]->geometry.overlap_list = *raw_overlap_lists[iterate];
free(raw_overlap_lists[iterate]);

// FINALLY, next to the other lists (Union_master in both flavours, and Union_master_GPU)
if (Volumes[volume_index]->geometry.overlap_list.num_elements > 0)
  free (Volumes[volume_index]->geometry.overlap_list.elements);

union_mcdisplay_polyhedron can then use g->overlap_list. The signatures of generate_lists and union_mcdisplay_polyhedron stay as they are, and the DECLARE pointer, the NULL argument and the separate free loop go away. I have not compiled this.

Would you like us to push a commit with this simplification to your branch, or do you prefer to implement it yourself?

The NULL checks on masks look right to me, and worth keeping for the speedup.

Please also check that the OpenACC CI leg is green. #3185 first failed there.

🤖 Generated with Claude Code

@mads-bertelsen

Copy link
Copy Markdown
Contributor

Great with this new version, that's a huge time saver!

The overlap list might not actually be the smallest list we can use, but it's a pretty safe bet. Only annoying thing is we need to get it allocated in the main scope, will look into if one of the lists already in main scope can be used, it might be the one called next_volume, but that might have an issue with masks.

@mads-bertelsen

Copy link
Copy Markdown
Contributor

One other avenue of optimization is the r_has_highest_priority call which is executed a very large number of times to check what triangles need to be cut, I believe that currently checks all other geometries in the current master, it could also be reduced to a small subset using an appropriate list

@Lomholy

Lomholy commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

@willend

union_mcdisplay_polyhedron can then use g->overlap_list. The signatures of generate_lists and union_mcdisplay_polyhedron stay as they are, and the DECLARE pointer, the NULL argument and the separate free loop go away. I have not compiled this.

Would you like us to push a commit with this simplification to your branch, or do you prefer to implement it yourself?

The NULL checks on masks look right to me, and worth keeping for the speedup.

Please also check that the OpenACC CI leg is green. #3185 first failed there.

Please push the commit!

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.

4 participants

⌂ Proxy