Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-07 10:25:37

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 orange/OrangeParams.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <memory>
0010 #include <string>
0011 
0012 #include "corecel/Types.hh"
0013 #include "corecel/cont/LabelIdMultiMap.hh"
0014 #include "corecel/data/ParamsDataInterface.hh"
0015 #include "corecel/data/ParamsDataStore.hh"
0016 #include "geocel/BoundingBox.hh"
0017 #include "geocel/GeoParamsInterface.hh"
0018 #include "geocel/inp/Model.hh"
0019 
0020 #include "OrangeData.hh"
0021 #include "OrangeTypes.hh"
0022 
0023 class G4VPhysicalVolume;
0024 
0025 namespace celeritas
0026 {
0027 struct OrangeInput;
0028 class GeantGeoParams;
0029 class VolumeParams;
0030 
0031 //---------------------------------------------------------------------------//
0032 /*!
0033  * Persistent model data for an ORANGE geometry.
0034  *
0035  * This class initializes and manages the data used by ORANGE (surfaces,
0036  * volumes) and provides a host-based interface for accessing metadata.
0037  */
0038 class OrangeParams final : public GeoParamsInterface,
0039                            public ParamsDataInterface<OrangeParamsData>
0040 {
0041   public:
0042     //!@{
0043     //! \name Type aliases
0044     using SurfaceMap = LabelIdMultiMap<ImplSurfaceId>;
0045     using UniverseMap = LabelIdMultiMap<UnivId>;
0046     using SPConstVolumes = std::shared_ptr<VolumeParams const>;
0047     //!@}
0048 
0049   public:
0050     //!@{
0051     //! \name Static constructor helpers
0052 
0053     // Build by loading a GDML file
0054     static std::shared_ptr<OrangeParams> from_gdml(std::string const& filename);
0055 
0056     // Build from a Geant4 geometry
0057     static std::shared_ptr<OrangeParams> from_geant(
0058         std::shared_ptr<GeantGeoParams const> const& geo,
0059         SPConstVolumes volumes);
0060 
0061     // Build from a Geant4 geometry (no volumes available?)
0062     static std::shared_ptr<OrangeParams> from_geant(
0063         std::shared_ptr<GeantGeoParams const> const& geo);
0064 
0065     // Build from a JSON input
0066     static std::shared_ptr<OrangeParams> from_json(std::string const& filename);
0067 
0068     //!@}
0069 
0070     // ADVANCED usage: construct from explicit host data
0071     OrangeParams(OrangeInput&& input);
0072 
0073     // ADVANCED usage: construct from explicit host data with volumes
0074     OrangeParams(OrangeInput&& input, SPConstVolumes&& volumes);
0075 
0076     // Default destructor to anchor vtable
0077     ~OrangeParams() final;
0078 
0079     // Moving would leave the class in an unspecified state
0080     CELER_DELETE_COPY_MOVE(OrangeParams);
0081 
0082     //! Whether safety distance calculations are accurate and precise
0083     bool supports_safety() const final { return supports_safety_; }
0084 
0085     //! Outer bounding box of geometry
0086     BBox const& bbox() const final { return bbox_; }
0087 
0088     // Number of levels deep universes can be (not geometry volume level!)
0089     inline UnivLevelId::size_type num_univ_levels() const;
0090 
0091     // Create model parameters corresponding to our internal representation
0092     inp::Model make_model_input() const final;
0093 
0094     //// LABELS AND MAPPING ////
0095 
0096     // Get surface metadata
0097     inline SurfaceMap const& surfaces() const;
0098 
0099     // Get universe metadata
0100     inline UniverseMap const& universes() const;
0101 
0102     // Get volume metadata
0103     inline ImplVolumeMap const& impl_volumes() const final;
0104 
0105     // Get structural volume metadata
0106     SPConstVolumeParams const& volumes() const final { return volumes_; }
0107 
0108     // Get the canonical volume IDs corresponding to an implementation volume
0109     inline VolumeId volume_id(ImplVolumeId) const final;
0110 
0111     // Get the (canonical) volume instance ID corresponding to an
0112     // implementation volume
0113     inline VolumeInstanceId volume_instance_id(ImplVolumeId) const;
0114 
0115     // Get the volume instance containing the global point
0116     VolumeInstanceId find_volume_instance_at(Real3 const&) const final;
0117 
0118     //// DATA ACCESS ////
0119 
0120     //! Reference to CPU geometry data
0121     HostRef const& host_ref() const final { return data_.host_ref(); }
0122 
0123     //! Reference to managed GPU geometry data
0124     DeviceRef const& device_ref() const final { return data_.device_ref(); }
0125 
0126   private:
0127     // Host metadata/access
0128     SurfaceMap impl_surf_labels_;
0129     UniverseMap univ_labels_;
0130     ImplVolumeMap impl_vol_labels_;
0131     BBox bbox_;
0132     bool supports_safety_{};
0133 
0134     // Retain volumes since we save a pointer for debugging
0135     SPConstVolumes volumes_;
0136 
0137     // Host/device storage and reference
0138     ParamsDataStore<OrangeParamsData> data_;
0139 };
0140 
0141 //---------------------------------------------------------------------------//
0142 
0143 extern template class ParamsDataStore<OrangeParamsData>;
0144 extern template class ParamsDataInterface<OrangeParamsData>;
0145 
0146 //---------------------------------------------------------------------------//
0147 // INLINE DEFINITIONS
0148 //---------------------------------------------------------------------------//
0149 /*!
0150  * Number of levels in the most deeply nested universe path.
0151  */
0152 UnivLevelId::size_type OrangeParams::num_univ_levels() const
0153 {
0154     return this->host_ref().scalars.num_univ_levels;
0155 }
0156 
0157 //---------------------------------------------------------------------------//
0158 /*!
0159  * Get surface metadata.
0160  */
0161 auto OrangeParams::surfaces() const -> SurfaceMap const&
0162 {
0163     return impl_surf_labels_;
0164 }
0165 
0166 //---------------------------------------------------------------------------//
0167 /*!
0168  * Get universe metadata.
0169  */
0170 auto OrangeParams::universes() const -> UniverseMap const&
0171 {
0172     return univ_labels_;
0173 }
0174 
0175 //---------------------------------------------------------------------------//
0176 /*!
0177  * Get volume metadata.
0178  */
0179 auto OrangeParams::impl_volumes() const -> ImplVolumeMap const&
0180 {
0181     return impl_vol_labels_;
0182 }
0183 
0184 //---------------------------------------------------------------------------//
0185 /*!
0186  * Get the canonical volume IDs corresponding to an implementation volume.
0187  */
0188 VolumeId OrangeParams::volume_id(ImplVolumeId iv_id) const
0189 {
0190     auto const& volume_id_map = this->host_ref().volume_ids;
0191     CELER_EXPECT(iv_id < volume_id_map.size());
0192     return volume_id_map[iv_id];
0193 }
0194 
0195 //---------------------------------------------------------------------------//
0196 /*!
0197  * Get the canonical volume instance corresponding to an implementation volume.
0198  *
0199  * This may be null if the local volume corresponds to a "background" volume or
0200  * "outside".
0201  */
0202 VolumeInstanceId OrangeParams::volume_instance_id(ImplVolumeId iv_id) const
0203 {
0204     auto const& volume_inst_id_map = this->host_ref().volume_instance_ids;
0205     CELER_EXPECT(iv_id < volume_inst_id_map.size());
0206     return volume_inst_id_map[iv_id];
0207 }
0208 
0209 //---------------------------------------------------------------------------//
0210 }  // namespace celeritas