Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:54:05

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/detail/GridAccessor.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include "corecel/Assert.hh"
0010 #include "corecel/Macros.hh"
0011 #include "corecel/Types.hh"
0012 #include "corecel/cont/Span.hh"
0013 
0014 #include "../NonuniformGridData.hh"
0015 #include "../UniformGrid.hh"
0016 #include "../UniformGridData.hh"
0017 
0018 namespace celeritas
0019 {
0020 namespace detail
0021 {
0022 //---------------------------------------------------------------------------//
0023 /*!
0024  * Helper class for accessing grid data in different formats.
0025  */
0026 class GridAccessor
0027 {
0028   public:
0029     //!@{
0030     //! \name Type aliases
0031     using SpanConstReal = Span<real_type const>;
0032     using Values
0033         = Collection<real_type, Ownership::const_reference, MemSpace::native>;
0034     //!@}
0035 
0036   public:
0037     // Virtual destructor for polymorphic deletion
0038     virtual ~GridAccessor() = default;
0039 
0040     //! Get the x grid value at the given index
0041     virtual real_type x(size_type index) const = 0;
0042 
0043     //! Get the y grid value at the given index
0044     virtual real_type y(size_type index) const = 0;
0045 
0046     //! Get the grid size
0047     virtual size_type size() const = 0;
0048 
0049     // Calculate \f$ \Delta x_i = x_{i + 1} - x_i \f$
0050     inline real_type delta_x(size_type index) const;
0051 
0052     // Calculate \f$ \Delta y_i = y_{i + 1} - y_i \f$
0053     inline real_type delta_y(size_type index) const;
0054 
0055     // Calculate \f$ r_i - r_{i - 1} \f$
0056     inline real_type delta_slope(size_type index) const;
0057 };
0058 
0059 //---------------------------------------------------------------------------//
0060 /*!
0061  * Helper class for accessing grid data from a nonuniform grid.
0062  */
0063 class NonuniformGridAccessor : public GridAccessor
0064 {
0065   public:
0066     // Construct with spans
0067     inline NonuniformGridAccessor(SpanConstReal x_values,
0068                                   SpanConstReal y_values);
0069 
0070     // Construct with a nonuniform grid
0071     inline NonuniformGridAccessor(NonuniformGridRecord const& grid,
0072                                   Values const& values);
0073 
0074     // Get the x grid value at the given index
0075     inline real_type x(size_type index) const final;
0076 
0077     // Get the y grid value at the given index
0078     inline real_type y(size_type index) const final;
0079 
0080     //! Get the grid size
0081     size_type size() const final { return x_values_.size(); }
0082 
0083   private:
0084     SpanConstReal x_values_;
0085     SpanConstReal y_values_;
0086 };
0087 
0088 //---------------------------------------------------------------------------//
0089 /*!
0090  * Helper class for accessing grid data from a uniform grid.
0091  */
0092 class UniformGridAccessor : public GridAccessor
0093 {
0094   public:
0095     // Construct with grid data
0096     inline UniformGridAccessor(UniformGridRecord const& grid,
0097                                Values const& values);
0098 
0099     //! Get the x grid value at the given index
0100     inline real_type x(size_type index) const final;
0101 
0102     //! Get the y grid value at the given index
0103     inline real_type y(size_type index) const final;
0104 
0105     //! Get the grid size
0106     size_type size() const final { return loge_grid_.size(); }
0107 
0108   private:
0109     UniformGridRecord const& data_;
0110     Values const& reals_;
0111     UniformGrid loge_grid_;
0112 };
0113 
0114 //---------------------------------------------------------------------------//
0115 /*!
0116  * Helper class for accessing grid data from an inverse uniform grid.
0117  */
0118 class InverseGridAccessor : public GridAccessor
0119 {
0120   public:
0121     //!@{
0122     //! \name Type aliases
0123     using Values
0124         = Collection<real_type, Ownership::const_reference, MemSpace::native>;
0125     //!@}
0126 
0127   public:
0128     // Construct with grid data
0129     inline InverseGridAccessor(UniformGridRecord const& grid,
0130                                Values const& values);
0131 
0132     //! Get the x grid value at the given index
0133     inline real_type x(size_type index) const final;
0134 
0135     //! Get the y grid value at the given index
0136     inline real_type y(size_type index) const final;
0137 
0138     //! Get the grid size
0139     size_type size() const final { return loge_grid_.size(); }
0140 
0141   private:
0142     UniformGridRecord const& data_;
0143     Values const& reals_;
0144     UniformGrid loge_grid_;
0145 };
0146 
0147 //---------------------------------------------------------------------------//
0148 // INLINE DEFINITIONS
0149 //---------------------------------------------------------------------------//
0150 /*!
0151  * Calculate \f$ x_{i + 1} - x_i \f$.
0152  */
0153 real_type GridAccessor::delta_x(size_type index) const
0154 {
0155     return this->x(index + 1) - this->x(index);
0156 }
0157 
0158 //---------------------------------------------------------------------------//
0159 /*!
0160  * Calculate \f$ y_{i + 1} - y_i \f$
0161  */
0162 real_type GridAccessor::delta_y(size_type index) const
0163 {
0164     return this->y(index + 1) - this->y(index);
0165 }
0166 
0167 //---------------------------------------------------------------------------//
0168 /*!
0169  * Calculate \f$ r_i - r_{i - 1} \f$.
0170  *
0171  * This calculates \f$ \Delta r_i = \frac{\Delta y_i}{\Delta x_i} -
0172  * \frac{\Delta y_{i - 1}}{\Delta x_{i - 1}} \f$
0173  */
0174 real_type GridAccessor::delta_slope(size_type index) const
0175 {
0176     CELER_EXPECT(index > 0);
0177     return this->delta_y(index) / this->delta_x(index)
0178            - this->delta_y(index - 1) / this->delta_x(index - 1);
0179 }
0180 
0181 //---------------------------------------------------------------------------//
0182 /*!
0183  * Construct from spans.
0184  */
0185 NonuniformGridAccessor::NonuniformGridAccessor(SpanConstReal x_values,
0186                                                SpanConstReal y_values)
0187     : x_values_(x_values), y_values_(y_values)
0188 {
0189     CELER_EXPECT(x_values_.size() == y_values_.size());
0190 }
0191 
0192 //---------------------------------------------------------------------------//
0193 /*!
0194  * Construct from a nonuniform grid.
0195  */
0196 NonuniformGridAccessor::NonuniformGridAccessor(NonuniformGridRecord const& grid,
0197                                                Values const& values)
0198 
0199     : NonuniformGridAccessor(values[grid.grid], values[grid.value])
0200 {
0201 }
0202 
0203 //---------------------------------------------------------------------------//
0204 /*!
0205  * Get the x grid value at the given index.
0206  */
0207 real_type NonuniformGridAccessor::x(size_type index) const
0208 {
0209     CELER_EXPECT(index < this->size());
0210     return x_values_[index];
0211 }
0212 
0213 //---------------------------------------------------------------------------//
0214 /*!
0215  * Get the y grid value at the given index.
0216  */
0217 real_type NonuniformGridAccessor::y(size_type index) const
0218 {
0219     CELER_EXPECT(index < this->size());
0220     return y_values_[index];
0221 }
0222 
0223 //---------------------------------------------------------------------------//
0224 /*!
0225  * Construct from cross section grid.
0226  */
0227 UniformGridAccessor::UniformGridAccessor(UniformGridRecord const& grid,
0228                                          Values const& values)
0229     : data_(grid), reals_(values), loge_grid_(data_.grid)
0230 {
0231     CELER_EXPECT(data_);
0232 }
0233 
0234 //---------------------------------------------------------------------------//
0235 /*!
0236  * Get the x grid value at the given index.
0237  */
0238 real_type UniformGridAccessor::x(size_type index) const
0239 {
0240     CELER_EXPECT(index < this->size());
0241     return std::exp(loge_grid_[index]);
0242 }
0243 
0244 //---------------------------------------------------------------------------//
0245 /*!
0246  * Get the y grid value at the given index.
0247  */
0248 real_type UniformGridAccessor::y(size_type index) const
0249 {
0250     CELER_EXPECT(index < this->size());
0251     return reals_[data_.value[index]];
0252 }
0253 
0254 //---------------------------------------------------------------------------//
0255 /*!
0256  * Contruct from cross section grid.
0257  */
0258 InverseGridAccessor::InverseGridAccessor(UniformGridRecord const& grid,
0259                                          Values const& values)
0260     : data_(grid), reals_(values), loge_grid_(data_.grid)
0261 {
0262     CELER_EXPECT(data_);
0263 }
0264 
0265 //---------------------------------------------------------------------------//
0266 /*!
0267  * Get the x grid value at the given index.
0268  */
0269 real_type InverseGridAccessor::x(size_type index) const
0270 {
0271     CELER_EXPECT(index < this->size());
0272     return reals_[data_.value[index]];
0273 }
0274 
0275 //---------------------------------------------------------------------------//
0276 /*!
0277  * Get the y grid value at the given index.
0278  */
0279 real_type InverseGridAccessor::y(size_type index) const
0280 {
0281     CELER_EXPECT(index < this->size());
0282     return std::exp(loge_grid_[index]);
0283 }
0284 
0285 //---------------------------------------------------------------------------//
0286 }  // namespace detail
0287 }  // namespace celeritas