Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-01 10:10:51

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/VecgeomParams.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <string>
0010 #include <unordered_map>
0011 #include <vector>
0012 
0013 #include "corecel/Config.hh"
0014 
0015 #include "corecel/Types.hh"
0016 #include "corecel/cont/LabelIdMultiMap.hh"
0017 #include "corecel/data/ParamsDataInterface.hh"
0018 #include "corecel/data/ParamsDataStore.hh"
0019 #include "geocel/BoundingBox.hh"
0020 #include "geocel/GeoParamsInterface.hh"
0021 #include "geocel/Types.hh"
0022 #include "geocel/inp/Model.hh"
0023 
0024 #include "VecgeomData.hh"
0025 
0026 class G4VPhysicalVolume;
0027 
0028 // clang-format off
0029 #if CELERITAS_VECGEOM_VERSION < 0x020000
0030 #  if defined(__CUDACC__) || defined(__NVCC__)
0031 #    define CELERITAS_VECGEOM_GM_CXX_NS_BEGIN namespace cxx {
0032 #  else
0033 #    define CELERITAS_VECGEOM_GM_CXX_NS_BEGIN inline namespace cxx {
0034 #  endif
0035 #  define CELERITAS_VECGEOM_GM_CXX_NS_END } // end namespace cxx
0036 #else
0037 #  define CELERITAS_VECGEOM_GM_CXX_NS_BEGIN
0038 #  define CELERITAS_VECGEOM_GM_CXX_NS_END
0039 #endif
0040 // clang-format on
0041 
0042 namespace vecgeom
0043 {
0044 CELERITAS_VECGEOM_GM_CXX_NS_BEGIN
0045 class GeoManager;
0046 CELERITAS_VECGEOM_GM_CXX_NS_END
0047 }  // namespace vecgeom
0048 
0049 namespace celeritas
0050 {
0051 class GeantGeoParams;
0052 //---------------------------------------------------------------------------//
0053 /*!
0054  * Shared model parameters for a VecGeom geometry.
0055  *
0056  * The model defines the shapes, volumes, etc.
0057  */
0058 class VecgeomParams final : public GeoParamsInterface,
0059                             public ParamsDataInterface<VecgeomParamsData>
0060 {
0061   public:
0062     //!@{
0063     //! \name Type aliases
0064     using VecLv = std::vector<G4LogicalVolume const*>;
0065     using VecPv = std::vector<G4VPhysicalVolume const*>;
0066     using ImplVolInstanceId = VgVolumeInstanceId;
0067     using ImplVolInstanceMap = LabelIdMultiMap<ImplVolInstanceId>;
0068     //!@}
0069 
0070   public:
0071     // Whether VecGeom GDML is being used to load the geometry
0072     static bool use_vgdml();
0073 
0074     //!@{
0075     //! \name Static constructor helpers
0076     //! \todo Move these to a "model" abstraction
0077 
0078     // Build by loading a GDML file (best available method)
0079     static std::shared_ptr<VecgeomParams> from_gdml(
0080         std::string const& filename);
0081 
0082     // Build by loading a GDML file through Geant4
0083     static std::shared_ptr<VecgeomParams> from_gdml_g4(
0084         std::string const& filename);
0085 
0086     // Build by loading a GDML file through VecGeom VGDML
0087     static std::shared_ptr<VecgeomParams> from_gdml_vg(
0088         std::string const& filename);
0089 
0090     // Build from a Geant4 geometry
0091     static std::shared_ptr<VecgeomParams> from_geant(
0092         std::shared_ptr<GeantGeoParams const> const& geo);
0093 
0094     //!@}
0095 
0096     // Build from existing geometry, with ownership and mappings
0097     VecgeomParams(
0098         vecgeom::GeoManager const&, Ownership, VecLv const&, VecPv const&);
0099 
0100     // Clean up VecGeom on destruction
0101     ~VecgeomParams() final;
0102 
0103     //! Whether safety distance calculations are accurate and precise
0104     bool supports_safety() const final { return true; }
0105 
0106     //! Outer bounding box of geometry
0107     BBox const& bbox() const final { return bbox_; }
0108 
0109     // Maximum nested geometry depth
0110     inline VolumeLevelId::size_type num_volume_levels() const;
0111 
0112     // Create model parameters corresponding to our internal representation
0113     inp::Model make_model_input() const final;
0114 
0115     //// VOLUMES ////
0116 
0117     // Get volume metadata for VG logical volumes
0118     inline ImplVolumeMap const& impl_volumes() const final;
0119 
0120     // Get structural volume metadata
0121     SPConstVolumeParams const& volumes() const final { return volumes_; }
0122 
0123     // Get volume metadata for VG placed volumes
0124     inline ImplVolInstanceMap const& impl_volume_instances() const;
0125 
0126     // Get the canonical volume IDs corresponding to an implementation volume
0127     inline VolumeId volume_id(ImplVolumeId) const final;
0128 
0129     // Get the volume instance containing the global point
0130     VolumeInstanceId find_volume_instance_at(Real3 const&) const final;
0131 
0132     //// DATA ACCESS ////
0133 
0134     //! Access geometry data on host
0135     HostRef const& host_ref() const final { return data_.host_ref(); }
0136 
0137     //! Access geometry data on device
0138     DeviceRef const& device_ref() const final { return data_.device_ref(); }
0139 
0140   private:
0141     //// DATA ////
0142 
0143     // Flag for resetting VecGeom on destruction
0144     Ownership host_ownership_{Ownership::reference};
0145     Ownership device_ownership_{Ownership::reference};
0146 
0147     // Geant4 model used to construct
0148     std::shared_ptr<GeantGeoParams const> geant_geo_;
0149     SPConstVolumeParams volumes_;
0150 
0151     // Host metadata/access
0152     LabelIdMultiMap<ImplVolumeId> impl_volumes_;
0153     ImplVolInstanceMap impl_vol_instances_;
0154 
0155     BBox bbox_;
0156 
0157     // Host/device storage and reference
0158     ParamsDataStore<VecgeomParamsData> data_;
0159 
0160     //// HELPER FUNCTIONS ////
0161 
0162     void build_volume_tracking();
0163 };
0164 
0165 //---------------------------------------------------------------------------//
0166 
0167 extern template class ParamsDataStore<VecgeomParamsData>;
0168 extern template class ParamsDataInterface<VecgeomParamsData>;
0169 
0170 //---------------------------------------------------------------------------//
0171 // INLINE DEFINITIONS
0172 //---------------------------------------------------------------------------//
0173 /*!
0174  * Maximum nested geometry depth.
0175  *
0176  * \todo Only use in VolumeParams
0177  */
0178 VolumeLevelId::size_type VecgeomParams::num_volume_levels() const
0179 {
0180     return this->host_ref().scalars.num_volume_levels;
0181 }
0182 //
0183 //---------------------------------------------------------------------------//
0184 /*!
0185  * Get volume metadata.
0186  */
0187 auto VecgeomParams::impl_volumes() const -> ImplVolumeMap const&
0188 {
0189     return impl_volumes_;
0190 }
0191 
0192 //---------------------------------------------------------------------------//
0193 /*!
0194  * Get volume instance metadata.
0195  *
0196  * Volume instances correspond directly to Geant4 physical volumes.
0197  */
0198 auto VecgeomParams::impl_volume_instances() const -> ImplVolInstanceMap const&
0199 {
0200     return impl_vol_instances_;
0201 }
0202 
0203 //---------------------------------------------------------------------------//
0204 /*!
0205  * Get the canonical volume IDs corresponding to an implementation volume.
0206  *
0207  * \note See make_inp_volumes : for now, volume IDs and impl IDs are identical
0208  */
0209 inline VolumeId VecgeomParams::volume_id(ImplVolumeId iv_id) const
0210 {
0211     auto const& vol_ids = this->host_ref().volumes;
0212     CELER_EXPECT(!vol_ids.empty());
0213 
0214     return vol_ids[iv_id];
0215 }
0216 
0217 //---------------------------------------------------------------------------//
0218 }  // namespace celeritas