Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:05:58

0001 //----------------------------------*-C++-*----------------------------------//
0002 // Copyright 2023-2024 UT-Battelle, LLC, and other Celeritas developers.
0003 // See the top-level COPYRIGHT file for details.
0004 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0005 //---------------------------------------------------------------------------//
0006 //! \file orange/univ/TrackerVisitor.hh
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include "corecel/math/Algorithms.hh"
0011 #include "orange/OrangeData.hh"
0012 
0013 #include "RectArrayTracker.hh"
0014 #include "SimpleUnitTracker.hh"
0015 #include "UniverseTypeTraits.hh"
0016 
0017 namespace celeritas
0018 {
0019 //---------------------------------------------------------------------------//
0020 /*!
0021  * Apply a functor to a universe tracker of unknown type.
0022  *
0023  * An instance of this class is like \c std::visit but accepting a UniverseId
0024  * rather than a \c std::variant .
0025  *
0026  * Example: \code
0027  TrackerVisitor visit_tracker{params_};
0028  auto new_pos = visit_tracker(
0029     [&pos](auto&& u) { return u.initialize(pos); },
0030     uid);
0031  \endcode
0032  */
0033 class TrackerVisitor
0034 {
0035   public:
0036     //!@{
0037     //! \name Type aliases
0038     using ParamsRef = NativeCRef<OrangeParamsData>;
0039     //!@}
0040 
0041   public:
0042     // Construct from ORANGE params
0043     explicit inline CELER_FUNCTION TrackerVisitor(ParamsRef const& params);
0044 
0045     // Apply the function to the universe specified by the given ID
0046     template<class F>
0047     CELER_FUNCTION decltype(auto) operator()(F&& func, UniverseId id);
0048 
0049   private:
0050     ParamsRef const& params_;
0051 };
0052 
0053 //---------------------------------------------------------------------------//
0054 // INLINE DEFINITIONS
0055 //---------------------------------------------------------------------------//
0056 /*!
0057  * Construct from ORANGE params.
0058  */
0059 CELER_FUNCTION TrackerVisitor::TrackerVisitor(ParamsRef const& params)
0060     : params_(params)
0061 {
0062 }
0063 
0064 #if !defined(__DOXYGEN__) || __DOXYGEN__ > 0x010908
0065 //---------------------------------------------------------------------------//
0066 /*!
0067  * Apply the function to the universe specified by the given ID.
0068  */
0069 template<class F>
0070 CELER_FUNCTION decltype(auto)
0071 TrackerVisitor::operator()(F&& func, UniverseId id)
0072 {
0073     CELER_EXPECT(id < params_.universe_types.size());
0074     size_type universe_idx = params_.universe_indices[id];
0075 
0076     // Apply type-deleted functor based on type
0077     return visit_universe_type(
0078         [&](auto u_traits) {
0079             using UTraits = decltype(u_traits);
0080             using UId = OpaqueId<typename UTraits::record_type>;
0081             using Tracker = typename UTraits::tracker_type;
0082             return func(Tracker{params_, UId{universe_idx}});
0083         },
0084         params_.universe_types[id]);
0085 }
0086 #endif
0087 
0088 //---------------------------------------------------------------------------//
0089 }  // namespace celeritas