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/VecgeomData.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <VecGeom/base/Version.h>
0010 
0011 #include "corecel/Config.hh"
0012 
0013 #include "corecel/Macros.hh"
0014 #include "corecel/Types.hh"
0015 #include "corecel/data/Collection.hh"
0016 #include "corecel/sys/ThreadId.hh"
0017 #include "geocel/Types.hh"
0018 
0019 #include "VecgeomTypes.hh"
0020 
0021 #if CELERITAS_VECGEOM_VERSION >= 0x020000 && defined(VECGEOM_ENABLE_CUDA)
0022 // IWYU errors from navindex/tuple
0023 #    include <VecGeom/base/Cuda.h>
0024 #    include <VecGeom/base/Global.h>
0025 #    include <VecGeom/management/CudaManager.h>
0026 #endif
0027 
0028 #if CELER_VGNAV == CELER_VGNAV_PATH
0029 #    include "detail/VecgeomNavCollection.hh"
0030 #elif CELER_VGNAV == CELER_VGNAV_TUPLE
0031 #    include <VecGeom/navigation/NavStateTuple.h>
0032 #else
0033 #    include <VecGeom/navigation/NavStateIndex.h>
0034 #endif
0035 
0036 namespace celeritas
0037 {
0038 //---------------------------------------------------------------------------//
0039 // PARAMS
0040 //---------------------------------------------------------------------------//
0041 struct VecgeomScalars
0042 {
0043     using vol_level_uint = VolumeLevelId::size_type;
0044 
0045     VgPlacedVolume<MemSpace::host> const* host_world{nullptr};
0046     VgPlacedVolume<MemSpace::device> const* device_world{nullptr};
0047     vol_level_uint num_volume_levels{0};
0048 
0049     template<MemSpace M>
0050     CELER_FUNCTION VgPlacedVolume<M> const* world() const
0051     {
0052         if constexpr (M == MemSpace::host)
0053         {
0054             return host_world;
0055         }
0056         else if constexpr (M == MemSpace::device)
0057         {
0058             return device_world;
0059         }
0060 #if CELER_CUDACC_BUGGY_IF_CONSTEXPR
0061         CELER_ASSERT_UNREACHABLE();
0062 #endif
0063     }
0064 
0065     //! Whether the scalars are valid (device may be null)
0066     explicit CELER_FUNCTION operator bool() const
0067     {
0068         return host_world != nullptr && num_volume_levels > 0;
0069     }
0070 };
0071 
0072 //---------------------------------------------------------------------------//
0073 /*!
0074  * Persistent data used by VecGeom implementation.
0075  *
0076  * The volumes and volume instance mappings are set when constructing from an
0077  * external model, using VolumeParams to map to Geant4 geometry. For models
0078  * loaded through VGDML, the mapping is currently one-to-one for implementation
0079  * and placed volumes.
0080  */
0081 template<Ownership W, MemSpace M>
0082 struct VecgeomParamsData
0083 {
0084     using ImplVolInstanceId = VgVolumeInstanceId;
0085 
0086     //! Values that don't require host/device copying
0087     VecgeomScalars scalars;
0088 
0089     //! Map logical volume ID to canonical
0090     Collection<VolumeId, W, M, ImplVolumeId> volumes;
0091     //! Map placed volume ID to canonical
0092     Collection<VolumeInstanceId, W, M, ImplVolInstanceId> volume_instances;
0093 
0094     //! Whether the data is initialized
0095     explicit CELER_FUNCTION operator bool() const
0096     {
0097         // Volumes/instances can be absent if built from GDML
0098         return scalars && !volumes.empty() && !volume_instances.empty();
0099     }
0100 
0101     //! Assign from another set of data
0102     template<Ownership W2, MemSpace M2>
0103     VecgeomParamsData& operator=(VecgeomParamsData<W2, M2>& other)
0104     {
0105         CELER_EXPECT(other);
0106         scalars = other.scalars;
0107         volumes = other.volumes;
0108         volume_instances = other.volume_instances;
0109         return *this;
0110     }
0111 };
0112 
0113 //---------------------------------------------------------------------------//
0114 // STATE
0115 //---------------------------------------------------------------------------//
0116 /*!
0117  * Interface for VecGeom state information.
0118  *
0119  * The current and next navigation states are native VecGeom states that store
0120  * the boundary flag and the last exited volume. The "next" state is set by
0121  * \c find_next_step and consumed by \c cross_boundary .
0122  */
0123 template<Ownership W, MemSpace M>
0124 struct VecgeomStateData
0125 {
0126     //// TYPES ////
0127 
0128     template<class T>
0129     using StateItems = StateCollection<T, W, M>;
0130 #if CELER_VGNAV == CELER_VGNAV_PATH
0131     using VgStateItems = detail::VecgeomNavCollection<W, M>;
0132 #else
0133     using VgStateItems = StateItems<VgNavState>;
0134 #endif
0135 
0136     //// DATA ////
0137 
0138     // Physical state
0139     StateItems<Real3> pos;
0140     StateItems<Real3> dir;
0141 
0142     // Logical volumetric state
0143     VgStateItems state;
0144     VgStateItems next_state;
0145 
0146     //// METHODS ////
0147 
0148     //! True if sizes are consistent and states are assigned
0149     explicit CELER_FUNCTION operator bool() const
0150     {
0151         // clang-format off
0152         return pos.size() > 0
0153             && dir.size() == pos.size()
0154             && state.size() == pos.size()
0155             && next_state.size() == pos.size();
0156         // clang-format on
0157     }
0158 
0159     //! State size
0160     CELER_FUNCTION TrackSlotId::size_type size() const { return pos.size(); }
0161 
0162     //! Assign from another set of data
0163     template<Ownership W2, MemSpace M2>
0164     VecgeomStateData& operator=(VecgeomStateData<W2, M2>& other)
0165     {
0166         CELER_EXPECT(other);
0167         pos = other.pos;
0168         dir = other.dir;
0169         state = other.state;
0170         next_state = other.next_state;
0171         return *this;
0172     }
0173 };
0174 
0175 //---------------------------------------------------------------------------//
0176 // Resize geometry states
0177 template<MemSpace M>
0178 void resize(VecgeomStateData<Ownership::value, M>* data,
0179             HostCRef<VecgeomParamsData> const& params,
0180             size_type size);
0181 
0182 extern template void resize<MemSpace::host>(
0183     VecgeomStateData<Ownership::value, MemSpace::host>*,
0184     HostCRef<VecgeomParamsData> const&,
0185     size_type);
0186 
0187 extern template void resize<MemSpace::device>(
0188     VecgeomStateData<Ownership::value, MemSpace::device>*,
0189     HostCRef<VecgeomParamsData> const&,
0190     size_type);
0191 
0192 //---------------------------------------------------------------------------//
0193 }  // namespace celeritas