Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-15 10:24:38

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/detail/UniverseIndexer.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include "corecel/data/Collection.hh"
0010 #include "corecel/math/Algorithms.hh"
0011 
0012 #include "../OrangeData.hh"
0013 #include "../OrangeTypes.hh"
0014 
0015 namespace celeritas
0016 {
0017 namespace detail
0018 {
0019 //---------------------------------------------------------------------------//
0020 /*!
0021  * Convert a unit input to params data.
0022  *
0023  * Linearize the data in a UnitInput and add it to the host.
0024  */
0025 class UniverseIndexer
0026 {
0027   public:
0028     //!@{
0029     //! \name Type aliases
0030     using UniverseIndexerDataRef
0031         = UniverseIndexerData<Ownership::const_reference, MemSpace::native>;
0032 
0033     struct LocalSurface
0034     {
0035         UnivId univ;
0036         LocalSurfaceId surface;
0037     };
0038 
0039     struct LocalVolume
0040     {
0041         UnivId univ;
0042         LocalVolumeId volume;
0043     };
0044     //!@}
0045 
0046   public:
0047     // Construct from UniverseIndexerData
0048     explicit inline CELER_FUNCTION UniverseIndexer(
0049         UniverseIndexerDataRef const& data);
0050 
0051     // Local-to-global
0052     inline CELER_FUNCTION ImplSurfaceId global_surface(
0053         UnivId uni, LocalSurfaceId surface) const;
0054     inline CELER_FUNCTION ImplVolumeId global_volume(
0055         UnivId uni, LocalVolumeId volume) const;
0056 
0057     // Global-to-local
0058     inline CELER_FUNCTION LocalSurface local_surface(ImplSurfaceId id) const;
0059     inline CELER_FUNCTION LocalVolume local_volume(ImplVolumeId id) const;
0060 
0061     //! Total number of universes
0062     CELER_FUNCTION size_type num_universes() const
0063     {
0064         return data_.surfaces.size() - 1;
0065     }
0066 
0067     //! Total number of surfaces
0068     CELER_FUNCTION size_type num_surfaces() const
0069     {
0070         return data_.surfaces[AllVals{}].back();
0071     }
0072 
0073     //! Total number of cells
0074     CELER_FUNCTION size_type num_volumes() const
0075     {
0076         return data_.volumes[AllVals{}].back();
0077     }
0078 
0079   private:
0080     using DataRef
0081         = Collection<size_type, Ownership::const_reference, MemSpace::native>;
0082     using AllVals = AllItems<size_type, MemSpace::native>;
0083     using SizeId = OpaqueId<size_type>;
0084     using SpanIter = typename DataRef::SpanConstT::const_iterator;
0085 
0086     //// DATA ////
0087     UniverseIndexerDataRef const& data_;
0088 
0089     //// IMPLEMENTATION METHODS ////
0090     static inline CELER_FUNCTION SpanIter find_local(DataRef offsets,
0091                                                      size_type id);
0092     static inline CELER_FUNCTION size_type local_size(DataRef offsets,
0093                                                       UnivId uni);
0094 };
0095 
0096 //---------------------------------------------------------------------------//
0097 /*!
0098  * Construct from UniverseIndexerData.
0099  */
0100 CELER_FUNCTION UniverseIndexer::UniverseIndexer(
0101     UniverseIndexerDataRef const& data)
0102     : data_(data)
0103 {
0104     using namespace celeritas::literals;
0105     CELER_EXPECT(data_.surfaces.size() == data_.volumes.size());
0106     CELER_EXPECT(data_.surfaces[AllVals{}].front() == 0_sz);
0107     CELER_EXPECT(data_.volumes[AllVals{}].front() == 0_sz);
0108 }
0109 
0110 //---------------------------------------------------------------------------//
0111 /*!
0112  * Transform local to global surface ID.
0113  */
0114 CELER_FUNCTION ImplSurfaceId UniverseIndexer::global_surface(
0115     UnivId uni, LocalSurfaceId surf) const
0116 {
0117     CELER_EXPECT(uni < this->num_universes());
0118     CELER_EXPECT(surf < this->local_size(data_.surfaces, uni));
0119 
0120     return ImplSurfaceId(
0121         data_.surfaces[SizeId{uni.unchecked_get()}] + surf.unchecked_get());
0122 }
0123 
0124 //---------------------------------------------------------------------------//
0125 /*!
0126  * Transform local to global volume ID.
0127  */
0128 CELER_FUNCTION ImplVolumeId UniverseIndexer::global_volume(
0129     UnivId uni, LocalVolumeId volume) const
0130 {
0131     CELER_EXPECT(uni < this->num_universes());
0132     CELER_EXPECT(volume < this->local_size(data_.volumes, uni));
0133 
0134     return ImplVolumeId(
0135         data_.volumes[SizeId{uni.unchecked_get()}] + volume.unchecked_get());
0136 }
0137 
0138 //---------------------------------------------------------------------------//
0139 /*!
0140  * Transform global to local surface ID.
0141  */
0142 CELER_FUNCTION UniverseIndexer::LocalSurface UniverseIndexer::local_surface(
0143     ImplSurfaceId id) const
0144 {
0145     CELER_EXPECT(id < this->num_surfaces());
0146     auto iter = this->find_local(data_.surfaces, id.unchecked_get());
0147 
0148     auto uni = id_cast<UnivId>(iter - data_.surfaces[AllVals{}].begin());
0149     auto surface = id_cast<LocalSurfaceId>(id.unchecked_get() - *iter);
0150     CELER_ENSURE(uni < this->num_universes());
0151     return {uni, surface};
0152 }
0153 
0154 //---------------------------------------------------------------------------//
0155 /*!
0156  * Transform global to local volume ID.
0157  */
0158 CELER_FUNCTION UniverseIndexer::LocalVolume UniverseIndexer::local_volume(
0159     ImplVolumeId id) const
0160 {
0161     CELER_EXPECT(id < this->num_volumes());
0162     auto iter = this->find_local(data_.volumes, id.unchecked_get());
0163 
0164     auto uni = id_cast<UnivId>(iter - data_.volumes[AllVals{}].begin());
0165     auto volume = id_cast<LocalVolumeId>(id.unchecked_get() - *iter);
0166     CELER_ENSURE(uni < this->num_universes());
0167     return {uni, volume};
0168 }
0169 
0170 //---------------------------------------------------------------------------//
0171 // IMPLEMENTATION METHODS
0172 //---------------------------------------------------------------------------//
0173 /*!
0174  * Locate the given ID in the list of offsets.
0175  */
0176 CELER_FUNCTION UniverseIndexer::SpanIter UniverseIndexer::find_local(
0177     DataRef offsets, size_type id)
0178 {
0179     CELER_EXPECT(id < offsets[SizeId{offsets.size() - 1}]);
0180 
0181     // Use upper bound to skip past universes with zero surfaces.
0182     auto iter = upper_bound(
0183         offsets[AllVals{}].begin(), offsets[AllVals{}].end(), id);
0184 
0185     CELER_ASSERT(iter != offsets[AllVals{}].end());
0186     --iter;
0187 
0188     CELER_ENSURE(*iter <= id);
0189     CELER_ENSURE(id < *(iter + 1));
0190     return iter;
0191 }
0192 
0193 //---------------------------------------------------------------------------//
0194 /*!
0195  * Get the number of elements in the given universe.
0196  */
0197 CELER_FUNCTION size_type UniverseIndexer::local_size(DataRef offsets,
0198                                                      UnivId uni)
0199 {
0200     CELER_EXPECT(uni && uni.unchecked_get() + 1 < offsets.size());
0201     return offsets[SizeId{uni.unchecked_get() + 1}]
0202            - offsets[SizeId{uni.unchecked_get()}];
0203 }
0204 
0205 //---------------------------------------------------------------------------//
0206 }  // namespace detail
0207 }  // namespace celeritas