Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support surface-based VecGeom 2.x navigator #1422

Draft
wants to merge 36 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
2cb9b11
Adapt to VG 2.x templated ABBoxManager
mrguilima Sep 16, 2024
ef6eec4
Updates to use the latest BVHSurfNavigator from VecGeom 2.x
mrguilima Sep 20, 2024
6fa1b18
Temporary fix to four-levels.gdml: replace unsupported orb with suppo…
mrguilima Sep 18, 2024
228ed58
Fixes to Vecgeom:FourLevelsTest*
mrguilima Sep 18, 2024
9956e5d
Revert "Fixes to Vecgeom:FourLevelsTest*"
sethrj Sep 23, 2024
5265793
Add orb->sphere translation and disable unimplemented surface solids
sethrj Sep 23, 2024
0c943b7
Add surface geometry to CI
sethrj Sep 24, 2024
f272247
Merge remote-tracking branch 'upstream/develop' into vg-2.x
sethrj Sep 24, 2024
94eba34
REVERTME: delete other CI builds
sethrj Sep 24, 2024
97be3f7
Fix infinite loop in field propagator due to misbehaving boundary
sethrj Sep 25, 2024
162bfa2
Simplify initializer
sethrj Sep 25, 2024
f4ce1c8
Add two-boxes test and re-enable other vecgeom tests
sethrj Sep 25, 2024
6d7acf4
Fix distance-to-boundary by initializing hit surface to -1
sethrj Sep 25, 2024
161823a
Fix SurfNavigator for TwoBoxes*: new field, VecgeomTrackView::hit_surf_
mrguilima Oct 2, 2024
8682d4b
Merge remote-tracking branch 'upstream/develop' into vg-2.x
sethrj Oct 7, 2024
a64570c
Revert orb-to-sphere conversion after vecgeom#1231
sethrj Sep 25, 2024
88e4716
Always save cache if build succeeds
sethrj Oct 8, 2024
a0f38b2
fixup! Always save cache if build succeeds
sethrj Oct 8, 2024
f9d8fb0
New reference values for CmseTest.trace
mrguilima Oct 6, 2024
f29529d
Always reset hit_surf before calling finding next step
mrguilima Oct 10, 2024
69a1de2
No more warnings during loading of four-levels geometry
mrguilima Oct 10, 2024
f75b87b
No more warnings during loading of Cmse.gdml
mrguilima Oct 10, 2024
976089d
Add CELERITAS_VECGEOM_SURFACE macro
sethrj Oct 15, 2024
3b0385b
Make next surface persistent
sethrj Oct 15, 2024
e45bc55
Loosen move-internal requirement for VecGeom
sethrj Oct 15, 2024
137ae12
Remove warning expectation from vecgeom 2
sethrj Oct 15, 2024
4eeed43
Fix build
sethrj Oct 15, 2024
a107ae9
Disabled two tests doing tracking outside of world
mrguilima Oct 15, 2024
b9cbe7f
GL temp: missing code
mrguilima Oct 15, 2024
9426140
Merge branch 'develop' into vg-2.x
mrguilima Oct 23, 2024
a85bc2f
Disable tests related to navigation from the outside
mrguilima Oct 14, 2024
39941c0
Fixes for VecGeom v2.x
mrguilima Oct 29, 2024
970100b
Switch test expected value depending on VecGeom version
mrguilima Nov 4, 2024
7f9b580
More temporary fixes to Vecgeom tests
mrguilima Nov 5, 2024
ce5ec00
Merge branch 'develop' into vg-2.x
mrguilima Nov 5, 2024
b7a5aaa
Merge branch 'develop' into vg-2.x
mrguilima Nov 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions src/geocel/g4vg/SolidConverter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,17 @@ make_unplaced_boolean(VPlacedVolume const* left, VPlacedVolume const* right)
return GeoManager::MakeInstance<UnplacedBooleanVolume<Op>>(Op, left, right);
}

//---------------------------------------------------------------------------//
VUnplacedVolume* make_sphere(double radius)
{
CELER_EXPECT(radius > 0);
#ifdef VECGEOM_USE_SURF
return GeoManager::MakeInstance<UnplacedSphere>(
0, radius, 0, vecgeom::kPi * 2, 0, vecgeom::kPi);
#endif
return GeoManager::MakeInstance<UnplacedOrb>(radius);
}

//---------------------------------------------------------------------------//
} // namespace

