Repository navigation
Union polyhedron display: only check volumes that overlap - #3192
lomholy-agentic wants to merge 1 commit into
Conversation
--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
left a comment
There was a problem hiding this comment.
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
|
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. |
|
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 |
Please push the commit! |
Free-form text area
--trace=2draws 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_listsalready finds, in INITIALIZE, which volumes overlap each volume, butgenerate_listsfreed that result. This PR keeps it for the display:generate_liststakes an optional out-pointer. When it is set, the raw overlap lists (geometric overlap, masks included) are handed back instead of freed.Union_master_GPUpassesNULL, so it frees them as before.Union_masterstores the lists, passes N's list tounion_mcdisplay_polyhedron, and frees them in FINALLY.union_mcdisplay_polyhedronleaves out (NULL) every volume that does not overlap N. The loops indraw_line_with_highest_priorityandr_has_highest_priorityskip 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:MAGIC_mastermainTested (
MCDISPLAYoutput with--trace=2,mainagainst this branch):Radiography_Sword,Geometry_test,Many_meshes,Conditional_test,Test_logger_3D_spaceandTest_mesh_boxes.Test_maskdraws no polyhedra; its fullMCDISPLAYoutput is identicalTest_meshandGeometry_test-n 1e4 --seed=1234) ofTest_mask,Test_mesh_boxesandGeometry_test: identical detector outputTest_lib_union_libin both flavours:PSD_I=10as in%TestPowderCompton_unionandTest_KN_Comp_Rayl_uniondo not link here onmaineither (xraylib missing)The display now relies on the overlap tests never missing a real overlap. Some of them are approximate (e.g.
mesh_overlaps_meshsamples points), but tracing already depends on the same lists.Declaration of use of AI-tools
Co-Authored-Byline naming the tool, see CONTRIBUTING.mdDevelopment 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
mccode-clangformattool to apply the standard McCode component indentation schemeMy work touches / adds to the runtime lib code (.c,.h etc in multiple locations
Test_lib_union_libexists 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