Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-18 09:09:39

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 corecel/grid/UniformGrid.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include "corecel/Assert.hh"
0010 #include "corecel/Macros.hh"
0011 #include "corecel/Types.hh"
0012 
0013 #include "UniformGridData.hh"
0014 
0015 namespace celeritas
0016 {
0017 //---------------------------------------------------------------------------//
0018 /*!
0019  * Interact with a uniform grid of increasing values.
0020  *
0021  * This simple class is used by physics vectors and classes that need to do
0022  * lookups on a uniform grid.
0023  */
0024 class UniformGrid
0025 {
0026   public:
0027     //!@{
0028     //! \name Type aliases
0029     using size_type = ::celeritas::size_type;
0030     using value_type = ::celeritas::real_type;
0031     //!@}
0032 
0033   public:
0034     // Construct with data
0035     explicit inline CELER_FUNCTION UniformGrid(UniformGridData const& data);
0036 
0037     //! Number of grid points
0038     CELER_FORCEINLINE_FUNCTION size_type size() const { return data_.size; }
0039 
0040     //! Minimum/first value
0041     CELER_FORCEINLINE_FUNCTION value_type front() const { return data_.front; }
0042 
0043     //! Maximum/last value
0044     CELER_FORCEINLINE_FUNCTION value_type back() const { return data_.back; }
0045 
0046     // Calculate the value at the given grid point
0047     inline CELER_FUNCTION value_type operator[](size_type i) const;
0048 
0049     // Find the index of the given value (*must* be in bounds)
0050     inline CELER_FUNCTION size_type find(value_type value) const;
0051 
0052     //! Get the data used to construct this class
0053     CELER_FUNCTION UniformGridData const& data() const { return data_; }
0054 
0055   private:
0056     UniformGridData const& data_;
0057 };
0058 
0059 //---------------------------------------------------------------------------//
0060 // INLINE DEFINITIONS
0061 //---------------------------------------------------------------------------//
0062 /*!
0063  * Construct with data.
0064  */
0065 CELER_FUNCTION
0066 UniformGrid::UniformGrid(UniformGridData const& data) : data_(data)
0067 {
0068     CELER_EXPECT(data_);
0069 }
0070 
0071 //---------------------------------------------------------------------------//
0072 /*!
0073  * Get the value at the given grid point.
0074  */
0075 CELER_FUNCTION auto UniformGrid::operator[](size_type i) const -> value_type
0076 {
0077     CELER_EXPECT(i < data_.size);
0078     return data_.front + data_.delta * i;
0079 }
0080 
0081 //---------------------------------------------------------------------------//
0082 /*!
0083  * Find the value bin such that data[result] <= value < data[result + 1].
0084  *
0085  * The given value *must* be in range, because out-of-bounds values usually
0086  * require different treatment (e.g. clipping to the boundary values rather
0087  * than interpolating). It's easier to test the exceptional cases (final grid
0088  * point) outside of the grid view.
0089  */
0090 CELER_FUNCTION size_type UniformGrid::find(value_type value) const
0091 {
0092     CELER_EXPECT(value >= this->front() && value < this->back());
0093     auto bin = static_cast<size_type>((value - data_.front) / data_.delta);
0094     CELER_ENSURE(bin + 1 < this->size());
0095     return bin;
0096 }
0097 
0098 //---------------------------------------------------------------------------//
0099 }  // namespace celeritas