Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-09 10:21:11

0001 //------------------------------- -*- C++ -*- -------------------------------//
0002 // Copyright Celeritas contributors: see top-level COPYRIGHT file for details
0003 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0004 //---------------------------------------------------------------------------//
0005 //! \file geocel/vg/detail/SolidsNavigator.hh
0006 //! \sa geocel/vg/Vecgeom.test.cc
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include <limits>
0011 #include <VecGeom/navigation/BVHNavigator.h>
0012 
0013 #include "corecel/Macros.hh"
0014 #include "corecel/Types.hh"
0015 #include "geocel/vg/VecgeomTypes.hh"
0016 
0017 namespace celeritas
0018 {
0019 namespace detail
0020 {
0021 //---------------------------------------------------------------------------//
0022 /*!
0023  * Adapt VecGeom's solid navigation to Celeritas navigation state.
0024  *
0025  * Host and device queries use VecGeom's IndexedBVH-backed BVHNavigator
0026  * directly on the stored VecGeom navigation state, which retains the boundary
0027  * flag between calls. The last exited volume is retained only from
0028  * \c ComputeStepAndNextVolume until it is used and cleared by
0029  * \c RelocateToNextVolume .
0030  */
0031 class SolidsNavigator
0032 {
0033   public:
0034     using VgPlacedVol = VgPlacedVolume<MemSpace::native>;
0035     using NavState = VgNavState;
0036 
0037     //-----------------------------------------------------------------------//
0038     // Locate a point in the geometry hierarchy
0039     CELER_FUNCTION static void LocatePointIn(
0040         VgPlacedVol const* vol,
0041         VgReal3 const& point,
0042         NavState& nav,
0043         bool top,
0044         VgPlacedVol const* exclude = nullptr)
0045     {
0046         vecgeom::BVHNavigator::LocatePointIn(vol, point, nav, top, exclude);
0047 
0048         // Location alone does not establish a crossed boundary. In particular,
0049         // do not push a newly initialized track past a nearby surface.
0050         nav.SetBoundaryState(false);
0051     }
0052 
0053     //-----------------------------------------------------------------------//
0054     // Find the next boundary and prepare the output state for relocation
0055     CELER_FUNCTION static vg_real_type ComputeStepAndNextVolume(
0056         VgReal3 const& glpos,
0057         VgReal3 const& gldir,
0058         vg_real_type step_limit,
0059         NavState const& in_state,
0060         NavState& out_state)
0061     {
0062         // VecGeom treats sub-tolerance step limits as boundary crossings.
0063         // Query beyond its boundary push, then apply the physics limit here.
0064         auto query_limit = vecCore::math::Max(
0065             step_limit, 2 * vecgeom::BVHNavigator::kBoundaryPush);
0066         auto step = vecgeom::BVHNavigator::ComputeStepAndNextVolume(
0067             glpos, gldir, query_limit, in_state, out_state);
0068         if (step > step_limit)
0069         {
0070             out_state = in_state;
0071             out_state.SetBoundaryState(false);
0072             return step_limit;
0073         }
0074 
0075         return step;
0076     }
0077 
0078     //-----------------------------------------------------------------------//
0079     // Computes the isotropic safety from the globalpoint
0080     CELER_FUNCTION static double ComputeSafety(
0081         VgReal3 const& glpos,
0082         NavState const& curr,
0083         vg_real_type safety = std::numeric_limits<vg_real_type>::infinity())
0084     {
0085         real_type result
0086             = vecgeom::BVHNavigator::ComputeSafety(glpos, curr, safety);
0087         result = vecCore::math::Min(result, safety);
0088 
0089         return result;
0090     }
0091 
0092     //-----------------------------------------------------------------------//
0093     // Relocate a state that was returned from ComputeStepAndNextVolume
0094     CELER_FUNCTION static void RelocateToNextVolume(VgReal3 const& glpos,
0095                                                     VgReal3 const& gldir,
0096                                                     NavState const&,
0097                                                     NavState& out_state)
0098     {
0099         // The last exited volume was recorded in the output state by
0100         // ComputeStepAndNextVolume, preventing reentry at the exact boundary
0101         vecgeom::BVHNavigator::RelocateToNextVolume(glpos, gldir, out_state);
0102 
0103         // The exited volume only applies to this crossing: clear it so that a
0104         // later crossing (e.g., after reflection) may reenter it, and so that
0105         // entering a daughter never inherits an unrelated excluded volume
0106         out_state.SetLastExited(decltype(out_state.GetLastExitedState()){});
0107     }
0108 };
0109 
0110 //---------------------------------------------------------------------------//
0111 }  // namespace detail
0112 }  // namespace celeritas