Skip to content

Convert G4MultiUnion solids - #48

Draft
austinschneider wants to merge 2 commits into
celeritas-project:mainfrom
austinschneider:multiunion-support
Draft

austinschneider wants to merge 2 commits into
celeritas-project:mainfrom
austinschneider:multiunion-support

Conversation

@austinschneider

Copy link
Copy Markdown

This offers G4MultiUnion conversion support that the CCM experiment has been carrying in production since mid-2025. The code originates from a standalone G4VG snapshot dated 2025-06-30 (authored by @sethrj — credit preserved via co-author trailer; happy to re-attribute however you prefer). We extracted the MultiUnion delta against the snapshot's base (the tree matches upstream at #36 plus exactly these 125 lines) and rebased it onto current main; it applies cleanly with no other changes.

On our side this has compiled and run in every GPU production stack build for the CCM liquid-argon geometry (VecGeom 1.2.11, CUDA 12.8, sm_80), where the detector geometry contains G4MultiUnion solids.

One caveat for the device leg: vecgeom::UnplacedMultiUnion::CopyToGpu takes the generic byte-copy path and never copies the constituent node structure, so first device use of a converted MultiUnion is an illegal memory access until that is fixed in VecGeom — we run with a workaround and are reporting the VecGeom defect separately. Host-side conversion and use are unaffected.

Opening as a draft; glad to add a unit test in the repo's Gdml/Custom test style if you want this to proceed.

austinschneider and others added 2 commits August 10, 2026 16:14
Extracted from the standalone G4VG snapshot dated 2025-06-30 that the
CCM experiment has carried in production since, rebased onto current
main. The conversion code is unchanged from that snapshot.

Co-authored-by: Seth R. Johnson <johnsonsr@ornl.gov>

@sethrj sethrj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks a bunch @austinschneider ! There are a couple things (most importantly testing) to address but this is great looking work.

Comment on lines +780 to +783
G4Transform3D const& tr = solid.GetTransformation(i);
G4RotationMatrix const rot = tr.getRotation().inverse();
G4ThreeVector const trans = tr.getTranslation();
Transformation3D const vgtr = transform_(trans, &rot);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It would be a little nicer to use the same technique as convert_bool_impl:

Suggested change
G4Transform3D const& tr = solid.GetTransformation(i);
G4RotationMatrix const rot = tr.getRotation().inverse();
G4ThreeVector const trans = tr.getTranslation();
Transformation3D const vgtr = transform_(trans, &rot);
Transformation3D const vgtr = transform_(solid.GetTransformation(i).inverse());

by defining a new overload to Transformer:

    inline result_type operator()(G4Transform3D const& t) const;

}
result_type result = make_unplaced_boolean<kUnion>(
placed[0], placed.size() > 1 ? placed[1] : placed[0]);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A diagnostic message would be nice to indicate that the union has been converted to a nested bool:

G4VG_LOG(info) << "Converted multiunion '" << solid.GetName() << "' to a depth-" << level << " union";

Comment on lines +737 to +744
// A G4MultiUnion's constituents may overlap, which VecGeom's
// UnplacedMultiUnion handles via its own BVH but a tree of binary
// booleans does not: overlapping constituents leave internal
// boundaries that the navigator reports as real surface crossings.
// Native mode is therefore the physically faithful conversion, but
// UnplacedMultiUnion has no working GPU copy (its nodes are not
// transferred), so the boolean tree is the only device-capable form.
// G4VG_MULTIUNION=native selects the faithful path (host only).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is the vecgeom team aware of this bug?

<< "': " << n_inside << "/" << n_total
<< " points inside, " << n_mismatch
<< " containment mismatches";
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This would be a great comparison check to add to testing, but probably shouldn't be part of the conversion process itself. (It doesn't need any temporary internal data from the conversion process.) Could you try adding a GDML file test/data/multiunions.gdml and a MultiUnionsTest that does this check?

Comment on lines +745 to +748
{
char const* mode = std::getenv("G4VG_MULTIUNION");
if (mode && std::string_view{mode} == "native")
{

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Instead of being an environment variable, this should live in Options (see G4VG.hh) and be passed into the SolidConverter. Once the bug is fixed upstream, we can set the default conversion option based on the VecGeom version being used (and/or whether CUDA is enabled).

Comment on lines +816 to +817
result_type result = make_unplaced_boolean<kUnion>(
placed[0], placed.size() > 1 ? placed[1] : placed[0]);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not sure this is correct: if the size is 1 then it makes a union of the shape and itself? What if you changed the loop to placed.size() > 1, then assert that placed.size() == 1 and the result is just placed.front()?

@sethrj

sethrj commented Nov 2, 2026

Copy link
Copy Markdown
Member

hi @austinschneider ! I think your PR here was auto-generated, but it would still be useful if you want to send your robot minions to finish the work 😄 it woudl be a valuable additoin.

@sethrj

sethrj commented Dec 1, 2026

Copy link
Copy Markdown
Member

Ping @austinschneider

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