Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-22 10:31:23

0001 //----------------------------------*-C++-*----------------------------------//
0002 // Copyright 2021-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 celeritas/grid/GridIdFinder.hh
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include "corecel/Assert.hh"
0011 #include "corecel/Macros.hh"
0012 #include "corecel/cont/Span.hh"
0013 #include "corecel/data/LdgIterator.hh"
0014 #include "corecel/math/Algorithms.hh"
0015 
0016 namespace celeritas
0017 {
0018 //---------------------------------------------------------------------------//
0019 /*!
0020  * Map an input grid to an ID type, returning invalid ID if outside bounds.
0021  *
0022  * The input grid should be a monotonic increasing series, and the
0023  * corresponding ID values should be one fewer (cell-centered data). Values
0024  * outside the grid bounds are unassigned, and grid points are attached to the
0025  * model ID above the corresponding value.
0026  *
0027  * \code
0028     GridIdFinder<MevEnergy, ActionId> find_model(energy, values);
0029 
0030     ActionId applicable_model = find_model(particle.energy());
0031    \endcode
0032  */
0033 template<class KeyQuantity, class ValueId>
0034 class GridIdFinder
0035 {
0036     static_assert(KeyQuantity::unit_type::value() > 0, "Invalid Quantity");
0037     static_assert(sizeof(typename ValueId::size_type), "Invalid OpaqueId");
0038 
0039   public:
0040     //!@{
0041     //! \name Type aliases
0042     using argument_type = KeyQuantity;
0043     using result_type = ValueId;
0044 
0045     using SpanConstGrid = LdgSpan<typename KeyQuantity::value_type const>;
0046     using SpanConstValue = LdgSpan<result_type const>;
0047     //!@}
0048 
0049   public:
0050     // Construct from grid and values.
0051     inline CELER_FUNCTION GridIdFinder(SpanConstGrid, SpanConstValue);
0052 
0053     // Find the given grid point
0054     inline CELER_FUNCTION result_type operator()(argument_type arg) const;
0055 
0056   private:
0057     SpanConstGrid grid_;
0058     SpanConstValue value_;
0059 };
0060 
0061 //---------------------------------------------------------------------------//
0062 // INLINE DEFINITIONS
0063 //---------------------------------------------------------------------------//
0064 /*!
0065  * Construct from grid and values.
0066  *
0067  * \todo Construct from reference to collections and ranges so we can benefit
0068  * from
0069  * __ldg as needed
0070  */
0071 template<class K, class V>
0072 CELER_FUNCTION
0073 GridIdFinder<K, V>::GridIdFinder(SpanConstGrid grid, SpanConstValue value)
0074     : grid_(grid), value_(value)
0075 {
0076     CELER_EXPECT(grid_.size() == value_.size() + 1);
0077 }
0078 
0079 //---------------------------------------------------------------------------//
0080 /*!
0081  * Find the ID corresponding to the given value.
0082  */
0083 template<class K, class V>
0084 CELER_FUNCTION auto
0085 GridIdFinder<K, V>::operator()(argument_type quant) const -> result_type
0086 {
0087     auto iter
0088         = celeritas::lower_bound(grid_.begin(), grid_.end(), quant.value());
0089     if (iter == grid_.end())
0090     {
0091         // Higher than end point
0092         return {};
0093     }
0094     else if (iter == grid_.begin() && quant.value() != *iter)
0095     {
0096         // Below first point
0097         return {};
0098     }
0099     else if (iter + 1 == grid_.end() || quant.value() != *iter)
0100     {
0101         // Exactly on end grid point, or not on a grid point at all: move to
0102         // previous bin
0103         --iter;
0104     }
0105     return value_[iter - grid_.begin()];
0106 }
0107 
0108 //---------------------------------------------------------------------------//
0109 }  // namespace celeritas