Back to home page

EIC code displayed by LXR

 
 

    


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

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