Expand Down Expand Up @@ -156,8 +167,7 @@ auto SolidConverter::operator()(arg_type solid_base) -> result_type
auto SolidConverter::to_sphere(arg_type solid_base) const -> result_type
{
double vol = this->calc_capacity(solid_base);
double radius = std::cbrt(vol / (4.0 / 3.0 * constants::pi));
return GeoManager::MakeInstance<UnplacedOrb>(radius);
return make_sphere(std::cbrt(vol / (4.0 / 3.0 * constants::pi)));
}

//---------------------------------------------------------------------------//
Expand All @@ -177,17 +187,25 @@ auto SolidConverter::convert_impl(arg_type solid_base) -> result_type
VGSC_TYPE_FUNC(Box , box),
VGSC_TYPE_FUNC(Cons , cons),
VGSC_TYPE_FUNC(CutTubs , cuttubs),
#ifndef VECGEOM_USE_SURF
VGSC_TYPE_FUNC(Ellipsoid , ellipsoid),
VGSC_TYPE_FUNC(EllipticalCone , ellipticalcone),
VGSC_TYPE_FUNC(EllipticalTube , ellipticaltube),
#endif
VGSC_TYPE_FUNC(ExtrudedSolid , extrudedsolid),
#ifndef VECGEOM_USE_SURF
VGSC_TYPE_FUNC(GenericPolycone , genericpolycone),
#endif
VGSC_TYPE_FUNC(GenericTrap , generictrap),
#ifndef VECGEOM_USE_SURF
VGSC_TYPE_FUNC(Hype , hype),
#endif
VGSC_TYPE_FUNC(IntersectionSolid, intersectionsolid),
VGSC_TYPE_FUNC(Orb , orb),
VGSC_TYPE_FUNC(Para , para),
#ifndef VECGEOM_USE_SURF
VGSC_TYPE_FUNC(Paraboloid , paraboloid),
#endif
VGSC_TYPE_FUNC(Polycone , polycone),
VGSC_TYPE_FUNC(Polyhedra , polyhedra),
VGSC_TYPE_FUNC(ReflectedSolid , reflectedsolid),
Expand Down Expand Up @@ -416,7 +434,12 @@ auto SolidConverter::intersectionsolid(arg_type solid_base) -> result_type
auto SolidConverter::orb(arg_type solid_base) -> result_type
{
auto const& solid = dynamic_cast<G4Orb const&>(solid_base);
return GeoManager::MakeInstance<UnplacedOrb>(scale_(solid.GetRadius()));
auto radius = scale_(solid.GetRadius());
#ifdef VECGEOM_USE_SURF
CELER_LOG(info) << "Replacing orb with equivalent sphere for surface "
"geometry";
#endif
return make_sphere(radius);
}

//---------------------------------------------------------------------------//
Expand Down
13 changes: 11 additions & 2 deletions src/geocel/vg/VecgeomParams.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <vector>
#include <VecGeom/base/Config.h>
#include <VecGeom/base/Cuda.h>
#include <VecGeom/base/Version.h>
Copy link
Member

Choose a reason for hiding this comment

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

I've verified that this header (and the VECGEOM_VERSION macro therein) are present in v1.2.4, which is the minimum vecgeom required by Celeritas.

#include <VecGeom/management/ABBoxManager.h>
#include <VecGeom/management/BVHManager.h>
#include <VecGeom/management/GeoManager.h>
Expand Down Expand Up @@ -410,7 +411,11 @@ void VecgeomParams::build_volume_tracking()

{
ScopedTimeAndRedirect time_and_output_("vecgeom::ABBoxManager");
#if VECGEOM_VERSION < 0x020000
vecgeom::ABBoxManager::Instance().InitABBoxesForCompleteGeometry();
#else
vecgeom::ABBoxManager<real_type>::Instance().InitABBoxesForCompleteGeometry();
#endif
}

// Init the bounding volume hierarchy structure
Expand Down Expand Up @@ -570,9 +575,13 @@ void VecgeomParams::build_metadata()
VPlacedVolume const* pv = GeoManager::Instance().GetWorld();

// Calculate bounding box
#if VECGEOM_VERSION < 0x020000
auto bbox_mgr = ABBoxManager::Instance();
#else
auto bbox_mgr = ABBoxManager<real_type>::Instance();
#endif
Vector3D<real_type> lower, upper;
ABBoxManager::Instance().ComputeABBox(pv, &lower, &upper);

