Back to home page

EIC code displayed by LXR

 
 

    


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

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/TwodGridCalculator.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include "corecel/cont/Array.hh"
0010 #include "corecel/data/Collection.hh"
0011 
0012 #include "FindInterp.hh"
0013 #include "TwodGridData.hh"
0014 #include "TwodSubgridCalculator.hh"
0015 
0016 namespace celeritas
0017 {
0018 //---------------------------------------------------------------------------//
0019 /*!
0020  * Find and do bilinear interpolation on a nonuniform 2D grid of reals.
0021  *
0022  * Values should be node-centered, at the intersection of the two grids.
0023  *
0024  * \code
0025     TwodGridCalculator calc(grid, params.reals);
0026     real_type interpolated = calc({energy.value(), exit});
0027     // Or if the incident energy is reused...
0028     auto calc2 = calc(energy.value());
0029     interpolated = calc2(exit);
0030    \endcode
0031  */
0032 class TwodGridCalculator
0033 {
0034   public:
0035     //!@{
0036     //! \name Type aliases
0037     using Point = Array<real_type, 2>;
0038     using Values
0039         = Collection<real_type, Ownership::const_reference, MemSpace::native>;
0040     //!@}
0041 
0042   public:
0043     // Construct with grid data and backend values
0044     inline CELER_FUNCTION
0045     TwodGridCalculator(TwodGridData const& grid, Values const& storage);
0046 
0047     // Calculate the value at the given x, y coordinates
0048     inline CELER_FUNCTION real_type operator()(Point const& xy) const;
0049 
0050     // Get an interpolator for calculating y values for a given x
0051     inline CELER_FUNCTION TwodSubgridCalculator operator()(real_type x) const;
0052 
0053   private:
0054     TwodGridData const& grids_;
0055     Values const& storage_;
0056 };
0057 
0058 //---------------------------------------------------------------------------//
0059 // INLINE DEFINITIONS
0060 //---------------------------------------------------------------------------//
0061 /*!
0062  * Construct with grids and node-centered data.
0063  */
0064 CELER_FUNCTION TwodGridCalculator::TwodGridCalculator(TwodGridData const& grids,
0065                                                       Values const& storage)
0066     : grids_{grids}, storage_(storage)
0067 {
0068     CELER_EXPECT(grids);
0069     CELER_EXPECT(grids.values.back() < storage.size());
0070 }
0071 
0072 //---------------------------------------------------------------------------//
0073 /*!
0074  * Calculate the value at the given (x, y) coordinates.
0075  *
0076  * The coordinates must be inside \f$0 <= x < x_\mathrm{max}\f$ and
0077  * \f$0 <= y < y_\mathrm{max}\f$.
0078  *
0079  * \todo We may need to add logic inside the axis loop to account for points
0080  * outside the grid.
0081  */
0082 CELER_FUNCTION real_type TwodGridCalculator::operator()(Point const& inp) const
0083 {
0084     return (*this)(inp[0])(inp[1]);
0085 }
0086 
0087 //---------------------------------------------------------------------------//
0088 /*!
0089  * Get an interpolator for a preselected x value.
0090  */
0091 CELER_FUNCTION TwodSubgridCalculator
0092 TwodGridCalculator::operator()(real_type x) const
0093 {
0094     NonuniformGrid<real_type> const x_grid{grids_.x, storage_};
0095     CELER_EXPECT(x >= x_grid.front() && x < x_grid.back());
0096     return {grids_, storage_, find_interp(x_grid, x)};
0097 }
0098 
0099 //---------------------------------------------------------------------------//
0100 }  // namespace celeritas