Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:03:42

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