bbox_mgr.ComputeABBox(pv, &lower, &upper);
return BBox{detail::to_array(lower), detail::to_array(upper)};
}();
}
Expand Down
34 changes: 20 additions & 14 deletions src/geocel/vg/VecgeomTrackView.hh
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@

#include "detail/VecgeomCompatibility.hh"

#if VECGEOM_VERSION < 0x020000
# include "detail/BVHNavigator.hh"
#elif defined(VECGEOM_USE_SURF)
# include "detail/SurfNavigator.hh"
#if defined(VECGEOM_USE_SURF)
# include "geocel/vg/detail/SurfNavigator.hh"
#else
# include <VecGeom/navigation/BVHNavigator.h>
# include "geocel/vg/detail/BVHNavigator.hh"
#endif

namespace celeritas
Expand All @@ -57,12 +55,10 @@ class VecgeomTrackView
using Initializer_t = GeoTrackInitializer;
using ParamsRef = NativeCRef<VecgeomParamsData>;
using StateRef = NativeRef<VecgeomStateData>;
#if VECGEOM_VERSION < 0x020000
using Navigator = celeritas::detail::BVHNavigator;
#elif defined(VECGEOM_USE_SURF)
#if defined(VECGEOM_USE_SURF)
using Navigator = celeritas::detail::SurfNavigator;
#else
using Navigator = vecgeom::BVHNavigator;
using Navigator = celeritas::detail::BVHNavigator;
#endif
//!@}

Expand Down Expand Up @@ -215,13 +211,17 @@ VecgeomTrackView::operator=(Initializer_t const& init)

// Set up current state and locate daughter volume.
vgstate_.Clear();
vecgeom::VPlacedVolume const* worldvol = params_.world_volume;
bool const contains_point = true;

#ifdef VECGEOM_USE_SURF
auto world_id = vecgeom::NavigationState::WorldId();
// LocatePointIn sets `vgstate_`
Navigator::LocatePointIn(
worldvol, detail::to_vector(pos_), vgstate_, contains_point);

Navigator::LocatePointIn(world_id, detail::to_vector(pos_), vgstate_, contains_point);
#else
vecgeom::VPlacedVolume const* worldvol = params_.world_volume;
// LocatePointIn sets `vgstate_`
Navigator::LocatePointIn(worldvol,
detail::to_vector(pos_), vgstate_, contains_point);
#endif
return *this;
}

Expand Down Expand Up @@ -421,10 +421,16 @@ CELER_FUNCTION void VecgeomTrackView::cross_boundary()
// Relocate to next tracking volume (maybe across multiple boundaries)
if (vgnext_.Top() != nullptr)
{
#ifdef VECGEOM_USE_SURF
long hitsurf_id = -1;
Navigator::RelocateToNextVolume(detail::to_vector(this->pos_),
detail::to_vector(this->dir_), hitsurf_id, vgnext_);
#else
// Some navigators require an lvalue temp_pos
auto temp_pos = detail::to_vector(this->pos_);
Navigator::RelocateToNextVolume(
temp_pos, detail::to_vector(this->dir_), vgnext_);
#endif
}

vgstate_ = vgnext_;
Expand Down
72 changes: 44 additions & 28 deletions src/geocel/vg/detail/SurfNavigator.hh
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
#include <VecGeom/base/Config.h>
#include <VecGeom/base/Global.h>
#include <VecGeom/base/Vector3D.h>
#include <VecGeom/navigation/NavStateIndex.h>
#include <VecGeom/surfaces/Navigator.h>
#include <VecGeom/navigation/NavigationState.h>
#include <VecGeom/surfaces/BVHSurfNavigator.h>

#ifdef VECGEOM_ENABLE_CUDA
# include <VecGeom/backend/cuda/Interface.h>
Expand All @@ -32,29 +32,39 @@ class SurfNavigator
public:
using Precision = vecgeom::Precision;
using Vector3D = vecgeom::Vector3D<vecgeom::Precision>;
using VPlacedVolumePtr_t = vecgeom::VPlacedVolume const*;
using SurfData = vgbrep::SurfData<Precision>;
using Real_b = typename SurfData::Real_b;
using VPlacedVolumePtr_t = vecgeom::VPlacedVolume const*;

static constexpr Precision kBoundaryPush = 10 * vecgeom::kTolerance;

