Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-27 10:40: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/OrangeData.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include "corecel/Assert.hh"
0010 #include "corecel/Macros.hh"
0011 #include "corecel/OpaqueId.hh"
0012 #include "corecel/Types.hh"
0013 #include "corecel/cont/Range.hh"
0014 #include "corecel/data/Collection.hh"
0015 #include "corecel/data/CollectionBuilder.hh"
0016 #include "corecel/sys/ThreadId.hh"
0017 #include "geocel/BoundingBox.hh"  // IWYU pragma: keep
0018 
0019 #include "OrangeTypes.hh"
0020 
0021 #include "detail/BvhData.hh"
0022 
0023 namespace celeritas
0024 {
0025 class OrangeParams;
0026 class VolumeParams;
0027 //---------------------------------------------------------------------------//
0028 // CONSTANTS
0029 //---------------------------------------------------------------------------//
0030 
0031 //! Local ID of exterior volume for unit-type universes
0032 inline constexpr LocalVolumeId orange_exterior_volume{0};
0033 
0034 //! ID of the top (root/global/world, level=0) universe (scene)
0035 inline constexpr UnivId orange_global_univ{0};
0036 
0037 //! ID of the global universe
0038 inline constexpr UnivLevelId orange_global_univ_level{0};
0039 
0040 //! Logic notation used for boolean expressions
0041 inline constexpr auto orange_tracking_logic{LogicNotation::infix};
0042 
0043 //---------------------------------------------------------------------------//
0044 // PARAMS
0045 //---------------------------------------------------------------------------//
0046 /*!
0047  * Scalar values particular to an ORANGE geometry instance.
0048  *
0049  * Some of these are currently needed for state sizes (univ_levels, faces,
0050  * intersections), others are tested during construction (csg_levels), and
0051  * others are only for debugging on host (geo, volume params).
0052  */
0053 struct OrangeParamsScalars
0054 {
0055     size_type num_univ_levels{};
0056     size_type max_faces{};
0057     size_type max_intersections{};
0058     size_type max_csg_levels{};
0059 
0060     // Canonical volume depth: maximum stacked volume instances
0061     vol_level_uint num_vol_levels{};
0062 
0063     // Soft comparison and dynamic "bumping" values
0064     Tolerance<> tol;
0065 
0066     // Raw pointers to externally owned memory for debug output
0067     OrangeParams const* host_geo_params{nullptr};
0068     VolumeParams const* host_volume_params{nullptr};
0069 
0070     //! True if assigned
0071     explicit CELER_FUNCTION operator bool() const
0072     {
0073         return num_univ_levels > 0 && max_faces > 0 && max_intersections > 0
0074                && tol;
0075     }
0076 };
0077 
0078 //---------------------------------------------------------------------------//
0079 /*!
0080  * Data for a single local volume definition inside a unit.
0081  *
0082  * - This record defines an *implementation volume* which may correspond to
0083  *   a canonical "volume instance".
0084  * - Surface IDs are local to the unit.
0085  * - The encoded logic references the local surface IDs.
0086  *
0087  * \sa LocalVolumeView
0088  */
0089 struct LocalVolumeRecord
0090 {
0091     ItemRange<LocalSurfaceId> faces;
0092     ItemRange<logic_int> logic;
0093 
0094     logic_int max_intersections{0};
0095     logic_int flags{0};
0096     DaughterId daughter_id;
0097     OrientedBoundingZoneId obz_id;
0098 
0099     //! \todo For KENO geometry we will need zorder
0100 
0101     //! Flag values (bit field)
0102     enum Flags : logic_int
0103     {
0104         internal_surfaces = 0x1,  //!< "Complex" distance-to-boundary
0105         implicit_vol = 0x2,  //!< Background/exterior volume
0106         simple_safety = 0x4,  //!< Fast safety calculation
0107         embedded_universe = 0x8  //!< Volume contains embedded universe
0108     };
0109 };
0110 
0111 //---------------------------------------------------------------------------//
0112 /*!
0113  * Data for surfaces within a single unit.
0114  *
0115  * Surfaces each have a compile-time number of real data needed to define them.
0116  * (These usually are the nonzero coefficients of the quadric equation.) The
0117  * two fields in this record point to the collapsed surface types and
0118  * linearized data for all surfaces in a unit.
0119  *
0120  * The "types" and "data offsets" are both indexed into using the local surface
0121  * ID. The result of accessing "data offset" is an index into the \c real_ids
0122  * array, which then points us to the start address in \c reals. This marks the
0123  * beginning of the data used by the surface. Since the surface type tells us
0124  * the number of real values needed for that surface, we implicitly get a Span
0125  * of real values with a single indirection.
0126  */
0127 struct SurfacesRecord
0128 {
0129     using SurfaceTypeId = ItemId<SurfaceType>;
0130     using RealId = ItemId<real_type>;  // Pointer to a real
0131     using RealIdId = ItemId<RealId>;  // Pointer to pointer to real
0132 
0133     ItemMap<LocalSurfaceId, SurfaceTypeId> types;
0134     ItemMap<LocalSurfaceId, RealIdId> data_offsets;
0135 
0136     //! Number of surfaces stored
0137     CELER_FUNCTION size_type size() const { return types.size(); }
0138 
0139     //! True if defined consistently
0140     explicit CELER_FUNCTION operator bool() const
0141     {
0142         return data_offsets.size() == types.size();
0143     }
0144 };
0145 
0146 //---------------------------------------------------------------------------//
0147 /*!
0148  * Data for surface-to-volume connectivity.
0149  *
0150  * This struct is associated with a specific surface; the \c neighbors range is
0151  * a list of local volume IDs for that surface.
0152  */
0153 struct ConnectivityRecord
0154 {
0155     ItemRange<LocalVolumeId> neighbors;
0156 };
0157 
0158 //---------------------------------------------------------------------------//
0159 /*!
0160  * Data for a single OrientedBoundingZone.
0161  */
0162 struct OrientedBoundingZoneRecord
0163 {
0164     using Real3 = Array<fast_real_type, 3>;
0165 
0166     //! Half-widths of the inner and outer boxes
0167     Array<Real3, 2> half_widths;
0168 
0169     //! Offset from to center of inner and outer boxes to center of OBZ
0170     //! coordinate system
0171     Array<TransformId, 2> offset_ids;
0172 
0173     // Transformation from the OBZ coordinate system to the unit coordinate
0174     // system
0175     TransformId trans_id;
0176 
0177     //! True if assigned
0178     explicit CELER_FUNCTION operator bool() const
0179     {
0180         return offset_ids[0] && offset_ids[1] && trans_id;
0181     }
0182 };
0183 
0184 //---------------------------------------------------------------------------//
0185 /*!
0186  * Class for storing offset data for RaggedRightIndexer.
0187  */
0188 template<std::size_t N>
0189 struct RaggedRightIndexerData
0190 {
0191     using Sizes = Array<size_type, N>;
0192     using Offsets = Array<size_type, N + 1>;
0193 
0194     Offsets offsets;
0195 
0196     //! Construct with the an array denoting the size of each dimension.
0197     static RaggedRightIndexerData from_sizes(Sizes sizes)
0198     {
0199         CELER_EXPECT(sizes.size() > 0);
0200 
0201         Offsets offs;
0202         offs[0] = 0;
0203         for (auto i : range(N))
0204         {
0205             CELER_EXPECT(sizes[i] > 0);
0206             offs[i + 1] = sizes[i] + offs[i];
0207         }
0208         return RaggedRightIndexerData{offs};
0209     }
0210 };
0211 
0212 //---------------------------------------------------------------------------//
0213 /*!
0214  * Type-deleted transform.
0215  */
0216 struct TransformRecord
0217 {
0218     using RealId = OpaqueId<real_type>;
0219     TransformType type{TransformType::size_};
0220     RealId data_offset;
0221 
0222     //! True if values are set
0223     explicit CELER_FUNCTION operator bool() const
0224     {
0225         return type != TransformType::size_ && data_offset;
0226     }
0227 };
0228 
0229 //---------------------------------------------------------------------------//
0230 /*!
0231  * Scalar data for a single "unit" of volumes defined by surfaces.
0232  *
0233  * If using a HEP geometry implementation and this unit represents multiple
0234  * parent/child levels of the geometry hierarchy (via "inlining" during
0235  * construction; see \c g4org::ProtoConstructor):
0236  *
0237  * - Local parent is a map of local-to-local impl volume IDs: the \em value is
0238  *   an "implementation" placement of the canonical volume that is the
0239  *   \em parent enclosing the canonical volume placed at the local impl volume
0240  *   \em key
0241  * - Local level for each local impl volume is the difference between the
0242  *   corresponding canonical volume's depth and the highest canonical volume in
0243  *   in this unit. The (singular) local volume associated with the most-global
0244  *   canonical volume in this unit has value 0, a child volume "inlined" into
0245  *   this unit has value 1, etc.
0246  *
0247  * If this unit does not represent a Geant4/HEP volume hierarchy, those two
0248  * arrays will be empty.
0249  */
0250 struct SimpleUnitRecord
0251 {
0252     using LocalVolumeRecordId = ItemId<LocalVolumeRecord>;
0253     using ConnectivityRecordId = ItemId<ConnectivityRecord>;
0254     using LocalVolumeIdId = ItemId<LocalVolumeId>;
0255     using VolDepthUint = ItemId<vol_level_uint>;
0256 
0257     // Surface data
0258     SurfacesRecord surfaces;
0259     ItemMap<LocalSurfaceId, ConnectivityRecordId> connectivity;
0260 
0261     // Local volume data [index by LocalVolumeId]
0262     ItemMap<LocalVolumeId, LocalVolumeRecordId> volumes;
0263     // For volume instance mapping
0264     ItemMap<LocalVolumeId, LocalVolumeIdId> local_parent;
0265     ItemMap<LocalVolumeId, VolDepthUint> local_vol_level;
0266 
0267     // Bounding Volume Hierarchy tree parameters
0268     detail::BvhTreeRecord bvh_tree;
0269 
0270     LocalVolumeId background{};  //!< Default if not in any other volume
0271     bool simple_safety{};
0272 
0273     //! True if defined
0274     explicit CELER_FUNCTION operator bool() const
0275     {
0276         return surfaces && connectivity.size() == surfaces.types.size()
0277                && !volumes.empty();
0278     }
0279 };
0280 
0281 //---------------------------------------------------------------------------//
0282 /*!
0283  * Data for a single rectilinear array universe.
0284  */
0285 struct RectArrayRecord
0286 {
0287     using Dims = Array<size_type, 3>;
0288     using Grid = Array<ItemRange<real_type>, 3>;
0289     using SurfaceIndexerData = RaggedRightIndexerData<3>;
0290 
0291     // Daughter data [index by LocalVolumeId]
0292     ItemMap<LocalVolumeId, DaughterId> daughters;
0293 
0294     // Array data
0295     Dims dims;
0296     Grid grid;
0297     SurfaceIndexerData surface_indexer_data;
0298 
0299     //! Cursory check for validity
0300     explicit CELER_FUNCTION operator bool() const
0301     {
0302         return !daughters.empty() && !grid[to_int(Axis::x)].empty()
0303                && !grid[to_int(Axis::y)].empty()
0304                && !grid[to_int(Axis::z)].empty();
0305     }
0306 };
0307 
0308 //---------------------------------------------------------------------------//
0309 /*!
0310  * Surface and volume offsets to convert between local and global indices.
0311  *
0312  * Each collection should be of length num_universes + 1. The first entry is
0313  * zero and the last item should be the total number of surfaces or volumes.
0314  *
0315  * \todo These should be indexed into by UnivId, not the default
0316  * OpaqueId<size_type>.
0317  * \todo move to detail/UniverseIndexerData
0318  */
0319 template<Ownership W, MemSpace M>
0320 struct UniverseIndexerData
0321 {
0322     Collection<size_type, W, M> surfaces;
0323     Collection<size_type, W, M> volumes;
0324 
0325     template<Ownership W2, MemSpace M2>
0326     UniverseIndexerData& operator=(UniverseIndexerData<W2, M2> const& other)
0327     {
0328         CELER_EXPECT(other);
0329 
0330         surfaces = other.surfaces;
0331         volumes = other.volumes;
0332 
0333         CELER_ENSURE(*this);
0334         return *this;
0335     }
0336 
0337     explicit CELER_FUNCTION operator bool() const
0338     {
0339         return !surfaces.empty() && volumes.size() == surfaces.size();
0340     }
0341 };
0342 
0343 //---------------------------------------------------------------------------//
0344 /*!
0345  * Persistent data used by ORANGE implementation.
0346  *
0347  * Most data will be accessed through the individual units, which reference
0348  * data in the "storage" below. The type and index for a universe ID will
0349  * determine the class type and data of the Tracker to instantiate. If *only*
0350  * simple units are present, then the \c simple_units data structure will just
0351  * be equal to a range (with the total number of universes present). Use
0352  * \c univ_types to switch on the type of universe; then \c universe_indices
0353  * to index into `simple_units` or `rect_arrays` or ...
0354  */
0355 template<Ownership W, MemSpace M>
0356 struct OrangeParamsData
0357 {
0358     template<class T>
0359     using Items = Collection<T, W, M>;
0360     template<class T>
0361     using ImplVolumeItems = Collection<T, W, M, ImplVolumeId>;
0362     template<class T>
0363     using UnivItems = Collection<T, W, M, UnivId>;
0364 
0365     using RealId = SurfacesRecord::RealId;
0366 
0367     //// DATA ////
0368 
0369     // Scalar attributes
0370     OrangeParamsScalars scalars;
0371 
0372     // High-level universe definitions
0373     UnivItems<UnivType> univ_types;
0374     UnivItems<size_type> univ_indices;
0375     Items<SimpleUnitRecord> simple_units;
0376     Items<RectArrayRecord> rect_arrays;
0377     Items<TransformRecord> transforms;
0378 
0379     // Mappings used to reconstruct canonical volumes and hierarchy
0380     ImplVolumeItems<VolumeId> volume_ids;
0381     ImplVolumeItems<VolumeInstanceId> volume_instance_ids;
0382 
0383     // BVH tree storage
0384     detail::BvhTreeData<W, M> bvh_tree_data;
0385 
0386     // Low-level storage
0387     Items<LocalSurfaceId> local_surface_ids;
0388     Items<LocalVolumeId> local_volume_ids;
0389     Items<RealId> real_ids;
0390     Items<vol_level_uint> vl_uints;
0391     Items<logic_int> logic_ints;
0392     Items<real_type> reals;
0393     Items<SurfaceType> surface_types;
0394     Items<ConnectivityRecord> connectivity_records;
0395     Items<LocalVolumeRecord> volume_records;
0396     Items<Daughter> daughters;
0397     Items<OrientedBoundingZoneRecord> obz_records;
0398 
0399     UniverseIndexerData<W, M> univ_indexer_data;
0400 
0401     //// METHODS ////
0402 
0403     //! True if assigned
0404     explicit CELER_FUNCTION operator bool() const
0405     {
0406         return scalars && !univ_types.empty()
0407                && univ_indices.size() == univ_types.size()
0408                && !volume_ids.empty()
0409                && volume_ids.size() == volume_instance_ids.size()
0410                && (bvh_tree_data || !simple_units.empty())
0411                && ((!local_volume_ids.empty() && !logic_ints.empty()
0412                     && !reals.empty())
0413                    || surface_types.empty())
0414                && !volume_records.empty() && univ_indexer_data;
0415     }
0416 
0417     //! Assign from another set of data
0418     template<Ownership W2, MemSpace M2>
0419     OrangeParamsData& operator=(OrangeParamsData<W2, M2> const& other)
0420     {
0421         scalars = other.scalars;
0422 
0423         univ_types = other.univ_types;
0424         univ_indices = other.univ_indices;
0425         simple_units = other.simple_units;
0426         rect_arrays = other.rect_arrays;
0427         transforms = other.transforms;
0428 
0429         volume_ids = other.volume_ids;
0430         volume_instance_ids = other.volume_instance_ids;
0431 
0432         bvh_tree_data = other.bvh_tree_data;
0433 
0434         local_surface_ids = other.local_surface_ids;
0435         local_volume_ids = other.local_volume_ids;
0436         real_ids = other.real_ids;
0437         vl_uints = other.vl_uints;
0438         logic_ints = other.logic_ints;
0439         reals = other.reals;
0440         surface_types = other.surface_types;
0441         connectivity_records = other.connectivity_records;
0442         volume_records = other.volume_records;
0443         obz_records = other.obz_records;
0444         daughters = other.daughters;
0445         univ_indexer_data = other.univ_indexer_data;
0446 
0447         CELER_ENSURE(static_cast<bool>(*this) == static_cast<bool>(other));
0448         return *this;
0449     }
0450 };
0451 
0452 //---------------------------------------------------------------------------//
0453 // STATE
0454 //---------------------------------------------------------------------------//
0455 /*!
0456  * ORANGE state data.
0457  */
0458 template<Ownership W, MemSpace M>
0459 struct OrangeStateData
0460 {
0461     //// TYPES ////
0462 
0463     template<class T>
0464     using StateItems = StateCollection<T, W, M>;
0465     template<class T>
0466     using Items = Collection<T, W, M>;
0467 
0468     //// DATA ////
0469 
0470     // State with dimensions {num_tracks}
0471     StateItems<UnivLevelId> univ_level;
0472     StateItems<UnivLevelId> surface_univ_level;
0473     StateItems<LocalSurfaceId> surf;
0474     StateItems<Sense> sense;
0475     StateItems<GeoStatus> status;
0476 
0477     // "Local" state, needed for Shift {num_tracks}
0478     StateItems<real_type> next_step;
0479     StateItems<UnivLevelId> next_univ_level;
0480     StateItems<LocalSurfaceId> next_surf;
0481     StateItems<Sense> next_sense;
0482 
0483     // State with dimensions {num_tracks, scalars.num_univ_levels}
0484     Items<Real3> pos;
0485     Items<Real3> dir;
0486     Items<LocalVolumeId> vol;
0487     Items<UnivId> univ;
0488 
0489     // Scratch space with dimensions {track}{max_intersections}
0490     Items<FaceId> temp_face;
0491     Items<real_type> temp_distance;
0492     Items<size_type> temp_isect;
0493 
0494     //// METHODS ////
0495 
0496     //! True if sizes are consistent and nonzero
0497     explicit CELER_FUNCTION operator bool() const
0498     {
0499         // clang-format off
0500         return !univ_level.empty()
0501             && surface_univ_level.size() == this->size()
0502             && surf.size() == this->size()
0503             && sense.size() == this->size()
0504             && status.size() == this->size()
0505             && next_step.size() == this->size()
0506             && next_univ_level.size() == this->size()
0507             && next_surf.size() == this->size()
0508             && next_sense.size() == this->size()
0509             && pos.size() >= this->size()
0510             && dir.size() == pos.size()
0511             && vol.size() == pos.size()
0512             && univ.size() == pos.size()
0513             && !temp_face.empty()
0514             && temp_distance.size() == temp_face.size()
0515             && temp_isect.size() == temp_face.size();
0516         // clang-format on
0517     }
0518 
0519     //! State size
0520     CELER_FUNCTION TrackSlotId::size_type size() const
0521     {
0522         return univ_level.size();
0523     }
0524 
0525     //! Assign from another set of data
0526     template<Ownership W2, MemSpace M2>
0527     OrangeStateData& operator=(OrangeStateData<W2, M2>& other)
0528     {
0529         CELER_EXPECT(other);
0530 
0531         univ_level = other.univ_level;
0532         surface_univ_level = other.surface_univ_level;
0533         surf = other.surf;
0534         sense = other.sense;
0535         status = other.status;
0536 
0537         next_step = other.next_step;
0538         next_univ_level = other.next_univ_level;
0539         next_surf = other.next_surf;
0540         next_sense = other.next_sense;
0541 
0542         pos = other.pos;
0543         dir = other.dir;
0544         vol = other.vol;
0545         univ = other.univ;
0546 
0547         temp_face = other.temp_face;
0548         temp_distance = other.temp_distance;
0549         temp_isect = other.temp_isect;
0550 
0551         CELER_ENSURE(*this);
0552         return *this;
0553     }
0554 };
0555 
0556 //---------------------------------------------------------------------------//
0557 /*!
0558  * Resize geometry tracking states.
0559  */
0560 template<MemSpace M>
0561 inline void resize(OrangeStateData<Ownership::value, M>* data,
0562                    HostCRef<OrangeParamsData> const& params,
0563                    size_type num_tracks)
0564 {
0565     CELER_EXPECT(data);
0566     CELER_EXPECT(num_tracks > 0);
0567 
0568     resize(&data->univ_level, num_tracks);
0569     resize(&data->surface_univ_level, num_tracks);
0570     resize(&data->surf, num_tracks);
0571     resize(&data->sense, num_tracks);
0572     resize(&data->status, num_tracks);
0573 
0574     resize(&data->next_step, num_tracks);
0575     resize(&data->next_univ_level, num_tracks);
0576     resize(&data->next_surf, num_tracks);
0577     resize(&data->next_sense, num_tracks);
0578 
0579     size_type num_track_univ = params.scalars.num_univ_levels * num_tracks;
0580     resize(&data->pos, num_track_univ);
0581     resize(&data->dir, num_track_univ);
0582     resize(&data->vol, num_track_univ);
0583     resize(&data->univ, num_track_univ);
0584 
0585     size_type num_track_isect = params.scalars.max_intersections * num_tracks;
0586     resize(&data->temp_face, num_track_isect);
0587     resize(&data->temp_distance, num_track_isect);
0588     resize(&data->temp_isect, num_track_isect);
0589 
0590     CELER_ENSURE(*data);
0591 }
0592 
0593 //---------------------------------------------------------------------------//
0594 }  // namespace celeritas