Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-14 08:50:41

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 celeritas/phys/MacroXsCalculator.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 #include "celeritas/Quantities.hh"
0014 #include "celeritas/UnitTypes.hh"
0015 #include "celeritas/mat/MaterialView.hh"
0016 
0017 namespace celeritas
0018 {
0019 //---------------------------------------------------------------------------//
0020 /*!
0021  * Calculates the macroscopic cross section.
0022  *
0023  * \tparam MicroXsT microscopic (element) cross section calculator
0024  */
0025 template<class MicroXsT>
0026 class MacroXsCalculator
0027 {
0028   public:
0029     //!@{
0030     //! \name Type aliases
0031     using ParamsRef = typename MicroXsT::ParamsRef;
0032     using Energy = typename MicroXsT::Energy;
0033     using MicroXs = units::BarnXs;
0034     using MacroXsUnits = units::Native;  // [1/len]
0035     //!@}
0036 
0037   public:
0038     // Construct with shared and material
0039     inline CELER_FUNCTION
0040     MacroXsCalculator(ParamsRef const& shared, MaterialView const& material);
0041 
0042     // Compute the macroscopic cross section on the fly at the given energy
0043     inline CELER_FUNCTION real_type operator()(Energy energy) const;
0044 
0045   private:
0046     ParamsRef const& shared_;
0047     Span<MatElementComponent const> elements_;
0048     real_type number_density_;
0049 };
0050 
0051 //---------------------------------------------------------------------------//
0052 // INLINE DEFINITIONS
0053 //---------------------------------------------------------------------------//
0054 /*!
0055  * Construct with shared and material data.
0056  */
0057 template<class MicroXsT>
0058 CELER_FUNCTION
0059 MacroXsCalculator<MicroXsT>::MacroXsCalculator(ParamsRef const& shared,
0060                                                MaterialView const& material)
0061     : shared_(shared)
0062     , elements_(material.elements())
0063     , number_density_(material.number_density())
0064 {
0065     CELER_EXPECT(!elements_.empty());
0066 }
0067 
0068 //---------------------------------------------------------------------------//
0069 /*!
0070  * Compute the macroscopic cross section on the fly at the given energy.
0071  */
0072 template<class MicroXsT>
0073 CELER_FUNCTION real_type
0074 MacroXsCalculator<MicroXsT>::operator()(Energy energy) const
0075 {
0076     real_type result = 0.;
0077     MicroXsT calc_micro_xs(shared_, energy);
0078     for (auto const& el_comp : elements_)
0079     {
0080         real_type micro_xs = value_as<MicroXs>(calc_micro_xs(el_comp.element));
0081         CELER_ASSERT(micro_xs >= 0);
0082         result += micro_xs * el_comp.fraction;
0083     }
0084     result = native_value_from(MicroXs{result}) * number_density_;
0085     CELER_ENSURE(result >= 0);
0086     return result;
0087 }
0088 
0089 //---------------------------------------------------------------------------//
0090 }  // namespace celeritas