// Locates the point in the geometry volume tree
CELER_FUNCTION static VPlacedVolumePtr_t
LocatePointIn(VPlacedVolumePtr_t vol,
/// @brief Locates the point in the geometry volume tree
/// @param pvol_id Placed volume id to be checked first
/// @param point Point to be checked, in the local frame of pvol
/// @param path Path to a parent of pvol that must contain the point
/// @param top Check first if pvol contains the point
/// @param exclude Placed volume id to exclude from the search
/// @return Index of the placed volume that contains the point
CELER_FUNCTION static int
LocatePointIn(int pvol_id,
Vector3D const& point,
vecgeom::NavStateIndex& path,
bool top)
vecgeom::NavigationState& path,
bool top,
int *exclude = nullptr)
{
return vgbrep::protonav::LocatePointIn(vol, point, path, top);
return vgbrep::protonav::BVHSurfNavigator<Precision>::LocatePointIn(pvol_id, point, path, top, exclude);
}

// Computes the isotropic safety from the globalpoint.
/// @brief Computes the isotropic safety from the globalpoint.
/// @param globalpoint Point in global coordinates
/// @param state Path where to compute safety
/// @return Isotropic safe distance
CELER_FUNCTION static Precision
ComputeSafety(Vector3D const& globalpoint,
vecgeom::NavStateIndex const& state)
vecgeom::NavigationState const& state)
{
int closest_surf = 0;
return vgbrep::protonav::ComputeSafety(
globalpoint, state, closest_surf);
auto safety = vgbrep::protonav::BVHSurfNavigator<Precision>::ComputeSafety(globalpoint, state);
return safety;
}

// Computes a step from the globalpoint (which must be in the current
Expand All @@ -68,9 +78,8 @@ class SurfNavigator
ComputeStepAndNextVolume(Vector3D const& globalpoint,
Vector3D const& globaldir,
Precision step_limit,
vecgeom::NavStateIndex const& in_state,
vecgeom::NavStateIndex& out_state,
[[maybe_unused]] Precision push = 0)
vecgeom::NavigationState const& in_state,
vecgeom::NavigationState& out_state)
{
if (step_limit <= 0)
{
Expand All @@ -79,9 +88,9 @@ class SurfNavigator
return step_limit;
}

int exit_surf = 0;
auto step = vgbrep::protonav::ComputeStepAndHit(
globalpoint, globaldir, in_state, out_state, exit_surf, step_limit);
long hitsurf_id = 0;
auto step = vgbrep::protonav::BVHSurfNavigator<Precision>::ComputeStepAndNextSurface(
globalpoint, globaldir, in_state, out_state, hitsurf_id, step_limit);
Copy link
Member

Choose a reason for hiding this comment

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

Something here is misbehaving compared to the volume version:

54: /Users/seth/Code/celeritas-temp/test/geocel/vg/Vecgeom.test.cc:164: Failure
54: Value of: next.boundary
54:   Actual: true
54: Expected: false

for the calls

    auto next = geo.find_next_step(from_cm(100));
    EXPECT_SOFT_EQ(100, to_cm(next.distance));
    EXPECT_FALSE(next.boundary);

So it's claiming to have a boundary for the next surface, even though it doesn't hit it within the step limit.

return step;
}

Expand All @@ -93,22 +102,29 @@ class SurfNavigator
ComputeStepAndPropagatedState(Vector3D const& globalpoint,
Vector3D const& globaldir,
Precision step_limit,
vecgeom::NavStateIndex const& in_state,
vecgeom::NavStateIndex& out_state,
Precision push = 0)
vecgeom::NavigationState const& in,
vecgeom::NavigationState& out)
{
return ComputeStepAndNextVolume(
globalpoint, globaldir, step_limit, in_state, out_state, push);
return ComputeStepAndNextVolume(globalpoint, globaldir, step_limit, in, out);
}

// Relocate a state that was returned from ComputeStepAndNextVolume: the
// surface model does this computation within ComputeStepAndNextVolume, so
// the relocation does nothing
CELER_FUNCTION static void
RelocateToNextVolume(Vector3D const& /*globalpoint*/,
Vector3D const& /*globaldir*/,
vecgeom::NavStateIndex& /*state*/)
RelocateToNextVolume(Vector3D const& globalpoint,
Vector3D const& globaldir,
long hitsurf_index,
vecgeom::NavigationState& out_state)
{
vgbrep::CrossedSurface crossed_surf;
vgbrep::protonav::BVHSurfNavigator<Precision>::RelocateToNextVolume(
globalpoint,
globaldir,
Precision(0),
hitsurf_index,
out_state,
crossed_surf);
}
};

Expand Down
Loading