Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-22 10:31:25

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 celeritas/mat/TabulatedElementSelector.hh
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include "corecel/Assert.hh"
0011 #include "corecel/Macros.hh"
0012 #include "corecel/Types.hh"
0013 #include "celeritas/Types.hh"
0014 #include "celeritas/grid/XsCalculator.hh"
0015 #include "celeritas/phys/PhysicsData.hh"
0016 #include "celeritas/random/distribution/GenerateCanonical.hh"
0017 
0018 namespace celeritas
0019 {
0020 //---------------------------------------------------------------------------//
0021 /*!
0022  * Make a weighted random selection of an element.
0023  *
0024  * This selects an elemental component (atom) of a material based on the
0025  * precalculated cross section CDF tables of the elements in the material.
0026  * Unlike \c ElementSelector which calculates the microscopic cross sections on
0027  * the fly, this interpolates the values using tabulated CDF grids.
0028  */
0029 class TabulatedElementSelector
0030 {
0031   public:
0032     //!@{
0033     //! \name Type aliases
0034     using Energy = Quantity<XsGridData::EnergyUnits>;
0035     using GridValues
0036         = Collection<ValueGrid, Ownership::const_reference, MemSpace::native>;
0037     using GridIdValues
0038         = Collection<ValueGridId, Ownership::const_reference, MemSpace::native>;
0039     using Values
0040         = Collection<real_type, Ownership::const_reference, MemSpace::native>;
0041     //!@}
0042 
0043   public:
0044     // Construct with xs CDF data for a particular model and material
0045     inline CELER_FUNCTION TabulatedElementSelector(ValueTable const& table,
0046                                                    GridValues const& grids,
0047                                                    GridIdValues const& ids,
0048                                                    Values const& reals,
0049                                                    Energy energy);
0050 
0051     // Sample with the given RNG
0052     template<class Engine>
0053     inline CELER_FUNCTION ElementComponentId operator()(Engine& rng) const;
0054 
0055   private:
0056     ValueTable const& table_;
0057     GridValues const& grids_;
0058     GridIdValues const& ids_;
0059     Values const& reals_;
0060     Energy const energy_;
0061 };
0062 
0063 //---------------------------------------------------------------------------//
0064 // INLINE DEFINITIONS
0065 //---------------------------------------------------------------------------//
0066 /*!
0067  * Construct with xs CDF data for a particular model and material.
0068  */
0069 CELER_FUNCTION
0070 TabulatedElementSelector::TabulatedElementSelector(ValueTable const& table,
0071                                                    GridValues const& grids,
0072                                                    GridIdValues const& ids,
0073                                                    Values const& reals,
0074                                                    Energy energy)
0075     : table_(table), grids_(grids), ids_(ids), reals_(reals), energy_(energy)
0076 {
0077     CELER_EXPECT(table);
0078 }
0079 
0080 //---------------------------------------------------------------------------//
0081 /*!
0082  * Sample the element with the given RNG.
0083  */
0084 template<class Engine>
0085 CELER_FUNCTION ElementComponentId
0086 TabulatedElementSelector::operator()(Engine& rng) const
0087 {
0088     size_type i = 0;
0089     real_type u = generate_canonical(rng);
0090     for (; i < table_.grids.size() - 1; ++i)
0091     {
0092         ValueGridId grid_id = ids_[table_.grids[i]];
0093         CELER_ASSERT(grid_id < grids_.size());
0094         XsCalculator calc_xs(grids_[grid_id], reals_);
0095         if (calc_xs(energy_) > u)
0096             break;
0097     }
0098     return ElementComponentId{i};
0099 }
0100 
0101 //---------------------------------------------------------------------------//
0102 }  // namespace celeritas